| 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.computer.dart.invocation; | 5 library services.completion.computer.dart.invocation; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/services/completion/completion_suggestion.da
rt'; | |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 9 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 10 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; |
| 11 import 'package:analyzer/src/generated/ast.dart'; | 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/element.dart'; | 12 import 'package:analyzer/src/generated/element.dart'; |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * A computer for calculating invocation / access suggestions | 15 * A computer for calculating invocation / access suggestions |
| 16 * `completion.getSuggestions` request results. | 16 * `completion.getSuggestions` request results. |
| 17 */ | 17 */ |
| 18 class InvocationComputer extends DartCompletionComputer { | 18 class InvocationComputer extends DartCompletionComputer { |
| 19 | 19 |
| 20 @override | 20 @override |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 final DartCompletionRequest request; | 78 final DartCompletionRequest request; |
| 79 | 79 |
| 80 _InvocationElementVisitor(this.request); | 80 _InvocationElementVisitor(this.request); |
| 81 | 81 |
| 82 @override | 82 @override |
| 83 Future<bool> visitElement(Element element) { | 83 Future<bool> visitElement(Element element) { |
| 84 return new Future.value(false); | 84 return new Future.value(false); |
| 85 } | 85 } |
| 86 | 86 |
| 87 @override | 87 @override |
| 88 Future<bool> visitPrefixElement(PrefixElement element) { |
| 89 //TODO (danrubel) reimplement to use prefixElement.importedLibraries |
| 90 // once that accessor is implemented and available in Dart |
| 91 bool modified = false; |
| 92 // Find the import directive with the given prefix |
| 93 request.unit.directives.forEach((Directive directive) { |
| 94 if (directive is ImportDirective) { |
| 95 if (directive.prefix != null) { |
| 96 if (directive.prefix.name == element.name) { |
| 97 // Suggest elements from the imported library |
| 98 LibraryElement library = directive.uriElement; |
| 99 LibraryElementSuggestionBuilder.suggestionsFor(request, library); |
| 100 modified = true; |
| 101 } |
| 102 } |
| 103 } |
| 104 }); |
| 105 |
| 106 element.importedLibraries.forEach((LibraryElement library) { |
| 107 modified = true; |
| 108 library.accept(new ClassElementSuggestionBuilder(request)); |
| 109 }); |
| 110 return new Future.value(modified); |
| 111 } |
| 112 |
| 113 @override |
| 88 Future<bool> visitVariableElement(VariableElement element) { | 114 Future<bool> visitVariableElement(VariableElement element) { |
| 89 return _addSuggestions(element.type); | 115 DartType type = element.type; |
| 90 } | 116 if (type != null) { |
| 91 | 117 Element typeElement = type.element; |
| 92 Future<bool> _addSuggestions(DartType type) { | 118 ClassElementSuggestionBuilder.suggestionsFor(request, typeElement); |
| 93 if (type != null && type.element != null) { | |
| 94 type.element.accept(new _SuggestionBuilderVisitor(request)); | |
| 95 return new Future.value(true); | |
| 96 } | 119 } |
| 97 return new Future.value(false); | 120 return new Future.value(true); |
| 98 } | 121 } |
| 99 } | 122 } |
| 100 | |
| 101 /** | |
| 102 * An [Element] visitor that builds suggestions by recursively visiting | |
| 103 * elements in a type hierarchy. | |
| 104 */ | |
| 105 class _SuggestionBuilderVisitor extends GeneralizingElementVisitor { | |
| 106 final DartCompletionRequest request; | |
| 107 | |
| 108 _SuggestionBuilderVisitor(this.request); | |
| 109 | |
| 110 @override | |
| 111 visitClassElement(ClassElement element) { | |
| 112 //TODO (danrubel): filter private members if not in the same library | |
| 113 element.visitChildren(this); | |
| 114 } | |
| 115 | |
| 116 @override | |
| 117 visitElement(Element element) { | |
| 118 // ignored | |
| 119 } | |
| 120 | |
| 121 @override | |
| 122 visitFieldElement(FieldElement element) { | |
| 123 if (!element.isSynthetic) { | |
| 124 _addSuggestion(element, CompletionSuggestionKind.FIELD); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 @override | |
| 129 visitMethodElement(MethodElement element) { | |
| 130 if (!element.isSynthetic) { | |
| 131 _addSuggestion(element, CompletionSuggestionKind.METHOD); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 @override | |
| 136 visitPropertyAccessorElement(PropertyAccessorElement element) { | |
| 137 if (!element.isSynthetic) { | |
| 138 if (element.isGetter) { | |
| 139 _addSuggestion(element, CompletionSuggestionKind.GETTER); | |
| 140 } else if (element.isSetter) { | |
| 141 _addSuggestion( | |
| 142 element, | |
| 143 CompletionSuggestionKind.SETTER, | |
| 144 element.displayName); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void _addSuggestion(Element element, CompletionSuggestionKind kind, | |
| 150 [String completion = null]) { | |
| 151 if (completion == null) { | |
| 152 completion = element.name; | |
| 153 } | |
| 154 if (completion != null && completion.length > 0) { | |
| 155 request.suggestions.add( | |
| 156 new CompletionSuggestion( | |
| 157 kind, | |
| 158 CompletionRelevance.DEFAULT, | |
| 159 completion, | |
| 160 completion.length, | |
| 161 0, | |
| 162 element.isDeprecated, | |
| 163 false)); | |
| 164 } | |
| 165 } | |
| 166 } | |
| OLD | NEW |