| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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.completion.contributor.dart.toplevel; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:collection'; | |
| 9 | |
| 10 import 'package:analysis_server/src/protocol_server.dart' | |
| 11 hide Element, ElementKind; | |
| 12 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; | |
| 13 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 14 import 'package:analysis_server/src/services/completion/optype.dart'; | |
| 15 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; | |
| 16 import 'package:analyzer/src/generated/ast.dart'; | |
| 17 import 'package:analyzer/src/generated/element.dart'; | |
| 18 | |
| 19 /** | |
| 20 * A contributor for calculating imported class and top level variable | |
| 21 * `completion.getSuggestions` request results. | |
| 22 */ | |
| 23 class ImportedReferenceContributor extends DartCompletionContributor { | |
| 24 bool shouldWaitForLowPrioritySuggestions; | |
| 25 bool suggestionsComputed; | |
| 26 _ImportedSuggestionBuilder builder; | |
| 27 | |
| 28 ImportedReferenceContributor({this.shouldWaitForLowPrioritySuggestions: false}
); | |
| 29 | |
| 30 @override | |
| 31 bool computeFast(DartCompletionRequest request) { | |
| 32 OpType optype = request.optype; | |
| 33 if (optype.includeReturnValueSuggestions || | |
| 34 optype.includeTypeNameSuggestions || | |
| 35 optype.includeVoidReturnSuggestions || | |
| 36 optype.includeConstructorSuggestions) { | |
| 37 builder = new _ImportedSuggestionBuilder(request, optype); | |
| 38 builder.shouldWaitForLowPrioritySuggestions = | |
| 39 shouldWaitForLowPrioritySuggestions; | |
| 40 // If target is an argument in an argument list | |
| 41 // then suggestions may need to be adjusted | |
| 42 suggestionsComputed = builder.computeFast(request.target.containingNode); | |
| 43 return suggestionsComputed && request.target.argIndex == null; | |
| 44 } | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 @override | |
| 49 Future<bool> computeFull(DartCompletionRequest request) async { | |
| 50 if (builder != null) { | |
| 51 if (!suggestionsComputed) { | |
| 52 bool result = await builder.computeFull(request.target.containingNode); | |
| 53 _updateSuggestions(request); | |
| 54 return result; | |
| 55 } | |
| 56 _updateSuggestions(request); | |
| 57 return true; | |
| 58 } | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * If target is a function argument, suggest identifiers not invocations | |
| 64 */ | |
| 65 void _updateSuggestions(DartCompletionRequest request) { | |
| 66 if (request.target.isFunctionalArgument()) { | |
| 67 request.convertInvocationsToIdentifiers(); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * [_ImportedSuggestionBuilder] traverses the imports and builds suggestions | |
| 74 * based upon imported elements. | |
| 75 */ | |
| 76 class _ImportedSuggestionBuilder extends ElementSuggestionBuilder | |
| 77 implements SuggestionBuilder { | |
| 78 bool shouldWaitForLowPrioritySuggestions; | |
| 79 final DartCompletionRequest request; | |
| 80 final OpType optype; | |
| 81 DartCompletionCache cache; | |
| 82 | |
| 83 _ImportedSuggestionBuilder(this.request, this.optype) { | |
| 84 cache = request.cache; | |
| 85 } | |
| 86 | |
| 87 @override | |
| 88 CompletionSuggestionKind get kind => CompletionSuggestionKind.INVOCATION; | |
| 89 | |
| 90 /** | |
| 91 * If the needed information is cached, then add suggestions and return `true` | |
| 92 * else return `false` indicating that additional work is necessary. | |
| 93 */ | |
| 94 bool computeFast(AstNode node) { | |
| 95 CompilationUnit unit = request.unit; | |
| 96 if (cache.isImportInfoCached(unit)) { | |
| 97 _addSuggestions(node); | |
| 98 return true; | |
| 99 } | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Compute suggested based upon imported elements. | |
| 105 */ | |
| 106 Future<bool> computeFull(AstNode node) { | |
| 107 Future<bool> addSuggestions(_) { | |
| 108 _addSuggestions(node); | |
| 109 return new Future.value(true); | |
| 110 } | |
| 111 | |
| 112 Future future = null; | |
| 113 if (!cache.isImportInfoCached(request.unit)) { | |
| 114 future = cache.computeImportInfo(request.unit, request.searchEngine, | |
| 115 shouldWaitForLowPrioritySuggestions); | |
| 116 } | |
| 117 if (future != null) { | |
| 118 return future.then(addSuggestions); | |
| 119 } | |
| 120 return addSuggestions(true); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Add constructor and library prefix suggestions from the cache. | |
| 125 * To reduce the number of suggestions sent to the client, | |
| 126 * filter the suggestions based upon the first character typed. | |
| 127 * If no characters are available to use for filtering, | |
| 128 * then exclude all low priority suggestions. | |
| 129 */ | |
| 130 void _addConstructorSuggestions() { | |
| 131 String filterText = request.filterText; | |
| 132 if (filterText.length > 1) { | |
| 133 filterText = filterText.substring(0, 1); | |
| 134 } | |
| 135 DartCompletionCache cache = request.cache; | |
| 136 _addFilteredSuggestions(filterText, cache.importedConstructorSuggestions); | |
| 137 _addFilteredSuggestions(filterText, cache.libraryPrefixSuggestions); | |
| 138 } | |
| 139 | |
| 140 /** | |
| 141 * Add imported element suggestions. | |
| 142 */ | |
| 143 void _addElementSuggestions(List<Element> elements, | |
| 144 {int relevance: DART_RELEVANCE_DEFAULT}) { | |
| 145 for (Element elem in elements) { | |
| 146 if (elem is! ClassElement) { | |
| 147 if (optype.includeOnlyTypeNameSuggestions) { | |
| 148 return; | |
| 149 } | |
| 150 if (elem is ExecutableElement) { | |
| 151 DartType returnType = elem.returnType; | |
| 152 if (returnType != null && returnType.isVoid) { | |
| 153 if (!optype.includeVoidReturnSuggestions) { | |
| 154 return; | |
| 155 } | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 addSuggestion(elem, relevance: relevance); | |
| 160 } | |
| 161 ; | |
| 162 } | |
| 163 | |
| 164 /** | |
| 165 * Add suggestions which start with the given text. | |
| 166 */ | |
| 167 _addFilteredSuggestions( | |
| 168 String filterText, List<CompletionSuggestion> unfiltered) { | |
| 169 //TODO (danrubel) Revisit this filtering once paged API has been added | |
| 170 unfiltered.forEach((CompletionSuggestion suggestion) { | |
| 171 if (filterText.length > 0) { | |
| 172 if (suggestion.completion.startsWith(filterText)) { | |
| 173 request.addSuggestion(suggestion); | |
| 174 } | |
| 175 } else { | |
| 176 if (suggestion.relevance != DART_RELEVANCE_LOW) { | |
| 177 request.addSuggestion(suggestion); | |
| 178 } | |
| 179 } | |
| 180 }); | |
| 181 } | |
| 182 | |
| 183 /** | |
| 184 * Add suggestions for any inherited imported members. | |
| 185 */ | |
| 186 void _addInheritedSuggestions(AstNode node) { | |
| 187 var classDecl = node.getAncestor((p) => p is ClassDeclaration); | |
| 188 if (classDecl is ClassDeclaration) { | |
| 189 // Build a list of inherited types that are imported | |
| 190 // and include any inherited imported members | |
| 191 List<String> inheritedTypes = new List<String>(); | |
| 192 visitInheritedTypes(classDecl, (_) { | |
| 193 // local declarations are handled by the local reference contributor | |
| 194 }, (String typeName) { | |
| 195 inheritedTypes.add(typeName); | |
| 196 }); | |
| 197 HashSet<String> visited = new HashSet<String>(); | |
| 198 while (inheritedTypes.length > 0) { | |
| 199 String name = inheritedTypes.removeLast(); | |
| 200 ClassElement elem = cache.importedClassMap[name]; | |
| 201 if (visited.add(name) && elem != null) { | |
| 202 _addElementSuggestions(elem.fields, | |
| 203 relevance: DART_RELEVANCE_INHERITED_FIELD); | |
| 204 _addElementSuggestions(elem.accessors, | |
| 205 relevance: DART_RELEVANCE_INHERITED_ACCESSOR); | |
| 206 _addElementSuggestions(elem.methods, | |
| 207 relevance: DART_RELEVANCE_INHERITED_METHOD); | |
| 208 elem.allSupertypes.forEach((InterfaceType type) { | |
| 209 if (visited.add(type.name) && type.element != null) { | |
| 210 _addElementSuggestions(type.element.fields, | |
| 211 relevance: DART_RELEVANCE_INHERITED_FIELD); | |
| 212 _addElementSuggestions(type.element.accessors, | |
| 213 relevance: DART_RELEVANCE_INHERITED_ACCESSOR); | |
| 214 _addElementSuggestions(type.element.methods, | |
| 215 relevance: DART_RELEVANCE_INHERITED_METHOD); | |
| 216 } | |
| 217 }); | |
| 218 } | |
| 219 } | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 /** | |
| 224 * Add suggested based upon imported elements. | |
| 225 */ | |
| 226 void _addSuggestions(AstNode node) { | |
| 227 if (optype.includeConstructorSuggestions) { | |
| 228 _addConstructorSuggestions(); | |
| 229 } | |
| 230 if (optype.includeReturnValueSuggestions || | |
| 231 optype.includeTypeNameSuggestions || | |
| 232 optype.includeVoidReturnSuggestions) { | |
| 233 _addInheritedSuggestions(node); | |
| 234 _addTopLevelSuggestions(); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 /** | |
| 239 * Add top level suggestions from the cache. | |
| 240 * To reduce the number of suggestions sent to the client, | |
| 241 * filter the suggestions based upon the first character typed. | |
| 242 * If no characters are available to use for filtering, | |
| 243 * then exclude all low priority suggestions. | |
| 244 */ | |
| 245 void _addTopLevelSuggestions() { | |
| 246 String filterText = request.filterText; | |
| 247 if (filterText.length > 1) { | |
| 248 filterText = filterText.substring(0, 1); | |
| 249 } | |
| 250 DartCompletionCache cache = request.cache; | |
| 251 if (optype.includeTypeNameSuggestions) { | |
| 252 _addFilteredSuggestions(filterText, cache.importedTypeSuggestions); | |
| 253 _addFilteredSuggestions(filterText, cache.libraryPrefixSuggestions); | |
| 254 } | |
| 255 if (optype.includeReturnValueSuggestions) { | |
| 256 _addFilteredSuggestions(filterText, cache.otherImportedSuggestions); | |
| 257 } | |
| 258 if (optype.includeVoidReturnSuggestions) { | |
| 259 _addFilteredSuggestions(filterText, cache.importedVoidReturnSuggestions); | |
| 260 } | |
| 261 } | |
| 262 } | |
| OLD | NEW |