OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library services.src.search.search_engine; | 5 library services.src.search.search_engine2; |
Brian Wilkerson
2016/03/14 18:57:32
nit: I was going to suggest dropping the "2", but
scheglov
2016/03/14 19:18:38
Done.
| |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/src/provisional/index/index_core.dart'; | |
10 import 'package:analysis_server/src/services/correction/source_range.dart'; | 9 import 'package:analysis_server/src/services/correction/source_range.dart'; |
11 import 'package:analysis_server/src/services/index/index.dart'; | 10 import 'package:analysis_server/src/services/index/index.dart'; |
12 import 'package:analysis_server/src/services/index/indexable_element.dart'; | |
13 import 'package:analysis_server/src/services/search/search_engine.dart'; | 11 import 'package:analysis_server/src/services/search/search_engine.dart'; |
12 import 'package:analyzer/dart/ast/ast.dart'; | |
13 import 'package:analyzer/dart/ast/visitor.dart'; | |
14 import 'package:analyzer/dart/element/element.dart'; | 14 import 'package:analyzer/dart/element/element.dart'; |
15 import 'package:analyzer/src/dart/element/member.dart'; | 15 import 'package:analyzer/src/dart/element/member.dart'; |
16 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; |
17 import 'package:analyzer/src/generated/resolver.dart' show NamespaceBuilder; | |
18 import 'package:analyzer/src/generated/source.dart' show Source, SourceRange; | |
19 import 'package:analyzer/src/summary/idl.dart'; | |
17 | 20 |
18 /** | 21 /** |
19 * A [SearchEngine] implementation. | 22 * A [SearchEngine] implementation. |
20 */ | 23 */ |
21 class SearchEngineImpl implements SearchEngine { | 24 class SearchEngineImpl implements SearchEngine { |
22 final Index _index; | 25 final Index _index; |
23 | 26 |
24 SearchEngineImpl(this._index); | 27 SearchEngineImpl(this._index); |
25 | 28 |
26 @override | 29 @override |
27 Future<List<SearchMatch>> searchAllSubtypes(ClassElement type) { | 30 Future<List<SearchMatch>> searchAllSubtypes(ClassElement type) async { |
28 _Requestor requestor = new _Requestor(_index); | 31 List<SearchMatch> matches = <SearchMatch>[]; |
29 requestor.addElement( | 32 await _addMatches( |
30 type, IndexConstants.HAS_ANCESTOR, MatchKind.DECLARATION); | 33 matches, type, IndexRelationKind.IS_ANCESTOR_OF, MatchKind.DECLARATION); |
31 return requestor.merge(); | 34 return matches; |
32 } | 35 } |
33 | 36 |
34 @override | 37 @override |
35 Future<List<SearchMatch>> searchMemberDeclarations(String name) async { | 38 Future<List<SearchMatch>> searchMemberDeclarations(String pattern) { |
36 List<SearchMatch> matches; | 39 return _searchDefinedNames(pattern, IndexNameKind.classMember); |
37 { | 40 } |
38 IndexableName indexableName = new IndexableName(name); | 41 |
39 _Requestor requestor = new _Requestor(_index); | 42 @override |
40 requestor.add(indexableName, IndexConstants.NAME_IS_DEFINED_BY, | 43 Future<List<SearchMatch>> searchMemberReferences(String name) async { |
41 MatchKind.DECLARATION); | 44 List<Location> locations = await _index.getUnresolvedMemberReferences(name); |
42 matches = await requestor.merge(); | 45 return locations.map((location) { |
43 } | 46 return _newMatchForLocation(location, null); |
44 return matches.where((match) { | |
45 return match.element.enclosingElement is ClassElement; | |
46 }).toList(); | 47 }).toList(); |
47 } | 48 } |
48 | 49 |
49 @override | 50 @override |
50 Future<List<SearchMatch>> searchMemberReferences(String name) { | |
51 IndexableName indexableName = new IndexableName(name); | |
52 _Requestor requestor = new _Requestor(_index); | |
53 requestor.add( | |
54 indexableName, IndexConstants.IS_INVOKED_BY, MatchKind.INVOCATION); | |
55 requestor.add(indexableName, IndexConstants.IS_READ_BY, MatchKind.READ); | |
56 requestor.add( | |
57 indexableName, IndexConstants.IS_READ_WRITTEN_BY, MatchKind.READ_WRITE); | |
58 requestor.add(indexableName, IndexConstants.IS_WRITTEN_BY, MatchKind.WRITE); | |
59 return requestor.merge(); | |
60 } | |
61 | |
62 @override | |
63 Future<List<SearchMatch>> searchReferences(Element element) { | 51 Future<List<SearchMatch>> searchReferences(Element element) { |
64 if (element.kind == ElementKind.CLASS) { | 52 ElementKind kind = element.kind; |
53 if (kind == ElementKind.CLASS || | |
54 kind == ElementKind.COMPILATION_UNIT || | |
55 kind == ElementKind.CONSTRUCTOR || | |
56 kind == ElementKind.FUNCTION_TYPE_ALIAS || | |
57 kind == ElementKind.SETTER) { | |
65 return _searchReferences(element); | 58 return _searchReferences(element); |
66 } else if (element.kind == ElementKind.COMPILATION_UNIT) { | 59 } else if (kind == ElementKind.GETTER) { |
67 return _searchReferences(element); | 60 return _searchReferences_Getter(element); |
68 } else if (element.kind == ElementKind.CONSTRUCTOR) { | 61 } else if (kind == ElementKind.FIELD || |
69 return _searchReferences_Constructor(element as ConstructorElement); | 62 kind == ElementKind.TOP_LEVEL_VARIABLE) { |
70 } else if (element.kind == ElementKind.FIELD || | 63 return _searchReferences_Field(element); |
71 element.kind == ElementKind.TOP_LEVEL_VARIABLE) { | 64 } else if (kind == ElementKind.FUNCTION || kind == ElementKind.METHOD) { |
72 return _searchReferences_Field(element as PropertyInducingElement); | 65 if (element.enclosingElement is ExecutableElement) { |
73 } else if (element.kind == ElementKind.FUNCTION) { | 66 return _searchReferences_Local(element, (n) => n is Block); |
74 return _searchReferences_Function(element as FunctionElement); | 67 } |
75 } else if (element.kind == ElementKind.GETTER || | 68 return _searchReferences_Function(element); |
76 element.kind == ElementKind.SETTER) { | 69 } else if (kind == ElementKind.IMPORT) { |
77 return _searchReferences(element); | 70 return _searchReferences_Import(element); |
78 } else if (element.kind == ElementKind.IMPORT) { | 71 } else if (kind == ElementKind.LABEL || |
79 return _searchReferences(element); | 72 kind == ElementKind.LOCAL_VARIABLE) { |
80 } else if (element.kind == ElementKind.LABEL) { | 73 return _searchReferences_Local(element, (n) => n is Block); |
81 return _searchReferences(element); | 74 } else if (kind == ElementKind.LIBRARY) { |
82 } else if (element.kind == ElementKind.LIBRARY) { | 75 return _searchReferences_Library(element); |
83 return _searchReferences(element); | 76 } else if (kind == ElementKind.PARAMETER) { |
84 } else if (element.kind == ElementKind.LOCAL_VARIABLE) { | 77 return _searchReferences_Parameter(element); |
85 return _searchReferences_LocalVariable(element as LocalVariableElement); | 78 } else if (kind == ElementKind.PREFIX) { |
86 } else if (element.kind == ElementKind.METHOD) { | 79 return _searchReferences_Prefix(element); |
87 return _searchReferences_Method(element as MethodElement); | 80 } else if (kind == ElementKind.TYPE_PARAMETER) { |
88 } else if (element.kind == ElementKind.PARAMETER) { | 81 return _searchReferences_Local(element, (n) => n is ClassDeclaration); |
89 return _searchReferences_Parameter(element as ParameterElement); | |
90 } else if (element.kind == ElementKind.PREFIX) { | |
91 return _searchReferences(element); | |
92 } else if (element.kind == ElementKind.FUNCTION_TYPE_ALIAS) { | |
93 return _searchReferences(element); | |
94 } else if (element.kind == ElementKind.TYPE_PARAMETER) { | |
95 return _searchReferences(element); | |
96 } | 82 } |
97 return new Future.value(<SearchMatch>[]); | 83 return new Future.value(<SearchMatch>[]); |
98 } | 84 } |
99 | 85 |
100 @override | 86 @override |
101 Future<List<SearchMatch>> searchSubtypes(ClassElement type) { | 87 Future<List<SearchMatch>> searchSubtypes(ClassElement type) async { |
102 _Requestor requestor = new _Requestor(_index); | 88 List<SearchMatch> matches = <SearchMatch>[]; |
103 requestor.addElement( | 89 await _addMatches( |
104 type, IndexConstants.IS_EXTENDED_BY, MatchKind.REFERENCE); | 90 matches, type, IndexRelationKind.IS_EXTENDED_BY, MatchKind.REFERENCE); |
105 requestor.addElement( | 91 await _addMatches( |
106 type, IndexConstants.IS_MIXED_IN_BY, MatchKind.REFERENCE); | 92 matches, type, IndexRelationKind.IS_MIXED_IN_BY, MatchKind.REFERENCE); |
107 requestor.addElement( | 93 await _addMatches(matches, type, IndexRelationKind.IS_IMPLEMENTED_BY, |
108 type, IndexConstants.IS_IMPLEMENTED_BY, MatchKind.REFERENCE); | 94 MatchKind.REFERENCE); |
109 return requestor.merge(); | 95 return matches; |
110 } | 96 } |
111 | 97 |
112 @override | 98 @override |
113 Future<List<SearchMatch>> searchTopLevelDeclarations(String pattern) { | 99 Future<List<SearchMatch>> searchTopLevelDeclarations(String pattern) { |
100 return _searchDefinedNames(pattern, IndexNameKind.topLevel); | |
101 } | |
102 | |
103 _addMatches(List<SearchMatch> matches, Element element, | |
104 IndexRelationKind relationKind, MatchKind kind) async { | |
105 List<Location> locations = await _index.getRelations(element, relationKind); | |
106 for (Location location in locations) { | |
107 SearchMatch match = _newMatchForLocation(location, kind); | |
108 matches.add(match); | |
109 } | |
110 } | |
111 | |
112 SearchMatch _newMatchForLocation(Location location, MatchKind kind) { | |
113 if (kind == null) { | |
114 IndexRelationKind relationKind = location.kind; | |
115 if (relationKind == IndexRelationKind.IS_INVOKED_BY) { | |
116 kind = MatchKind.INVOCATION; | |
117 } else if (relationKind == IndexRelationKind.IS_REFERENCED_BY) { | |
118 kind = MatchKind.REFERENCE; | |
119 } else if (relationKind == IndexRelationKind.IS_READ_BY) { | |
120 kind = MatchKind.READ; | |
121 } else if (relationKind == IndexRelationKind.IS_READ_WRITTEN_BY) { | |
122 kind = MatchKind.READ_WRITE; | |
123 } else if (relationKind == IndexRelationKind.IS_WRITTEN_BY) { | |
124 kind = MatchKind.WRITE; | |
125 } else { | |
126 throw new ArgumentError('Unsupported relation kind $relationKind'); | |
127 } | |
128 } | |
129 return new SearchMatch( | |
130 location.context, | |
131 location.libraryUri, | |
132 location.unitUri, | |
133 kind, | |
134 new SourceRange(location.offset, location.length), | |
135 location.isResolved, | |
136 location.isQualified); | |
137 } | |
138 | |
139 Future<List<SearchMatch>> _searchDefinedNames( | |
140 String pattern, IndexNameKind nameKind) async { | |
114 RegExp regExp = new RegExp(pattern); | 141 RegExp regExp = new RegExp(pattern); |
115 List<Element> elements = | 142 List<Location> locations = await _index.getDefinedNames(regExp, nameKind); |
116 _index.getTopLevelDeclarations((String name) => regExp.hasMatch(name)); | 143 return locations.map((location) { |
117 List<SearchMatch> matches = <SearchMatch>[]; | 144 return _newMatchForLocation(location, MatchKind.DECLARATION); |
118 for (Element element in elements) { | 145 }).toList(); |
119 matches.add(new SearchMatch( | 146 } |
120 element.context, | 147 |
121 element.library.source.uri.toString(), | 148 Future<List<SearchMatch>> _searchReferences(Element element) async { |
122 element.source.uri.toString(), | 149 List<SearchMatch> matches = <SearchMatch>[]; |
123 MatchKind.DECLARATION, | 150 await _addMatches(matches, element, IndexRelationKind.IS_REFERENCED_BY, |
124 rangeElementName(element), | 151 MatchKind.REFERENCE); |
125 true, | 152 return matches; |
126 false)); | |
127 } | |
128 return new Future.value(matches); | |
129 } | |
130 | |
131 Future<List<SearchMatch>> _searchReferences(Element element) { | |
132 _Requestor requestor = new _Requestor(_index); | |
133 requestor.addElement( | |
134 element, IndexConstants.IS_REFERENCED_BY, MatchKind.REFERENCE); | |
135 return requestor.merge(); | |
136 } | |
137 | |
138 Future<List<SearchMatch>> _searchReferences_Constructor( | |
139 ConstructorElement constructor) { | |
140 _Requestor requestor = new _Requestor(_index); | |
141 requestor.addElement( | |
142 constructor, IndexConstants.IS_REFERENCED_BY, MatchKind.REFERENCE); | |
143 return requestor.merge(); | |
144 } | 153 } |
145 | 154 |
146 Future<List<SearchMatch>> _searchReferences_Field( | 155 Future<List<SearchMatch>> _searchReferences_Field( |
147 PropertyInducingElement field) { | 156 PropertyInducingElement field) async { |
157 List<SearchMatch> matches = <SearchMatch>[]; | |
148 PropertyAccessorElement getter = field.getter; | 158 PropertyAccessorElement getter = field.getter; |
149 PropertyAccessorElement setter = field.setter; | 159 PropertyAccessorElement setter = field.setter; |
150 _Requestor requestor = new _Requestor(_index); | |
151 // field itself | 160 // field itself |
152 requestor.addElement( | 161 if (!field.isSynthetic) { |
153 field, IndexConstants.IS_REFERENCED_BY, MatchKind.REFERENCE); | 162 await _addMatches( |
154 requestor.addElement(field, IndexConstants.IS_WRITTEN_BY, MatchKind.WRITE); | 163 matches, field, IndexRelationKind.IS_WRITTEN_BY, MatchKind.WRITE); |
164 await _addMatches(matches, field, IndexRelationKind.IS_REFERENCED_BY, | |
165 MatchKind.REFERENCE); | |
166 } | |
155 // getter | 167 // getter |
156 if (getter != null) { | 168 if (getter != null) { |
157 requestor.addElement( | 169 await _addMatches( |
158 getter, IndexConstants.IS_REFERENCED_BY, MatchKind.READ); | 170 matches, getter, IndexRelationKind.IS_REFERENCED_BY, MatchKind.READ); |
159 requestor.addElement( | 171 await _addMatches(matches, getter, IndexRelationKind.IS_INVOKED_BY, |
160 getter, IndexConstants.IS_INVOKED_BY, MatchKind.INVOCATION); | 172 MatchKind.INVOCATION); |
161 } | 173 } |
162 // setter | 174 // setter |
163 if (setter != null) { | 175 if (setter != null) { |
164 requestor.addElement( | 176 await _addMatches( |
165 setter, IndexConstants.IS_REFERENCED_BY, MatchKind.WRITE); | 177 matches, setter, IndexRelationKind.IS_REFERENCED_BY, MatchKind.WRITE); |
166 } | 178 } |
167 // done | 179 // done |
168 return requestor.merge(); | 180 return matches; |
169 } | 181 } |
170 | 182 |
171 Future<List<SearchMatch>> _searchReferences_Function( | 183 Future<List<SearchMatch>> _searchReferences_Function(Element element) async { |
172 FunctionElement function) { | 184 if (element is Member) { |
173 _Requestor requestor = new _Requestor(_index); | 185 element = (element as Member).baseElement; |
174 requestor.addElement( | 186 } |
175 function, IndexConstants.IS_REFERENCED_BY, MatchKind.REFERENCE); | 187 List<SearchMatch> matches = <SearchMatch>[]; |
176 requestor.addElement( | 188 await _addMatches(matches, element, IndexRelationKind.IS_REFERENCED_BY, |
177 function, IndexConstants.IS_INVOKED_BY, MatchKind.INVOCATION); | 189 MatchKind.REFERENCE); |
178 return requestor.merge(); | 190 await _addMatches(matches, element, IndexRelationKind.IS_INVOKED_BY, |
179 } | 191 MatchKind.INVOCATION); |
180 | 192 return matches; |
181 Future<List<SearchMatch>> _searchReferences_LocalVariable( | 193 } |
182 LocalVariableElement variable) { | 194 |
183 _Requestor requestor = new _Requestor(_index); | 195 Future<List<SearchMatch>> _searchReferences_Getter( |
184 requestor.addElement(variable, IndexConstants.IS_READ_BY, MatchKind.READ); | 196 PropertyAccessorElement getter) async { |
185 requestor.addElement( | 197 List<SearchMatch> matches = <SearchMatch>[]; |
186 variable, IndexConstants.IS_READ_WRITTEN_BY, MatchKind.READ_WRITE); | 198 await _addMatches(matches, getter, IndexRelationKind.IS_REFERENCED_BY, |
187 requestor.addElement( | 199 MatchKind.REFERENCE); |
188 variable, IndexConstants.IS_WRITTEN_BY, MatchKind.WRITE); | 200 await _addMatches( |
189 requestor.addElement( | 201 matches, getter, IndexRelationKind.IS_INVOKED_BY, MatchKind.INVOCATION); |
190 variable, IndexConstants.IS_INVOKED_BY, MatchKind.INVOCATION); | 202 return matches; |
191 return requestor.merge(); | 203 } |
192 } | 204 |
193 | 205 Future<List<SearchMatch>> _searchReferences_Import( |
194 Future<List<SearchMatch>> _searchReferences_Method(MethodElement method) { | 206 ImportElement element) async { |
195 _Requestor requestor = new _Requestor(_index); | 207 List<SearchMatch> matches = <SearchMatch>[]; |
196 if (method is MethodMember) { | 208 LibraryElement libraryElement = element.library; |
197 method = (method as MethodMember).baseElement; | 209 Source librarySource = libraryElement.source; |
198 } | 210 AnalysisContext context = libraryElement.context; |
199 requestor.addElement( | 211 for (CompilationUnitElement unitElement in libraryElement.units) { |
200 method, IndexConstants.IS_REFERENCED_BY, MatchKind.REFERENCE); | 212 Source unitSource = unitElement.source; |
201 requestor.addElement( | 213 CompilationUnit unit = |
202 method, IndexConstants.IS_INVOKED_BY, MatchKind.INVOCATION); | 214 context.resolveCompilationUnit2(unitSource, librarySource); |
203 return requestor.merge(); | 215 _ImportElementReferencesVisitor visitor = |
216 new _ImportElementReferencesVisitor( | |
217 element, unitSource.uri.toString()); | |
218 unit.accept(visitor); | |
219 matches.addAll(visitor.matches); | |
220 } | |
221 return matches; | |
222 } | |
223 | |
224 Future<List<SearchMatch>> _searchReferences_Library(Element element) async { | |
225 List<SearchMatch> matches = <SearchMatch>[]; | |
226 LibraryElement libraryElement = element.library; | |
227 Source librarySource = libraryElement.source; | |
228 AnalysisContext context = libraryElement.context; | |
229 for (CompilationUnitElement unitElement in libraryElement.parts) { | |
230 Source unitSource = unitElement.source; | |
231 CompilationUnit unit = | |
232 context.resolveCompilationUnit2(unitSource, librarySource); | |
233 for (Directive directive in unit.directives) { | |
234 if (directive is PartOfDirective && | |
235 directive.element == libraryElement) { | |
236 matches.add(new SearchMatch( | |
237 context, | |
238 librarySource.uri.toString(), | |
239 unitSource.uri.toString(), | |
240 MatchKind.REFERENCE, | |
241 rangeNode(directive.libraryName), | |
242 true, | |
243 false)); | |
244 } | |
245 } | |
246 } | |
247 return matches; | |
248 } | |
249 | |
250 Future<List<SearchMatch>> _searchReferences_Local( | |
251 Element element, bool isRootNode(AstNode n)) async { | |
252 _LocalReferencesVisitor visitor = new _LocalReferencesVisitor(element); | |
253 AstNode node = element.computeNode(); | |
254 AstNode enclosingNode = node?.getAncestor(isRootNode); | |
255 enclosingNode?.accept(visitor); | |
256 return visitor.matches; | |
204 } | 257 } |
205 | 258 |
206 Future<List<SearchMatch>> _searchReferences_Parameter( | 259 Future<List<SearchMatch>> _searchReferences_Parameter( |
207 ParameterElement parameter) { | 260 ParameterElement parameter) async { |
208 _Requestor requestor = new _Requestor(_index); | 261 List<SearchMatch> matches = <SearchMatch>[]; |
209 requestor.addElement(parameter, IndexConstants.IS_READ_BY, MatchKind.READ); | 262 matches.addAll(await _searchReferences(parameter)); |
210 requestor.addElement( | 263 matches.addAll(await _searchReferences_Local( |
211 parameter, IndexConstants.IS_READ_WRITTEN_BY, MatchKind.READ_WRITE); | 264 parameter, (n) => n is MethodDeclaration || n is FunctionExpression)); |
212 requestor.addElement( | 265 return matches; |
213 parameter, IndexConstants.IS_WRITTEN_BY, MatchKind.WRITE); | 266 } |
214 requestor.addElement( | 267 |
215 parameter, IndexConstants.IS_REFERENCED_BY, MatchKind.REFERENCE); | 268 Future<List<SearchMatch>> _searchReferences_Prefix( |
216 requestor.addElement( | 269 PrefixElement element) async { |
217 parameter, IndexConstants.IS_INVOKED_BY, MatchKind.INVOCATION); | 270 List<SearchMatch> matches = <SearchMatch>[]; |
218 return requestor.merge(); | 271 LibraryElement libraryElement = element.library; |
272 Source librarySource = libraryElement.source; | |
273 AnalysisContext context = libraryElement.context; | |
274 for (CompilationUnitElement unitElement in libraryElement.units) { | |
275 Source unitSource = unitElement.source; | |
276 CompilationUnit unit = | |
277 context.resolveCompilationUnit2(unitSource, librarySource); | |
278 _LocalReferencesVisitor visitor = | |
279 new _LocalReferencesVisitor(element, unitSource.uri.toString()); | |
280 unit.accept(visitor); | |
281 matches.addAll(visitor.matches); | |
282 } | |
283 return matches; | |
219 } | 284 } |
220 } | 285 } |
221 | 286 |
222 class _Requestor { | 287 /** |
223 final List<Future<List<SearchMatch>>> futures = <Future<List<SearchMatch>>>[]; | 288 * Visitor that adds [SearchMatch]es for [importElement], both with an explicit |
224 final Index index; | 289 * prefix or an implicit one. |
225 | 290 */ |
226 _Requestor(this.index); | 291 class _ImportElementReferencesVisitor extends RecursiveAstVisitor { |
227 | 292 final List<SearchMatch> matches = <SearchMatch>[]; |
228 void add(IndexableObject indexable, RelationshipImpl relationship, | 293 |
229 MatchKind kind) { | 294 final ImportElement importElement; |
230 Future relationsFuture = index.getRelationships(indexable, relationship); | 295 final AnalysisContext context; |
231 Future matchesFuture = relationsFuture.then((List<LocationImpl> locations) { | 296 final String libraryUri; |
232 List<SearchMatch> matches = <SearchMatch>[]; | 297 final String unitUri; |
233 for (LocationImpl location in locations) { | 298 Set<Element> importedElements; |
234 IndexableObject indexable = location.indexable; | 299 |
235 if (indexable is IndexableElement) { | 300 _ImportElementReferencesVisitor(ImportElement element, this.unitUri) |
236 Element element = indexable.element; | 301 : importElement = element, |
237 matches.add(new SearchMatch( | 302 context = element.context, |
238 element.context, | 303 libraryUri = element.library.source.uri.toString() { |
239 element.library.source.uri.toString(), | 304 importedElements = new NamespaceBuilder() |
240 element.source.uri.toString(), | 305 .createImportNamespaceForDirective(element) |
241 kind, | 306 .definedNames |
242 new SourceRange(location.offset, location.length), | 307 .values |
243 location.isResolved, | 308 .toSet(); |
244 location.isQualified)); | 309 } |
245 } | 310 |
246 } | 311 @override |
247 return matches; | 312 visitExportDirective(ExportDirective node) {} |
248 }); | 313 |
249 futures.add(matchesFuture); | 314 @override |
250 } | 315 visitImportDirective(ImportDirective node) {} |
251 | 316 |
252 void addElement( | 317 @override |
253 Element element, RelationshipImpl relationship, MatchKind kind) { | 318 visitSimpleIdentifier(SimpleIdentifier node) { |
254 IndexableElement indexable = new IndexableElement(element); | 319 if (node.inDeclarationContext()) { |
255 add(indexable, relationship, kind); | 320 return; |
256 } | 321 } |
257 | 322 if (importElement.prefix != null) { |
258 Future<List<SearchMatch>> merge() { | 323 if (node.staticElement == importElement.prefix) { |
259 return Future.wait(futures).then((List<List<SearchMatch>> matchesList) { | 324 AstNode parent = node.parent; |
260 return matchesList.expand((matches) => matches).toList(); | 325 if (parent is PrefixedIdentifier && parent.prefix == node) { |
261 }); | 326 if (importedElements.contains(parent.staticElement)) { |
327 _addMatchForPrefix(node, parent.identifier); | |
328 } | |
329 } | |
330 if (parent is MethodInvocation && parent.target == node) { | |
331 if (importedElements.contains(parent.methodName.staticElement)) { | |
332 _addMatchForPrefix(node, parent.methodName); | |
333 } | |
334 } | |
335 } | |
336 } else { | |
337 if (importedElements.contains(node.staticElement)) { | |
338 SourceRange range = rangeStartLength(node, 0); | |
339 _addMatchForRange(range); | |
340 } | |
341 } | |
342 } | |
343 | |
344 void _addMatchForPrefix(SimpleIdentifier prefixNode, AstNode nextNode) { | |
345 SourceRange range = rangeStartStart(prefixNode, nextNode); | |
346 _addMatchForRange(range); | |
347 } | |
348 | |
349 void _addMatchForRange(SourceRange range) { | |
350 matches.add(new SearchMatch( | |
351 context, libraryUri, unitUri, MatchKind.REFERENCE, range, true, false)); | |
262 } | 352 } |
263 } | 353 } |
354 | |
355 /** | |
356 * Visitor that adds [SearchMatch]es for local elements of a block, method, | |
357 * class or a library - labels, local functions, local variables and parameters, | |
358 * type parameters, import prefixes. | |
359 */ | |
360 class _LocalReferencesVisitor extends RecursiveAstVisitor { | |
361 final List<SearchMatch> matches = <SearchMatch>[]; | |
362 | |
363 final Element element; | |
364 final AnalysisContext context; | |
365 final String libraryUri; | |
366 final String unitUri; | |
367 | |
368 _LocalReferencesVisitor(Element element, [String unitUri]) | |
369 : element = element, | |
370 context = element.context, | |
371 libraryUri = element.library.source.uri.toString(), | |
372 unitUri = unitUri ?? element.source.uri.toString(); | |
373 | |
374 @override | |
375 visitSimpleIdentifier(SimpleIdentifier node) { | |
376 if (node.inDeclarationContext()) { | |
377 return; | |
378 } | |
379 if (node.bestElement == element) { | |
380 AstNode parent = node.parent; | |
381 MatchKind kind = MatchKind.REFERENCE; | |
382 if (element is FunctionElement) { | |
383 if (parent is MethodInvocation && parent.methodName == node) { | |
384 kind = MatchKind.INVOCATION; | |
385 } | |
386 } else if (element is VariableElement) { | |
387 bool isGet = node.inGetterContext(); | |
388 bool isSet = node.inSetterContext(); | |
389 if (isGet && isSet) { | |
390 kind = MatchKind.READ_WRITE; | |
391 } else if (isGet) { | |
392 if (parent is MethodInvocation && parent.methodName == node) { | |
393 kind = MatchKind.INVOCATION; | |
394 } else { | |
395 kind = MatchKind.READ; | |
396 } | |
397 } else if (isSet) { | |
398 kind = MatchKind.WRITE; | |
399 } | |
400 } | |
401 _addMatch(node, kind); | |
402 } | |
403 } | |
404 | |
405 void _addMatch(AstNode node, MatchKind kind) { | |
406 bool isQualified = node is SimpleIdentifier && node.isQualified; | |
407 matches.add(new SearchMatch(context, libraryUri, unitUri, kind, | |
408 rangeNode(node), true, isQualified)); | |
409 } | |
410 } | |
OLD | NEW |