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