MCPcopy Create free account
hub / github.com/angular/angular / getTargetAtPosition

Function getTargetAtPosition

packages/language-service/src/template_target.ts:274–389  ·  view source on GitHub ↗
(
  template: TmplAstNode[],
  position: number,
)

Source from the content-addressed store, hash-verified

272 * @param position target cursor position
273 */
274export 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 };

Calls 6

isWithinFunction · 0.90
isWithinTagBodyFunction · 0.85
popMethod · 0.80
visitTemplateMethod · 0.65
toStringMethod · 0.65
filterMethod · 0.45

Tested by

no test coverage detected