Index: pkg/analysis_server/lib/src/services/completion/suggestion_builder.dart |
diff --git a/pkg/analysis_server/lib/src/services/completion/suggestion_builder.dart b/pkg/analysis_server/lib/src/services/completion/suggestion_builder.dart |
index ac87e30dd1d1263d6c6de605aced7022f910eb37..5c572e43bf5ed535b0da3b860039745fca1cbc22 100644 |
--- a/pkg/analysis_server/lib/src/services/completion/suggestion_builder.dart |
+++ b/pkg/analysis_server/lib/src/services/completion/suggestion_builder.dart |
@@ -14,16 +14,20 @@ import 'package:analysis_server/src/services/completion/dart_completion_manager. |
import 'package:analyzer/src/generated/ast.dart'; |
import 'package:analyzer/src/generated/element.dart'; |
import 'package:analyzer/src/generated/utilities_dart.dart'; |
+import 'package:analyzer/src/generated/source.dart'; |
+import 'package:path/path.dart' as path; |
const String DYNAMIC = 'dynamic'; |
/** |
* Return a suggestion based upon the given element |
* or `null` if a suggestion is not appropriate for the given element. |
+ * If the suggestion is not currently in scope, then specify |
+ * importForSource as the source to which an import should be added. |
*/ |
CompletionSuggestion createSuggestion(Element element, |
{CompletionSuggestionKind kind: CompletionSuggestionKind.INVOCATION, |
- int relevance: DART_RELEVANCE_DEFAULT}) { |
+ int relevance: DART_RELEVANCE_DEFAULT, Source importForSource}) { |
String nameForType(DartType type) { |
if (type == null) { |
return DYNAMIC; |
@@ -78,6 +82,34 @@ CompletionSuggestion createSuggestion(Element element, |
(ParameterElement parameter) => |
parameter.parameterKind == ParameterKind.NAMED); |
} |
+ if (importForSource != null) { |
+ String srcPath = path.dirname(importForSource.fullName); |
+ LibraryElement libElem = element.library; |
+ if (libElem != null) { |
+ Source libSource = libElem.source; |
+ if (libSource != null) { |
+ UriKind uriKind = libSource.uriKind; |
+ if (uriKind == UriKind.DART_URI) { |
+ suggestion.importUri = libSource.uri.toString(); |
+ } else if (uriKind == UriKind.PACKAGE_URI) { |
+ // TODO(danrubel) implement |
+ } else if (uriKind == UriKind.FILE_URI && |
+ element.source.uriKind == UriKind.FILE_URI) { |
+ try { |
+ suggestion.importUri = |
+ path.relative(libSource.fullName, from: srcPath); |
+ } catch (_) { |
+ // ignored |
+ } |
+ } |
+ } |
+ } |
+ if (suggestion.importUri == null) { |
+ // Do not include out of scope suggestions |
+ // for which we cannot determine an import |
+ return null; |
+ } |
+ } |
return suggestion; |
} |