| 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_services/completion/completion_computer.dart'; | |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; | 9 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 10 import 'package:analysis_services/src/completion/dart_completion_manager.dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; | 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * A computer for calculating `completion.getSuggestions` request results | 14 * A computer for calculating `completion.getSuggestions` request results |
| 15 * for the local library in which the completion is requested. | 15 * for the local library in which the completion is requested. |
| 16 */ | 16 */ |
| 17 class LocalComputer extends CompletionComputer { | 17 class LocalComputer extends DartCompletionComputer { |
| 18 | 18 |
| 19 @override | 19 @override |
| 20 bool computeFast(CompilationUnit unit, AstNode node, | 20 bool computeFast(DartCompletionRequest request) { |
| 21 List<CompletionSuggestion> suggestions) { | |
| 22 | 21 |
| 23 // Find the specific child [AstNode] that contains the completion offset | 22 // Find the specific child [AstNode] that contains the completion offset |
| 24 // and collect suggestions starting with that node | 23 // and collect suggestions starting with that node |
| 25 if (node != null) { | 24 request.node.accept(new _LocalVisitor(request)); |
| 26 node.accept(new _LocalVisitor(offset, suggestions)); | |
| 27 } | |
| 28 | 25 |
| 29 // If the unit is not a part and does not reference any parts | 26 // If the unit is not a part and does not reference any parts |
| 30 // then work is complete | 27 // then work is complete |
| 31 return !unit.directives.any( | 28 return !request.unit.directives.any( |
| 32 (Directive directive) => | 29 (Directive directive) => |
| 33 directive is PartOfDirective || directive is PartDirective); | 30 directive is PartOfDirective || directive is PartDirective); |
| 34 } | 31 } |
| 35 | 32 |
| 36 @override | 33 @override |
| 37 Future<bool> computeFull(CompilationUnit unit, AstNode node, | 34 Future<bool> computeFull(DartCompletionRequest request) { |
| 38 List<CompletionSuggestion> suggestions) { | |
| 39 // TODO: implement computeFull | 35 // TODO: implement computeFull |
| 40 // include results from part files that are included in the library | 36 // include results from part files that are included in the library |
| 41 return new Future.value(false); | 37 return new Future.value(false); |
| 42 } | 38 } |
| 43 } | 39 } |
| 44 | 40 |
| 45 /** | 41 /** |
| 46 * A visitor for collecting suggestions from the most specific child [AstNode] | 42 * A visitor for collecting suggestions from the most specific child [AstNode] |
| 47 * that contains the completion offset to the [CompilationUnit]. | 43 * that contains the completion offset to the [CompilationUnit]. |
| 48 */ | 44 */ |
| 49 class _LocalVisitor extends GeneralizingAstVisitor<dynamic> { | 45 class _LocalVisitor extends GeneralizingAstVisitor<dynamic> { |
| 50 final int offset; | 46 final DartCompletionRequest request; |
| 51 final List<CompletionSuggestion> suggestions; | |
| 52 | 47 |
| 53 _LocalVisitor(this.offset, this.suggestions); | 48 _LocalVisitor(this.request); |
| 54 | 49 |
| 55 void addSuggestion(SimpleIdentifier id, CompletionSuggestionKind kind) { | 50 @override |
| 56 if (id != null) { | |
| 57 String completion = id.name; | |
| 58 if (completion != null && completion.length > 0) { | |
| 59 suggestions.add( | |
| 60 new CompletionSuggestion( | |
| 61 kind, | |
| 62 CompletionRelevance.DEFAULT, | |
| 63 completion, | |
| 64 completion.length, | |
| 65 0, | |
| 66 false, | |
| 67 false)); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void addSuggestions(VariableDeclarationList variables, | |
| 73 CompletionSuggestionKind kind) { | |
| 74 variables.variables.forEach((VariableDeclaration varDecl) { | |
| 75 addSuggestion(varDecl.name, kind); | |
| 76 }); | |
| 77 } | |
| 78 | |
| 79 visitBlock(Block node) { | 51 visitBlock(Block node) { |
| 80 node.statements.forEach((Statement stmt) { | 52 node.statements.forEach((Statement stmt) { |
| 81 if (stmt.offset < offset) { | 53 if (stmt.offset < request.offset) { |
| 82 if (stmt is LabeledStatement) { | 54 if (stmt is LabeledStatement) { |
| 83 stmt.labels.forEach((Label label) { | 55 stmt.labels.forEach((Label label) { |
| 84 // addSuggestion(label.label, CompletionSuggestionKind.LABEL); | 56 // _addSuggestion(label.label, CompletionSuggestionKind.LABEL); |
| 85 }); | 57 }); |
| 86 } else if (stmt is VariableDeclarationStatement) { | 58 } else if (stmt is VariableDeclarationStatement) { |
| 87 stmt.variables.variables.forEach((VariableDeclaration varDecl) { | 59 stmt.variables.variables.forEach((VariableDeclaration varDecl) { |
| 88 if (varDecl.end < offset) { | 60 if (varDecl.end < request.offset) { |
| 89 addSuggestion( | 61 _addSuggestion( |
| 90 varDecl.name, | 62 varDecl.name, |
| 91 CompletionSuggestionKind.LOCAL_VARIABLE); | 63 CompletionSuggestionKind.LOCAL_VARIABLE); |
| 92 } | 64 } |
| 93 }); | 65 }); |
| 94 } | 66 } |
| 95 } | 67 } |
| 96 }); | 68 }); |
| 97 visitNode(node); | 69 visitNode(node); |
| 98 } | 70 } |
| 99 | 71 |
| 72 @override |
| 100 visitCatchClause(CatchClause node) { | 73 visitCatchClause(CatchClause node) { |
| 101 addSuggestion(node.exceptionParameter, CompletionSuggestionKind.PARAMETER); | 74 _addSuggestion(node.exceptionParameter, CompletionSuggestionKind.PARAMETER); |
| 102 addSuggestion(node.stackTraceParameter, CompletionSuggestionKind.PARAMETER); | 75 _addSuggestion(node.stackTraceParameter, CompletionSuggestionKind.PARAMETER)
; |
| 103 visitNode(node); | 76 visitNode(node); |
| 104 } | 77 } |
| 105 | 78 |
| 79 @override |
| 106 visitClassDeclaration(ClassDeclaration node) { | 80 visitClassDeclaration(ClassDeclaration node) { |
| 107 node.members.forEach((ClassMember classMbr) { | 81 node.members.forEach((ClassMember classMbr) { |
| 108 if (classMbr is FieldDeclaration) { | 82 if (classMbr is FieldDeclaration) { |
| 109 addSuggestions(classMbr.fields, CompletionSuggestionKind.FIELD); | 83 _addSuggestions(classMbr.fields, CompletionSuggestionKind.FIELD); |
| 110 } else if (classMbr is MethodDeclaration) { | 84 } else if (classMbr is MethodDeclaration) { |
| 111 addSuggestion(classMbr.name, CompletionSuggestionKind.METHOD_NAME); | 85 _addSuggestion(classMbr.name, CompletionSuggestionKind.METHOD_NAME); |
| 112 } | 86 } |
| 113 }); | 87 }); |
| 114 visitNode(node); | 88 visitNode(node); |
| 115 } | 89 } |
| 116 | 90 |
| 91 @override |
| 117 visitCompilationUnit(CompilationUnit node) { | 92 visitCompilationUnit(CompilationUnit node) { |
| 118 node.directives.forEach((Directive directive) { | 93 node.directives.forEach((Directive directive) { |
| 119 if (directive is ImportDirective) { | 94 if (directive is ImportDirective) { |
| 120 addSuggestion( | 95 _addSuggestion( |
| 121 directive.prefix, | 96 directive.prefix, |
| 122 CompletionSuggestionKind.LIBRARY_PREFIX); | 97 CompletionSuggestionKind.LIBRARY_PREFIX); |
| 123 } | 98 } |
| 124 }); | 99 }); |
| 125 node.declarations.forEach((Declaration declaration) { | 100 node.declarations.forEach((Declaration declaration) { |
| 126 if (declaration is ClassDeclaration) { | 101 if (declaration is ClassDeclaration) { |
| 127 addSuggestion(declaration.name, CompletionSuggestionKind.CLASS); | 102 _addSuggestion(declaration.name, CompletionSuggestionKind.CLASS); |
| 128 } else if (declaration is EnumDeclaration) { | 103 } else if (declaration is EnumDeclaration) { |
| 129 // addSuggestion(d.name, CompletionSuggestionKind.ENUM); | 104 // _addSuggestion(d.name, CompletionSuggestionKind.ENUM); |
| 130 } else if (declaration is FunctionDeclaration) { | 105 } else if (declaration is FunctionDeclaration) { |
| 131 addSuggestion(declaration.name, CompletionSuggestionKind.FUNCTION); | 106 _addSuggestion(declaration.name, CompletionSuggestionKind.FUNCTION); |
| 132 } else if (declaration is TopLevelVariableDeclaration) { | 107 } else if (declaration is TopLevelVariableDeclaration) { |
| 133 addSuggestions( | 108 _addSuggestions( |
| 134 declaration.variables, | 109 declaration.variables, |
| 135 CompletionSuggestionKind.TOP_LEVEL_VARIABLE); | 110 CompletionSuggestionKind.TOP_LEVEL_VARIABLE); |
| 136 } else if (declaration is ClassTypeAlias) { | 111 } else if (declaration is ClassTypeAlias) { |
| 137 addSuggestion(declaration.name, CompletionSuggestionKind.CLASS_ALIAS); | 112 _addSuggestion(declaration.name, CompletionSuggestionKind.CLASS_ALIAS); |
| 138 } else if (declaration is FunctionTypeAlias) { | 113 } else if (declaration is FunctionTypeAlias) { |
| 139 addSuggestion( | 114 _addSuggestion( |
| 140 declaration.name, | 115 declaration.name, |
| 141 CompletionSuggestionKind.FUNCTION_TYPE_ALIAS); | 116 CompletionSuggestionKind.FUNCTION_TYPE_ALIAS); |
| 142 } | 117 } |
| 143 }); | 118 }); |
| 144 } | 119 } |
| 145 | 120 |
| 121 @override |
| 146 visitForEachStatement(ForEachStatement node) { | 122 visitForEachStatement(ForEachStatement node) { |
| 147 addSuggestion(node.identifier, CompletionSuggestionKind.LOCAL_VARIABLE); | 123 _addSuggestion(node.identifier, CompletionSuggestionKind.LOCAL_VARIABLE); |
| 148 visitNode(node); | 124 visitNode(node); |
| 149 } | 125 } |
| 150 | 126 |
| 127 @override |
| 151 visitForStatement(ForStatement node) { | 128 visitForStatement(ForStatement node) { |
| 152 addSuggestions(node.variables, CompletionSuggestionKind.LOCAL_VARIABLE); | 129 _addSuggestions(node.variables, CompletionSuggestionKind.LOCAL_VARIABLE); |
| 153 visitNode(node); | 130 visitNode(node); |
| 154 } | 131 } |
| 155 | 132 |
| 133 @override |
| 156 visitFunctionDeclaration(FunctionDeclaration node) { | 134 visitFunctionDeclaration(FunctionDeclaration node) { |
| 157 // This is added by the compilation unit containing it | 135 // This is added by the compilation unit containing it |
| 158 //addSuggestion(node.name, CompletionSuggestionKind.FUNCTION); | 136 //_addSuggestion(node.name, CompletionSuggestionKind.FUNCTION); |
| 159 visitNode(node); | 137 visitNode(node); |
| 160 } | 138 } |
| 161 | 139 |
| 140 @override |
| 162 visitFunctionExpression(FunctionExpression node) { | 141 visitFunctionExpression(FunctionExpression node) { |
| 163 node.parameters.parameters.forEach((FormalParameter param) { | 142 node.parameters.parameters.forEach((FormalParameter param) { |
| 164 addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); | 143 _addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); |
| 165 }); | 144 }); |
| 166 visitNode(node); | 145 visitNode(node); |
| 167 } | 146 } |
| 168 | 147 |
| 148 @override |
| 169 visitMethodDeclaration(MethodDeclaration node) { | 149 visitMethodDeclaration(MethodDeclaration node) { |
| 170 node.parameters.parameters.forEach((FormalParameter param) { | 150 node.parameters.parameters.forEach((FormalParameter param) { |
| 171 if (param.identifier != null) { | 151 if (param.identifier != null) { |
| 172 addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); | 152 _addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); |
| 173 } | 153 } |
| 174 }); | 154 }); |
| 175 visitNode(node); | 155 visitNode(node); |
| 176 } | 156 } |
| 177 | 157 |
| 158 @override |
| 178 visitNode(AstNode node) { | 159 visitNode(AstNode node) { |
| 179 node.parent.accept(this); | 160 node.parent.accept(this); |
| 180 } | 161 } |
| 181 | 162 |
| 163 @override |
| 182 visitVariableDeclaration(VariableDeclaration node) { | 164 visitVariableDeclaration(VariableDeclaration node) { |
| 183 // Do not add suggestions if editing the name in a var declaration | 165 // Do not add suggestions if editing the name in a var declaration |
| 184 SimpleIdentifier name = node.name; | 166 SimpleIdentifier name = node.name; |
| 185 if (name == null || name.offset < offset || offset > name.end) { | 167 if (name == null || |
| 168 name.offset < request.offset || |
| 169 request.offset > name.end) { |
| 186 visitNode(node); | 170 visitNode(node); |
| 187 } | 171 } |
| 188 } | 172 } |
| 173 |
| 174 void _addSuggestion(SimpleIdentifier id, CompletionSuggestionKind kind) { |
| 175 if (id != null) { |
| 176 String completion = id.name; |
| 177 if (completion != null && completion.length > 0) { |
| 178 request.suggestions.add( |
| 179 new CompletionSuggestion( |
| 180 kind, |
| 181 CompletionRelevance.DEFAULT, |
| 182 completion, |
| 183 completion.length, |
| 184 0, |
| 185 false, |
| 186 false)); |
| 187 } |
| 188 } |
| 189 } |
| 190 |
| 191 void _addSuggestions(VariableDeclarationList variables, |
| 192 CompletionSuggestionKind kind) { |
| 193 variables.variables.forEach((VariableDeclaration varDecl) { |
| 194 _addSuggestion(varDecl.name, kind); |
| 195 }); |
| 196 } |
| 189 } | 197 } |
| OLD | NEW |