| 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, |
| 11 ElementKind; | 11 ElementKind; |
| 12 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 12 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 13 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 13 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; | 14 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; |
| 14 import 'package:analysis_server/src/services/search/search_engine.dart'; | 15 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 15 import 'package:analyzer/src/generated/ast.dart'; | 16 import 'package:analyzer/src/generated/ast.dart'; |
| 16 import 'package:analyzer/src/generated/element.dart'; | 17 import 'package:analyzer/src/generated/element.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/resolver.dart'; | 19 import 'package:analyzer/src/generated/resolver.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * The `DartCompletionCache` contains cached information from a prior code | 23 * The `DartCompletionCache` contains cached information from a prior code |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 */ | 171 */ |
| 171 void _addDartCoreSuggestions() { | 172 void _addDartCoreSuggestions() { |
| 172 Source coreUri = context.sourceFactory.forUri('dart:core'); | 173 Source coreUri = context.sourceFactory.forUri('dart:core'); |
| 173 LibraryElement coreLib = context.getLibraryElement(coreUri); | 174 LibraryElement coreLib = context.getLibraryElement(coreUri); |
| 174 Namespace coreNamespace = | 175 Namespace coreNamespace = |
| 175 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); | 176 new NamespaceBuilder().createPublicNamespaceForLibrary(coreLib); |
| 176 coreNamespace.definedNames.forEach((String name, Element elem) { | 177 coreNamespace.definedNames.forEach((String name, Element elem) { |
| 177 if (elem is ClassElement) { | 178 if (elem is ClassElement) { |
| 178 importedClassMap[name] = elem; | 179 importedClassMap[name] = elem; |
| 179 } | 180 } |
| 180 _addSuggestion(elem, CompletionRelevance.DEFAULT); | 181 _addSuggestion(elem, COMPLETION_RELEVANCE_DEFAULT); |
| 181 }); | 182 }); |
| 182 } | 183 } |
| 183 | 184 |
| 184 /** | 185 /** |
| 185 * Add suggestions for explicitly imported and part elements in the given | 186 * Add suggestions for explicitly imported and part elements in the given |
| 186 * library. Add libraries that should not have their elements suggested | 187 * library. Add libraries that should not have their elements suggested |
| 187 * even as low priority to [excludedLibs]. | 188 * even as low priority to [excludedLibs]. |
| 188 */ | 189 */ |
| 189 void _addImportedElemSuggestions(Source libSource, CompilationUnit libUnit, | 190 void _addImportedElemSuggestions(Source libSource, CompilationUnit libUnit, |
| 190 Set<LibraryElement> excludedLibs) { | 191 Set<LibraryElement> excludedLibs) { |
| 191 if (libUnit != null) { | 192 if (libUnit != null) { |
| 192 libUnit.directives.forEach((Directive directive) { | 193 libUnit.directives.forEach((Directive directive) { |
| 193 if (directive is ImportDirective) { | 194 if (directive is ImportDirective) { |
| 194 ImportElement importElem = directive.element; | 195 ImportElement importElem = directive.element; |
| 195 if (importElem != null && importElem.importedLibrary != null) { | 196 if (importElem != null && importElem.importedLibrary != null) { |
| 196 if (directive.prefix == null) { | 197 if (directive.prefix == null) { |
| 197 Namespace importNamespace = | 198 Namespace importNamespace = |
| 198 new NamespaceBuilder().createImportNamespaceForDirective(impor
tElem); | 199 new NamespaceBuilder().createImportNamespaceForDirective(impor
tElem); |
| 199 // Include top level elements | 200 // Include top level elements |
| 200 importNamespace.definedNames.forEach((String name, Element elem) { | 201 importNamespace.definedNames.forEach((String name, Element elem) { |
| 201 if (elem is ClassElement) { | 202 if (elem is ClassElement) { |
| 202 importedClassMap[name] = elem; | 203 importedClassMap[name] = elem; |
| 203 } | 204 } |
| 204 _addSuggestion(elem, CompletionRelevance.DEFAULT); | 205 _addSuggestion(elem, COMPLETION_RELEVANCE_DEFAULT); |
| 205 }); | 206 }); |
| 206 } else { | 207 } else { |
| 207 // Exclude elements from prefixed imports | 208 // Exclude elements from prefixed imports |
| 208 // because they are provided by InvocationComputer | 209 // because they are provided by InvocationComputer |
| 209 _addLibraryPrefixSuggestion(importElem); | 210 _addLibraryPrefixSuggestion(importElem); |
| 210 excludedLibs.add(importElem.importedLibrary); | 211 excludedLibs.add(importElem.importedLibrary); |
| 211 } | 212 } |
| 212 } | 213 } |
| 213 } else if (directive is PartDirective) { | 214 } else if (directive is PartDirective) { |
| 214 CompilationUnitElement partElem = directive.element; | 215 CompilationUnitElement partElem = directive.element; |
| 215 if (partElem != null && partElem.source != source) { | 216 if (partElem != null && partElem.source != source) { |
| 216 partElem.accept(new _NonLocalElementCacheVisitor(this)); | 217 partElem.accept(new _NonLocalElementCacheVisitor(this)); |
| 217 } | 218 } |
| 218 } | 219 } |
| 219 }); | 220 }); |
| 220 if (libSource != source) { | 221 if (libSource != source) { |
| 221 libUnit.element.accept(new _NonLocalElementCacheVisitor(this)); | 222 libUnit.element.accept(new _NonLocalElementCacheVisitor(this)); |
| 222 } | 223 } |
| 223 } | 224 } |
| 224 } | 225 } |
| 225 | 226 |
| 226 void _addLibraryPrefixSuggestion(ImportElement importElem) { | 227 void _addLibraryPrefixSuggestion(ImportElement importElem) { |
| 227 CompletionSuggestion suggestion = null; | 228 CompletionSuggestion suggestion = null; |
| 228 String completion = importElem.prefix.displayName; | 229 String completion = importElem.prefix.displayName; |
| 229 if (completion != null && completion.length > 0) { | 230 if (completion != null && completion.length > 0) { |
| 230 suggestion = new CompletionSuggestion( | 231 suggestion = new CompletionSuggestion( |
| 231 CompletionSuggestionKind.INVOCATION, | 232 CompletionSuggestionKind.INVOCATION, |
| 232 CompletionRelevance.DEFAULT, | 233 COMPLETION_RELEVANCE_DEFAULT, |
| 233 completion, | 234 completion, |
| 234 completion.length, | 235 completion.length, |
| 235 0, | 236 0, |
| 236 importElem.isDeprecated, | 237 importElem.isDeprecated, |
| 237 false); | 238 false); |
| 238 LibraryElement lib = importElem.importedLibrary; | 239 LibraryElement lib = importElem.importedLibrary; |
| 239 if (lib != null) { | 240 if (lib != null) { |
| 240 suggestion.element = newElement_fromEngine(lib); | 241 suggestion.element = newElement_fromEngine(lib); |
| 241 } | 242 } |
| 242 libraryPrefixSuggestions.add(suggestion); | 243 libraryPrefixSuggestions.add(suggestion); |
| 243 _importedCompletions.add(suggestion.completion); | 244 _importedCompletions.add(suggestion.completion); |
| 244 } | 245 } |
| 245 } | 246 } |
| 246 | 247 |
| 247 /** | 248 /** |
| 248 * Add suggestions for all top level elements in the context | 249 * Add suggestions for all top level elements in the context |
| 249 * excluding those elemnents for which suggestions have already been added. | 250 * excluding those elemnents for which suggestions have already been added. |
| 250 */ | 251 */ |
| 251 void _addNonImportedElementSuggestions(List<SearchMatch> matches, | 252 void _addNonImportedElementSuggestions(List<SearchMatch> matches, |
| 252 Set<LibraryElement> excludedLibs) { | 253 Set<LibraryElement> excludedLibs) { |
| 253 matches.forEach((SearchMatch match) { | 254 matches.forEach((SearchMatch match) { |
| 254 if (match.kind == MatchKind.DECLARATION) { | 255 if (match.kind == MatchKind.DECLARATION) { |
| 255 Element element = match.element; | 256 Element element = match.element; |
| 256 if (element.context == context && | 257 if (element.context == context && |
| 257 element.isPublic && | 258 element.isPublic && |
| 258 !excludedLibs.contains(element.library) && | 259 !excludedLibs.contains(element.library) && |
| 259 !_importedCompletions.contains(element.displayName)) { | 260 !_importedCompletions.contains(element.displayName)) { |
| 260 _addSuggestion(element, CompletionRelevance.LOW); | 261 _addSuggestion(element, COMPLETION_RELEVANCE_LOW); |
| 261 } | 262 } |
| 262 } | 263 } |
| 263 }); | 264 }); |
| 264 } | 265 } |
| 265 | 266 |
| 266 /** | 267 /** |
| 267 * Add a suggestion for the given element. | 268 * Add a suggestion for the given element. |
| 268 */ | 269 */ |
| 269 void _addSuggestion(Element element, CompletionRelevance relevance) { | 270 void _addSuggestion(Element element, int relevance) { |
| 270 | 271 |
| 271 if (element is ExecutableElement) { | 272 if (element is ExecutableElement) { |
| 272 if (element.isOperator) { | 273 if (element.isOperator) { |
| 273 return; | 274 return; |
| 274 } | 275 } |
| 275 } | 276 } |
| 276 | 277 |
| 277 CompletionSuggestion suggestion = | 278 CompletionSuggestion suggestion = |
| 278 createElementSuggestion(element, relevance: relevance); | 279 createElementSuggestion(element, relevance: relevance); |
| 279 | 280 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 * a source file contained in the same library but not the same as | 327 * a source file contained in the same library but not the same as |
| 327 * the source in which the completions are being requested. | 328 * the source in which the completions are being requested. |
| 328 */ | 329 */ |
| 329 class _NonLocalElementCacheVisitor extends GeneralizingElementVisitor { | 330 class _NonLocalElementCacheVisitor extends GeneralizingElementVisitor { |
| 330 final DartCompletionCache cache; | 331 final DartCompletionCache cache; |
| 331 | 332 |
| 332 _NonLocalElementCacheVisitor(this.cache); | 333 _NonLocalElementCacheVisitor(this.cache); |
| 333 | 334 |
| 334 @override | 335 @override |
| 335 void visitClassElement(ClassElement element) { | 336 void visitClassElement(ClassElement element) { |
| 336 cache._addSuggestion(element, CompletionRelevance.DEFAULT); | 337 cache._addSuggestion(element, COMPLETION_RELEVANCE_DEFAULT); |
| 337 } | 338 } |
| 338 | 339 |
| 339 @override | 340 @override |
| 340 void visitCompilationUnitElement(CompilationUnitElement element) { | 341 void visitCompilationUnitElement(CompilationUnitElement element) { |
| 341 element.visitChildren(this); | 342 element.visitChildren(this); |
| 342 } | 343 } |
| 343 | 344 |
| 344 @override | 345 @override |
| 345 void visitElement(Element element) { | 346 void visitElement(Element element) { |
| 346 // ignored | 347 // ignored |
| 347 } | 348 } |
| 348 | 349 |
| 349 @override | 350 @override |
| 350 void visitFunctionElement(FunctionElement element) { | 351 void visitFunctionElement(FunctionElement element) { |
| 351 cache._addSuggestion(element, CompletionRelevance.DEFAULT); | 352 cache._addSuggestion(element, COMPLETION_RELEVANCE_DEFAULT); |
| 352 } | 353 } |
| 353 | 354 |
| 354 @override | 355 @override |
| 355 void visitFunctionTypeAliasElement(FunctionTypeAliasElement element) { | 356 void visitFunctionTypeAliasElement(FunctionTypeAliasElement element) { |
| 356 cache._addSuggestion(element, CompletionRelevance.DEFAULT); | 357 cache._addSuggestion(element, COMPLETION_RELEVANCE_DEFAULT); |
| 357 } | 358 } |
| 358 | 359 |
| 359 @override | 360 @override |
| 360 void visitTopLevelVariableElement(TopLevelVariableElement element) { | 361 void visitTopLevelVariableElement(TopLevelVariableElement element) { |
| 361 cache._addSuggestion(element, CompletionRelevance.DEFAULT); | 362 cache._addSuggestion(element, COMPLETION_RELEVANCE_DEFAULT); |
| 362 } | 363 } |
| 363 } | 364 } |
| OLD | NEW |