| 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_computer.dart'; | 9 import 'package:analysis_server/src/services/completion/keyword_computer.dart'; |
| 10 import 'package:analyzer/src/generated/scanner.dart'; | 10 import 'package:analyzer/src/generated/scanner.dart'; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 Keyword.FACTORY, | 26 Keyword.FACTORY, |
| 27 Keyword.FINAL, | 27 Keyword.FINAL, |
| 28 Keyword.GET, | 28 Keyword.GET, |
| 29 Keyword.OPERATOR, | 29 Keyword.OPERATOR, |
| 30 Keyword.SET, | 30 Keyword.SET, |
| 31 Keyword.STATIC, | 31 Keyword.STATIC, |
| 32 Keyword.VAR, | 32 Keyword.VAR, |
| 33 Keyword.VOID | 33 Keyword.VOID |
| 34 ]; | 34 ]; |
| 35 | 35 |
| 36 static const List<Keyword> DECLARATION_KEYWORDS = const [ |
| 37 Keyword.ABSTRACT, |
| 38 Keyword.CLASS, |
| 39 Keyword.CONST, |
| 40 Keyword.DYNAMIC, |
| 41 Keyword.FINAL, |
| 42 Keyword.TYPEDEF, |
| 43 Keyword.VAR, |
| 44 Keyword.VOID |
| 45 ]; |
| 46 |
| 47 static const List<Keyword> DIRECTIVE_AND_DECLARATION_KEYWORDS = const [ |
| 48 Keyword.ABSTRACT, |
| 49 Keyword.CLASS, |
| 50 Keyword.CONST, |
| 51 Keyword.DYNAMIC, |
| 52 Keyword.EXPORT, |
| 53 Keyword.FINAL, |
| 54 Keyword.IMPORT, |
| 55 Keyword.PART, |
| 56 Keyword.TYPEDEF, |
| 57 Keyword.VAR, |
| 58 Keyword.VOID |
| 59 ]; |
| 60 |
| 61 static const List<Keyword> DIRECTIVE_DECLARATION_AND_LIBRARY_KEYWORDS = |
| 62 const [ |
| 63 Keyword.ABSTRACT, |
| 64 Keyword.CLASS, |
| 65 Keyword.CONST, |
| 66 Keyword.DYNAMIC, |
| 67 Keyword.EXPORT, |
| 68 Keyword.FINAL, |
| 69 Keyword.IMPORT, |
| 70 Keyword.LIBRARY, |
| 71 Keyword.PART, |
| 72 Keyword.TYPEDEF, |
| 73 Keyword.VAR, |
| 74 Keyword.VOID |
| 75 ]; |
| 76 |
| 36 static const List<Keyword> IN_BLOCK_IN_CLASS = const [ | 77 static const List<Keyword> IN_BLOCK_IN_CLASS = const [ |
| 37 Keyword.ASSERT, | 78 Keyword.ASSERT, |
| 38 Keyword.CASE, | 79 Keyword.CASE, |
| 39 Keyword.CONTINUE, | 80 Keyword.CONTINUE, |
| 40 Keyword.DO, | 81 Keyword.DO, |
| 41 Keyword.FINAL, | 82 Keyword.FINAL, |
| 42 Keyword.FOR, | 83 Keyword.FOR, |
| 43 Keyword.IF, | 84 Keyword.IF, |
| 44 Keyword.NEW, | 85 Keyword.NEW, |
| 45 Keyword.RETHROW, | 86 Keyword.RETHROW, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 if (s.kind == CompletionSuggestionKind.KEYWORD) { | 121 if (s.kind == CompletionSuggestionKind.KEYWORD) { |
| 81 Keyword k = Keyword.keywords[s.completion]; | 122 Keyword k = Keyword.keywords[s.completion]; |
| 82 if (k == null) { | 123 if (k == null) { |
| 83 fail('Invalid keyword suggested: ${s.completion}'); | 124 fail('Invalid keyword suggested: ${s.completion}'); |
| 84 } else { | 125 } else { |
| 85 if (!actualKeywords.add(k)) { | 126 if (!actualKeywords.add(k)) { |
| 86 fail('Duplicate keyword suggested: ${s.completion}'); | 127 fail('Duplicate keyword suggested: ${s.completion}'); |
| 87 } | 128 } |
| 88 } | 129 } |
| 89 } | 130 } |
| 90 }; | 131 } |
| 91 if (expectedKeywords.any((k) => k is String)) { | 132 if (expectedKeywords.any((k) => k is String)) { |
| 92 StringBuffer msg = new StringBuffer(); | 133 StringBuffer msg = new StringBuffer(); |
| 93 msg.writeln('Expected set should be:'); | 134 msg.writeln('Expected set should be:'); |
| 94 expectedKeywords.forEach((n) { | 135 expectedKeywords.forEach((n) { |
| 95 Keyword k = Keyword.keywords[n]; | 136 Keyword k = Keyword.keywords[n]; |
| 96 msg.writeln(' Keyword.${k.name},'); | 137 msg.writeln(' Keyword.${k.name},'); |
| 97 }); | 138 }); |
| 98 fail(msg.toString()); | 139 fail(msg.toString()); |
| 99 } | 140 } |
| 100 if (!_equalSets(expectedKeywords, actualKeywords)) { | 141 if (!_equalSets(expectedKeywords, actualKeywords)) { |
| 101 StringBuffer msg = new StringBuffer(); | 142 StringBuffer msg = new StringBuffer(); |
| 102 msg.writeln('Expected:'); | 143 msg.writeln('Expected:'); |
| 103 _appendKeywords(msg, expectedKeywords); | 144 _appendKeywords(msg, expectedKeywords); |
| 104 msg.writeln('but found:'); | 145 msg.writeln('but found:'); |
| 105 _appendKeywords(msg, actualKeywords); | 146 _appendKeywords(msg, actualKeywords); |
| 106 fail(msg.toString()); | 147 fail(msg.toString()); |
| 107 } | 148 } |
| 108 for (CompletionSuggestion s in request.suggestions) { | 149 for (CompletionSuggestion s in request.suggestions) { |
| 109 if (s.kind == CompletionSuggestionKind.KEYWORD) { | 150 if (s.kind == CompletionSuggestionKind.KEYWORD) { |
| 110 Keyword k = Keyword.keywords[s.completion]; | 151 Keyword k = Keyword.keywords[s.completion]; |
| 111 expect(s.relevance, equals(relevance), reason: k.toString()); | 152 expect(s.relevance, equals(relevance), reason: k.toString()); |
| 112 expect(s.selectionOffset, equals(s.completion.length)); | 153 expect(s.selectionOffset, equals(s.completion.length)); |
| 113 expect(s.selectionLength, equals(0)); | 154 expect(s.selectionLength, equals(0)); |
| 114 expect(s.isDeprecated, equals(false)); | 155 expect(s.isDeprecated, equals(false)); |
| 115 expect(s.isPotential, equals(false)); | 156 expect(s.isPotential, equals(false)); |
| 116 } | 157 } |
| 117 }; | 158 } |
| 118 } | 159 } |
| 119 | 160 |
| 120 @override | 161 @override |
| 121 void setUpComputer() { | 162 void setUpComputer() { |
| 122 computer = new KeywordComputer(); | 163 computer = new KeywordComputer(); |
| 123 } | 164 } |
| 124 | 165 |
| 125 test_after_class() { | 166 test_after_class() { |
| 126 addTestSource('class A {} ^'); | 167 addTestSource('class A {} ^'); |
| 127 expect(computeFast(), isTrue); | 168 expect(computeFast(), isTrue); |
| 128 assertSuggestKeywords([ | 169 assertSuggestKeywords(DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 129 Keyword.ABSTRACT, | |
| 130 Keyword.CLASS, | |
| 131 Keyword.CONST, | |
| 132 Keyword.FINAL, | |
| 133 Keyword.TYPEDEF, | |
| 134 Keyword.VAR | |
| 135 ], DART_RELEVANCE_HIGH); | |
| 136 } | 170 } |
| 137 | 171 |
| 138 test_after_class2() { | 172 test_after_class2() { |
| 139 addTestSource('class A {} c^'); | 173 addTestSource('class A {} c^'); |
| 140 expect(computeFast(), isTrue); | 174 expect(computeFast(), isTrue); |
| 141 assertSuggestKeywords([ | 175 assertSuggestKeywords(DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 142 Keyword.ABSTRACT, | |
| 143 Keyword.CLASS, | |
| 144 Keyword.CONST, | |
| 145 Keyword.FINAL, | |
| 146 Keyword.TYPEDEF, | |
| 147 Keyword.VAR | |
| 148 ], DART_RELEVANCE_HIGH); | |
| 149 } | 176 } |
| 150 | 177 |
| 151 test_after_import() { | 178 test_after_import() { |
| 152 addTestSource('import foo; ^'); | 179 addTestSource('import foo; ^'); |
| 153 expect(computeFast(), isTrue); | 180 expect(computeFast(), isTrue); |
| 154 assertSuggestKeywords([ | 181 assertSuggestKeywords( |
| 155 Keyword.ABSTRACT, | 182 DIRECTIVE_AND_DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 156 Keyword.CLASS, | |
| 157 Keyword.CONST, | |
| 158 Keyword.EXPORT, | |
| 159 Keyword.FINAL, | |
| 160 Keyword.IMPORT, | |
| 161 Keyword.PART, | |
| 162 Keyword.TYPEDEF, | |
| 163 Keyword.VAR | |
| 164 ], DART_RELEVANCE_HIGH); | |
| 165 } | 183 } |
| 166 | 184 |
| 167 test_after_import2() { | 185 test_after_import2() { |
| 168 addTestSource('import foo; c^'); | 186 addTestSource('import foo; c^'); |
| 169 expect(computeFast(), isTrue); | 187 expect(computeFast(), isTrue); |
| 170 assertSuggestKeywords([ | 188 assertSuggestKeywords( |
| 171 Keyword.ABSTRACT, | 189 DIRECTIVE_AND_DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 172 Keyword.CLASS, | |
| 173 Keyword.CONST, | |
| 174 Keyword.EXPORT, | |
| 175 Keyword.FINAL, | |
| 176 Keyword.IMPORT, | |
| 177 Keyword.PART, | |
| 178 Keyword.TYPEDEF, | |
| 179 Keyword.VAR | |
| 180 ], DART_RELEVANCE_HIGH); | |
| 181 } | 190 } |
| 182 | 191 |
| 183 test_before_import() { | 192 test_before_import() { |
| 184 addTestSource('^ import foo;'); | 193 addTestSource('^ import foo;'); |
| 185 expect(computeFast(), isTrue); | 194 expect(computeFast(), isTrue); |
| 186 assertSuggestKeywords([ | 195 assertSuggestKeywords([ |
| 187 Keyword.EXPORT, | 196 Keyword.EXPORT, |
| 188 Keyword.IMPORT, | 197 Keyword.IMPORT, |
| 189 Keyword.LIBRARY, | 198 Keyword.LIBRARY, |
| 190 Keyword.PART | 199 Keyword.PART |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 | 333 |
| 325 test_class_with_name() { | 334 test_class_with_name() { |
| 326 addTestSource('class A extends foo with ^'); | 335 addTestSource('class A extends foo with ^'); |
| 327 expect(computeFast(), isTrue); | 336 expect(computeFast(), isTrue); |
| 328 assertSuggestKeywords([]); | 337 assertSuggestKeywords([]); |
| 329 } | 338 } |
| 330 | 339 |
| 331 test_empty() { | 340 test_empty() { |
| 332 addTestSource('^'); | 341 addTestSource('^'); |
| 333 expect(computeFast(), isTrue); | 342 expect(computeFast(), isTrue); |
| 334 assertSuggestKeywords([ | 343 assertSuggestKeywords( |
| 335 Keyword.ABSTRACT, | 344 DIRECTIVE_DECLARATION_AND_LIBRARY_KEYWORDS, DART_RELEVANCE_HIGH); |
| 336 Keyword.CLASS, | |
| 337 Keyword.CONST, | |
| 338 Keyword.EXPORT, | |
| 339 Keyword.FINAL, | |
| 340 Keyword.IMPORT, | |
| 341 Keyword.LIBRARY, | |
| 342 Keyword.PART, | |
| 343 Keyword.TYPEDEF, | |
| 344 Keyword.VAR | |
| 345 ], DART_RELEVANCE_HIGH); | |
| 346 } | 345 } |
| 347 | 346 |
| 348 test_function_body_inClass_constructorInitializer() { | 347 test_function_body_inClass_constructorInitializer() { |
| 349 addTestSource(r''' | 348 addTestSource(r''' |
| 350 foo(p) {} | 349 foo(p) {} |
| 351 class A { | 350 class A { |
| 352 final f; | 351 final f; |
| 353 A() : f = foo(() {^}); | 352 A() : f = foo(() {^}); |
| 354 } | 353 } |
| 355 '''); | 354 '''); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 | 400 |
| 402 test_function_body_inUnit_afterBlock() { | 401 test_function_body_inUnit_afterBlock() { |
| 403 addTestSource('main() {{}^}'); | 402 addTestSource('main() {{}^}'); |
| 404 expect(computeFast(), isTrue); | 403 expect(computeFast(), isTrue); |
| 405 assertSuggestKeywords(IN_BLOCK_NOT_IN_CLASS); | 404 assertSuggestKeywords(IN_BLOCK_NOT_IN_CLASS); |
| 406 } | 405 } |
| 407 | 406 |
| 408 test_library() { | 407 test_library() { |
| 409 addTestSource('library foo;^'); | 408 addTestSource('library foo;^'); |
| 410 expect(computeFast(), isTrue); | 409 expect(computeFast(), isTrue); |
| 411 assertSuggestKeywords([ | 410 assertSuggestKeywords( |
| 412 Keyword.ABSTRACT, | 411 DIRECTIVE_AND_DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 413 Keyword.CLASS, | |
| 414 Keyword.CONST, | |
| 415 Keyword.EXPORT, | |
| 416 Keyword.FINAL, | |
| 417 Keyword.IMPORT, | |
| 418 Keyword.PART, | |
| 419 Keyword.TYPEDEF, | |
| 420 Keyword.VAR | |
| 421 ], DART_RELEVANCE_HIGH); | |
| 422 } | 412 } |
| 423 | 413 |
| 424 test_library_name() { | 414 test_library_name() { |
| 425 addTestSource('library ^'); | 415 addTestSource('library ^'); |
| 426 expect(computeFast(), isTrue); | 416 expect(computeFast(), isTrue); |
| 427 assertSuggestKeywords([]); | 417 assertSuggestKeywords([]); |
| 428 } | 418 } |
| 429 | 419 |
| 430 test_method_body() { | 420 test_method_body() { |
| 431 addTestSource('class A { foo() {^}}'); | 421 addTestSource('class A { foo() {^}}'); |
| 432 expect(computeFast(), isTrue); | 422 expect(computeFast(), isTrue); |
| 433 assertSuggestKeywords(IN_BLOCK_IN_CLASS); | 423 assertSuggestKeywords(IN_BLOCK_IN_CLASS); |
| 434 } | 424 } |
| 435 | 425 |
| 436 test_named_constructor_invocation() { | 426 test_named_constructor_invocation() { |
| 437 addTestSource('void main() {new Future.^}'); | 427 addTestSource('void main() {new Future.^}'); |
| 438 expect(computeFast(), isTrue); | 428 expect(computeFast(), isTrue); |
| 439 assertSuggestKeywords([]); | 429 assertSuggestKeywords([]); |
| 440 } | 430 } |
| 441 | 431 |
| 442 test_part_of() { | 432 test_part_of() { |
| 443 addTestSource('part of foo;^'); | 433 addTestSource('part of foo;^'); |
| 444 expect(computeFast(), isTrue); | 434 expect(computeFast(), isTrue); |
| 445 assertSuggestKeywords([ | 435 assertSuggestKeywords( |
| 446 Keyword.ABSTRACT, | 436 DIRECTIVE_AND_DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 447 Keyword.CLASS, | |
| 448 Keyword.CONST, | |
| 449 Keyword.EXPORT, | |
| 450 Keyword.FINAL, | |
| 451 Keyword.IMPORT, | |
| 452 Keyword.PART, | |
| 453 Keyword.TYPEDEF, | |
| 454 Keyword.VAR | |
| 455 ], DART_RELEVANCE_HIGH); | |
| 456 } | 437 } |
| 457 | 438 |
| 458 test_partial_class() { | 439 test_partial_class() { |
| 459 addTestSource('cl^'); | 440 addTestSource('cl^'); |
| 460 expect(computeFast(), isTrue); | 441 expect(computeFast(), isTrue); |
| 461 assertSuggestKeywords([ | 442 assertSuggestKeywords( |
| 462 Keyword.ABSTRACT, | 443 DIRECTIVE_DECLARATION_AND_LIBRARY_KEYWORDS, DART_RELEVANCE_HIGH); |
| 463 Keyword.CLASS, | |
| 464 Keyword.CONST, | |
| 465 Keyword.EXPORT, | |
| 466 Keyword.FINAL, | |
| 467 Keyword.IMPORT, | |
| 468 Keyword.LIBRARY, | |
| 469 Keyword.PART, | |
| 470 Keyword.TYPEDEF, | |
| 471 Keyword.VAR | |
| 472 ], DART_RELEVANCE_HIGH); | |
| 473 } | 444 } |
| 474 | 445 |
| 475 test_partial_class2() { | 446 test_partial_class2() { |
| 476 addTestSource('library a; cl^'); | 447 addTestSource('library a; cl^'); |
| 477 expect(computeFast(), isTrue); | 448 expect(computeFast(), isTrue); |
| 478 assertSuggestKeywords([ | 449 assertSuggestKeywords( |
| 479 Keyword.ABSTRACT, | 450 DIRECTIVE_AND_DECLARATION_KEYWORDS, DART_RELEVANCE_HIGH); |
| 480 Keyword.CLASS, | |
| 481 Keyword.CONST, | |
| 482 Keyword.EXPORT, | |
| 483 Keyword.FINAL, | |
| 484 Keyword.IMPORT, | |
| 485 Keyword.PART, | |
| 486 Keyword.TYPEDEF, | |
| 487 Keyword.VAR | |
| 488 ], DART_RELEVANCE_HIGH); | |
| 489 } | 451 } |
| 490 | 452 |
| 491 void _appendKeywords(StringBuffer msg, Iterable<Keyword> keywords) { | 453 void _appendKeywords(StringBuffer msg, Iterable<Keyword> keywords) { |
| 492 List<Keyword> sorted = keywords.toList(); | 454 List<Keyword> sorted = keywords.toList(); |
| 493 sorted.sort((k1, k2) => k1.name.compareTo(k2.name)); | 455 sorted.sort((k1, k2) => k1.name.compareTo(k2.name)); |
| 494 sorted.forEach((k) => msg.writeln(' Keyword.${k.name},')); | 456 sorted.forEach((k) => msg.writeln(' Keyword.${k.name},')); |
| 495 } | 457 } |
| 496 | 458 |
| 497 bool _equalSets(Iterable<Keyword> iter1, Iterable<Keyword> iter2) { | 459 bool _equalSets(Iterable<Keyword> iter1, Iterable<Keyword> iter2) { |
| 498 if (iter1.length != iter2.length) return false; | 460 if (iter1.length != iter2.length) return false; |
| 499 if (iter1.any((k) => !iter2.contains(k))) return false; | 461 if (iter1.any((k) => !iter2.contains(k))) return false; |
| 500 if (iter2.any((k) => !iter1.contains(k))) return false; | 462 if (iter2.any((k) => !iter1.contains(k))) return false; |
| 501 return true; | 463 return true; |
| 502 } | 464 } |
| 503 } | 465 } |
| OLD | NEW |