1 | /**
|
2 | * @license
|
3 | * Copyright Google LLC All Rights Reserved.
|
4 | *
|
5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | * found in the LICENSE file at https://angular.io/license
|
7 | */
|
8 | /// <amd-module name="@angular/language-service/ivy/template_target" />
|
9 | import * as e from '@angular/compiler/src/expression_parser/ast';
|
10 | import * as t from '@angular/compiler/src/render3/r3_ast';
|
11 | /**
|
12 | * Contextual information for a target position within the template.
|
13 | */
|
14 | export interface TemplateTarget {
|
15 | /**
|
16 | * Target position within the template.
|
17 | */
|
18 | position: number;
|
19 | /**
|
20 | * The template (or AST expression) node or nodes closest to the search position.
|
21 | */
|
22 | context: TargetContext;
|
23 | /**
|
24 | * The `t.Template` which contains the found node or expression (or `null` if in the root
|
25 | * template).
|
26 | */
|
27 | template: t.Template | null;
|
28 | /**
|
29 | * The immediate parent node of the targeted node.
|
30 | */
|
31 | parent: t.Node | e.AST | null;
|
32 | }
|
33 | /**
|
34 | * A node or nodes targeted at a given position in the template, including potential contextual
|
35 | * information about the specific aspect of the node being referenced.
|
36 | *
|
37 | * Some nodes have multiple interior contexts. For example, `t.Element` nodes have both a tag name
|
38 | * as well as a body, and a given position definitively points to one or the other. `TargetNode`
|
39 | * captures the node itself, as well as this additional contextual disambiguation.
|
40 | */
|
41 | export declare type TargetContext = SingleNodeTarget | MultiNodeTarget;
|
42 | /** Contexts which logically target only a single node in the template AST. */
|
43 | export declare type SingleNodeTarget = RawExpression | RawTemplateNode | ElementInBodyContext | ElementInTagContext | AttributeInKeyContext | AttributeInValueContext;
|
44 | /**
|
45 | * Contexts which logically target multiple nodes in the template AST, which cannot be
|
46 | * disambiguated given a single position because they are all equally relavent. For example, in the
|
47 | * banana-in-a-box syntax `[(ngModel)]="formValues.person"`, the position in the template for the
|
48 | * key `ngModel` refers to both the bound event `ngModelChange` and the input `ngModel`.
|
49 | */
|
50 | export declare type MultiNodeTarget = TwoWayBindingContext;
|
51 | /**
|
52 | * Differentiates the various kinds of `TargetNode`s.
|
53 | */
|
54 | export declare enum TargetNodeKind {
|
55 | RawExpression = 0,
|
56 | RawTemplateNode = 1,
|
57 | ElementInTagContext = 2,
|
58 | ElementInBodyContext = 3,
|
59 | AttributeInKeyContext = 4,
|
60 | AttributeInValueContext = 5,
|
61 | TwoWayBindingContext = 6
|
62 | }
|
63 | /**
|
64 | * An `e.AST` expression that's targeted at a given position, with no additional context.
|
65 | */
|
66 | export interface RawExpression {
|
67 | kind: TargetNodeKind.RawExpression;
|
68 | node: e.AST;
|
69 | }
|
70 | /**
|
71 | * A `t.Node` template node that's targeted at a given position, with no additional context.
|
72 | */
|
73 | export interface RawTemplateNode {
|
74 | kind: TargetNodeKind.RawTemplateNode;
|
75 | node: t.Node;
|
76 | }
|
77 | /**
|
78 | * A `t.Element` (or `t.Template`) element node that's targeted, where the given position is within
|
79 | * the tag name.
|
80 | */
|
81 | export interface ElementInTagContext {
|
82 | kind: TargetNodeKind.ElementInTagContext;
|
83 | node: t.Element | t.Template;
|
84 | }
|
85 | /**
|
86 | * A `t.Element` (or `t.Template`) element node that's targeted, where the given position is within
|
87 | * the element body.
|
88 | */
|
89 | export interface ElementInBodyContext {
|
90 | kind: TargetNodeKind.ElementInBodyContext;
|
91 | node: t.Element | t.Template;
|
92 | }
|
93 | export interface AttributeInKeyContext {
|
94 | kind: TargetNodeKind.AttributeInKeyContext;
|
95 | node: t.TextAttribute | t.BoundAttribute | t.BoundEvent;
|
96 | }
|
97 | export interface AttributeInValueContext {
|
98 | kind: TargetNodeKind.AttributeInValueContext;
|
99 | node: t.TextAttribute | t.BoundAttribute | t.BoundEvent;
|
100 | }
|
101 | /**
|
102 | * A `t.BoundAttribute` and `t.BoundEvent` pair that are targeted, where the given position is
|
103 | * within the key span of both.
|
104 | */
|
105 | export interface TwoWayBindingContext {
|
106 | kind: TargetNodeKind.TwoWayBindingContext;
|
107 | nodes: [t.BoundAttribute, t.BoundEvent];
|
108 | }
|
109 | /**
|
110 | * Return the template AST node or expression AST node that most accurately
|
111 | * represents the node at the specified cursor `position`.
|
112 | *
|
113 | * @param template AST tree of the template
|
114 | * @param position target cursor position
|
115 | */
|
116 | export declare function getTargetAtPosition(template: t.Node[], position: number): TemplateTarget | null;
|