| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library services.completion.dart.cache; | 5 library services.completion.dart.cache; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'package:analysis_server/src/protocol_server.dart' | 10 import 'package:analysis_server/src/protocol_server.dart' |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 */ | 49 */ |
| 50 List<CompletionSuggestion> importedVoidReturnSuggestions; | 50 List<CompletionSuggestion> importedVoidReturnSuggestions; |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Other suggestions based upon imports, | 53 * Other suggestions based upon imports, |
| 54 * or `null` if nothing has been cached. | 54 * or `null` if nothing has been cached. |
| 55 */ | 55 */ |
| 56 List<CompletionSuggestion> otherImportedSuggestions; | 56 List<CompletionSuggestion> otherImportedSuggestions; |
| 57 | 57 |
| 58 /** | 58 /** |
| 59 * Suggestions for constructors |
| 60 * or `null` if nothing has been cached. |
| 61 */ |
| 62 List<CompletionSuggestion> importedConstructorSuggestions; |
| 63 |
| 64 /** |
| 59 * A collection of all imported completions | 65 * A collection of all imported completions |
| 60 * or `null` if nothing has been cached. | 66 * or `null` if nothing has been cached. |
| 61 */ | 67 */ |
| 62 HashSet<String> _importedCompletions; | 68 HashSet<String> _importedCompletions; |
| 63 | 69 |
| 64 /** | 70 /** |
| 65 * A map of simple identifier to imported class element | 71 * A map of simple identifier to imported class element |
| 66 * or `null` if nothing has been cached. | 72 * or `null` if nothing has been cached. |
| 67 */ | 73 */ |
| 68 Map<String, ClassElement> importedClassMap; | 74 Map<String, ClassElement> importedClassMap; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 * the returned future will complete sooner, but the cache will not include | 110 * the returned future will complete sooner, but the cache will not include |
| 105 * the lower priority suggestions added as a result of a global search. | 111 * the lower priority suggestions added as a result of a global search. |
| 106 * In this case, those lower priority suggestions will be added later | 112 * In this case, those lower priority suggestions will be added later |
| 107 * when the index has been updated and the global search completes. | 113 * when the index has been updated and the global search completes. |
| 108 */ | 114 */ |
| 109 Future<bool> computeImportInfo(CompilationUnit unit, | 115 Future<bool> computeImportInfo(CompilationUnit unit, |
| 110 SearchEngine searchEngine, bool shouldWaitForLowPrioritySuggestions) { | 116 SearchEngine searchEngine, bool shouldWaitForLowPrioritySuggestions) { |
| 111 importedTypeSuggestions = <CompletionSuggestion>[]; | 117 importedTypeSuggestions = <CompletionSuggestion>[]; |
| 112 libraryPrefixSuggestions = <CompletionSuggestion>[]; | 118 libraryPrefixSuggestions = <CompletionSuggestion>[]; |
| 113 otherImportedSuggestions = <CompletionSuggestion>[]; | 119 otherImportedSuggestions = <CompletionSuggestion>[]; |
| 120 importedConstructorSuggestions = <CompletionSuggestion>[]; |
| 114 importedVoidReturnSuggestions = <CompletionSuggestion>[]; | 121 importedVoidReturnSuggestions = <CompletionSuggestion>[]; |
| 115 importedClassMap = new Map<String, ClassElement>(); | 122 importedClassMap = new Map<String, ClassElement>(); |
| 116 _importedCompletions = new HashSet<String>(); | 123 _importedCompletions = new HashSet<String>(); |
| 117 | 124 |
| 118 // Assert that the compilation unit is resolved | 125 // Assert that the compilation unit is resolved |
| 119 // and represents the expected source | 126 // and represents the expected source |
| 120 assert(unit.element.source == source); | 127 assert(unit.element.source == source); |
| 121 | 128 |
| 122 // Exclude elements from local library | 129 // Exclude elements from local library |
| 123 // because they are provided by LocalComputer | 130 // because they are provided by LocalComputer |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 168 } |
| 162 | 169 |
| 163 /** | 170 /** |
| 164 * Return `true` if the import information is cached for the given | 171 * Return `true` if the import information is cached for the given |
| 165 * compilation unit. | 172 * compilation unit. |
| 166 */ | 173 */ |
| 167 bool isImportInfoCached(CompilationUnit unit) => | 174 bool isImportInfoCached(CompilationUnit unit) => |
| 168 _importKey != null && _importKey == _computeImportKey(unit); | 175 _importKey != null && _importKey == _computeImportKey(unit); |
| 169 | 176 |
| 170 /** | 177 /** |
| 178 * Add constructor suggestions for the given class. |
| 179 */ |
| 180 void _addConstructorSuggestions(ClassElement classElem, int relevance) { |
| 181 String className = classElem.name; |
| 182 for (ConstructorElement constructor in classElem.constructors) { |
| 183 if (!constructor.isPrivate) { |
| 184 CompletionSuggestion suggestion = |
| 185 createSuggestion(constructor, relevance: relevance); |
| 186 String name = suggestion.completion; |
| 187 name = name.length > 0 ? '$className.$name' : className; |
| 188 suggestion.completion = name; |
| 189 suggestion.element.name = name; |
| 190 suggestion.selectionOffset = suggestion.completion.length; |
| 191 importedConstructorSuggestions.add(suggestion); |
| 192 } |
| 193 } |
| 194 } |
| 195 |
| 196 /** |
| 171 * Add suggestions for implicitly imported elements in dart:core. | 197 * Add suggestions for implicitly imported elements in dart:core. |
| 172 */ | 198 */ |
| 173 void _addDartCoreSuggestions() { | 199 void _addDartCoreSuggestions() { |
| 174 Source coreUri = context.sourceFactory.forUri('dart:core'); | 200 Source coreUri = context.sourceFactory.forUri('dart:core'); |
| 175 LibraryElement coreLib = context.getLibraryElement(coreUri); | 201 LibraryElement coreLib = context.getLibraryElement(coreUri); |
| 176 Namespace coreNamespace = | 202 Namespace coreNamespace = |
| 177 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); | 203 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); |
| 178 coreNamespace.definedNames.forEach((String name, Element elem) { | 204 coreNamespace.definedNames.forEach((String name, Element elem) { |
| 179 if (elem is ClassElement) { | 205 if (elem is ClassElement) { |
| 180 importedClassMap[name] = elem; | 206 importedClassMap[name] = elem; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 | 301 |
| 276 if (element is ExecutableElement) { | 302 if (element is ExecutableElement) { |
| 277 DartType returnType = element.returnType; | 303 DartType returnType = element.returnType; |
| 278 if (returnType != null && returnType.isVoid) { | 304 if (returnType != null && returnType.isVoid) { |
| 279 importedVoidReturnSuggestions.add(suggestion); | 305 importedVoidReturnSuggestions.add(suggestion); |
| 280 } else { | 306 } else { |
| 281 otherImportedSuggestions.add(suggestion); | 307 otherImportedSuggestions.add(suggestion); |
| 282 } | 308 } |
| 283 } else if (element is ClassElement) { | 309 } else if (element is ClassElement) { |
| 284 importedTypeSuggestions.add(suggestion); | 310 importedTypeSuggestions.add(suggestion); |
| 311 _addConstructorSuggestions(element, relevance); |
| 285 } else { | 312 } else { |
| 286 otherImportedSuggestions.add(suggestion); | 313 otherImportedSuggestions.add(suggestion); |
| 287 } | 314 } |
| 288 _importedCompletions.add(suggestion.completion); | 315 _importedCompletions.add(suggestion.completion); |
| 289 } | 316 } |
| 290 | 317 |
| 291 /** | 318 /** |
| 292 * Compute the hash of the imports for the given compilation unit. | 319 * Compute the hash of the imports for the given compilation unit. |
| 293 */ | 320 */ |
| 294 String _computeImportKey(CompilationUnit unit) { | 321 String _computeImportKey(CompilationUnit unit) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 @override | 377 @override |
| 351 void visitFunctionTypeAliasElement(FunctionTypeAliasElement element) { | 378 void visitFunctionTypeAliasElement(FunctionTypeAliasElement element) { |
| 352 cache._addSuggestion(element, DART_RELEVANCE_DEFAULT); | 379 cache._addSuggestion(element, DART_RELEVANCE_DEFAULT); |
| 353 } | 380 } |
| 354 | 381 |
| 355 @override | 382 @override |
| 356 void visitTopLevelVariableElement(TopLevelVariableElement element) { | 383 void visitTopLevelVariableElement(TopLevelVariableElement element) { |
| 357 cache._addSuggestion(element, DART_RELEVANCE_DEFAULT); | 384 cache._addSuggestion(element, DART_RELEVANCE_DEFAULT); |
| 358 } | 385 } |
| 359 } | 386 } |
| OLD | NEW |