| 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.local; | 5 library services.completion.computer.dart.local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol.dart'; | 9 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 @override | 50 @override |
| 51 visitBlock(Block node) { | 51 visitBlock(Block node) { |
| 52 node.statements.forEach((Statement stmt) { | 52 node.statements.forEach((Statement stmt) { |
| 53 if (stmt.offset < request.offset) { | 53 if (stmt.offset < request.offset) { |
| 54 if (stmt is LabeledStatement) { | 54 if (stmt is LabeledStatement) { |
| 55 stmt.labels.forEach((Label label) { | 55 stmt.labels.forEach((Label label) { |
| 56 // _addSuggestion(label.label, CompletionSuggestionKind.LABEL); | 56 // _addSuggestion(label.label, CompletionSuggestionKind.LABEL); |
| 57 }); | 57 }); |
| 58 } else if (stmt is VariableDeclarationStatement) { | 58 } else if (stmt is VariableDeclarationStatement) { |
| 59 stmt.variables.variables.forEach((VariableDeclaration varDecl) { | 59 VariableDeclarationList varList = stmt.variables; |
| 60 if (varDecl.end < request.offset) { | 60 if (varList != null) { |
| 61 _addSuggestion( | 61 varList.variables.forEach((VariableDeclaration varDecl) { |
| 62 varDecl.name, | 62 if (varDecl.end < request.offset) { |
| 63 CompletionSuggestionKind.LOCAL_VARIABLE); | 63 _addSuggestion( |
| 64 } | 64 varDecl.name, |
| 65 }); | 65 CompletionSuggestionKind.LOCAL_VARIABLE, |
| 66 varList.type, |
| 67 null); |
| 68 } |
| 69 }); |
| 70 } |
| 66 } | 71 } |
| 67 } | 72 } |
| 68 }); | 73 }); |
| 69 visitNode(node); | 74 visitNode(node); |
| 70 } | 75 } |
| 71 | 76 |
| 72 @override | 77 @override |
| 73 visitCatchClause(CatchClause node) { | 78 visitCatchClause(CatchClause node) { |
| 74 _addSuggestion(node.exceptionParameter, CompletionSuggestionKind.PARAMETER); | 79 _addSuggestion( |
| 75 _addSuggestion(node.stackTraceParameter, CompletionSuggestionKind.PARAMETER)
; | 80 node.exceptionParameter, |
| 81 CompletionSuggestionKind.PARAMETER, |
| 82 node.exceptionType, |
| 83 null); |
| 84 // TODO (danrubel) add stack trace types |
| 85 _addSuggestion( |
| 86 node.stackTraceParameter, |
| 87 CompletionSuggestionKind.PARAMETER, |
| 88 null, |
| 89 null); |
| 76 visitNode(node); | 90 visitNode(node); |
| 77 } | 91 } |
| 78 | 92 |
| 79 @override | 93 @override |
| 80 visitClassDeclaration(ClassDeclaration node) { | 94 visitClassDeclaration(ClassDeclaration node) { |
| 81 node.members.forEach((ClassMember classMbr) { | 95 node.members.forEach((ClassMember classMbr) { |
| 82 if (classMbr is FieldDeclaration) { | 96 if (classMbr is FieldDeclaration) { |
| 83 _addSuggestions(classMbr.fields, CompletionSuggestionKind.FIELD); | 97 _addVarListSuggestions( |
| 98 classMbr.fields, |
| 99 CompletionSuggestionKind.FIELD, |
| 100 node); |
| 84 } else if (classMbr is MethodDeclaration) { | 101 } else if (classMbr is MethodDeclaration) { |
| 85 _addSuggestion(classMbr.name, CompletionSuggestionKind.METHOD_NAME); | 102 _addSuggestion( |
| 103 classMbr.name, |
| 104 CompletionSuggestionKind.METHOD_NAME, |
| 105 classMbr.returnType, |
| 106 node); |
| 86 } | 107 } |
| 87 }); | 108 }); |
| 88 visitNode(node); | 109 visitNode(node); |
| 89 } | 110 } |
| 90 | 111 |
| 91 @override | 112 @override |
| 92 visitCompilationUnit(CompilationUnit node) { | 113 visitCompilationUnit(CompilationUnit node) { |
| 93 node.directives.forEach((Directive directive) { | 114 node.directives.forEach((Directive directive) { |
| 94 if (directive is ImportDirective) { | 115 if (directive is ImportDirective) { |
| 95 _addSuggestion( | 116 _addSuggestion( |
| 96 directive.prefix, | 117 directive.prefix, |
| 97 CompletionSuggestionKind.LIBRARY_PREFIX); | 118 CompletionSuggestionKind.LIBRARY_PREFIX, |
| 119 null, |
| 120 null); |
| 98 } | 121 } |
| 99 }); | 122 }); |
| 100 node.declarations.forEach((Declaration declaration) { | 123 node.declarations.forEach((Declaration declaration) { |
| 101 if (declaration is ClassDeclaration) { | 124 if (declaration is ClassDeclaration) { |
| 102 _addSuggestion(declaration.name, CompletionSuggestionKind.CLASS); | 125 _addSuggestion( |
| 126 declaration.name, |
| 127 CompletionSuggestionKind.CLASS, |
| 128 null, |
| 129 null); |
| 103 } else if (declaration is EnumDeclaration) { | 130 } else if (declaration is EnumDeclaration) { |
| 104 // _addSuggestion(d.name, CompletionSuggestionKind.ENUM); | 131 // _addSuggestion(d.name, CompletionSuggestionKind.ENUM); |
| 105 } else if (declaration is FunctionDeclaration) { | 132 } else if (declaration is FunctionDeclaration) { |
| 106 _addSuggestion(declaration.name, CompletionSuggestionKind.FUNCTION); | 133 _addSuggestion( |
| 134 declaration.name, |
| 135 CompletionSuggestionKind.FUNCTION, |
| 136 declaration.returnType, |
| 137 null); |
| 107 } else if (declaration is TopLevelVariableDeclaration) { | 138 } else if (declaration is TopLevelVariableDeclaration) { |
| 108 _addSuggestions( | 139 _addVarListSuggestions( |
| 109 declaration.variables, | 140 declaration.variables, |
| 110 CompletionSuggestionKind.TOP_LEVEL_VARIABLE); | 141 CompletionSuggestionKind.TOP_LEVEL_VARIABLE, |
| 142 null); |
| 111 } else if (declaration is ClassTypeAlias) { | 143 } else if (declaration is ClassTypeAlias) { |
| 112 _addSuggestion(declaration.name, CompletionSuggestionKind.CLASS_ALIAS); | 144 _addSuggestion( |
| 145 declaration.name, |
| 146 CompletionSuggestionKind.CLASS_ALIAS, |
| 147 null, |
| 148 null); |
| 113 } else if (declaration is FunctionTypeAlias) { | 149 } else if (declaration is FunctionTypeAlias) { |
| 114 _addSuggestion( | 150 _addSuggestion( |
| 115 declaration.name, | 151 declaration.name, |
| 116 CompletionSuggestionKind.FUNCTION_TYPE_ALIAS); | 152 CompletionSuggestionKind.FUNCTION_TYPE_ALIAS, |
| 153 declaration.returnType, |
| 154 null); |
| 117 } | 155 } |
| 118 }); | 156 }); |
| 119 } | 157 } |
| 120 | 158 |
| 121 @override | 159 @override |
| 160 visitExpressionStatement(ExpressionStatement node) { |
| 161 Expression expression = node.expression; |
| 162 if (expression is SimpleIdentifier) { |
| 163 if (expression.end < request.offset) { |
| 164 // TODO (danrubel) suggest possible names for variable declaration |
| 165 // based upon variable type |
| 166 return; |
| 167 } |
| 168 } |
| 169 visitNode(node); |
| 170 } |
| 171 |
| 172 @override |
| 122 visitForEachStatement(ForEachStatement node) { | 173 visitForEachStatement(ForEachStatement node) { |
| 123 _addSuggestion(node.identifier, CompletionSuggestionKind.LOCAL_VARIABLE); | 174 // TODO (danrubel) supply type of local variable |
| 175 _addSuggestion( |
| 176 node.identifier, |
| 177 CompletionSuggestionKind.LOCAL_VARIABLE, |
| 178 null, |
| 179 null); |
| 124 visitNode(node); | 180 visitNode(node); |
| 125 } | 181 } |
| 126 | 182 |
| 127 @override | 183 @override |
| 128 visitForStatement(ForStatement node) { | 184 visitForStatement(ForStatement node) { |
| 129 _addSuggestions(node.variables, CompletionSuggestionKind.LOCAL_VARIABLE); | 185 _addVarListSuggestions( |
| 186 node.variables, |
| 187 CompletionSuggestionKind.LOCAL_VARIABLE, |
| 188 null); |
| 130 visitNode(node); | 189 visitNode(node); |
| 131 } | 190 } |
| 132 | 191 |
| 133 @override | 192 @override |
| 134 visitFunctionDeclaration(FunctionDeclaration node) { | 193 visitFunctionDeclaration(FunctionDeclaration node) { |
| 135 // This is added by the compilation unit containing it | 194 // This is added by the compilation unit containing it |
| 136 //_addSuggestion(node.name, CompletionSuggestionKind.FUNCTION); | 195 //_addSuggestion(node.name, CompletionSuggestionKind.FUNCTION); |
| 137 visitNode(node); | 196 visitNode(node); |
| 138 } | 197 } |
| 139 | 198 |
| 140 @override | 199 @override |
| 141 visitFunctionExpression(FunctionExpression node) { | 200 visitFunctionExpression(FunctionExpression node) { |
| 142 node.parameters.parameters.forEach((FormalParameter param) { | 201 _addParamListSuggestions(node.parameters); |
| 143 _addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); | |
| 144 }); | |
| 145 visitNode(node); | 202 visitNode(node); |
| 146 } | 203 } |
| 147 | 204 |
| 148 @override | 205 @override |
| 149 visitMethodDeclaration(MethodDeclaration node) { | 206 visitMethodDeclaration(MethodDeclaration node) { |
| 150 node.parameters.parameters.forEach((FormalParameter param) { | 207 _addParamListSuggestions(node.parameters); |
| 151 if (param.identifier != null) { | |
| 152 _addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); | |
| 153 } | |
| 154 }); | |
| 155 visitNode(node); | 208 visitNode(node); |
| 156 } | 209 } |
| 157 | 210 |
| 158 @override | 211 @override |
| 159 visitNode(AstNode node) { | 212 visitNode(AstNode node) { |
| 160 node.parent.accept(this); | 213 node.parent.accept(this); |
| 161 } | 214 } |
| 162 | 215 |
| 163 @override | 216 @override |
| 164 visitVariableDeclaration(VariableDeclaration node) { | 217 visitVariableDeclaration(VariableDeclaration node) { |
| 165 // Do not add suggestions if editing the name in a var declaration | 218 // Do not add suggestions if editing the name in a var declaration |
| 166 SimpleIdentifier name = node.name; | 219 SimpleIdentifier name = node.name; |
| 167 if (name == null || | 220 if (name == null || |
| 168 name.offset < request.offset || | 221 name.offset < request.offset || |
| 169 request.offset > name.end) { | 222 request.offset > name.end) { |
| 170 visitNode(node); | 223 visitNode(node); |
| 171 } | 224 } |
| 172 } | 225 } |
| 173 | 226 |
| 174 void _addSuggestion(SimpleIdentifier id, CompletionSuggestionKind kind) { | 227 void _addParamListSuggestions(FormalParameterList paramList) { |
| 228 if (paramList != null) { |
| 229 paramList.parameters.forEach((FormalParameter param) { |
| 230 NormalFormalParameter normalParam; |
| 231 if (param is DefaultFormalParameter) { |
| 232 normalParam = param.parameter; |
| 233 } else if (param is NormalFormalParameter) { |
| 234 normalParam = param; |
| 235 } |
| 236 TypeName type = null; |
| 237 if (normalParam is FieldFormalParameter) { |
| 238 type = normalParam.type; |
| 239 } else if (normalParam is FunctionTypedFormalParameter) { |
| 240 type = normalParam.returnType; |
| 241 } else if (normalParam is SimpleFormalParameter) { |
| 242 type = normalParam.type; |
| 243 } |
| 244 _addSuggestion( |
| 245 param.identifier, |
| 246 CompletionSuggestionKind.PARAMETER, |
| 247 type, |
| 248 null); |
| 249 }); |
| 250 } |
| 251 } |
| 252 |
| 253 void _addSuggestion(SimpleIdentifier id, CompletionSuggestionKind kind, |
| 254 TypeName typeName, ClassDeclaration classDecl) { |
| 175 if (id != null) { | 255 if (id != null) { |
| 176 String completion = id.name; | 256 String completion = id.name; |
| 177 if (completion != null && completion.length > 0) { | 257 if (completion != null && completion.length > 0) { |
| 178 request.suggestions.add( | 258 CompletionSuggestion suggestion = new CompletionSuggestion( |
| 179 new CompletionSuggestion( | 259 kind, |
| 180 kind, | 260 CompletionRelevance.DEFAULT, |
| 181 CompletionRelevance.DEFAULT, | 261 completion, |
| 182 completion, | 262 completion.length, |
| 183 completion.length, | 263 0, |
| 184 0, | 264 false, |
| 185 false, | 265 false); |
| 186 false)); | 266 if (classDecl != null) { |
| 267 SimpleIdentifier identifier = classDecl.name; |
| 268 if (identifier != null) { |
| 269 String name = identifier.name; |
| 270 if (name != null && name.length > 0) { |
| 271 suggestion.declaringType = name; |
| 272 } |
| 273 } |
| 274 } |
| 275 if (typeName != null) { |
| 276 Identifier identifier = typeName.name; |
| 277 if (identifier != null) { |
| 278 String name = identifier.name; |
| 279 if (name != null && name.length > 0) { |
| 280 suggestion.returnType = name; |
| 281 } |
| 282 } |
| 283 } |
| 284 request.suggestions.add(suggestion); |
| 187 } | 285 } |
| 188 } | 286 } |
| 189 } | 287 } |
| 190 | 288 |
| 191 void _addSuggestions(VariableDeclarationList variables, | 289 void _addVarListSuggestions(VariableDeclarationList variables, |
| 192 CompletionSuggestionKind kind) { | 290 CompletionSuggestionKind kind, ClassDeclaration classDecl) { |
| 193 variables.variables.forEach((VariableDeclaration varDecl) { | 291 variables.variables.forEach((VariableDeclaration varDecl) { |
| 194 _addSuggestion(varDecl.name, kind); | 292 _addSuggestion(varDecl.name, kind, variables.type, classDecl); |
| 195 }); | 293 }); |
| 196 } | 294 } |
| 197 } | 295 } |
| OLD | NEW |