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

Unified Diff: pkg/analysis_server/lib/src/services/completion/imported_computer.dart

Issue 629633002: suggest re-exported elements of imported libraries (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merge Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/completion/completion_test_util.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/services/completion/imported_computer.dart
diff --git a/pkg/analysis_server/lib/src/services/completion/imported_computer.dart b/pkg/analysis_server/lib/src/services/completion/imported_computer.dart
index d622334845b30167de447e6986b5bd78d7cec347..f8e19a96f0fdbf44ac2256b6c3e3b42dcf0265de 100644
--- a/pkg/analysis_server/lib/src/services/completion/imported_computer.dart
+++ b/pkg/analysis_server/lib/src/services/completion/imported_computer.dart
@@ -14,6 +14,8 @@ import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
import 'package:analysis_server/src/services/search/search_engine.dart';
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';
+import 'package:analyzer/src/generated/resolver.dart';
+import 'package:analyzer/src/generated/source.dart';
/**
* A computer for calculating imported class and top level variable
@@ -74,116 +76,118 @@ class _ImportedVisitor extends GeneralizingAstVisitor<Future<bool>> {
LibraryElementSuggestionBuilder.suggestionsFor(request, library);
return new Future.value(true);
}
+
return new Future.value(false);
}
Future<bool> _addImportedElementSuggestions() {
- var future = request.searchEngine.searchTopLevelDeclarations('');
- return future.then((List<SearchMatch> matches) {
- Set<LibraryElement> visibleLibs = new Set<LibraryElement>();
- Set<LibraryElement> excludedLibs = new Set<LibraryElement>();
-
- Map<LibraryElement, Set<String>> showNames =
- new Map<LibraryElement, Set<String>>();
- Map<LibraryElement, Set<String>> hideNames =
- new Map<LibraryElement, Set<String>>();
-
- // Exclude elements from the local library
- // as they will be included by the LocalComputer
- excludedLibs.add(request.unit.element.library);
-
- // Build the set of visible and excluded libraries
- // and the list of names that should be shown or hidden
- request.unit.directives.forEach((Directive directive) {
- if (directive is ImportDirective) {
- ImportElement element = directive.element;
- if (element != null) {
- LibraryElement lib = element.importedLibrary;
- SimpleIdentifier prefix = directive.prefix;
- if (prefix == null) {
- visibleLibs.add(lib);
- directive.combinators.forEach((Combinator combinator) {
- if (combinator is ShowCombinator) {
- showNames[lib] =
- combinator.shownNames.map((SimpleIdentifier id) => id.name).toSet();
- } else if (combinator is HideCombinator) {
- hideNames[lib] =
- combinator.hiddenNames.map((SimpleIdentifier id) => id.name).toSet();
- }
- });
- } else {
- String completion = prefix.name;
- if (completion != null && completion.length > 0) {
- CompletionSuggestion suggestion = new CompletionSuggestion(
- CompletionSuggestionKind.LIBRARY_PREFIX,
- CompletionRelevance.DEFAULT,
- completion,
- completion.length,
- 0,
- element.isDeprecated,
- false);
- suggestion.element = new protocol.Element.fromEngine(lib);
- request.suggestions.add(suggestion);
- }
- excludedLibs.add(lib);
- }
+ // Exclude elements from local library
+ // because they are provided by LocalComputer
+ Set<LibraryElement> excludedLibs = new Set<LibraryElement>();
+ excludedLibs.add(request.unit.element.enclosingElement);
+
+ // Include explicitly imported elements
+ request.unit.directives.forEach((Directive directive) {
+ if (directive is ImportDirective) {
+ ImportElement importElem = directive.element;
+ if (importElem != null && importElem.importedLibrary != null) {
+ if (directive.prefix == null) {
+ Namespace importNamespace =
+ new NamespaceBuilder().createImportNamespaceForDirective(importElem);
+ importNamespace.definedNames.forEach((_, Element element) {
+ _addElementSuggestion(element, CompletionRelevance.DEFAULT);
+ });
+ } else {
+ // Exclude elements from prefixed imports
+ // because they are provided by InvocationComputer
+ excludedLibs.add(importElem.importedLibrary);
+ _addLibraryPrefixSuggestion(importElem);
}
}
- });
+ }
+ });
- // Compute the set of possible classes, functions, and top level variables
+ // Include implicitly imported dart:core elements
+ Source coreUri = request.context.sourceFactory.forUri('dart:core');
+ LibraryElement coreLib = request.context.getLibraryElement(coreUri);
+ Namespace coreNamespace =
+ new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib);
+ coreNamespace.definedNames.forEach((_, Element element) {
+ _addElementSuggestion(element, CompletionRelevance.DEFAULT);
+ });
+
+ // Add non-imported elements as low relevance
+ var future = request.searchEngine.searchTopLevelDeclarations('');
+ return future.then((List<SearchMatch> matches) {
+ Set<String> completionSet = new Set<String>();
+ request.suggestions.forEach((CompletionSuggestion suggestion) {
+ completionSet.add(suggestion.completion);
+ });
matches.forEach((SearchMatch match) {
if (match.kind == MatchKind.DECLARATION) {
Element element = match.element;
- LibraryElement lib = element.library;
- if (element.isPublic && !excludedLibs.contains(lib)) {
- String completion = element.displayName;
- Set<String> show = showNames[lib];
- Set<String> hide = hideNames[lib];
- if ((show == null || show.contains(completion)) &&
- (hide == null || !hide.contains(completion))) {
-
- CompletionSuggestionKind kind =
- new CompletionSuggestionKind.fromElementKind(element.kind);
-
- CompletionRelevance relevance;
- if (visibleLibs.contains(lib) || lib.isDartCore) {
- relevance = CompletionRelevance.DEFAULT;
- } else {
- relevance = CompletionRelevance.LOW;
- }
-
- CompletionSuggestion suggestion = new CompletionSuggestion(
- kind,
- relevance,
- completion,
- completion.length,
- 0,
- element.isDeprecated,
- false);
-
- suggestion.element = new protocol.Element.fromEngine(element);
-
- DartType type;
- if (element is TopLevelVariableElement) {
- type = element.type;
- } else if (element is FunctionElement) {
- type = element.returnType;
- }
- if (type != null) {
- String name = type.displayName;
- if (name != null && name.length > 0 && name != 'dynamic') {
- suggestion.returnType = name;
- }
- }
-
- request.suggestions.add(suggestion);
- }
+ if (element.isPublic &&
+ !excludedLibs.contains(element.library) &&
+ !completionSet.contains(element.displayName)) {
+ _addElementSuggestion(element, CompletionRelevance.LOW);
}
}
});
return true;
});
}
+
+ void _addElementSuggestion(Element element, CompletionRelevance relevance) {
+ CompletionSuggestionKind kind =
+ new CompletionSuggestionKind.fromElementKind(element.kind);
+
+ String completion = element.displayName;
+ CompletionSuggestion suggestion = new CompletionSuggestion(
+ kind,
+ relevance,
+ completion,
+ completion.length,
+ 0,
+ element.isDeprecated,
+ false);
+
+ suggestion.element = new protocol.Element.fromEngine(element);
+
+ DartType type;
+ if (element is FunctionElement) {
+ type = element.returnType;
+ } else if (element is PropertyAccessorElement && element.isGetter) {
+ type = element.returnType;
+ } else if (element is TopLevelVariableElement) {
+ type = element.type;
+ }
+ if (type != null) {
+ String name = type.displayName;
+ if (name != null && name.length > 0 && name != 'dynamic') {
+ suggestion.returnType = name;
+ }
+ }
+
+ request.suggestions.add(suggestion);
+ }
+
+ void _addLibraryPrefixSuggestion(ImportElement importElem) {
+ String completion = importElem.prefix.displayName;
+ if (completion != null && completion.length > 0) {
+ CompletionSuggestion suggestion = new CompletionSuggestion(
+ CompletionSuggestionKind.LIBRARY_PREFIX,
+ CompletionRelevance.DEFAULT,
+ completion,
+ completion.length,
+ 0,
+ importElem.isDeprecated,
+ false);
+ LibraryElement lib = importElem.importedLibrary;
+ if (lib != null) {
+ suggestion.element = new protocol.Element.fromEngine(lib);
+ }
+ request.suggestions.add(suggestion);
+ }
+ }
}
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/completion/completion_test_util.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698