( template: TmplAstNode[], position: number, )
| 272 | * @param position target cursor position |
| 273 | */ |
| 274 | export function getTargetAtPosition( |
| 275 | template: TmplAstNode[], |
| 276 | position: number, |
| 277 | ): TemplateTarget | null { |
| 278 | const path = TemplateTargetVisitor.visitTemplate(template, position); |
| 279 | if (path.length === 0) { |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | const candidate = path[path.length - 1]; |
| 284 | |
| 285 | // Walk up the result nodes to find the nearest `TmplAstTemplate` which contains the targeted |
| 286 | // node. |
| 287 | let context: TmplAstTemplate | null = null; |
| 288 | for (let i = path.length - 2; i >= 0; i--) { |
| 289 | const node = path[i]; |
| 290 | if (node instanceof TmplAstTemplate) { |
| 291 | context = node; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Given the candidate node, determine the full targeted context. |
| 297 | let nodeInContext: TargetContext; |
| 298 | if ( |
| 299 | (candidate instanceof Call || candidate instanceof SafeCall) && |
| 300 | isWithin(position, candidate.argumentSpan) |
| 301 | ) { |
| 302 | nodeInContext = { |
| 303 | kind: TargetNodeKind.CallExpressionInArgContext, |
| 304 | node: candidate, |
| 305 | }; |
| 306 | } else if (candidate instanceof AST) { |
| 307 | const parents = path.filter((value: AST | TmplAstNode): value is AST => value instanceof AST); |
| 308 | // Remove the current node from the parents list. |
| 309 | parents.pop(); |
| 310 | |
| 311 | nodeInContext = { |
| 312 | kind: TargetNodeKind.RawExpression, |
| 313 | node: candidate, |
| 314 | parents, |
| 315 | }; |
| 316 | } else if (candidate instanceof TmplAstElement) { |
| 317 | // Elements have two contexts: the tag context (position is within the element tag) or the |
| 318 | // element body context (position is outside of the tag name, but still in the element). |
| 319 | nodeInContext = { |
| 320 | kind: isWithinTagBody(position, candidate) |
| 321 | ? TargetNodeKind.ElementInBodyContext |
| 322 | : TargetNodeKind.ElementInTagContext, |
| 323 | node: candidate, |
| 324 | }; |
| 325 | } else if (candidate instanceof TmplAstComponent) { |
| 326 | nodeInContext = { |
| 327 | kind: isWithinTagBody(position, candidate) |
| 328 | ? TargetNodeKind.ComponentInBodyContext |
| 329 | : TargetNodeKind.ComponentInTagContext, |
| 330 | node: candidate, |
| 331 | }; |
no test coverage detected