| 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.dart.cache; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
| 9 |
| 10 import 'package:analysis_server/src/protocol_server.dart' hide Element, |
| 11 ElementKind; |
| 12 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 13 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; |
| 14 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 15 import 'package:analyzer/src/generated/ast.dart'; |
| 16 import 'package:analyzer/src/generated/element.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/resolver.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 |
| 21 /** |
| 22 * The `DartCompletionCache` contains cached information from a prior code |
| 23 * completion operation. |
| 24 */ |
| 25 class DartCompletionCache extends CompletionCache { |
| 26 |
| 27 /** |
| 28 * A hash of the import directives |
| 29 * or `null` if nothing has been cached. |
| 30 */ |
| 31 String _importKey; |
| 32 |
| 33 /** |
| 34 * Library prefix suggestions based upon imports, |
| 35 * or `null` if nothing has been cached. |
| 36 */ |
| 37 List<CompletionSuggestion> libraryPrefixSuggestions; |
| 38 |
| 39 /** |
| 40 * Type suggestions based upon imports, |
| 41 * or `null` if nothing has been cached. |
| 42 */ |
| 43 List<CompletionSuggestion> importedTypeSuggestions; |
| 44 |
| 45 /** |
| 46 * Suggestions for methods and functions that have void return type, |
| 47 * or `null` if nothing has been cached. |
| 48 */ |
| 49 List<CompletionSuggestion> importedVoidReturnSuggestions; |
| 50 |
| 51 /** |
| 52 * Other suggestions based upon imports, |
| 53 * or `null` if nothing has been cached. |
| 54 */ |
| 55 List<CompletionSuggestion> otherImportedSuggestions; |
| 56 |
| 57 /** |
| 58 * A collection of all imported completions |
| 59 * or `null` if nothing has been cached. |
| 60 */ |
| 61 HashSet<String> _importedCompletions; |
| 62 |
| 63 /** |
| 64 * A map of simple identifier to imported class element |
| 65 * or `null` if nothing has been cached. |
| 66 */ |
| 67 Map<String, ClassElement> importedClassMap; |
| 68 |
| 69 DartCompletionCache(AnalysisContext context, Source source) |
| 70 : super(context, source); |
| 71 |
| 72 /** |
| 73 * Return a hash of the import directives for the cached import info |
| 74 * or `null` if nothing has been cached. |
| 75 */ |
| 76 String get importKey => _importKey; |
| 77 |
| 78 /** |
| 79 * Compute suggestions based upon the imports in the given compilation unit. |
| 80 * Return a future that completes when the information has been cached. |
| 81 */ |
| 82 Future<bool> computeImportInfo(CompilationUnit unit, |
| 83 SearchEngine searchEngine) { |
| 84 importedTypeSuggestions = <CompletionSuggestion>[]; |
| 85 libraryPrefixSuggestions = <CompletionSuggestion>[]; |
| 86 otherImportedSuggestions = <CompletionSuggestion>[]; |
| 87 importedVoidReturnSuggestions = <CompletionSuggestion>[]; |
| 88 importedClassMap = new Map<String, ClassElement>(); |
| 89 _importedCompletions = new HashSet<String>(); |
| 90 |
| 91 // Exclude elements from local library |
| 92 // because they are provided by LocalComputer |
| 93 Set<LibraryElement> excludedLibs = new Set<LibraryElement>(); |
| 94 excludedLibs.add(unit.element.enclosingElement); |
| 95 |
| 96 // Include explicitly imported elements |
| 97 unit.directives.forEach((Directive directive) { |
| 98 if (directive is ImportDirective) { |
| 99 ImportElement importElem = directive.element; |
| 100 if (importElem != null && importElem.importedLibrary != null) { |
| 101 if (directive.prefix == null) { |
| 102 Namespace importNamespace = |
| 103 new NamespaceBuilder().createImportNamespaceForDirective(importE
lem); |
| 104 // Include top level elements |
| 105 importNamespace.definedNames.forEach((String name, Element elem) { |
| 106 if (elem is ClassElement) { |
| 107 importedClassMap[name] = elem; |
| 108 } |
| 109 addSuggestion(elem, CompletionRelevance.DEFAULT); |
| 110 }); |
| 111 } else { |
| 112 // Exclude elements from prefixed imports |
| 113 // because they are provided by InvocationComputer |
| 114 excludedLibs.add(importElem.importedLibrary); |
| 115 _addLibraryPrefixSuggestion(importElem); |
| 116 } |
| 117 } |
| 118 } |
| 119 }); |
| 120 |
| 121 // Include implicitly imported dart:core elements |
| 122 Source coreUri = context.sourceFactory.forUri('dart:core'); |
| 123 LibraryElement coreLib = context.getLibraryElement(coreUri); |
| 124 Namespace coreNamespace = |
| 125 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); |
| 126 coreNamespace.definedNames.forEach((String name, Element elem) { |
| 127 if (elem is ClassElement) { |
| 128 importedClassMap[name] = elem; |
| 129 } |
| 130 addSuggestion(elem, CompletionRelevance.DEFAULT); |
| 131 }); |
| 132 |
| 133 // Add non-imported elements as low relevance |
| 134 var future = searchEngine.searchTopLevelDeclarations(''); |
| 135 return future.then((List<SearchMatch> matches) { |
| 136 matches.forEach((SearchMatch match) { |
| 137 if (match.kind == MatchKind.DECLARATION) { |
| 138 Element element = match.element; |
| 139 if (element.isPublic && |
| 140 !excludedLibs.contains(element.library) && |
| 141 !_importedCompletions.contains(element.displayName)) { |
| 142 addSuggestion(element, CompletionRelevance.LOW); |
| 143 } |
| 144 } |
| 145 }); |
| 146 _importKey = _computeImportKey(unit); |
| 147 return true; |
| 148 }); |
| 149 } |
| 150 |
| 151 /** |
| 152 * Return `true` if the import information is cached for the given |
| 153 * compilation unit. |
| 154 */ |
| 155 bool isImportInfoCached(CompilationUnit unit) => |
| 156 _importKey != null && _importKey == _computeImportKey(unit); |
| 157 |
| 158 void _addLibraryPrefixSuggestion(ImportElement importElem) { |
| 159 CompletionSuggestion suggestion = null; |
| 160 String completion = importElem.prefix.displayName; |
| 161 if (completion != null && completion.length > 0) { |
| 162 suggestion = new CompletionSuggestion( |
| 163 CompletionSuggestionKind.INVOCATION, |
| 164 CompletionRelevance.DEFAULT, |
| 165 completion, |
| 166 completion.length, |
| 167 0, |
| 168 importElem.isDeprecated, |
| 169 false); |
| 170 LibraryElement lib = importElem.importedLibrary; |
| 171 if (lib != null) { |
| 172 suggestion.element = newElement_fromEngine(lib); |
| 173 } |
| 174 libraryPrefixSuggestions.add(suggestion); |
| 175 _importedCompletions.add(suggestion.completion); |
| 176 } |
| 177 } |
| 178 |
| 179 void addSuggestion(Element element, CompletionRelevance relevance) { |
| 180 |
| 181 if (element is ExecutableElement) { |
| 182 if (element.isOperator) { |
| 183 return; |
| 184 } |
| 185 } |
| 186 |
| 187 CompletionSuggestion suggestion = |
| 188 createElementSuggestion(element, relevance: relevance); |
| 189 |
| 190 if (element is ExecutableElement) { |
| 191 DartType returnType = element.returnType; |
| 192 if (returnType != null && returnType.isVoid) { |
| 193 importedVoidReturnSuggestions.add(suggestion); |
| 194 } else { |
| 195 otherImportedSuggestions.add(suggestion); |
| 196 } |
| 197 } else if (element is ClassElement) { |
| 198 importedTypeSuggestions.add(suggestion); |
| 199 } else { |
| 200 otherImportedSuggestions.add(suggestion); |
| 201 } |
| 202 _importedCompletions.add(suggestion.completion); |
| 203 } |
| 204 |
| 205 /** |
| 206 * Compute the hash of the imports for the given compilation unit. |
| 207 */ |
| 208 String _computeImportKey(CompilationUnit unit) { |
| 209 StringBuffer sb = new StringBuffer(); |
| 210 unit.directives.forEach((Directive directive) { |
| 211 if (directive is ImportDirective) { |
| 212 sb.write(directive.toSource()); |
| 213 } |
| 214 }); |
| 215 return sb.toString(); |
| 216 } |
| 217 } |
| OLD | NEW |