| 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 test.services.completion.dart.keyword; | 5 library test.services.completion.dart.keyword; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart'; | 7 import 'package:analysis_server/src/protocol.dart'; |
| 8 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 8 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 9 import 'package:analysis_server/src/services/completion/keyword_contributor.dart
'; | 9 import 'package:analysis_server/src/services/completion/keyword_contributor.dart
'; |
| 10 import 'package:analyzer/src/generated/scanner.dart'; | 10 import 'package:analyzer/src/generated/scanner.dart'; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 Keyword.TRUE, | 165 Keyword.TRUE, |
| 166 ]; | 166 ]; |
| 167 | 167 |
| 168 static const List<Keyword> EXPRESSION_START_NO_INSTANCE = const [ | 168 static const List<Keyword> EXPRESSION_START_NO_INSTANCE = const [ |
| 169 Keyword.FALSE, | 169 Keyword.FALSE, |
| 170 Keyword.NEW, | 170 Keyword.NEW, |
| 171 Keyword.NULL, | 171 Keyword.NULL, |
| 172 Keyword.TRUE, | 172 Keyword.TRUE, |
| 173 ]; | 173 ]; |
| 174 | 174 |
| 175 static final Map<String, List<String>> keywordTemplates = |
| 176 <String, List<String>>{ |
| 177 Keyword.IMPORT.syntax: [ |
| 178 "import '^';", |
| 179 "import '^' as ;", |
| 180 "import '^' hide ;", |
| 181 "import '^' show ;" |
| 182 ], |
| 183 Keyword.FOR.syntax: ['for (^)'] |
| 184 }; |
| 185 |
| 175 void assertSuggestKeywords(Iterable<Keyword> expectedKeywords, | 186 void assertSuggestKeywords(Iterable<Keyword> expectedKeywords, |
| 176 {List<String> pseudoKeywords: NO_PSEUDO_KEYWORDS, | 187 {List<String> pseudoKeywords: NO_PSEUDO_KEYWORDS, |
| 177 int relevance: DART_RELEVANCE_KEYWORD}) { | 188 int relevance: DART_RELEVANCE_KEYWORD}) { |
| 178 Set<String> expectedCompletions = new Set<String>(); | 189 Set<String> expectedCompletions = new Set<String>(); |
| 190 Map<String, int> expectedOffsets = <String, int>{}; |
| 179 Set<String> actualCompletions = new Set<String>(); | 191 Set<String> actualCompletions = new Set<String>(); |
| 180 expectedCompletions.addAll(expectedKeywords.map((k) => k.syntax)); | 192 expectedCompletions.addAll(expectedKeywords.map((k) => k.syntax)); |
| 181 expectedCompletions.addAll(pseudoKeywords); | 193 expectedCompletions.addAll(pseudoKeywords); |
| 182 if (expectedCompletions.contains(Keyword.IMPORT.syntax)) { | 194 keywordTemplates.forEach((String key, List<String> templates) { |
| 183 expectedCompletions.remove(Keyword.IMPORT.syntax); | 195 if (expectedCompletions.remove(key)) { |
| 184 expectedCompletions.add("import '';"); | 196 for (String t in templates) { |
| 185 expectedCompletions.add("import '' as ;"); | 197 int offset = t.indexOf('^'); |
| 186 expectedCompletions.add("import '' hide ;"); | 198 if (offset != -1) { |
| 187 expectedCompletions.add("import '' show ;"); | 199 t = '${t.substring(0, offset)}${t.substring(offset + 1)}'; |
| 188 } | 200 expectedOffsets[t] = offset; |
| 201 } |
| 202 expectedCompletions.add(t); |
| 203 } |
| 204 } |
| 205 }); |
| 189 for (CompletionSuggestion s in request.suggestions) { | 206 for (CompletionSuggestion s in request.suggestions) { |
| 190 if (s.kind == CompletionSuggestionKind.KEYWORD) { | 207 if (s.kind == CompletionSuggestionKind.KEYWORD) { |
| 191 Keyword k = Keyword.keywords[s.completion]; | 208 Keyword k = Keyword.keywords[s.completion]; |
| 192 if (k == null && !expectedCompletions.contains(s.completion)) { | 209 if (k == null && !expectedCompletions.contains(s.completion)) { |
| 193 fail('Invalid keyword suggested: ${s.completion}'); | 210 fail('Invalid keyword suggested: ${s.completion}'); |
| 194 } else { | 211 } else { |
| 195 if (!actualCompletions.add(s.completion)) { | 212 if (!actualCompletions.add(s.completion)) { |
| 196 fail('Duplicate keyword suggested: ${s.completion}'); | 213 fail('Duplicate keyword suggested: ${s.completion}'); |
| 197 } | 214 } |
| 198 } | 215 } |
| 199 } | 216 } |
| 200 } | 217 } |
| 201 if (!_equalSets(expectedCompletions, actualCompletions)) { | 218 if (!_equalSets(expectedCompletions, actualCompletions)) { |
| 202 StringBuffer msg = new StringBuffer(); | 219 StringBuffer msg = new StringBuffer(); |
| 203 msg.writeln('Expected:'); | 220 msg.writeln('Expected:'); |
| 204 _appendCompletions(msg, expectedCompletions, actualCompletions); | 221 _appendCompletions(msg, expectedCompletions, actualCompletions); |
| 205 msg.writeln('but found:'); | 222 msg.writeln('but found:'); |
| 206 _appendCompletions(msg, actualCompletions, expectedCompletions); | 223 _appendCompletions(msg, actualCompletions, expectedCompletions); |
| 207 fail(msg.toString()); | 224 fail(msg.toString()); |
| 208 } | 225 } |
| 209 for (CompletionSuggestion s in request.suggestions) { | 226 for (CompletionSuggestion s in request.suggestions) { |
| 210 if (s.kind == CompletionSuggestionKind.KEYWORD) { | 227 if (s.kind == CompletionSuggestionKind.KEYWORD) { |
| 211 if (s.completion.startsWith(Keyword.IMPORT.syntax)) { | 228 if (s.completion.startsWith(Keyword.IMPORT.syntax)) { |
| 212 int importRelevance = relevance; | 229 int importRelevance = relevance; |
| 213 if (importRelevance == DART_RELEVANCE_HIGH && | 230 if (importRelevance == DART_RELEVANCE_HIGH && |
| 214 s.completion == "import '';") { | 231 s.completion == "import '';") { |
| 215 ++importRelevance; | 232 ++importRelevance; |
| 216 } | 233 } |
| 217 expect(s.relevance, equals(importRelevance), reason: s.completion); | 234 expect(s.relevance, equals(importRelevance), reason: s.completion); |
| 218 expect(s.selectionOffset, equals(Keyword.IMPORT.syntax.length + 2)); | |
| 219 } else { | 235 } else { |
| 220 if (s.completion == Keyword.RETHROW.syntax) { | 236 if (s.completion == Keyword.RETHROW.syntax) { |
| 221 expect(s.relevance, equals(relevance - 1), reason: s.completion); | 237 expect(s.relevance, equals(relevance - 1), reason: s.completion); |
| 222 } else { | 238 } else { |
| 223 expect(s.relevance, equals(relevance), reason: s.completion); | 239 expect(s.relevance, equals(relevance), reason: s.completion); |
| 224 } | 240 } |
| 225 expect(s.selectionOffset, equals(s.completion.length)); | |
| 226 } | 241 } |
| 242 int expectedOffset = expectedOffsets[s.completion]; |
| 243 if (expectedOffset == null) { |
| 244 expectedOffset = s.completion.length; |
| 245 } |
| 246 expect(s.selectionOffset, equals(expectedOffset)); |
| 227 expect(s.selectionLength, equals(0)); | 247 expect(s.selectionLength, equals(0)); |
| 228 expect(s.isDeprecated, equals(false)); | 248 expect(s.isDeprecated, equals(false)); |
| 229 expect(s.isPotential, equals(false)); | 249 expect(s.isPotential, equals(false)); |
| 230 } | 250 } |
| 231 } | 251 } |
| 232 } | 252 } |
| 233 | 253 |
| 234 @override | 254 @override |
| 235 void setUpContributor() { | 255 void setUpContributor() { |
| 236 contributor = new KeywordContributor(); | 256 contributor = new KeywordContributor(); |
| (...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1128 (c) => msg.writeln(' $c, ${other.contains(c) ? '' : '<<<<<<<<<<<'}')); | 1148 (c) => msg.writeln(' $c, ${other.contains(c) ? '' : '<<<<<<<<<<<'}')); |
| 1129 } | 1149 } |
| 1130 | 1150 |
| 1131 bool _equalSets(Iterable<String> iter1, Iterable<String> iter2) { | 1151 bool _equalSets(Iterable<String> iter1, Iterable<String> iter2) { |
| 1132 if (iter1.length != iter2.length) return false; | 1152 if (iter1.length != iter2.length) return false; |
| 1133 if (iter1.any((c) => !iter2.contains(c))) return false; | 1153 if (iter1.any((c) => !iter2.contains(c))) return false; |
| 1134 if (iter2.any((c) => !iter1.contains(c))) return false; | 1154 if (iter2.any((c) => !iter1.contains(c))) return false; |
| 1135 return true; | 1155 return true; |
| 1136 } | 1156 } |
| 1137 } | 1157 } |
| OLD | NEW |