Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: pkg/analysis_server/lib/src/services/search/search_engine_internal2.dart

Issue 1801883002: Remove old index and search implementations. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library services.src.search.search_engine2;
6
7 import 'dart:async';
8
9 import 'package:analysis_server/src/services/correction/source_range.dart';
10 import 'package:analysis_server/src/services/index2/index2.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';
15 import 'package:analyzer/src/dart/element/member.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';
20
21 /**
22 * A [SearchEngine] implementation.
23 */
24 class SearchEngineImpl2 implements SearchEngine {
25 final Index2 _index;
26
27 SearchEngineImpl2(this._index);
28
29 @override
30 Future<List<SearchMatch>> searchAllSubtypes(ClassElement type) async {
31 List<SearchMatch> matches = <SearchMatch>[];
32 await _addMatches(
33 matches, type, IndexRelationKind.IS_ANCESTOR_OF, MatchKind.DECLARATION);
34 return matches;
35 }
36
37 @override
38 Future<List<SearchMatch>> searchMemberDeclarations(String pattern) {
39 return _searchDefinedNames(pattern, IndexNameKind.classMember);
40 }
41
42 @override
43 Future<List<SearchMatch>> searchMemberReferences(String name) async {
44 List<Location> locations = await _index.getUnresolvedMemberReferences(name);
45 return locations.map((location) {
46 return _newMatchForLocation(location, null);
47 }).toList();
48 }
49
50 @override
51 Future<List<SearchMatch>> searchReferences(Element element) {
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) {
58 return _searchReferences(element);
59 } else if (kind == ElementKind.GETTER) {
60 return _searchReferences_Getter(element);
61 } else if (kind == ElementKind.FIELD ||
62 kind == ElementKind.TOP_LEVEL_VARIABLE) {
63 return _searchReferences_Field(element);
64 } else if (kind == ElementKind.FUNCTION || kind == ElementKind.METHOD) {
65 if (element.enclosingElement is ExecutableElement) {
66 return _searchReferences_Local(element, (n) => n is Block);
67 }
68 return _searchReferences_Function(element);
69 } else if (kind == ElementKind.IMPORT) {
70 return _searchReferences_Import(element);
71 } else if (kind == ElementKind.LABEL ||
72 kind == ElementKind.LOCAL_VARIABLE) {
73 return _searchReferences_Local(element, (n) => n is Block);
74 } else if (kind == ElementKind.LIBRARY) {
75 return _searchReferences_Library(element);
76 } else if (kind == ElementKind.PARAMETER) {
77 return _searchReferences_Parameter(element);
78 } else if (kind == ElementKind.PREFIX) {
79 return _searchReferences_Prefix(element);
80 } else if (kind == ElementKind.TYPE_PARAMETER) {
81 return _searchReferences_Local(element, (n) => n is ClassDeclaration);
82 }
83 return new Future.value(<SearchMatch>[]);
84 }
85
86 @override
87 Future<List<SearchMatch>> searchSubtypes(ClassElement type) async {
88 List<SearchMatch> matches = <SearchMatch>[];
89 await _addMatches(
90 matches, type, IndexRelationKind.IS_EXTENDED_BY, MatchKind.REFERENCE);
91 await _addMatches(
92 matches, type, IndexRelationKind.IS_MIXED_IN_BY, MatchKind.REFERENCE);
93 await _addMatches(matches, type, IndexRelationKind.IS_IMPLEMENTED_BY,
94 MatchKind.REFERENCE);
95 return matches;
96 }
97
98 @override
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 {
141 RegExp regExp = new RegExp(pattern);
142 List<Location> locations = await _index.getDefinedNames(regExp, nameKind);
143 return locations.map((location) {
144 return _newMatchForLocation(location, MatchKind.DECLARATION);
145 }).toList();
146 }
147
148 Future<List<SearchMatch>> _searchReferences(Element element) async {
149 List<SearchMatch> matches = <SearchMatch>[];
150 await _addMatches(matches, element, IndexRelationKind.IS_REFERENCED_BY,
151 MatchKind.REFERENCE);
152 return matches;
153 }
154
155 Future<List<SearchMatch>> _searchReferences_Field(
156 PropertyInducingElement field) async {
157 List<SearchMatch> matches = <SearchMatch>[];
158 PropertyAccessorElement getter = field.getter;
159 PropertyAccessorElement setter = field.setter;
160 // field itself
161 if (!field.isSynthetic) {
162 await _addMatches(
163 matches, field, IndexRelationKind.IS_WRITTEN_BY, MatchKind.WRITE);
164 await _addMatches(matches, field, IndexRelationKind.IS_REFERENCED_BY,
165 MatchKind.REFERENCE);
166 }
167 // getter
168 if (getter != null) {
169 await _addMatches(
170 matches, getter, IndexRelationKind.IS_REFERENCED_BY, MatchKind.READ);
171 await _addMatches(matches, getter, IndexRelationKind.IS_INVOKED_BY,
172 MatchKind.INVOCATION);
173 }
174 // setter
175 if (setter != null) {
176 await _addMatches(
177 matches, setter, IndexRelationKind.IS_REFERENCED_BY, MatchKind.WRITE);
178 }
179 // done
180 return matches;
181 }
182
183 Future<List<SearchMatch>> _searchReferences_Function(Element element) async {
184 if (element is Member) {
185 element = (element as Member).baseElement;
186 }
187 List<SearchMatch> matches = <SearchMatch>[];
188 await _addMatches(matches, element, IndexRelationKind.IS_REFERENCED_BY,
189 MatchKind.REFERENCE);
190 await _addMatches(matches, element, IndexRelationKind.IS_INVOKED_BY,
191 MatchKind.INVOCATION);
192 return matches;
193 }
194
195 Future<List<SearchMatch>> _searchReferences_Getter(
196 PropertyAccessorElement getter) async {
197 List<SearchMatch> matches = <SearchMatch>[];
198 await _addMatches(matches, getter, IndexRelationKind.IS_REFERENCED_BY,
199 MatchKind.REFERENCE);
200 await _addMatches(
201 matches, getter, IndexRelationKind.IS_INVOKED_BY, MatchKind.INVOCATION);
202 return matches;
203 }
204
205 Future<List<SearchMatch>> _searchReferences_Import(
206 ImportElement element) async {
207 List<SearchMatch> matches = <SearchMatch>[];
208 LibraryElement libraryElement = element.library;
209 Source librarySource = libraryElement.source;
210 AnalysisContext context = libraryElement.context;
211 for (CompilationUnitElement unitElement in libraryElement.units) {
212 Source unitSource = unitElement.source;
213 CompilationUnit unit =
214 context.resolveCompilationUnit2(unitSource, librarySource);
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;
257 }
258
259 Future<List<SearchMatch>> _searchReferences_Parameter(
260 ParameterElement parameter) async {
261 List<SearchMatch> matches = <SearchMatch>[];
262 matches.addAll(await _searchReferences(parameter));
263 matches.addAll(await _searchReferences_Local(
264 parameter, (n) => n is MethodDeclaration || n is FunctionExpression));
265 return matches;
266 }
267
268 Future<List<SearchMatch>> _searchReferences_Prefix(
269 PrefixElement element) async {
270 List<SearchMatch> matches = <SearchMatch>[];
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;
284 }
285 }
286
287 /**
288 * Visitor that adds [SearchMatch]es for [importElement], both with an explicit
289 * prefix or an implicit one.
290 */
291 class _ImportElementReferencesVisitor extends RecursiveAstVisitor {
292 final List<SearchMatch> matches = <SearchMatch>[];
293
294 final ImportElement importElement;
295 final AnalysisContext context;
296 final String libraryUri;
297 final String unitUri;
298 Set<Element> importedElements;
299
300 _ImportElementReferencesVisitor(ImportElement element, this.unitUri)
301 : importElement = element,
302 context = element.context,
303 libraryUri = element.library.source.uri.toString() {
304 importedElements = new NamespaceBuilder()
305 .createImportNamespaceForDirective(element)
306 .definedNames
307 .values
308 .toSet();
309 }
310
311 @override
312 visitExportDirective(ExportDirective node) {}
313
314 @override
315 visitImportDirective(ImportDirective node) {}
316
317 @override
318 visitSimpleIdentifier(SimpleIdentifier node) {
319 if (node.inDeclarationContext()) {
320 return;
321 }
322 if (importElement.prefix != null) {
323 if (node.staticElement == importElement.prefix) {
324 AstNode parent = node.parent;
325 if (parent is PrefixedIdentifier && parent.prefix == node) {
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));
352 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698