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