| 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.contributor.dart.invocation; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 10 import 'package:analysis_server/src/services/completion/local_declaration_visito
r.dart'; | |
| 11 import 'package:analysis_server/src/services/completion/optype.dart'; | |
| 12 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; | |
| 13 import 'package:analyzer/src/generated/ast.dart'; | |
| 14 import 'package:analyzer/src/generated/element.dart'; | |
| 15 | |
| 16 import '../../protocol_server.dart' show CompletionSuggestionKind; | |
| 17 | |
| 18 /** | |
| 19 * A contributor for calculating invocation / access suggestions | |
| 20 * `completion.getSuggestions` request results. | |
| 21 */ | |
| 22 class PrefixedElementContributor extends DartCompletionContributor { | |
| 23 SuggestionBuilder builder; | |
| 24 | |
| 25 @override | |
| 26 bool computeFast(DartCompletionRequest request) { | |
| 27 OpType optype = request.optype; | |
| 28 if (optype.includeInvocationSuggestions) { | |
| 29 builder = request.target.containingNode | |
| 30 .accept(new _InvocationAstVisitor(request)); | |
| 31 if (builder != null) { | |
| 32 return builder.computeFast(request.target.containingNode); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 @override | |
| 40 Future<bool> computeFull(DartCompletionRequest request) { | |
| 41 if (builder != null) { | |
| 42 return builder.computeFull(request.target.containingNode); | |
| 43 } | |
| 44 return new Future.value(false); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 class _ExpressionSuggestionBuilder implements SuggestionBuilder { | |
| 49 final DartCompletionRequest request; | |
| 50 | |
| 51 _ExpressionSuggestionBuilder(this.request); | |
| 52 | |
| 53 @override | |
| 54 bool computeFast(AstNode node) { | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 @override | |
| 59 Future<bool> computeFull(AstNode node) { | |
| 60 if (node is MethodInvocation) { | |
| 61 node = (node as MethodInvocation).realTarget; | |
| 62 } else if (node is PropertyAccess) { | |
| 63 node = (node as PropertyAccess).realTarget; | |
| 64 } | |
| 65 if (node is Identifier) { | |
| 66 Element elem = node.bestElement; | |
| 67 if (elem is ClassElement || elem is PrefixElement) { | |
| 68 elem.accept(new _PrefixedIdentifierSuggestionBuilder(request)); | |
| 69 return new Future.value(true); | |
| 70 } | |
| 71 } | |
| 72 if (node is Expression) { | |
| 73 InterfaceTypeSuggestionBuilder.suggestionsFor(request, node.bestType); | |
| 74 return new Future.value(true); | |
| 75 } | |
| 76 return new Future.value(false); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * An [AstNode] vistor for determining which suggestion builder | |
| 82 * should be used to build invocation/access suggestions. | |
| 83 */ | |
| 84 class _InvocationAstVisitor extends GeneralizingAstVisitor<SuggestionBuilder> { | |
| 85 final DartCompletionRequest request; | |
| 86 | |
| 87 _InvocationAstVisitor(this.request); | |
| 88 | |
| 89 @override | |
| 90 SuggestionBuilder visitConstructorName(ConstructorName node) { | |
| 91 // some PrefixedIdentifier nodes are transformed into | |
| 92 // ConstructorName nodes during the resolution process. | |
| 93 return new _PrefixedIdentifierSuggestionBuilder(request); | |
| 94 } | |
| 95 | |
| 96 @override | |
| 97 SuggestionBuilder visitMethodInvocation(MethodInvocation node) { | |
| 98 return new _ExpressionSuggestionBuilder(request); | |
| 99 } | |
| 100 | |
| 101 @override | |
| 102 SuggestionBuilder visitNode(AstNode node) { | |
| 103 return null; | |
| 104 } | |
| 105 | |
| 106 @override | |
| 107 SuggestionBuilder visitPrefixedIdentifier(PrefixedIdentifier node) { | |
| 108 // some PrefixedIdentifier nodes are transformed into | |
| 109 // ConstructorName nodes during the resolution process. | |
| 110 return new _PrefixedIdentifierSuggestionBuilder(request); | |
| 111 } | |
| 112 | |
| 113 @override | |
| 114 SuggestionBuilder visitPropertyAccess(PropertyAccess node) { | |
| 115 return new _ExpressionSuggestionBuilder(request); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * An [AstVisitor] which looks for a declaration with the given name | |
| 121 * and if found, tries to determine a type for that declaration. | |
| 122 */ | |
| 123 class _LocalBestTypeVisitor extends LocalDeclarationVisitor { | |
| 124 | |
| 125 /** | |
| 126 * The name for the declaration to be found. | |
| 127 */ | |
| 128 final String targetName; | |
| 129 | |
| 130 /** | |
| 131 * The best type for the found declaration, | |
| 132 * or `null` if no declaration found or failed to determine a type. | |
| 133 */ | |
| 134 DartType typeFound; | |
| 135 | |
| 136 /** | |
| 137 * Construct a new instance to search for a declaration | |
| 138 */ | |
| 139 _LocalBestTypeVisitor(this.targetName, int offset) : super(offset); | |
| 140 | |
| 141 @override | |
| 142 void declaredClass(ClassDeclaration declaration) { | |
| 143 if (declaration.name.name == targetName) { | |
| 144 // no type | |
| 145 finished(); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 @override | |
| 150 void declaredClassTypeAlias(ClassTypeAlias declaration) { | |
| 151 if (declaration.name.name == targetName) { | |
| 152 // no type | |
| 153 finished(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 @override | |
| 158 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) { | |
| 159 if (varDecl.name.name == targetName) { | |
| 160 // Type provided by the element in computeFull above | |
| 161 finished(); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 @override | |
| 166 void declaredFunction(FunctionDeclaration declaration) { | |
| 167 if (declaration.name.name == targetName) { | |
| 168 TypeName typeName = declaration.returnType; | |
| 169 if (typeName != null) { | |
| 170 typeFound = typeName.type; | |
| 171 } | |
| 172 finished(); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 @override | |
| 177 void declaredFunctionTypeAlias(FunctionTypeAlias declaration) { | |
| 178 if (declaration.name.name == targetName) { | |
| 179 TypeName typeName = declaration.returnType; | |
| 180 if (typeName != null) { | |
| 181 typeFound = typeName.type; | |
| 182 } | |
| 183 finished(); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 @override | |
| 188 void declaredLabel(Label label, bool isCaseLabel) { | |
| 189 if (label.label.name == targetName) { | |
| 190 // no type | |
| 191 finished(); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 @override | |
| 196 void declaredLocalVar(SimpleIdentifier name, TypeName type) { | |
| 197 if (name.name == targetName) { | |
| 198 typeFound = name.bestType; | |
| 199 finished(); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 @override | |
| 204 void declaredMethod(MethodDeclaration declaration) { | |
| 205 if (declaration.name.name == targetName) { | |
| 206 TypeName typeName = declaration.returnType; | |
| 207 if (typeName != null) { | |
| 208 typeFound = typeName.type; | |
| 209 } | |
| 210 finished(); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 @override | |
| 215 void declaredParam(SimpleIdentifier name, TypeName type) { | |
| 216 if (name.name == targetName) { | |
| 217 // Type provided by the element in computeFull above | |
| 218 finished(); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 @override | |
| 223 void declaredTopLevelVar( | |
| 224 VariableDeclarationList varList, VariableDeclaration varDecl) { | |
| 225 if (varDecl.name.name == targetName) { | |
| 226 // Type provided by the element in computeFull above | |
| 227 finished(); | |
| 228 } | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 /** | |
| 233 * An [Element] visitor for determining the appropriate invocation/access | |
| 234 * suggestions based upon the element for which the completion is requested. | |
| 235 */ | |
| 236 class _PrefixedIdentifierSuggestionBuilder | |
| 237 extends GeneralizingElementVisitor<Future<bool>> | |
| 238 implements SuggestionBuilder { | |
| 239 final DartCompletionRequest request; | |
| 240 | |
| 241 _PrefixedIdentifierSuggestionBuilder(this.request); | |
| 242 | |
| 243 @override | |
| 244 bool computeFast(AstNode node) { | |
| 245 return false; | |
| 246 } | |
| 247 | |
| 248 @override | |
| 249 Future<bool> computeFull(AstNode node) { | |
| 250 if (node is ConstructorName) { | |
| 251 // some PrefixedIdentifier nodes are transformed into | |
| 252 // ConstructorName nodes during the resolution process. | |
| 253 return new NamedConstructorSuggestionBuilder(request).computeFull(node); | |
| 254 } | |
| 255 if (node is PrefixedIdentifier) { | |
| 256 SimpleIdentifier prefix = node.prefix; | |
| 257 if (prefix != null) { | |
| 258 Element element = prefix.bestElement; | |
| 259 DartType type = prefix.bestType; | |
| 260 if (element is! ClassElement) { | |
| 261 if (type == null || type.isDynamic) { | |
| 262 // | |
| 263 // Given `g. int y = 0;`, the parser interprets `g` as a prefixed | |
| 264 // identifier with no type. | |
| 265 // If the user is requesting completions for `g`, | |
| 266 // then check for a function, getter, or similar with a type. | |
| 267 // | |
| 268 _LocalBestTypeVisitor visitor = | |
| 269 new _LocalBestTypeVisitor(prefix.name, request.offset); | |
| 270 if (visitor.visit(prefix)) { | |
| 271 type = visitor.typeFound; | |
| 272 } | |
| 273 } | |
| 274 if (type != null && !type.isDynamic) { | |
| 275 InterfaceTypeSuggestionBuilder.suggestionsFor(request, type); | |
| 276 return new Future.value(true); | |
| 277 } | |
| 278 } | |
| 279 if (element != null) { | |
| 280 return element.accept(this); | |
| 281 } | |
| 282 } | |
| 283 } | |
| 284 return new Future.value(false); | |
| 285 } | |
| 286 | |
| 287 @override | |
| 288 Future<bool> visitClassElement(ClassElement element) { | |
| 289 if (element != null) { | |
| 290 InterfaceType type = element.type; | |
| 291 if (type != null) { | |
| 292 StaticClassElementSuggestionBuilder.suggestionsFor( | |
| 293 request, type.element); | |
| 294 } | |
| 295 } | |
| 296 return new Future.value(false); | |
| 297 } | |
| 298 | |
| 299 @override | |
| 300 Future<bool> visitElement(Element element) { | |
| 301 return new Future.value(false); | |
| 302 } | |
| 303 | |
| 304 @override | |
| 305 Future<bool> visitPrefixElement(PrefixElement element) { | |
| 306 //TODO (danrubel) reimplement to use prefixElement.importedLibraries | |
| 307 // once that accessor is implemented and available in Dart | |
| 308 bool modified = false; | |
| 309 // Find the import directive with the given prefix | |
| 310 for (Directive directive in request.unit.directives) { | |
| 311 if (directive is ImportDirective) { | |
| 312 if (directive.prefix != null) { | |
| 313 if (directive.prefix.name == element.name) { | |
| 314 // Suggest elements from the imported library | |
| 315 LibraryElement library = directive.uriElement; | |
| 316 LibraryElementSuggestionBuilder.suggestionsFor(request, | |
| 317 CompletionSuggestionKind.INVOCATION, library, | |
| 318 request.target.containingNode.parent is TypeName); | |
| 319 modified = true; | |
| 320 } | |
| 321 } | |
| 322 } | |
| 323 } | |
| 324 return new Future.value(modified); | |
| 325 } | |
| 326 | |
| 327 @override | |
| 328 Future<bool> visitPropertyAccessorElement(PropertyAccessorElement element) { | |
| 329 if (element != null) { | |
| 330 PropertyInducingElement elemVar = element.variable; | |
| 331 if (elemVar != null) { | |
| 332 InterfaceTypeSuggestionBuilder.suggestionsFor(request, elemVar.type); | |
| 333 } | |
| 334 return new Future.value(true); | |
| 335 } | |
| 336 return new Future.value(false); | |
| 337 } | |
| 338 | |
| 339 @override | |
| 340 Future<bool> visitVariableElement(VariableElement element) { | |
| 341 InterfaceTypeSuggestionBuilder.suggestionsFor(request, element.type); | |
| 342 return new Future.value(true); | |
| 343 } | |
| 344 } | |
| OLD | NEW |