Chromium Code Reviews| 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.toplevel; | 5 library services.completion.computer.dart.toplevel; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol_server.dart' hide Element, ElementK ind; | 9 import 'package:analysis_server/src/protocol_server.dart' hide Element, |
| 10 ElementKind; | |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart'; | 11 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart'; |
| 11 import 'package:analysis_server/src/services/completion/suggestion_builder.dart' ; | 12 import 'package:analysis_server/src/services/completion/suggestion_builder.dart' ; |
| 12 import 'package:analysis_server/src/services/search/search_engine.dart'; | 13 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 13 import 'package:analyzer/src/generated/ast.dart'; | 14 import 'package:analyzer/src/generated/ast.dart'; |
| 14 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
| 15 import 'package:analyzer/src/generated/resolver.dart'; | 16 import 'package:analyzer/src/generated/resolver.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * A computer for calculating imported class and top level variable | 20 * A computer for calculating imported class and top level variable |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 44 final DartCompletionRequest request; | 45 final DartCompletionRequest request; |
| 45 | 46 |
| 46 _ImportedVisitor(this.request); | 47 _ImportedVisitor(this.request); |
| 47 | 48 |
| 48 @override | 49 @override |
| 49 Future<bool> visitBlock(Block node) { | 50 Future<bool> visitBlock(Block node) { |
| 50 return _addImportedElementSuggestions(); | 51 return _addImportedElementSuggestions(); |
| 51 } | 52 } |
| 52 | 53 |
| 53 @override | 54 @override |
| 55 Future<bool> visitCombinator(Combinator node) { | |
| 56 return _addCombinatorSuggestions(node); | |
| 57 } | |
| 58 | |
| 59 @override | |
| 60 Future<bool> visitExpressionStatement(ExpressionStatement node) { | |
| 61 Expression expression = node.expression; | |
| 62 // A pre-variable declaration (e.g. C ^) is parsed as an expression | |
| 63 // statement. Do not make suggestions for the variable name. | |
|
scheglov
2014/10/07 18:15:45
Actually it would be nice to propose variable name
| |
| 64 if (expression is SimpleIdentifier && request.offset <= expression.end) { | |
| 65 return _addImportedElementSuggestions(); | |
| 66 } | |
| 67 return new Future.value(false); | |
| 68 } | |
| 69 | |
| 70 @override | |
| 54 Future<bool> visitNode(AstNode node) { | 71 Future<bool> visitNode(AstNode node) { |
| 55 return new Future.value(false); | 72 return new Future.value(false); |
| 56 } | 73 } |
| 57 | 74 |
| 58 @override | 75 @override |
| 59 Future<bool> visitSimpleIdentifier(SimpleIdentifier node) { | 76 Future<bool> visitPrefixedIdentifier(PrefixedIdentifier node) { |
| 60 AstNode parent = node.parent; | 77 // Make suggestions for the prefix, but not for the selector |
| 61 if (parent is Combinator) { | 78 // InvocationComputer makes selector suggestions |
| 62 return _addCombinatorSuggestions(parent); | 79 if (request.offset <= node.prefix.end) { |
| 63 } | |
| 64 if (parent is ExpressionStatement) { | |
| 65 return _addImportedElementSuggestions(); | 80 return _addImportedElementSuggestions(); |
| 66 } | 81 } |
| 67 if (parent is PrefixedIdentifier) { | 82 return new Future.value(false); |
| 68 if (request.offset <= parent.prefix.end) { | 83 } |
| 69 return _addImportedElementSuggestions(); | 84 |
| 70 } | 85 @override |
| 86 Future<bool> visitCascadeExpression(CascadeExpression node) { | |
| 87 // Make suggestions for the target, but not for the selector | |
| 88 // InvocationComputer makes selector suggestions | |
| 89 Expression target = node.target; | |
| 90 if (target != null && request.offset <= target.end) { | |
| 91 return _addImportedElementSuggestions(); | |
| 71 } | 92 } |
| 72 return new Future.value(false); | 93 return new Future.value(false); |
| 73 } | 94 } |
| 74 | 95 |
| 96 @override | |
| 97 Future<bool> visitSimpleIdentifier(SimpleIdentifier node) { | |
| 98 return node.parent.accept(this); | |
| 99 } | |
| 100 | |
| 75 Future _addCombinatorSuggestions(Combinator node) { | 101 Future _addCombinatorSuggestions(Combinator node) { |
| 76 var directive = node.getAncestor((parent) => parent is NamespaceDirective); | 102 var directive = node.getAncestor((parent) => parent is NamespaceDirective); |
| 77 if (directive is NamespaceDirective) { | 103 if (directive is NamespaceDirective) { |
| 78 LibraryElement library = directive.uriElement; | 104 LibraryElement library = directive.uriElement; |
| 79 LibraryElementSuggestionBuilder.suggestionsFor(request, library); | 105 LibraryElementSuggestionBuilder.suggestionsFor(request, library); |
| 80 return new Future.value(true); | 106 return new Future.value(true); |
| 81 } | 107 } |
| 82 | 108 |
| 83 return new Future.value(false); | 109 return new Future.value(false); |
| 84 } | 110 } |
| 85 | 111 |
| 112 void _addElementSuggestion(Element element, CompletionRelevance relevance) { | |
| 113 CompletionSuggestionKind kind = | |
| 114 newCompletionSuggestionKind_fromElementKind(element.kind); | |
| 115 | |
| 116 String completion = element.displayName; | |
| 117 CompletionSuggestion suggestion = new CompletionSuggestion( | |
| 118 kind, | |
| 119 relevance, | |
| 120 completion, | |
| 121 completion.length, | |
| 122 0, | |
| 123 element.isDeprecated, | |
| 124 false); | |
| 125 | |
| 126 suggestion.element = newElement_fromEngine(element); | |
| 127 | |
| 128 DartType type; | |
| 129 if (element is FunctionElement) { | |
| 130 type = element.returnType; | |
| 131 } else if (element is PropertyAccessorElement && element.isGetter) { | |
| 132 type = element.returnType; | |
| 133 } else if (element is TopLevelVariableElement) { | |
| 134 type = element.type; | |
| 135 } | |
| 136 if (type != null) { | |
| 137 String name = type.displayName; | |
| 138 if (name != null && name.length > 0 && name != 'dynamic') { | |
| 139 suggestion.returnType = name; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 request.suggestions.add(suggestion); | |
| 144 } | |
| 145 | |
| 86 Future<bool> _addImportedElementSuggestions() { | 146 Future<bool> _addImportedElementSuggestions() { |
| 87 | 147 |
| 88 // Exclude elements from local library | 148 // Exclude elements from local library |
| 89 // because they are provided by LocalComputer | 149 // because they are provided by LocalComputer |
| 90 Set<LibraryElement> excludedLibs = new Set<LibraryElement>(); | 150 Set<LibraryElement> excludedLibs = new Set<LibraryElement>(); |
| 91 excludedLibs.add(request.unit.element.enclosingElement); | 151 excludedLibs.add(request.unit.element.enclosingElement); |
| 92 | 152 |
| 93 // Include explicitly imported elements | 153 // Include explicitly imported elements |
| 94 request.unit.directives.forEach((Directive directive) { | 154 request.unit.directives.forEach((Directive directive) { |
| 95 if (directive is ImportDirective) { | 155 if (directive is ImportDirective) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 !excludedLibs.contains(element.library) && | 194 !excludedLibs.contains(element.library) && |
| 135 !completionSet.contains(element.displayName)) { | 195 !completionSet.contains(element.displayName)) { |
| 136 _addElementSuggestion(element, CompletionRelevance.LOW); | 196 _addElementSuggestion(element, CompletionRelevance.LOW); |
| 137 } | 197 } |
| 138 } | 198 } |
| 139 }); | 199 }); |
| 140 return true; | 200 return true; |
| 141 }); | 201 }); |
| 142 } | 202 } |
| 143 | 203 |
| 144 void _addElementSuggestion(Element element, CompletionRelevance relevance) { | |
| 145 CompletionSuggestionKind kind = | |
| 146 newCompletionSuggestionKind_fromElementKind(element.kind); | |
| 147 | |
| 148 String completion = element.displayName; | |
| 149 CompletionSuggestion suggestion = new CompletionSuggestion( | |
| 150 kind, | |
| 151 relevance, | |
| 152 completion, | |
| 153 completion.length, | |
| 154 0, | |
| 155 element.isDeprecated, | |
| 156 false); | |
| 157 | |
| 158 suggestion.element = newElement_fromEngine(element); | |
| 159 | |
| 160 DartType type; | |
| 161 if (element is FunctionElement) { | |
| 162 type = element.returnType; | |
| 163 } else if (element is PropertyAccessorElement && element.isGetter) { | |
| 164 type = element.returnType; | |
| 165 } else if (element is TopLevelVariableElement) { | |
| 166 type = element.type; | |
| 167 } | |
| 168 if (type != null) { | |
| 169 String name = type.displayName; | |
| 170 if (name != null && name.length > 0 && name != 'dynamic') { | |
| 171 suggestion.returnType = name; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 request.suggestions.add(suggestion); | |
| 176 } | |
| 177 | |
| 178 void _addLibraryPrefixSuggestion(ImportElement importElem) { | 204 void _addLibraryPrefixSuggestion(ImportElement importElem) { |
| 179 String completion = importElem.prefix.displayName; | 205 String completion = importElem.prefix.displayName; |
| 180 if (completion != null && completion.length > 0) { | 206 if (completion != null && completion.length > 0) { |
| 181 CompletionSuggestion suggestion = new CompletionSuggestion( | 207 CompletionSuggestion suggestion = new CompletionSuggestion( |
| 182 CompletionSuggestionKind.LIBRARY_PREFIX, | 208 CompletionSuggestionKind.LIBRARY_PREFIX, |
| 183 CompletionRelevance.DEFAULT, | 209 CompletionRelevance.DEFAULT, |
| 184 completion, | 210 completion, |
| 185 completion.length, | 211 completion.length, |
| 186 0, | 212 0, |
| 187 importElem.isDeprecated, | 213 importElem.isDeprecated, |
| 188 false); | 214 false); |
| 189 LibraryElement lib = importElem.importedLibrary; | 215 LibraryElement lib = importElem.importedLibrary; |
| 190 if (lib != null) { | 216 if (lib != null) { |
| 191 suggestion.element = newElement_fromEngine(lib); | 217 suggestion.element = newElement_fromEngine(lib); |
| 192 } | 218 } |
| 193 request.suggestions.add(suggestion); | 219 request.suggestions.add(suggestion); |
| 194 } | 220 } |
| 195 } | 221 } |
| 196 } | 222 } |
| OLD | NEW |