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

Function getTargetAtPosition

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

Source from the content-addressed store, hash-verified

226 * @param position target cursor position
227 */
228export function getTargetAtPosition(
229 template: TmplAstNode[],
230 position: number,
231): TemplateTarget | null {
232 const path = TemplateTargetVisitor.visitTemplate(template, position);
233 if (path.length === 0) {
234 return null;
235 }
236
237 const candidate = path[path.length - 1];
238
239 // Walk up the result nodes to find the nearest `TmplAstTemplate` which contains the targeted
240 // node.
241 let context: TmplAstTemplate | null = null;
242 for (let i = path.length - 2; i >= 0; i--) {
243 const node = path[i];
244 if (node instanceof TmplAstTemplate) {
245 context = node;
246 break;
247 }
248 }
249
250 // Given the candidate node, determine the full targeted context.
251 let nodeInContext: TargetContext;
252 if (
253 (candidate instanceof Call || candidate instanceof SafeCall) &&
254 isWithin(position, candidate.argumentSpan)
255 ) {
256 nodeInContext = {
257 kind: TargetNodeKind.CallExpressionInArgContext,
258 node: candidate,
259 };
260 } else if (candidate instanceof AST) {
261 const parents = path.filter((value: AST | TmplAstNode): value is AST => value instanceof AST);
262 // Remove the current node from the parents list.
263 parents.pop();
264
265 nodeInContext = {
266 kind: TargetNodeKind.RawExpression,
267 node: candidate,
268 parents,
269 };
270 } else if (candidate instanceof TmplAstElement) {
271 // Elements have two contexts: the tag context (position is within the element tag) or the
272 // element body context (position is outside of the tag name, but still in the element).
273 nodeInContext = {
274 kind: isWithinTagBody(position, candidate)
275 ? TargetNodeKind.ElementInBodyContext
276 : TargetNodeKind.ElementInTagContext,
277 node: candidate,
278 };
279 } else if (
280 (candidate instanceof TmplAstBoundAttribute ||
281 candidate instanceof TmplAstBoundEvent ||
282 candidate instanceof TmplAstTextAttribute) &&
283 candidate.keySpan !== undefined
284 ) {
285 const previousCandidate = path[path.length - 2];

Calls 5

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

Tested by

no test coverage detected