| 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' hide Element, | 10 import 'package:analysis_server/src/protocol_server.dart' hide Element, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 : super(context, source); | 70 : super(context, source); |
| 71 | 71 |
| 72 /** | 72 /** |
| 73 * Return a hash of the import directives for the cached import info | 73 * Return a hash of the import directives for the cached import info |
| 74 * or `null` if nothing has been cached. | 74 * or `null` if nothing has been cached. |
| 75 */ | 75 */ |
| 76 String get importKey => _importKey; | 76 String get importKey => _importKey; |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * Compute suggestions based upon the imports in the given compilation unit. | 79 * Compute suggestions based upon the imports in the given compilation unit. |
| 80 * Return a future that completes when the information has been cached. | 80 * On return, the cache will be populated except for lower priority |
| 81 * suggestions added as a result of a global search. Callers may wait |
| 82 * on the returned future if they want to ensure those lower priority |
| 83 * suggestions are part of the cached suggestions. |
| 81 */ | 84 */ |
| 82 Future<bool> computeImportInfo(CompilationUnit unit, | 85 Future<bool> computeImportInfo(CompilationUnit unit, |
| 83 SearchEngine searchEngine) { | 86 SearchEngine searchEngine) { |
| 84 importedTypeSuggestions = <CompletionSuggestion>[]; | 87 importedTypeSuggestions = <CompletionSuggestion>[]; |
| 85 libraryPrefixSuggestions = <CompletionSuggestion>[]; | 88 libraryPrefixSuggestions = <CompletionSuggestion>[]; |
| 86 otherImportedSuggestions = <CompletionSuggestion>[]; | 89 otherImportedSuggestions = <CompletionSuggestion>[]; |
| 87 importedVoidReturnSuggestions = <CompletionSuggestion>[]; | 90 importedVoidReturnSuggestions = <CompletionSuggestion>[]; |
| 88 importedClassMap = new Map<String, ClassElement>(); | 91 importedClassMap = new Map<String, ClassElement>(); |
| 89 _importedCompletions = new HashSet<String>(); | 92 _importedCompletions = new HashSet<String>(); |
| 90 | 93 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 LibraryElement coreLib = context.getLibraryElement(coreUri); | 126 LibraryElement coreLib = context.getLibraryElement(coreUri); |
| 124 Namespace coreNamespace = | 127 Namespace coreNamespace = |
| 125 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); | 128 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); |
| 126 coreNamespace.definedNames.forEach((String name, Element elem) { | 129 coreNamespace.definedNames.forEach((String name, Element elem) { |
| 127 if (elem is ClassElement) { | 130 if (elem is ClassElement) { |
| 128 importedClassMap[name] = elem; | 131 importedClassMap[name] = elem; |
| 129 } | 132 } |
| 130 addSuggestion(elem, CompletionRelevance.DEFAULT); | 133 addSuggestion(elem, CompletionRelevance.DEFAULT); |
| 131 }); | 134 }); |
| 132 | 135 |
| 136 /* |
| 137 * Don't wait for search of lower relevance results to complete. |
| 138 * Set key indicating results are ready, and lower relevance results |
| 139 * will be added to the cache when the search completes. |
| 140 */ |
| 141 _importKey = _computeImportKey(unit); |
| 142 |
| 133 // Add non-imported elements as low relevance | 143 // Add non-imported elements as low relevance |
| 134 var future = searchEngine.searchTopLevelDeclarations(''); | 144 Future<List<SearchMatch>> future = |
| 145 searchEngine.searchTopLevelDeclarations(''); |
| 135 return future.then((List<SearchMatch> matches) { | 146 return future.then((List<SearchMatch> matches) { |
| 136 matches.forEach((SearchMatch match) { | 147 matches.forEach((SearchMatch match) { |
| 137 if (match.kind == MatchKind.DECLARATION) { | 148 if (match.kind == MatchKind.DECLARATION) { |
| 138 Element element = match.element; | 149 Element element = match.element; |
| 139 if (element.isPublic && | 150 if (element.isPublic && |
| 140 !excludedLibs.contains(element.library) && | 151 !excludedLibs.contains(element.library) && |
| 141 !_importedCompletions.contains(element.displayName)) { | 152 !_importedCompletions.contains(element.displayName)) { |
| 142 addSuggestion(element, CompletionRelevance.LOW); | 153 addSuggestion(element, CompletionRelevance.LOW); |
| 143 } | 154 } |
| 144 } | 155 } |
| 145 }); | 156 }); |
| 146 _importKey = _computeImportKey(unit); | |
| 147 return true; | 157 return true; |
| 148 }); | 158 }); |
| 149 } | 159 } |
| 150 | 160 |
| 151 /** | 161 /** |
| 152 * Return `true` if the import information is cached for the given | 162 * Return `true` if the import information is cached for the given |
| 153 * compilation unit. | 163 * compilation unit. |
| 154 */ | 164 */ |
| 155 bool isImportInfoCached(CompilationUnit unit) => | 165 bool isImportInfoCached(CompilationUnit unit) => |
| 156 _importKey != null && _importKey == _computeImportKey(unit); | 166 _importKey != null && _importKey == _computeImportKey(unit); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 String _computeImportKey(CompilationUnit unit) { | 218 String _computeImportKey(CompilationUnit unit) { |
| 209 StringBuffer sb = new StringBuffer(); | 219 StringBuffer sb = new StringBuffer(); |
| 210 unit.directives.forEach((Directive directive) { | 220 unit.directives.forEach((Directive directive) { |
| 211 if (directive is ImportDirective) { | 221 if (directive is ImportDirective) { |
| 212 sb.write(directive.toSource()); | 222 sb.write(directive.toSource()); |
| 213 } | 223 } |
| 214 }); | 224 }); |
| 215 return sb.toString(); | 225 return sb.toString(); |
| 216 } | 226 } |
| 217 } | 227 } |
| OLD | NEW |