| 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.contributor.dart.keyword; | 5 library services.completion.contributor.dart.keyword; |
| 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 25 matching lines...) Expand all Loading... |
| 36 */ | 36 */ |
| 37 class _KeywordVisitor extends GeneralizingAstVisitor { | 37 class _KeywordVisitor extends GeneralizingAstVisitor { |
| 38 final DartCompletionRequest request; | 38 final DartCompletionRequest request; |
| 39 final Object entity; | 39 final Object entity; |
| 40 | 40 |
| 41 _KeywordVisitor(DartCompletionRequest request) | 41 _KeywordVisitor(DartCompletionRequest request) |
| 42 : this.request = request, | 42 : this.request = request, |
| 43 this.entity = request.target.entity; | 43 this.entity = request.target.entity; |
| 44 | 44 |
| 45 @override | 45 @override |
| 46 visitArgumentList(ArgumentList node) { |
| 47 if (entity == node.rightParenthesis || |
| 48 (entity is SimpleIdentifier && node.arguments.contains(entity))) { |
| 49 _addExpressionKeywords(node); |
| 50 } |
| 51 } |
| 52 |
| 53 @override |
| 46 visitBlock(Block node) { | 54 visitBlock(Block node) { |
| 47 _addStatementKeywords(node); | 55 _addStatementKeywords(node); |
| 48 } | 56 } |
| 49 | 57 |
| 50 @override | 58 @override |
| 51 visitClassDeclaration(ClassDeclaration node) { | 59 visitClassDeclaration(ClassDeclaration node) { |
| 52 // Don't suggest class name | 60 // Don't suggest class name |
| 53 if (entity == node.name) { | 61 if (entity == node.name) { |
| 54 return; | 62 return; |
| 55 } | 63 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 previousMember.functionExpression.body is EmptyFunctionBody) { | 116 previousMember.functionExpression.body is EmptyFunctionBody) { |
| 109 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); | 117 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); |
| 110 } | 118 } |
| 111 _addCompilationUnitKeywords(); | 119 _addCompilationUnitKeywords(); |
| 112 } | 120 } |
| 113 } | 121 } |
| 114 | 122 |
| 115 @override | 123 @override |
| 116 visitExpressionFunctionBody(ExpressionFunctionBody node) { | 124 visitExpressionFunctionBody(ExpressionFunctionBody node) { |
| 117 if (entity == node.expression) { | 125 if (entity == node.expression) { |
| 118 _addStatementKeywords(node); | 126 _addExpressionKeywords(node); |
| 119 } | 127 } |
| 120 } | 128 } |
| 121 | 129 |
| 122 @override | 130 @override |
| 123 visitFormalParameterList(FormalParameterList node) { | 131 visitFormalParameterList(FormalParameterList node) { |
| 124 AstNode constructorDecl = | 132 AstNode constructorDecl = |
| 125 node.getAncestor((p) => p is ConstructorDeclaration); | 133 node.getAncestor((p) => p is ConstructorDeclaration); |
| 126 if (constructorDecl != null) { | 134 if (constructorDecl != null) { |
| 127 _addSuggestions([Keyword.THIS]); | 135 _addSuggestions([Keyword.THIS]); |
| 128 } | 136 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 if (entity == node.body) { | 172 if (entity == node.body) { |
| 165 if (node.body is EmptyFunctionBody) { | 173 if (node.body is EmptyFunctionBody) { |
| 166 _addClassBodyKeywords(); | 174 _addClassBodyKeywords(); |
| 167 _addSuggestion2(ASYNC); | 175 _addSuggestion2(ASYNC); |
| 168 } else { | 176 } else { |
| 169 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); | 177 _addSuggestion2(ASYNC, DART_RELEVANCE_HIGH); |
| 170 } | 178 } |
| 171 } | 179 } |
| 172 } | 180 } |
| 173 | 181 |
| 182 @override |
| 183 visitNamedExpression(NamedExpression node) { |
| 184 if (entity is SimpleIdentifier && entity == node.expression) { |
| 185 _addExpressionKeywords(node); |
| 186 } |
| 187 } |
| 188 |
| 189 @override |
| 190 visitNode(AstNode node) { |
| 191 // ignored |
| 192 } |
| 193 |
| 194 @override |
| 195 visitSwitchStatement(SwitchStatement node) { |
| 196 if (entity == node.expression) { |
| 197 _addExpressionKeywords(node); |
| 198 } else if (entity == node.rightBracket) { |
| 199 if (node.members.isEmpty) { |
| 200 _addSuggestions([Keyword.CASE, Keyword.DEFAULT], DART_RELEVANCE_HIGH); |
| 201 } else { |
| 202 _addSuggestions([Keyword.CASE, Keyword.DEFAULT]); |
| 203 _addStatementKeywords(node); |
| 204 } |
| 205 } |
| 206 if (node.members.contains(entity)) { |
| 207 if (entity == node.members.first) { |
| 208 _addSuggestions([Keyword.CASE, Keyword.DEFAULT], DART_RELEVANCE_HIGH); |
| 209 } else { |
| 210 _addSuggestions([Keyword.CASE, Keyword.DEFAULT]); |
| 211 _addStatementKeywords(node); |
| 212 } |
| 213 } |
| 214 } |
| 215 |
| 216 @override |
| 217 visitVariableDeclaration(VariableDeclaration node) { |
| 218 if (entity == node.initializer) { |
| 219 _addExpressionKeywords(node); |
| 220 } |
| 221 } |
| 222 |
| 174 void _addClassBodyKeywords() { | 223 void _addClassBodyKeywords() { |
| 175 _addSuggestions([ | 224 _addSuggestions([ |
| 176 Keyword.CONST, | 225 Keyword.CONST, |
| 177 Keyword.DYNAMIC, | 226 Keyword.DYNAMIC, |
| 178 Keyword.FACTORY, | 227 Keyword.FACTORY, |
| 179 Keyword.FINAL, | 228 Keyword.FINAL, |
| 180 Keyword.GET, | 229 Keyword.GET, |
| 181 Keyword.OPERATOR, | 230 Keyword.OPERATOR, |
| 182 Keyword.SET, | 231 Keyword.SET, |
| 183 Keyword.STATIC, | 232 Keyword.STATIC, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 205 Keyword.CLASS, | 254 Keyword.CLASS, |
| 206 Keyword.CONST, | 255 Keyword.CONST, |
| 207 Keyword.DYNAMIC, | 256 Keyword.DYNAMIC, |
| 208 Keyword.FINAL, | 257 Keyword.FINAL, |
| 209 Keyword.TYPEDEF, | 258 Keyword.TYPEDEF, |
| 210 Keyword.VAR, | 259 Keyword.VAR, |
| 211 Keyword.VOID | 260 Keyword.VOID |
| 212 ], DART_RELEVANCE_HIGH); | 261 ], DART_RELEVANCE_HIGH); |
| 213 } | 262 } |
| 214 | 263 |
| 264 void _addExpressionKeywords(AstNode node) { |
| 265 _addSuggestions([Keyword.FALSE, Keyword.NEW, Keyword.NULL, Keyword.TRUE,]); |
| 266 if (_inClassMemberBody(node)) { |
| 267 _addSuggestions([Keyword.SUPER, Keyword.THIS,]); |
| 268 } |
| 269 if (_inAsyncMethodOrFunction(node)) { |
| 270 _addSuggestion2(AWAIT); |
| 271 } |
| 272 } |
| 273 |
| 215 void _addImportDirectiveKeywords(ImportDirective node) { | 274 void _addImportDirectiveKeywords(ImportDirective node) { |
| 216 if (node.asKeyword == null) { | 275 if (node.asKeyword == null) { |
| 217 _addSuggestion(Keyword.AS, DART_RELEVANCE_HIGH); | 276 _addSuggestion(Keyword.AS, DART_RELEVANCE_HIGH); |
| 218 if (node.deferredKeyword == null) { | 277 if (node.deferredKeyword == null) { |
| 219 _addSuggestion(Keyword.DEFERRED, DART_RELEVANCE_HIGH); | 278 _addSuggestion(Keyword.DEFERRED, DART_RELEVANCE_HIGH); |
| 220 } | 279 } |
| 221 } | 280 } |
| 222 } | 281 } |
| 223 | 282 |
| 224 void _addStatementKeywords(AstNode node) { | 283 void _addStatementKeywords(AstNode node) { |
| 225 if (_inClassMemberBody(node)) { | 284 if (_inClassMemberBody(node)) { |
| 226 _addSuggestions([Keyword.SUPER, Keyword.THIS,]); | 285 _addSuggestions([Keyword.SUPER, Keyword.THIS,]); |
| 227 } | 286 } |
| 228 if (_inAsyncMethodOrFunction(node)) { | 287 if (_inAsyncMethodOrFunction(node)) { |
| 229 _addSuggestion2(AWAIT); | 288 _addSuggestion2(AWAIT); |
| 230 } | 289 } |
| 231 _addSuggestions([ | 290 _addSuggestions([ |
| 232 Keyword.ASSERT, | 291 Keyword.ASSERT, |
| 233 Keyword.CASE, | |
| 234 Keyword.CONTINUE, | 292 Keyword.CONTINUE, |
| 235 Keyword.DO, | 293 Keyword.DO, |
| 236 Keyword.FINAL, | 294 Keyword.FINAL, |
| 237 Keyword.FOR, | 295 Keyword.FOR, |
| 238 Keyword.IF, | 296 Keyword.IF, |
| 239 Keyword.NEW, | 297 Keyword.NEW, |
| 240 Keyword.RETHROW, | 298 Keyword.RETHROW, |
| 241 Keyword.RETURN, | 299 Keyword.RETURN, |
| 242 Keyword.SWITCH, | 300 Keyword.SWITCH, |
| 243 Keyword.THROW, | 301 Keyword.THROW, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 return false; | 337 return false; |
| 280 } | 338 } |
| 281 AstNode parent = body.parent; | 339 AstNode parent = body.parent; |
| 282 if (parent is ConstructorDeclaration || parent is MethodDeclaration) { | 340 if (parent is ConstructorDeclaration || parent is MethodDeclaration) { |
| 283 return true; | 341 return true; |
| 284 } | 342 } |
| 285 node = parent; | 343 node = parent; |
| 286 } | 344 } |
| 287 } | 345 } |
| 288 } | 346 } |
| OLD | NEW |