UNPKG

4.31 kBTypeScriptView Raw
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" />
9import * as e from '@angular/compiler/src/expression_parser/ast';
10import * as t from '@angular/compiler/src/render3/r3_ast';
11/**
12 * Contextual information for a target position within the template.
13 */
14export 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 */
41export declare type TargetContext = SingleNodeTarget | MultiNodeTarget;
42/** Contexts which logically target only a single node in the template AST. */
43export 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 */
50export declare type MultiNodeTarget = TwoWayBindingContext;
51/**
52 * Differentiates the various kinds of `TargetNode`s.
53 */
54export 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 */
66export 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 */
73export 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 */
81export 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 */
89export interface ElementInBodyContext {
90 kind: TargetNodeKind.ElementInBodyContext;
91 node: t.Element | t.Template;
92}
93export interface AttributeInKeyContext {
94 kind: TargetNodeKind.AttributeInKeyContext;
95 node: t.TextAttribute | t.BoundAttribute | t.BoundEvent;
96}
97export 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 */
105export 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 */
116export declare function getTargetAtPosition(template: t.Node[], position: number): TemplateTarget | null;