@@ -15,153 +15,6 @@ import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
1515
1616export 'package:analyzer/src/dart/ast/constant_evaluator.dart' ;
1717
18- /// An object used to locate the [AstNode] associated with a source range, given
19- /// the AST structure built from the source. More specifically, they will return
20- /// the [AstNode] with the shortest length whose source range completely
21- /// encompasses the specified range with some exceptions:
22- ///
23- /// - Offsets that fall between the name and type/formal parameter list of a
24- /// declaration will return the declaration node and not the parameter list
25- /// node.
26- class NodeLocator extends UnifyingAstVisitor <void > {
27- /// The start offset of the range used to identify the node.
28- final int _startOffset;
29-
30- /// The end offset of the range used to identify the node.
31- final int _endOffset;
32-
33- /// The element that was found that corresponds to the given source range, or
34- /// `null` if there is no such element.
35- AstNode ? _foundNode;
36-
37- /// Initialize a newly created locator to locate an [AstNode] by locating the
38- /// node within an AST structure that corresponds to the given range of
39- /// characters (between the [startOffset] and [endOffset] in the source.
40- NodeLocator (int startOffset, [int ? endOffset])
41- : _startOffset = startOffset,
42- _endOffset = endOffset ?? startOffset;
43-
44- /// Return the node that was found that corresponds to the given source range
45- /// or `null` if there is no such node.
46- AstNode ? get foundNode => _foundNode;
47-
48- /// Search within the given AST [node] for an identifier representing an
49- /// element in the specified source range. Return the element that was found,
50- /// or `null` if no element was found.
51- AstNode ? searchWithin (AstNode ? node) {
52- if (node == null ) {
53- return null ;
54- }
55- try {
56- node.accept (this );
57- } catch (exception, stackTrace) {
58- // TODO(39284): should this exception be silent?
59- AnalysisEngine .instance.instrumentationService.logException (
60- SilentException (
61- "Unable to locate element at offset ($_startOffset - $_endOffset )" ,
62- exception,
63- stackTrace));
64- return null ;
65- }
66-
67- return _foundNode;
68- }
69-
70- @override
71- void visitClassDeclaration (ClassDeclaration node) {
72- // Names do not have AstNodes but offsets at the end should be treated as
73- // part of the declaration (not parameter list).
74- if (_startOffset == _endOffset && _startOffset == node.name.end) {
75- _foundNode = node;
76- return ;
77- }
78-
79- super .visitClassDeclaration (node);
80- }
81-
82- @override
83- void visitConstructorDeclaration (ConstructorDeclaration node) {
84- // Names do not have AstNodes but offsets at the end should be treated as
85- // part of the declaration (not parameter list).
86- if (_startOffset == _endOffset &&
87- _startOffset == (node.name ?? node.returnType).end) {
88- _foundNode = node;
89- return ;
90- }
91-
92- super .visitConstructorDeclaration (node);
93- }
94-
95- @override
96- void visitFunctionDeclaration (FunctionDeclaration node) {
97- // Names do not have AstNodes but offsets at the end should be treated as
98- // part of the declaration (not parameter list).
99- if (_startOffset == _endOffset && _startOffset == node.name.end) {
100- _foundNode = node;
101- return ;
102- }
103-
104- super .visitFunctionDeclaration (node);
105- }
106-
107- @override
108- void visitMethodDeclaration (MethodDeclaration node) {
109- // Names do not have AstNodes but offsets at the end should be treated as
110- // part of the declaration (not parameter list).
111- if (_startOffset == _endOffset && _startOffset == node.name.end) {
112- _foundNode = node;
113- return ;
114- }
115-
116- super .visitMethodDeclaration (node);
117- }
118-
119- @override
120- void visitNode (AstNode node) {
121- // Don't visit a new tree if the result has been already found.
122- if (_foundNode != null ) {
123- return ;
124- }
125- // Check whether the current node covers the selection.
126- Token beginToken = node.beginToken;
127- Token endToken = node.endToken;
128- // Don't include synthetic tokens.
129- while (endToken != beginToken) {
130- // Fasta scanner reports unterminated string literal errors
131- // and generates a synthetic string token with non-zero length.
132- // Because of this, check for length > 0 rather than !isSynthetic.
133- if (endToken.isEof || endToken.length > 0 ) {
134- break ;
135- }
136- endToken = endToken.previous! ;
137- }
138- int end = endToken.end;
139- int start = node.offset;
140- if (end < _startOffset || start > _endOffset) {
141- return ;
142- }
143- // Check children.
144- try {
145- node.visitChildren (this );
146- } catch (exception, stackTrace) {
147- // Ignore the exception and proceed in order to visit the rest of the
148- // structure.
149- // TODO(39284): should this exception be silent?
150- AnalysisEngine .instance.instrumentationService.logException (
151- SilentException ("Exception caught while traversing an AST structure." ,
152- exception, stackTrace));
153- }
154- // Found a child.
155- if (_foundNode != null ) {
156- return ;
157- }
158- // Check this node.
159- if (start <= _startOffset && _endOffset <= end) {
160- _foundNode = node;
161- }
162- }
163- }
164-
16518/// An object used to locate the [AstNode] associated with a source range.
16619/// More specifically, they will return the deepest [AstNode] which completely
16720/// encompasses the specified range with some exceptions:
0 commit comments