| 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.arglist; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:analysis_server/src/protocol_server.dart' | |
| 10 hide Element, ElementKind; | |
| 11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 12 import 'package:analysis_server/src/services/completion/local_declaration_visito
r.dart'; | |
| 13 import 'package:analyzer/src/generated/ast.dart'; | |
| 14 import 'package:analyzer/src/generated/scanner.dart'; | |
| 15 | |
| 16 /** | |
| 17 * A contributor for calculating `completion.getSuggestions` request results | |
| 18 * when the cursor position is inside the arguments to a method call. | |
| 19 */ | |
| 20 class ArgListContributor extends DartCompletionContributor { | |
| 21 _ArgListSuggestionBuilder builder; | |
| 22 | |
| 23 @override | |
| 24 bool computeFast(DartCompletionRequest request) { | |
| 25 builder = | |
| 26 request.target.containingNode.accept(new _ArgListAstVisitor(request)); | |
| 27 return builder == null; | |
| 28 } | |
| 29 | |
| 30 @override | |
| 31 Future<bool> computeFull(DartCompletionRequest request) { | |
| 32 if (builder != null) { | |
| 33 return builder.compute(request.target.containingNode); | |
| 34 } | |
| 35 return new Future.value(false); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * A visitor for determining whether an argument list suggestion is needed | |
| 41 * and instantiating the builder to create the suggestion. | |
| 42 */ | |
| 43 class _ArgListAstVisitor | |
| 44 extends GeneralizingAstVisitor<_ArgListSuggestionBuilder> { | |
| 45 final DartCompletionRequest request; | |
| 46 | |
| 47 _ArgListAstVisitor(this.request); | |
| 48 | |
| 49 @override | |
| 50 _ArgListSuggestionBuilder visitArgumentList(ArgumentList node) { | |
| 51 Token leftParen = node.leftParenthesis; | |
| 52 if (leftParen != null && request.offset > leftParen.offset) { | |
| 53 AstNode parent = node.parent; | |
| 54 if (parent is MethodInvocation) { | |
| 55 SimpleIdentifier selector = parent.methodName; | |
| 56 if (selector != null) { | |
| 57 String name = selector.name; | |
| 58 if (name != null && name.length > 0) { | |
| 59 if (parent.operator == null) { | |
| 60 /* | |
| 61 * If a local declaration is found, then return null | |
| 62 * indicating that suggestions were added | |
| 63 * and no further action is necessary | |
| 64 */ | |
| 65 if (new _LocalDeclarationFinder(request, request.offset, name) | |
| 66 .visit(node)) { | |
| 67 return null; | |
| 68 } | |
| 69 } else { | |
| 70 // determine target | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 return new _ArgListSuggestionBuilder(request); | |
| 76 } | |
| 77 return null; | |
| 78 } | |
| 79 | |
| 80 @override | |
| 81 _ArgListSuggestionBuilder visitNode(AstNode node) { | |
| 82 return null; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * A `_ArgListSuggestionBuilder` determines which method or function is being | |
| 88 * invoked, then builds the argument list suggestion. | |
| 89 * This operation is instantiated during `computeFast` | |
| 90 * and calculates the suggestions during `computeFull`. | |
| 91 */ | |
| 92 class _ArgListSuggestionBuilder { | |
| 93 final DartCompletionRequest request; | |
| 94 | |
| 95 _ArgListSuggestionBuilder(this.request); | |
| 96 | |
| 97 Future<bool> compute(ArgumentList node) { | |
| 98 return new Future.value(false); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * `_LocalDeclarationFinder` visits an [AstNode] and its parent recursively | |
| 104 * looking for a matching declaration. If found, it adds the appropriate | |
| 105 * suggestions and sets finished to `true`. | |
| 106 */ | |
| 107 class _LocalDeclarationFinder extends LocalDeclarationVisitor { | |
| 108 final DartCompletionRequest request; | |
| 109 final String name; | |
| 110 | |
| 111 _LocalDeclarationFinder(this.request, int offset, this.name) : super(offset); | |
| 112 | |
| 113 @override | |
| 114 void declaredClass(ClassDeclaration declaration) {} | |
| 115 | |
| 116 @override | |
| 117 void declaredClassTypeAlias(ClassTypeAlias declaration) {} | |
| 118 | |
| 119 @override | |
| 120 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) {} | |
| 121 | |
| 122 @override | |
| 123 void declaredFunction(FunctionDeclaration declaration) { | |
| 124 SimpleIdentifier selector = declaration.name; | |
| 125 if (selector != null && name == selector.name) { | |
| 126 _addArgListSuggestion(declaration.functionExpression.parameters); | |
| 127 finished(); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 @override | |
| 132 void declaredFunctionTypeAlias(FunctionTypeAlias declaration) {} | |
| 133 | |
| 134 @override | |
| 135 void declaredLabel(Label label, bool isCaseLabel) {} | |
| 136 | |
| 137 @override | |
| 138 void declaredLocalVar(SimpleIdentifier name, TypeName type) {} | |
| 139 | |
| 140 @override | |
| 141 void declaredMethod(MethodDeclaration declaration) { | |
| 142 SimpleIdentifier selector = declaration.name; | |
| 143 if (selector != null && name == selector.name) { | |
| 144 _addArgListSuggestion(declaration.parameters); | |
| 145 finished(); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 @override | |
| 150 void declaredParam(SimpleIdentifier name, TypeName type) {} | |
| 151 | |
| 152 @override | |
| 153 void declaredTopLevelVar( | |
| 154 VariableDeclarationList varList, VariableDeclaration varDecl) {} | |
| 155 | |
| 156 void _addArgListSuggestion(FormalParameterList parameters) { | |
| 157 if (parameters.parameters.length == 0) { | |
| 158 return; | |
| 159 } | |
| 160 StringBuffer completion = new StringBuffer('('); | |
| 161 List<String> paramNames = new List<String>(); | |
| 162 List<String> paramTypes = new List<String>(); | |
| 163 for (FormalParameter param in parameters.parameters) { | |
| 164 SimpleIdentifier paramId = param.identifier; | |
| 165 if (paramId != null) { | |
| 166 String name = paramId.name; | |
| 167 if (name != null && name.length > 0) { | |
| 168 if (completion.length > 1) { | |
| 169 completion.write(', '); | |
| 170 } | |
| 171 completion.write(name); | |
| 172 paramNames.add(name); | |
| 173 paramTypes.add(_getParamType(param)); | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 completion.write(')'); | |
| 178 CompletionSuggestion suggestion = new CompletionSuggestion( | |
| 179 CompletionSuggestionKind.ARGUMENT_LIST, DART_RELEVANCE_HIGH, | |
| 180 completion.toString(), completion.length, 0, false, false); | |
| 181 suggestion.parameterNames = paramNames; | |
| 182 suggestion.parameterTypes = paramTypes; | |
| 183 request.addSuggestion(suggestion); | |
| 184 } | |
| 185 | |
| 186 String _getParamType(FormalParameter param) { | |
| 187 TypeName type; | |
| 188 if (param is SimpleFormalParameter) { | |
| 189 type = param.type; | |
| 190 } | |
| 191 if (type != null) { | |
| 192 Identifier id = type.name; | |
| 193 if (id != null) { | |
| 194 String name = id.name; | |
| 195 if (name != null && name.length > 0) { | |
| 196 return name; | |
| 197 } | |
| 198 } | |
| 199 } | |
| 200 return 'dynamic'; | |
| 201 } | |
| 202 } | |
| OLD | NEW |