mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 10:49:00 +00:00
Remove unused implementation of SearchEngine and rename the driver-based implementation
R=devoncarew@google.com Review-Url: https://codereview.chromium.org/2949823002 .
This commit is contained in:
parent
6e89075952
commit
53e01faa43
8 changed files with 159 additions and 743 deletions
|
@ -35,7 +35,7 @@ import 'package:analysis_server/src/server/diagnostic_server.dart';
|
|||
import 'package:analysis_server/src/services/correction/namespace.dart';
|
||||
import 'package:analysis_server/src/services/index/index.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal2.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal.dart';
|
||||
import 'package:analysis_server/src/single_context_manager.dart';
|
||||
import 'package:analysis_server/src/utilities/null_string_sink.dart';
|
||||
import 'package:analyzer/context/context_root.dart';
|
||||
|
@ -481,7 +481,7 @@ class AnalysisServer {
|
|||
});
|
||||
});
|
||||
_setupIndexInvalidation();
|
||||
searchEngine = new SearchEngineImpl2(driverMap.values);
|
||||
searchEngine = new SearchEngineImpl(driverMap.values);
|
||||
Notification notification = new ServerConnectedParams(VERSION, io.pid,
|
||||
sessionId: instrumentationService.sessionId)
|
||||
.toNotification();
|
||||
|
|
|
@ -100,13 +100,15 @@ class RenameConstructorRefactoringImpl extends RenameRefactoringImpl {
|
|||
sourceRange = new SourceRange(element.nameEnd, 0);
|
||||
}
|
||||
return new SourceReference(new SearchMatchImpl(
|
||||
element.context,
|
||||
element.library.source.uri.toString(),
|
||||
element.source.uri.toString(),
|
||||
MatchKind.DECLARATION,
|
||||
sourceRange,
|
||||
element.source.fullName,
|
||||
element.library.source,
|
||||
element.source,
|
||||
element.library,
|
||||
element,
|
||||
true,
|
||||
true));
|
||||
true,
|
||||
MatchKind.DECLARATION,
|
||||
sourceRange));
|
||||
}
|
||||
|
||||
Future<Null> _replaceSynthetic() async {
|
||||
|
|
|
@ -4,414 +4,141 @@
|
|||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:analysis_server/src/services/index/index.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine.dart';
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/visitor.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/visitor.dart';
|
||||
import 'package:analyzer/src/dart/element/ast_provider.dart';
|
||||
import 'package:analyzer/src/dart/element/element.dart';
|
||||
import 'package:analyzer/src/dart/element/member.dart';
|
||||
import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
|
||||
import 'package:analyzer/src/generated/resolver.dart' show NamespaceBuilder;
|
||||
import 'package:analyzer/src/dart/analysis/driver.dart';
|
||||
import 'package:analyzer/src/dart/analysis/search.dart';
|
||||
import 'package:analyzer/src/generated/source.dart' show Source, SourceRange;
|
||||
import 'package:analyzer/src/generated/utilities_general.dart';
|
||||
import 'package:analyzer/src/summary/idl.dart';
|
||||
import 'package:analyzer_plugin/utilities/range_factory.dart';
|
||||
|
||||
/**
|
||||
* The type of a function that returns the [AstProvider] managing the [file].
|
||||
*/
|
||||
typedef AstProvider GetAstProvider(String file);
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/**
|
||||
* A [SearchEngine] implementation.
|
||||
*/
|
||||
class SearchEngineImpl implements SearchEngine {
|
||||
final Index _index;
|
||||
final GetAstProvider _getAstProvider;
|
||||
final Iterable<AnalysisDriver> _drivers;
|
||||
|
||||
SearchEngineImpl(this._index, this._getAstProvider);
|
||||
SearchEngineImpl(this._drivers);
|
||||
|
||||
@override
|
||||
Future<Set<ClassElement>> searchAllSubtypes(ClassElement type) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
await _addMatches(
|
||||
matches, type, IndexRelationKind.IS_ANCESTOR_OF, MatchKind.DECLARATION);
|
||||
return matches.map((match) => match.element as ClassElement).toSet();
|
||||
Set<ClassElement> allSubtypes = new Set<ClassElement>();
|
||||
|
||||
Future<Null> addSubtypes(ClassElement type) async {
|
||||
List<SearchResult> directResults = await _searchDirectSubtypes(type);
|
||||
for (SearchResult directResult in directResults) {
|
||||
var directSubtype = directResult.enclosingElement as ClassElement;
|
||||
if (allSubtypes.add(directSubtype)) {
|
||||
await addSubtypes(directSubtype);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await addSubtypes(type);
|
||||
return allSubtypes;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchMemberDeclarations(String name) {
|
||||
String pattern = '^$name\$';
|
||||
return _searchDefinedNames(pattern, IndexNameKind.classMember);
|
||||
Future<List<SearchMatch>> searchMemberDeclarations(String name) async {
|
||||
List<SearchMatch> allDeclarations = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<Element> elements = await driver.search.classMembers(name);
|
||||
allDeclarations.addAll(elements.map(SearchMatchImpl.forElement));
|
||||
}
|
||||
return allDeclarations;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchMemberReferences(String name) async {
|
||||
List<Location> locations = await _index.getUnresolvedMemberReferences(name);
|
||||
return locations.map((location) {
|
||||
return _newMatchForLocation(location, null);
|
||||
}).toList();
|
||||
List<SearchResult> allResults = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<SearchResult> results =
|
||||
await driver.search.unresolvedMemberReferences(name);
|
||||
allResults.addAll(results);
|
||||
}
|
||||
return allResults.map(SearchMatchImpl.forSearchResult).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchReferences(Element element) {
|
||||
ElementKind kind = element.kind;
|
||||
if (kind == ElementKind.CLASS ||
|
||||
kind == ElementKind.COMPILATION_UNIT ||
|
||||
kind == ElementKind.CONSTRUCTOR ||
|
||||
kind == ElementKind.FUNCTION_TYPE_ALIAS ||
|
||||
kind == ElementKind.SETTER) {
|
||||
return _searchReferences(element);
|
||||
} else if (kind == ElementKind.GETTER) {
|
||||
return _searchReferences_Getter(element);
|
||||
} else if (kind == ElementKind.FIELD ||
|
||||
kind == ElementKind.TOP_LEVEL_VARIABLE) {
|
||||
return _searchReferences_Field(element);
|
||||
} else if (kind == ElementKind.FUNCTION || kind == ElementKind.METHOD) {
|
||||
if (element.enclosingElement is ExecutableElement) {
|
||||
return _searchReferences_Local(element, (n) => n is Block);
|
||||
}
|
||||
return _searchReferences_Function(element);
|
||||
} else if (kind == ElementKind.IMPORT) {
|
||||
return _searchReferences_Import(element);
|
||||
} else if (kind == ElementKind.LABEL ||
|
||||
kind == ElementKind.LOCAL_VARIABLE) {
|
||||
return _searchReferences_Local(element, (n) => n is Block);
|
||||
} else if (kind == ElementKind.LIBRARY) {
|
||||
return _searchReferences_Library(element);
|
||||
} else if (kind == ElementKind.PARAMETER) {
|
||||
return _searchReferences_Parameter(element);
|
||||
} else if (kind == ElementKind.PREFIX) {
|
||||
return _searchReferences_Prefix(element);
|
||||
} else if (kind == ElementKind.TYPE_PARAMETER) {
|
||||
return _searchReferences_Local(element, (n) => n is ClassDeclaration);
|
||||
Future<List<SearchMatch>> searchReferences(Element element) async {
|
||||
List<SearchResult> allResults = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<SearchResult> results = await driver.search.references(element);
|
||||
allResults.addAll(results);
|
||||
}
|
||||
return new Future.value(<SearchMatch>[]);
|
||||
return allResults.map(SearchMatchImpl.forSearchResult).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchSubtypes(ClassElement type) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
await _addMatches(
|
||||
matches, type, IndexRelationKind.IS_EXTENDED_BY, MatchKind.REFERENCE);
|
||||
await _addMatches(
|
||||
matches, type, IndexRelationKind.IS_MIXED_IN_BY, MatchKind.REFERENCE);
|
||||
await _addMatches(matches, type, IndexRelationKind.IS_IMPLEMENTED_BY,
|
||||
MatchKind.REFERENCE);
|
||||
return matches;
|
||||
List<SearchResult> results = await _searchDirectSubtypes(type);
|
||||
return results.map(SearchMatchImpl.forSearchResult).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchTopLevelDeclarations(String pattern) {
|
||||
return _searchDefinedNames(pattern, IndexNameKind.topLevel);
|
||||
}
|
||||
|
||||
_addMatches(List<SearchMatch> matches, Element element,
|
||||
IndexRelationKind relationKind, MatchKind kind) async {
|
||||
List<Location> locations = await _index.getRelations(element, relationKind);
|
||||
for (Location location in locations) {
|
||||
SearchMatch match = _newMatchForLocation(location, kind);
|
||||
matches.add(match);
|
||||
}
|
||||
}
|
||||
|
||||
SearchMatch _newMatchForLocation(Location location, MatchKind kind) {
|
||||
if (kind == null) {
|
||||
IndexRelationKind relationKind = location.kind;
|
||||
if (relationKind == IndexRelationKind.IS_INVOKED_BY) {
|
||||
kind = MatchKind.INVOCATION;
|
||||
} else if (relationKind == IndexRelationKind.IS_REFERENCED_BY) {
|
||||
kind = MatchKind.REFERENCE;
|
||||
} else if (relationKind == IndexRelationKind.IS_READ_BY) {
|
||||
kind = MatchKind.READ;
|
||||
} else if (relationKind == IndexRelationKind.IS_READ_WRITTEN_BY) {
|
||||
kind = MatchKind.READ_WRITE;
|
||||
} else if (relationKind == IndexRelationKind.IS_WRITTEN_BY) {
|
||||
kind = MatchKind.WRITE;
|
||||
} else {
|
||||
throw new ArgumentError('Unsupported relation kind $relationKind');
|
||||
}
|
||||
}
|
||||
return new SearchMatchImpl(
|
||||
location.context,
|
||||
location.libraryUri,
|
||||
location.unitUri,
|
||||
kind,
|
||||
new SourceRange(location.offset, location.length),
|
||||
location.isResolved,
|
||||
location.isQualified);
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchDefinedNames(
|
||||
String pattern, IndexNameKind nameKind) async {
|
||||
Future<List<SearchMatch>> searchTopLevelDeclarations(String pattern) async {
|
||||
List<SearchMatch> allDeclarations = [];
|
||||
RegExp regExp = new RegExp(pattern);
|
||||
List<Location> locations = await _index.getDefinedNames(regExp, nameKind);
|
||||
return locations.map((location) {
|
||||
return _newMatchForLocation(location, MatchKind.DECLARATION);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences(Element element) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
await _addMatches(matches, element, IndexRelationKind.IS_REFERENCED_BY,
|
||||
MatchKind.REFERENCE);
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Field(
|
||||
PropertyInducingElement field) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
PropertyAccessorElement getter = field.getter;
|
||||
PropertyAccessorElement setter = field.setter;
|
||||
// field itself
|
||||
if (!field.isSynthetic) {
|
||||
await _addMatches(
|
||||
matches, field, IndexRelationKind.IS_WRITTEN_BY, MatchKind.WRITE);
|
||||
await _addMatches(matches, field, IndexRelationKind.IS_REFERENCED_BY,
|
||||
MatchKind.REFERENCE);
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<Element> elements = await driver.search.topLevelElements(regExp);
|
||||
allDeclarations.addAll(elements.map(SearchMatchImpl.forElement));
|
||||
}
|
||||
// getter
|
||||
if (getter != null) {
|
||||
await _addMatches(
|
||||
matches, getter, IndexRelationKind.IS_REFERENCED_BY, MatchKind.READ);
|
||||
await _addMatches(matches, getter, IndexRelationKind.IS_INVOKED_BY,
|
||||
MatchKind.INVOCATION);
|
||||
return allDeclarations;
|
||||
}
|
||||
|
||||
Future<List<SearchResult>> _searchDirectSubtypes(ClassElement type) async {
|
||||
List<SearchResult> allResults = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<SearchResult> results = await driver.search.subTypes(type);
|
||||
allResults.addAll(results);
|
||||
}
|
||||
// setter
|
||||
if (setter != null) {
|
||||
await _addMatches(
|
||||
matches, setter, IndexRelationKind.IS_REFERENCED_BY, MatchKind.WRITE);
|
||||
}
|
||||
// done
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Function(Element element) async {
|
||||
if (element is Member) {
|
||||
element = (element as Member).baseElement;
|
||||
}
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
await _addMatches(matches, element, IndexRelationKind.IS_REFERENCED_BY,
|
||||
MatchKind.REFERENCE);
|
||||
await _addMatches(matches, element, IndexRelationKind.IS_INVOKED_BY,
|
||||
MatchKind.INVOCATION);
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Getter(
|
||||
PropertyAccessorElement getter) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
await _addMatches(matches, getter, IndexRelationKind.IS_REFERENCED_BY,
|
||||
MatchKind.REFERENCE);
|
||||
await _addMatches(
|
||||
matches, getter, IndexRelationKind.IS_INVOKED_BY, MatchKind.INVOCATION);
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Import(
|
||||
ImportElement element) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
LibraryElement libraryElement = element.library;
|
||||
Source librarySource = libraryElement.source;
|
||||
AnalysisContext context = libraryElement.context;
|
||||
for (CompilationUnitElement unitElement in libraryElement.units) {
|
||||
Source unitSource = unitElement.source;
|
||||
CompilationUnit unit =
|
||||
context.resolveCompilationUnit2(unitSource, librarySource);
|
||||
_ImportElementReferencesVisitor visitor =
|
||||
new _ImportElementReferencesVisitor(
|
||||
element, unitSource.uri.toString());
|
||||
unit.accept(visitor);
|
||||
matches.addAll(visitor.matches);
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Library(Element element) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
LibraryElement libraryElement = element.library;
|
||||
Source librarySource = libraryElement.source;
|
||||
AnalysisContext context = libraryElement.context;
|
||||
for (CompilationUnitElement unitElement in libraryElement.parts) {
|
||||
Source unitSource = unitElement.source;
|
||||
CompilationUnit unit =
|
||||
context.resolveCompilationUnit2(unitSource, librarySource);
|
||||
for (Directive directive in unit.directives) {
|
||||
if (directive is PartOfDirective &&
|
||||
directive.element == libraryElement) {
|
||||
matches.add(new SearchMatchImpl(
|
||||
context,
|
||||
librarySource.uri.toString(),
|
||||
unitSource.uri.toString(),
|
||||
MatchKind.REFERENCE,
|
||||
range.node(directive.libraryName),
|
||||
true,
|
||||
false));
|
||||
}
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Local(
|
||||
Element element, bool isRootNode(AstNode n)) async {
|
||||
_LocalReferencesVisitor visitor = new _LocalReferencesVisitor(element);
|
||||
AstProvider astProvider = _getAstProvider(element.source.fullName);
|
||||
AstNode name = await astProvider.getResolvedNameForElement(element);
|
||||
AstNode enclosingNode = name?.getAncestor(isRootNode);
|
||||
enclosingNode?.accept(visitor);
|
||||
return visitor.matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Parameter(
|
||||
ParameterElement parameter) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
matches.addAll(await _searchReferences(parameter));
|
||||
matches.addAll(await _searchReferences_Local(parameter, (AstNode node) {
|
||||
AstNode parent = node.parent;
|
||||
return parent is ClassDeclaration || parent is CompilationUnit;
|
||||
}));
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<List<SearchMatch>> _searchReferences_Prefix(
|
||||
PrefixElement element) async {
|
||||
List<SearchMatch> matches = <SearchMatch>[];
|
||||
LibraryElement libraryElement = element.library;
|
||||
Source librarySource = libraryElement.source;
|
||||
AnalysisContext context = libraryElement.context;
|
||||
for (CompilationUnitElement unitElement in libraryElement.units) {
|
||||
Source unitSource = unitElement.source;
|
||||
CompilationUnit unit =
|
||||
context.resolveCompilationUnit2(unitSource, librarySource);
|
||||
_LocalReferencesVisitor visitor =
|
||||
new _LocalReferencesVisitor(element, unitSource.uri.toString());
|
||||
unit.accept(visitor);
|
||||
matches.addAll(visitor.matches);
|
||||
}
|
||||
return matches;
|
||||
return allResults;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of [SearchMatch].
|
||||
*/
|
||||
@visibleForTesting
|
||||
class SearchMatchImpl implements SearchMatch {
|
||||
/**
|
||||
* The [AnalysisContext] containing the match.
|
||||
*/
|
||||
final AnalysisContext _context;
|
||||
@override
|
||||
final String file;
|
||||
|
||||
/**
|
||||
* The URI of the source of the library containing the match.
|
||||
*/
|
||||
final String libraryUri;
|
||||
@override
|
||||
final Source librarySource;
|
||||
|
||||
/**
|
||||
* The URI of the source of the unit containing the match.
|
||||
*/
|
||||
final String unitUri;
|
||||
@override
|
||||
final Source unitSource;
|
||||
|
||||
/**
|
||||
* The kind of the match.
|
||||
*/
|
||||
final MatchKind kind;
|
||||
@override
|
||||
final LibraryElement libraryElement;
|
||||
|
||||
/**
|
||||
* The source range that was matched.
|
||||
*/
|
||||
final SourceRange sourceRange;
|
||||
@override
|
||||
final Element element;
|
||||
|
||||
/**
|
||||
* Is `true` if the match is a resolved reference to some [Element].
|
||||
*/
|
||||
@override
|
||||
final bool isResolved;
|
||||
|
||||
/**
|
||||
* Is `true` if field or method access is done using qualifier.
|
||||
*/
|
||||
@override
|
||||
final bool isQualified;
|
||||
|
||||
Source _librarySource;
|
||||
Source _unitSource;
|
||||
LibraryElement _libraryElement;
|
||||
Element _element;
|
||||
|
||||
SearchMatchImpl(this._context, this.libraryUri, this.unitUri, this.kind,
|
||||
this.sourceRange, this.isResolved, this.isQualified);
|
||||
|
||||
/**
|
||||
* Return the [Element] containing the match. Can return `null` if the unit
|
||||
* does not exist, or its element was invalidated, or the element cannot be
|
||||
* found, etc.
|
||||
*/
|
||||
Element get element {
|
||||
if (_element == null) {
|
||||
CompilationUnitElement unitElement =
|
||||
_context.getCompilationUnitElement(unitSource, librarySource);
|
||||
if (unitElement != null) {
|
||||
_ContainingElementFinder finder =
|
||||
new _ContainingElementFinder(sourceRange.offset);
|
||||
unitElement.accept(finder);
|
||||
_element = finder.containingElement;
|
||||
}
|
||||
}
|
||||
return _element;
|
||||
}
|
||||
|
||||
/**
|
||||
* The absolute path of the file containing the match.
|
||||
*/
|
||||
String get file => unitSource.fullName;
|
||||
@override
|
||||
final MatchKind kind;
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return JenkinsSmiHash.hash4(libraryUri.hashCode, unitUri.hashCode,
|
||||
kind.hashCode, sourceRange.hashCode);
|
||||
}
|
||||
final SourceRange sourceRange;
|
||||
|
||||
/**
|
||||
* Return the [LibraryElement] for the [libraryUri] in the [context].
|
||||
*/
|
||||
LibraryElement get libraryElement {
|
||||
_libraryElement ??= _context.getLibraryElement(librarySource);
|
||||
return _libraryElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* The library [Source] of the reference.
|
||||
*/
|
||||
Source get librarySource {
|
||||
_librarySource ??= _context.sourceFactory.forUri(libraryUri);
|
||||
return _librarySource;
|
||||
}
|
||||
|
||||
/**
|
||||
* The unit [Source] of the reference.
|
||||
*/
|
||||
Source get unitSource {
|
||||
_unitSource ??= _context.sourceFactory.forUri(unitUri);
|
||||
return _unitSource;
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object object) {
|
||||
if (identical(object, this)) {
|
||||
return true;
|
||||
}
|
||||
if (object is SearchMatchImpl) {
|
||||
return kind == object.kind &&
|
||||
libraryUri == object.libraryUri &&
|
||||
unitUri == object.unitUri &&
|
||||
isResolved == object.isResolved &&
|
||||
isQualified == object.isQualified &&
|
||||
sourceRange == object.sourceRange;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
SearchMatchImpl(
|
||||
this.file,
|
||||
this.librarySource,
|
||||
this.unitSource,
|
||||
this.libraryElement,
|
||||
this.element,
|
||||
this.isResolved,
|
||||
this.isQualified,
|
||||
this.kind,
|
||||
this.sourceRange);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
|
@ -419,9 +146,9 @@ class SearchMatchImpl implements SearchMatch {
|
|||
buffer.write("SearchMatch(kind=");
|
||||
buffer.write(kind);
|
||||
buffer.write(", libraryUri=");
|
||||
buffer.write(libraryUri);
|
||||
buffer.write(librarySource.uri);
|
||||
buffer.write(", unitUri=");
|
||||
buffer.write(unitUri);
|
||||
buffer.write(unitSource.uri);
|
||||
buffer.write(", range=");
|
||||
buffer.write(sourceRange);
|
||||
buffer.write(", isResolved=");
|
||||
|
@ -432,157 +159,46 @@ class SearchMatchImpl implements SearchMatch {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return elements of [matches] which has not-null elements.
|
||||
*
|
||||
* When [SearchMatch.element] is not `null` we cache its value, so it cannot
|
||||
* become `null` later.
|
||||
*/
|
||||
static List<SearchMatch> withNotNullElement(List<SearchMatch> matches) {
|
||||
return matches.where((match) => match.element != null).toList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A visitor that finds the deep-most [Element] that contains the [offset].
|
||||
*/
|
||||
class _ContainingElementFinder extends GeneralizingElementVisitor {
|
||||
final int offset;
|
||||
Element containingElement;
|
||||
|
||||
_ContainingElementFinder(this.offset);
|
||||
|
||||
visitElement(Element element) {
|
||||
if (element is ElementImpl) {
|
||||
if (element.codeOffset != null &&
|
||||
element.codeOffset <= offset &&
|
||||
offset <= element.codeOffset + element.codeLength) {
|
||||
containingElement = element;
|
||||
super.visitElement(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visitor that adds [SearchMatch]es for [importElement], both with an explicit
|
||||
* prefix or an implicit one.
|
||||
*/
|
||||
class _ImportElementReferencesVisitor extends RecursiveAstVisitor {
|
||||
final List<SearchMatch> matches = <SearchMatch>[];
|
||||
|
||||
final ImportElement importElement;
|
||||
final AnalysisContext context;
|
||||
final String libraryUri;
|
||||
final String unitUri;
|
||||
Set<Element> importedElements;
|
||||
|
||||
_ImportElementReferencesVisitor(ImportElement element, this.unitUri)
|
||||
: importElement = element,
|
||||
context = element.context,
|
||||
libraryUri = element.library.source.uri.toString() {
|
||||
importedElements = new NamespaceBuilder()
|
||||
.createImportNamespaceForDirective(element)
|
||||
.definedNames
|
||||
.values
|
||||
.toSet();
|
||||
}
|
||||
|
||||
@override
|
||||
visitExportDirective(ExportDirective node) {}
|
||||
|
||||
@override
|
||||
visitImportDirective(ImportDirective node) {}
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(SimpleIdentifier node) {
|
||||
if (node.inDeclarationContext()) {
|
||||
return;
|
||||
}
|
||||
if (importElement.prefix != null) {
|
||||
if (node.staticElement == importElement.prefix) {
|
||||
AstNode parent = node.parent;
|
||||
if (parent is PrefixedIdentifier && parent.prefix == node) {
|
||||
if (importedElements.contains(parent.staticElement)) {
|
||||
_addMatchForPrefix(node, parent.identifier);
|
||||
}
|
||||
}
|
||||
if (parent is MethodInvocation && parent.target == node) {
|
||||
if (importedElements.contains(parent.methodName.staticElement)) {
|
||||
_addMatchForPrefix(node, parent.methodName);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (importedElements.contains(node.staticElement)) {
|
||||
_addMatchForRange(range.startLength(node, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _addMatchForPrefix(SimpleIdentifier prefixNode, AstNode nextNode) {
|
||||
_addMatchForRange(range.startStart(prefixNode, nextNode));
|
||||
}
|
||||
|
||||
void _addMatchForRange(SourceRange range) {
|
||||
matches.add(new SearchMatchImpl(
|
||||
context, libraryUri, unitUri, MatchKind.REFERENCE, range, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visitor that adds [SearchMatch]es for local elements of a block, method,
|
||||
* class or a library - labels, local functions, local variables and parameters,
|
||||
* type parameters, import prefixes.
|
||||
*/
|
||||
class _LocalReferencesVisitor extends RecursiveAstVisitor {
|
||||
final List<SearchMatch> matches = <SearchMatch>[];
|
||||
|
||||
final Element element;
|
||||
final AnalysisContext context;
|
||||
final String libraryUri;
|
||||
final String unitUri;
|
||||
|
||||
_LocalReferencesVisitor(Element element, [String unitUri])
|
||||
: element = element,
|
||||
context = element.context,
|
||||
libraryUri = element.library.source.uri.toString(),
|
||||
unitUri = unitUri ?? element.source.uri.toString();
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(SimpleIdentifier node) {
|
||||
if (node.inDeclarationContext()) {
|
||||
return;
|
||||
}
|
||||
if (node.bestElement == element) {
|
||||
AstNode parent = node.parent;
|
||||
MatchKind kind = MatchKind.REFERENCE;
|
||||
if (element is FunctionElement) {
|
||||
if (parent is MethodInvocation && parent.methodName == node) {
|
||||
kind = MatchKind.INVOCATION;
|
||||
}
|
||||
} else if (element is VariableElement) {
|
||||
bool isGet = node.inGetterContext();
|
||||
bool isSet = node.inSetterContext();
|
||||
if (isGet && isSet) {
|
||||
kind = MatchKind.READ_WRITE;
|
||||
} else if (isGet) {
|
||||
if (parent is MethodInvocation && parent.methodName == node) {
|
||||
kind = MatchKind.INVOCATION;
|
||||
} else {
|
||||
kind = MatchKind.READ;
|
||||
}
|
||||
} else if (isSet) {
|
||||
kind = MatchKind.WRITE;
|
||||
}
|
||||
}
|
||||
_addMatch(node, kind);
|
||||
}
|
||||
}
|
||||
|
||||
void _addMatch(AstNode node, MatchKind kind) {
|
||||
bool isQualified = node.parent is Label;
|
||||
matches.add(new SearchMatchImpl(context, libraryUri, unitUri, kind,
|
||||
range.node(node), true, isQualified));
|
||||
static SearchMatchImpl forElement(Element element) {
|
||||
return new SearchMatchImpl(
|
||||
element.source.fullName,
|
||||
element.librarySource,
|
||||
element.source,
|
||||
element.library,
|
||||
element,
|
||||
true,
|
||||
true,
|
||||
MatchKind.DECLARATION,
|
||||
new SourceRange(element.nameOffset, element.nameLength));
|
||||
}
|
||||
|
||||
static SearchMatchImpl forSearchResult(SearchResult result) {
|
||||
Element enclosingElement = result.enclosingElement;
|
||||
return new SearchMatchImpl(
|
||||
enclosingElement.source.fullName,
|
||||
enclosingElement.librarySource,
|
||||
enclosingElement.source,
|
||||
enclosingElement.library,
|
||||
enclosingElement,
|
||||
result.isResolved,
|
||||
result.isQualified,
|
||||
toMatchKind(result.kind),
|
||||
new SourceRange(result.offset, result.length));
|
||||
}
|
||||
|
||||
static MatchKind toMatchKind(SearchResultKind kind) {
|
||||
if (kind == SearchResultKind.READ) {
|
||||
return MatchKind.READ;
|
||||
}
|
||||
if (kind == SearchResultKind.READ_WRITE) {
|
||||
return MatchKind.READ_WRITE;
|
||||
}
|
||||
if (kind == SearchResultKind.WRITE) {
|
||||
return MatchKind.WRITE;
|
||||
}
|
||||
if (kind == SearchResultKind.INVOCATION) {
|
||||
return MatchKind.INVOCATION;
|
||||
}
|
||||
return MatchKind.REFERENCE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,202 +0,0 @@
|
|||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:analysis_server/src/services/search/search_engine.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/src/dart/analysis/driver.dart';
|
||||
import 'package:analyzer/src/dart/analysis/search.dart';
|
||||
import 'package:analyzer/src/generated/source.dart' show Source, SourceRange;
|
||||
|
||||
/**
|
||||
* A [SearchEngine] implementation.
|
||||
*/
|
||||
class SearchEngineImpl2 implements SearchEngine {
|
||||
final Iterable<AnalysisDriver> _drivers;
|
||||
|
||||
SearchEngineImpl2(this._drivers);
|
||||
|
||||
@override
|
||||
Future<Set<ClassElement>> searchAllSubtypes(ClassElement type) async {
|
||||
Set<ClassElement> allSubtypes = new Set<ClassElement>();
|
||||
|
||||
Future<Null> addSubtypes(ClassElement type) async {
|
||||
List<SearchResult> directResults = await _searchDirectSubtypes(type);
|
||||
for (SearchResult directResult in directResults) {
|
||||
var directSubtype = directResult.enclosingElement as ClassElement;
|
||||
if (allSubtypes.add(directSubtype)) {
|
||||
await addSubtypes(directSubtype);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await addSubtypes(type);
|
||||
return allSubtypes;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchMemberDeclarations(String name) async {
|
||||
List<SearchMatch> allDeclarations = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<Element> elements = await driver.search.classMembers(name);
|
||||
allDeclarations.addAll(elements.map(_SearchMatch.forElement));
|
||||
}
|
||||
return allDeclarations;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchMemberReferences(String name) async {
|
||||
List<SearchResult> allResults = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<SearchResult> results =
|
||||
await driver.search.unresolvedMemberReferences(name);
|
||||
allResults.addAll(results);
|
||||
}
|
||||
return allResults.map(_SearchMatch.forSearchResult).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchReferences(Element element) async {
|
||||
List<SearchResult> allResults = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<SearchResult> results = await driver.search.references(element);
|
||||
allResults.addAll(results);
|
||||
}
|
||||
return allResults.map(_SearchMatch.forSearchResult).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchSubtypes(ClassElement type) async {
|
||||
List<SearchResult> results = await _searchDirectSubtypes(type);
|
||||
return results.map(_SearchMatch.forSearchResult).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SearchMatch>> searchTopLevelDeclarations(String pattern) async {
|
||||
List<SearchMatch> allDeclarations = [];
|
||||
RegExp regExp = new RegExp(pattern);
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<Element> elements = await driver.search.topLevelElements(regExp);
|
||||
allDeclarations.addAll(elements.map(_SearchMatch.forElement));
|
||||
}
|
||||
return allDeclarations;
|
||||
}
|
||||
|
||||
Future<List<SearchResult>> _searchDirectSubtypes(ClassElement type) async {
|
||||
List<SearchResult> allResults = [];
|
||||
List<AnalysisDriver> drivers = _drivers.toList();
|
||||
for (AnalysisDriver driver in drivers) {
|
||||
List<SearchResult> results = await driver.search.subTypes(type);
|
||||
allResults.addAll(results);
|
||||
}
|
||||
return allResults;
|
||||
}
|
||||
}
|
||||
|
||||
class _SearchMatch implements SearchMatch {
|
||||
@override
|
||||
final String file;
|
||||
|
||||
@override
|
||||
final Source librarySource;
|
||||
|
||||
@override
|
||||
final Source unitSource;
|
||||
|
||||
@override
|
||||
final LibraryElement libraryElement;
|
||||
|
||||
@override
|
||||
final Element element;
|
||||
|
||||
@override
|
||||
final bool isResolved;
|
||||
|
||||
@override
|
||||
final bool isQualified;
|
||||
|
||||
@override
|
||||
final MatchKind kind;
|
||||
|
||||
@override
|
||||
final SourceRange sourceRange;
|
||||
|
||||
_SearchMatch(
|
||||
this.file,
|
||||
this.librarySource,
|
||||
this.unitSource,
|
||||
this.libraryElement,
|
||||
this.element,
|
||||
this.isResolved,
|
||||
this.isQualified,
|
||||
this.kind,
|
||||
this.sourceRange);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.write("SearchMatch(kind=");
|
||||
buffer.write(kind);
|
||||
buffer.write(", libraryUri=");
|
||||
buffer.write(librarySource.uri);
|
||||
buffer.write(", unitUri=");
|
||||
buffer.write(unitSource.uri);
|
||||
buffer.write(", range=");
|
||||
buffer.write(sourceRange);
|
||||
buffer.write(", isResolved=");
|
||||
buffer.write(isResolved);
|
||||
buffer.write(", isQualified=");
|
||||
buffer.write(isQualified);
|
||||
buffer.write(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
static _SearchMatch forElement(Element element) {
|
||||
return new _SearchMatch(
|
||||
element.source.fullName,
|
||||
element.librarySource,
|
||||
element.source,
|
||||
element.library,
|
||||
element,
|
||||
true,
|
||||
true,
|
||||
MatchKind.DECLARATION,
|
||||
new SourceRange(element.nameOffset, element.nameLength));
|
||||
}
|
||||
|
||||
static _SearchMatch forSearchResult(SearchResult result) {
|
||||
Element enclosingElement = result.enclosingElement;
|
||||
return new _SearchMatch(
|
||||
enclosingElement.source.fullName,
|
||||
enclosingElement.librarySource,
|
||||
enclosingElement.source,
|
||||
enclosingElement.library,
|
||||
enclosingElement,
|
||||
result.isResolved,
|
||||
result.isQualified,
|
||||
toMatchKind(result.kind),
|
||||
new SourceRange(result.offset, result.length));
|
||||
}
|
||||
|
||||
static MatchKind toMatchKind(SearchResultKind kind) {
|
||||
if (kind == SearchResultKind.READ) {
|
||||
return MatchKind.READ;
|
||||
}
|
||||
if (kind == SearchResultKind.READ_WRITE) {
|
||||
return MatchKind.READ_WRITE;
|
||||
}
|
||||
if (kind == SearchResultKind.WRITE) {
|
||||
return MatchKind.WRITE;
|
||||
}
|
||||
if (kind == SearchResultKind.INVOCATION) {
|
||||
return MatchKind.INVOCATION;
|
||||
}
|
||||
return MatchKind.REFERENCE;
|
||||
}
|
||||
}
|
|
@ -36,20 +36,20 @@ class RefactoringLocationTest extends AbstractSingleUnitTest {
|
|||
expect(location.startColumn, 7);
|
||||
}
|
||||
|
||||
@failingTest
|
||||
test_createLocation_forMatch() async {
|
||||
// The class SearchMatchImpl has not been converted to use the new driver.
|
||||
await resolveTestUnit('class MyClass {}');
|
||||
Element element = findElement('MyClass');
|
||||
SourceRange sourceRange = range.elementName(element);
|
||||
SearchMatch match = new SearchMatchImpl(
|
||||
element.context,
|
||||
element.library.source.uri.toString(),
|
||||
element.source.uri.toString(),
|
||||
null,
|
||||
sourceRange,
|
||||
element.source.fullName,
|
||||
element.library.source,
|
||||
element.source,
|
||||
element.library,
|
||||
element,
|
||||
true,
|
||||
false);
|
||||
false,
|
||||
MatchKind.DECLARATION,
|
||||
sourceRange);
|
||||
// check
|
||||
Location location = newLocation_fromMatch(match);
|
||||
expect(location.file, '/test.dart');
|
||||
|
|
|
@ -8,7 +8,7 @@ import 'package:analysis_server/src/services/correction/status.dart';
|
|||
import 'package:analysis_server/src/services/index/index.dart';
|
||||
import 'package:analysis_server/src/services/refactoring/refactoring.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal2.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal.dart';
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/element/element.dart' show Element;
|
||||
import 'package:analyzer/file_system/file_system.dart';
|
||||
|
@ -171,7 +171,7 @@ abstract class RefactoringTest extends AbstractSingleUnitTest {
|
|||
|
||||
void setUp() {
|
||||
super.setUp();
|
||||
searchEngine = new SearchEngineImpl2([driver]);
|
||||
searchEngine = new SearchEngineImpl([driver]);
|
||||
astProvider = new AstProviderForDriver(driver);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:analysis_server/src/services/search/hierarchy.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal2.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
@ -20,11 +20,11 @@ main() {
|
|||
|
||||
@reflectiveTest
|
||||
class HierarchyTest extends AbstractSingleUnitTest {
|
||||
SearchEngineImpl2 searchEngine;
|
||||
SearchEngineImpl searchEngine;
|
||||
|
||||
void setUp() {
|
||||
super.setUp();
|
||||
searchEngine = new SearchEngineImpl2([driver]);
|
||||
searchEngine = new SearchEngineImpl([driver]);
|
||||
}
|
||||
|
||||
test_getClassMembers() async {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:analysis_server/src/services/search/search_engine.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal2.dart';
|
||||
import 'package:analysis_server/src/services/search/search_engine_internal.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/file_system/file_system.dart';
|
||||
import 'package:analyzer/file_system/memory_file_system.dart';
|
||||
|
@ -64,7 +64,7 @@ class C implements B {}
|
|||
var resultA = await driver.getResult(p);
|
||||
ClassElement element = resultA.unit.element.types[0];
|
||||
|
||||
var searchEngine = new SearchEngineImpl2([driver]);
|
||||
var searchEngine = new SearchEngineImpl([driver]);
|
||||
Set<ClassElement> subtypes = await searchEngine.searchAllSubtypes(element);
|
||||
expect(subtypes, hasLength(3));
|
||||
expect(subtypes, contains(predicate((ClassElement e) => e.name == 'A')));
|
||||
|
@ -99,7 +99,7 @@ class C extends B {}
|
|||
var resultA = await driver1.getResult(a);
|
||||
ClassElement element = resultA.unit.element.types[0];
|
||||
|
||||
var searchEngine = new SearchEngineImpl2([driver1, driver2]);
|
||||
var searchEngine = new SearchEngineImpl([driver1, driver2]);
|
||||
Set<ClassElement> subtypes = await searchEngine.searchAllSubtypes(element);
|
||||
expect(subtypes, hasLength(3));
|
||||
expect(subtypes, contains(predicate((ClassElement e) => e.name == 'A')));
|
||||
|
@ -138,7 +138,7 @@ int test;
|
|||
await new Future.delayed(new Duration(milliseconds: 1));
|
||||
}
|
||||
|
||||
var searchEngine = new SearchEngineImpl2([driver1, driver2]);
|
||||
var searchEngine = new SearchEngineImpl([driver1, driver2]);
|
||||
List<SearchMatch> matches =
|
||||
await searchEngine.searchMemberDeclarations('test');
|
||||
expect(matches, hasLength(2));
|
||||
|
@ -185,7 +185,7 @@ bar(p) {
|
|||
driver1.addFile(a);
|
||||
driver2.addFile(b);
|
||||
|
||||
var searchEngine = new SearchEngineImpl2([driver1, driver2]);
|
||||
var searchEngine = new SearchEngineImpl([driver1, driver2]);
|
||||
List<SearchMatch> matches =
|
||||
await searchEngine.searchMemberReferences('test');
|
||||
expect(matches, hasLength(2));
|
||||
|
@ -225,7 +225,7 @@ T b;
|
|||
var resultA = await driver1.getResult(a);
|
||||
ClassElement element = resultA.unit.element.types[0];
|
||||
|
||||
var searchEngine = new SearchEngineImpl2([driver1, driver2]);
|
||||
var searchEngine = new SearchEngineImpl([driver1, driver2]);
|
||||
List<SearchMatch> matches = await searchEngine.searchReferences(element);
|
||||
expect(matches, hasLength(2));
|
||||
expect(
|
||||
|
@ -261,7 +261,7 @@ get b => 42;
|
|||
await new Future.delayed(new Duration(milliseconds: 1));
|
||||
}
|
||||
|
||||
var searchEngine = new SearchEngineImpl2([driver1, driver2]);
|
||||
var searchEngine = new SearchEngineImpl([driver1, driver2]);
|
||||
List<SearchMatch> matches =
|
||||
await searchEngine.searchTopLevelDeclarations('.*');
|
||||
expect(
|
||||
|
|
Loading…
Reference in a new issue