| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 * or `null` if nothing has been cached. | 59 * or `null` if nothing has been cached. |
| 60 */ | 60 */ |
| 61 HashSet<String> _importedCompletions; | 61 HashSet<String> _importedCompletions; |
| 62 | 62 |
| 63 /** | 63 /** |
| 64 * A map of simple identifier to imported class element | 64 * A map of simple identifier to imported class element |
| 65 * or `null` if nothing has been cached. | 65 * or `null` if nothing has been cached. |
| 66 */ | 66 */ |
| 67 Map<String, ClassElement> importedClassMap; | 67 Map<String, ClassElement> importedClassMap; |
| 68 | 68 |
| 69 /** |
| 70 * The [ClassElement] for Object. |
| 71 */ |
| 72 ClassElement _objectClassElement; |
| 73 |
| 69 DartCompletionCache(AnalysisContext context, Source source) | 74 DartCompletionCache(AnalysisContext context, Source source) |
| 70 : super(context, source); | 75 : super(context, source); |
| 71 | 76 |
| 72 /** | 77 /** |
| 73 * Return a hash of the import directives for the cached import info | 78 * Return a hash of the import directives for the cached import info |
| 74 * or `null` if nothing has been cached. | 79 * or `null` if nothing has been cached. |
| 75 */ | 80 */ |
| 76 String get importKey => _importKey; | 81 String get importKey => _importKey; |
| 77 | 82 |
| 78 /** | 83 /** |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 Source coreUri = context.sourceFactory.forUri('dart:core'); | 130 Source coreUri = context.sourceFactory.forUri('dart:core'); |
| 126 LibraryElement coreLib = context.getLibraryElement(coreUri); | 131 LibraryElement coreLib = context.getLibraryElement(coreUri); |
| 127 Namespace coreNamespace = | 132 Namespace coreNamespace = |
| 128 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); | 133 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); |
| 129 coreNamespace.definedNames.forEach((String name, Element elem) { | 134 coreNamespace.definedNames.forEach((String name, Element elem) { |
| 130 if (elem is ClassElement) { | 135 if (elem is ClassElement) { |
| 131 importedClassMap[name] = elem; | 136 importedClassMap[name] = elem; |
| 132 } | 137 } |
| 133 addSuggestion(elem, CompletionRelevance.DEFAULT); | 138 addSuggestion(elem, CompletionRelevance.DEFAULT); |
| 134 }); | 139 }); |
| 140 _objectClassElement = importedClassMap['Object']; |
| 135 | 141 |
| 136 /* | 142 /* |
| 137 * Don't wait for search of lower relevance results to complete. | 143 * Don't wait for search of lower relevance results to complete. |
| 138 * Set key indicating results are ready, and lower relevance results | 144 * Set key indicating results are ready, and lower relevance results |
| 139 * will be added to the cache when the search completes. | 145 * will be added to the cache when the search completes. |
| 140 */ | 146 */ |
| 141 _importKey = _computeImportKey(unit); | 147 _importKey = _computeImportKey(unit); |
| 142 | 148 |
| 143 // Add non-imported elements as low relevance | 149 // Add non-imported elements as low relevance |
| 144 Future<List<SearchMatch>> future = | 150 Future<List<SearchMatch>> future = |
| 145 searchEngine.searchTopLevelDeclarations(''); | 151 searchEngine.searchTopLevelDeclarations(''); |
| 146 return future.then((List<SearchMatch> matches) { | 152 return future.then((List<SearchMatch> matches) { |
| 147 matches.forEach((SearchMatch match) { | 153 matches.forEach((SearchMatch match) { |
| 148 if (match.kind == MatchKind.DECLARATION) { | 154 if (match.kind == MatchKind.DECLARATION) { |
| 149 Element element = match.element; | 155 Element element = match.element; |
| 150 if (element.isPublic && | 156 if (element.isPublic && |
| 151 !excludedLibs.contains(element.library) && | 157 !excludedLibs.contains(element.library) && |
| 152 !_importedCompletions.contains(element.displayName)) { | 158 !_importedCompletions.contains(element.displayName)) { |
| 153 addSuggestion(element, CompletionRelevance.LOW); | 159 addSuggestion(element, CompletionRelevance.LOW); |
| 154 } | 160 } |
| 155 } | 161 } |
| 156 }); | 162 }); |
| 157 return true; | 163 return true; |
| 158 }); | 164 }); |
| 159 } | 165 } |
| 160 | 166 |
| 161 /** | 167 /** |
| 168 * Return the [ClassElement] for Object. |
| 169 */ |
| 170 ClassElement get objectClassElement { |
| 171 if (_objectClassElement == null) { |
| 172 Source coreUri = context.sourceFactory.forUri('dart:core'); |
| 173 LibraryElement coreLib = context.getLibraryElement(coreUri); |
| 174 Namespace coreNamespace = |
| 175 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); |
| 176 _objectClassElement = coreNamespace.definedNames['Object']; |
| 177 } |
| 178 return _objectClassElement; |
| 179 } |
| 180 |
| 181 /** |
| 162 * Return `true` if the import information is cached for the given | 182 * Return `true` if the import information is cached for the given |
| 163 * compilation unit. | 183 * compilation unit. |
| 164 */ | 184 */ |
| 165 bool isImportInfoCached(CompilationUnit unit) => | 185 bool isImportInfoCached(CompilationUnit unit) => |
| 166 _importKey != null && _importKey == _computeImportKey(unit); | 186 _importKey != null && _importKey == _computeImportKey(unit); |
| 167 | 187 |
| 168 void _addLibraryPrefixSuggestion(ImportElement importElem) { | 188 void _addLibraryPrefixSuggestion(ImportElement importElem) { |
| 169 CompletionSuggestion suggestion = null; | 189 CompletionSuggestion suggestion = null; |
| 170 String completion = importElem.prefix.displayName; | 190 String completion = importElem.prefix.displayName; |
| 171 if (completion != null && completion.length > 0) { | 191 if (completion != null && completion.length > 0) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 String _computeImportKey(CompilationUnit unit) { | 238 String _computeImportKey(CompilationUnit unit) { |
| 219 StringBuffer sb = new StringBuffer(); | 239 StringBuffer sb = new StringBuffer(); |
| 220 unit.directives.forEach((Directive directive) { | 240 unit.directives.forEach((Directive directive) { |
| 221 if (directive is ImportDirective) { | 241 if (directive is ImportDirective) { |
| 222 sb.write(directive.toSource()); | 242 sb.write(directive.toSource()); |
| 223 } | 243 } |
| 224 }); | 244 }); |
| 225 return sb.toString(); | 245 return sb.toString(); |
| 226 } | 246 } |
| 227 } | 247 } |
| OLD | NEW |