| 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 23 matching lines...) Expand all Loading... |
| 34 class _KeywordVisitor extends GeneralizingAstVisitor { | 34 class _KeywordVisitor extends GeneralizingAstVisitor { |
| 35 final DartCompletionRequest request; | 35 final DartCompletionRequest request; |
| 36 final Object entity; | 36 final Object entity; |
| 37 | 37 |
| 38 _KeywordVisitor(DartCompletionRequest request) | 38 _KeywordVisitor(DartCompletionRequest request) |
| 39 : this.request = request, | 39 : this.request = request, |
| 40 this.entity = request.target.entity; | 40 this.entity = request.target.entity; |
| 41 | 41 |
| 42 @override | 42 @override |
| 43 visitBlock(Block node) { | 43 visitBlock(Block node) { |
| 44 if (_isInClassMemberBody(node)) { | 44 _addStatementKeywords(node); |
| 45 _addSuggestions([Keyword.SUPER, Keyword.THIS,]); | |
| 46 } | |
| 47 _addSuggestions([ | |
| 48 Keyword.ASSERT, | |
| 49 Keyword.CASE, | |
| 50 Keyword.CONTINUE, | |
| 51 Keyword.DO, | |
| 52 Keyword.FINAL, | |
| 53 Keyword.FOR, | |
| 54 Keyword.IF, | |
| 55 Keyword.NEW, | |
| 56 Keyword.RETHROW, | |
| 57 Keyword.RETURN, | |
| 58 Keyword.SWITCH, | |
| 59 Keyword.THROW, | |
| 60 Keyword.TRY, | |
| 61 Keyword.VAR, | |
| 62 Keyword.VOID, | |
| 63 Keyword.WHILE | |
| 64 ]); | |
| 65 } | 45 } |
| 66 | 46 |
| 67 @override | 47 @override |
| 68 visitClassDeclaration(ClassDeclaration node) { | 48 visitClassDeclaration(ClassDeclaration node) { |
| 69 // Don't suggest class name | 49 // Don't suggest class name |
| 70 if (entity == node.name) { | 50 if (entity == node.name) { |
| 71 return; | 51 return; |
| 72 } | 52 } |
| 73 if (entity == node.rightBracket || entity is ClassMember) { | 53 if (entity == node.rightBracket || entity is ClassMember) { |
| 74 _addSuggestions([ | 54 _addSuggestions([ |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 Keyword.DYNAMIC, | 111 Keyword.DYNAMIC, |
| 132 Keyword.FINAL, | 112 Keyword.FINAL, |
| 133 Keyword.TYPEDEF, | 113 Keyword.TYPEDEF, |
| 134 Keyword.VAR, | 114 Keyword.VAR, |
| 135 Keyword.VOID | 115 Keyword.VOID |
| 136 ], DART_RELEVANCE_HIGH); | 116 ], DART_RELEVANCE_HIGH); |
| 137 } | 117 } |
| 138 } | 118 } |
| 139 | 119 |
| 140 @override | 120 @override |
| 121 visitIfStatement(IfStatement node) { |
| 122 if (entity == node.thenStatement) { |
| 123 _addStatementKeywords(node); |
| 124 } |
| 125 } |
| 126 |
| 127 @override |
| 141 visitImportDirective(ImportDirective node) { | 128 visitImportDirective(ImportDirective node) { |
| 142 if (entity == node.asKeyword) { | 129 if (entity == node.asKeyword) { |
| 143 if (node.deferredKeyword == null) { | 130 if (node.deferredKeyword == null) { |
| 144 _addSuggestion(Keyword.DEFERRED, DART_RELEVANCE_HIGH); | 131 _addSuggestion(Keyword.DEFERRED, DART_RELEVANCE_HIGH); |
| 145 } | 132 } |
| 146 } | 133 } |
| 147 if (entity == node.semicolon || node.combinators.contains(entity)) { | 134 if (entity == node.semicolon || node.combinators.contains(entity)) { |
| 148 _addImportDirectiveKeywords(node); | 135 _addImportDirectiveKeywords(node); |
| 149 } | 136 } |
| 150 } | 137 } |
| 151 | 138 |
| 152 void _addImportDirectiveKeywords(ImportDirective node) { | |
| 153 if (node.asKeyword == null) { | |
| 154 _addSuggestion(Keyword.AS, DART_RELEVANCE_HIGH); | |
| 155 if (node.deferredKeyword == null) { | |
| 156 _addSuggestion(Keyword.DEFERRED, DART_RELEVANCE_HIGH); | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 void _addClassDeclarationKeywords(ClassDeclaration node) { | 139 void _addClassDeclarationKeywords(ClassDeclaration node) { |
| 162 // Very simplistic suggestion because analyzer will warn if | 140 // Very simplistic suggestion because analyzer will warn if |
| 163 // the extends / with / implements keywords are out of order | 141 // the extends / with / implements keywords are out of order |
| 164 if (node.extendsClause == null) { | 142 if (node.extendsClause == null) { |
| 165 _addSuggestion(Keyword.EXTENDS, DART_RELEVANCE_HIGH); | 143 _addSuggestion(Keyword.EXTENDS, DART_RELEVANCE_HIGH); |
| 166 } else if (node.withClause == null) { | 144 } else if (node.withClause == null) { |
| 167 _addSuggestion(Keyword.WITH, DART_RELEVANCE_HIGH); | 145 _addSuggestion(Keyword.WITH, DART_RELEVANCE_HIGH); |
| 168 } | 146 } |
| 169 if (node.implementsClause == null) { | 147 if (node.implementsClause == null) { |
| 170 _addSuggestion(Keyword.IMPLEMENTS, DART_RELEVANCE_HIGH); | 148 _addSuggestion(Keyword.IMPLEMENTS, DART_RELEVANCE_HIGH); |
| 171 } | 149 } |
| 172 } | 150 } |
| 173 | 151 |
| 152 void _addImportDirectiveKeywords(ImportDirective node) { |
| 153 if (node.asKeyword == null) { |
| 154 _addSuggestion(Keyword.AS, DART_RELEVANCE_HIGH); |
| 155 if (node.deferredKeyword == null) { |
| 156 _addSuggestion(Keyword.DEFERRED, DART_RELEVANCE_HIGH); |
| 157 } |
| 158 } |
| 159 } |
| 160 |
| 161 void _addStatementKeywords(AstNode node) { |
| 162 if (_isInClassMemberBody(node)) { |
| 163 _addSuggestions([Keyword.SUPER, Keyword.THIS,]); |
| 164 } |
| 165 _addSuggestions([ |
| 166 Keyword.ASSERT, |
| 167 Keyword.CASE, |
| 168 Keyword.CONTINUE, |
| 169 Keyword.DO, |
| 170 Keyword.FINAL, |
| 171 Keyword.FOR, |
| 172 Keyword.IF, |
| 173 Keyword.NEW, |
| 174 Keyword.RETHROW, |
| 175 Keyword.RETURN, |
| 176 Keyword.SWITCH, |
| 177 Keyword.THROW, |
| 178 Keyword.TRY, |
| 179 Keyword.VAR, |
| 180 Keyword.VOID, |
| 181 Keyword.WHILE |
| 182 ]); |
| 183 } |
| 184 |
| 174 void _addSuggestion(Keyword keyword, | 185 void _addSuggestion(Keyword keyword, |
| 175 [int relevance = DART_RELEVANCE_DEFAULT]) { | 186 [int relevance = DART_RELEVANCE_DEFAULT]) { |
| 176 String completion = keyword.syntax; | 187 String completion = keyword.syntax; |
| 177 request.addSuggestion(new CompletionSuggestion( | 188 request.addSuggestion(new CompletionSuggestion( |
| 178 CompletionSuggestionKind.KEYWORD, relevance, completion, | 189 CompletionSuggestionKind.KEYWORD, relevance, completion, |
| 179 completion.length, 0, false, false)); | 190 completion.length, 0, false, false)); |
| 180 } | 191 } |
| 181 | 192 |
| 182 void _addSuggestions(List<Keyword> keywords, | 193 void _addSuggestions(List<Keyword> keywords, |
| 183 [int relevance = DART_RELEVANCE_KEYWORD]) { | 194 [int relevance = DART_RELEVANCE_KEYWORD]) { |
| 184 keywords.forEach((Keyword keyword) { | 195 keywords.forEach((Keyword keyword) { |
| 185 _addSuggestion(keyword, relevance); | 196 _addSuggestion(keyword, relevance); |
| 186 }); | 197 }); |
| 187 } | 198 } |
| 188 | 199 |
| 189 bool _isInClassMemberBody(AstNode node) { | 200 bool _isInClassMemberBody(AstNode node) { |
| 190 while (true) { | 201 while (true) { |
| 191 AstNode body = node.getAncestor((n) => n is FunctionBody); | 202 AstNode body = node.getAncestor((n) => n is FunctionBody); |
| 192 if (body == null) { | 203 if (body == null) { |
| 193 return false; | 204 return false; |
| 194 } | 205 } |
| 195 AstNode parent = body.parent; | 206 AstNode parent = body.parent; |
| 196 if (parent is ConstructorDeclaration || parent is MethodDeclaration) { | 207 if (parent is ConstructorDeclaration || parent is MethodDeclaration) { |
| 197 return true; | 208 return true; |
| 198 } | 209 } |
| 199 node = parent; | 210 node = parent; |
| 200 } | 211 } |
| 201 } | 212 } |
| 202 } | 213 } |
| OLD | NEW |