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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 Keyword.PART, | 72 Keyword.PART, |
73 Keyword.TYPEDEF, | 73 Keyword.TYPEDEF, |
74 Keyword.VAR, | 74 Keyword.VAR, |
75 Keyword.VOID | 75 Keyword.VOID |
76 ]; | 76 ]; |
77 | 77 |
78 static const List<String> NO_PSEUDO_KEYWORDS = const []; | 78 static const List<String> NO_PSEUDO_KEYWORDS = const []; |
79 | 79 |
80 static const List<Keyword> STMT_START_IN_CLASS = const [ | 80 static const List<Keyword> STMT_START_IN_CLASS = const [ |
81 Keyword.ASSERT, | 81 Keyword.ASSERT, |
| 82 Keyword.DO, |
| 83 Keyword.FINAL, |
| 84 Keyword.FOR, |
| 85 Keyword.IF, |
| 86 Keyword.NEW, |
| 87 Keyword.RETHROW, |
| 88 Keyword.RETURN, |
| 89 Keyword.SUPER, |
| 90 Keyword.SWITCH, |
| 91 Keyword.THIS, |
| 92 Keyword.THROW, |
| 93 Keyword.TRY, |
| 94 Keyword.VAR, |
| 95 Keyword.VOID, |
| 96 Keyword.WHILE |
| 97 ]; |
| 98 |
| 99 static const List<Keyword> STMT_START_IN_LOOP_IN_CLASS = const [ |
| 100 Keyword.ASSERT, |
| 101 Keyword.BREAK, |
82 Keyword.CONTINUE, | 102 Keyword.CONTINUE, |
83 Keyword.DO, | 103 Keyword.DO, |
84 Keyword.FINAL, | 104 Keyword.FINAL, |
85 Keyword.FOR, | 105 Keyword.FOR, |
86 Keyword.IF, | 106 Keyword.IF, |
87 Keyword.NEW, | 107 Keyword.NEW, |
88 Keyword.RETHROW, | 108 Keyword.RETHROW, |
89 Keyword.RETURN, | 109 Keyword.RETURN, |
90 Keyword.SUPER, | 110 Keyword.SUPER, |
91 Keyword.SWITCH, | 111 Keyword.SWITCH, |
92 Keyword.THIS, | 112 Keyword.THIS, |
93 Keyword.THROW, | 113 Keyword.THROW, |
94 Keyword.TRY, | 114 Keyword.TRY, |
95 Keyword.VAR, | 115 Keyword.VAR, |
96 Keyword.VOID, | 116 Keyword.VOID, |
97 Keyword.WHILE | 117 Keyword.WHILE |
98 ]; | 118 ]; |
99 | 119 |
100 static const List<Keyword> STMT_START_IN_SWITCH_IN_CLASS = const [ | 120 static const List<Keyword> STMT_START_IN_SWITCH_IN_CLASS = const [ |
101 Keyword.ASSERT, | 121 Keyword.ASSERT, |
| 122 Keyword.BREAK, |
102 Keyword.CASE, | 123 Keyword.CASE, |
103 Keyword.CONTINUE, | |
104 Keyword.DEFAULT, | 124 Keyword.DEFAULT, |
105 Keyword.DO, | 125 Keyword.DO, |
106 Keyword.FINAL, | 126 Keyword.FINAL, |
107 Keyword.FOR, | 127 Keyword.FOR, |
108 Keyword.IF, | 128 Keyword.IF, |
109 Keyword.NEW, | 129 Keyword.NEW, |
110 Keyword.RETHROW, | 130 Keyword.RETHROW, |
111 Keyword.RETURN, | 131 Keyword.RETURN, |
112 Keyword.SUPER, | 132 Keyword.SUPER, |
113 Keyword.SWITCH, | 133 Keyword.SWITCH, |
114 Keyword.THIS, | 134 Keyword.THIS, |
115 Keyword.THROW, | 135 Keyword.THROW, |
116 Keyword.TRY, | 136 Keyword.TRY, |
117 Keyword.VAR, | 137 Keyword.VAR, |
118 Keyword.VOID, | 138 Keyword.VOID, |
119 Keyword.WHILE | 139 Keyword.WHILE |
120 ]; | 140 ]; |
121 | 141 |
122 static const List<Keyword> STMT_START_IN_SWITCH_OUTSIDE_CLASS = const [ | 142 static const List<Keyword> STMT_START_IN_SWITCH_OUTSIDE_CLASS = const [ |
123 Keyword.ASSERT, | 143 Keyword.ASSERT, |
| 144 Keyword.BREAK, |
124 Keyword.CASE, | 145 Keyword.CASE, |
125 Keyword.CONTINUE, | |
126 Keyword.DEFAULT, | 146 Keyword.DEFAULT, |
127 Keyword.DO, | 147 Keyword.DO, |
128 Keyword.FINAL, | 148 Keyword.FINAL, |
129 Keyword.FOR, | 149 Keyword.FOR, |
130 Keyword.IF, | 150 Keyword.IF, |
131 Keyword.NEW, | 151 Keyword.NEW, |
132 Keyword.RETHROW, | 152 Keyword.RETHROW, |
133 Keyword.RETURN, | 153 Keyword.RETURN, |
134 Keyword.SWITCH, | 154 Keyword.SWITCH, |
135 Keyword.THROW, | 155 Keyword.THROW, |
136 Keyword.TRY, | 156 Keyword.TRY, |
137 Keyword.VAR, | 157 Keyword.VAR, |
138 Keyword.VOID, | 158 Keyword.VOID, |
139 Keyword.WHILE | 159 Keyword.WHILE |
140 ]; | 160 ]; |
141 | 161 |
142 static const List<Keyword> STMT_START_OUTSIDE_CLASS = const [ | 162 static const List<Keyword> STMT_START_OUTSIDE_CLASS = const [ |
143 Keyword.ASSERT, | 163 Keyword.ASSERT, |
| 164 Keyword.DO, |
| 165 Keyword.FINAL, |
| 166 Keyword.FOR, |
| 167 Keyword.IF, |
| 168 Keyword.NEW, |
| 169 Keyword.RETHROW, |
| 170 Keyword.RETURN, |
| 171 Keyword.SWITCH, |
| 172 Keyword.THROW, |
| 173 Keyword.TRY, |
| 174 Keyword.VAR, |
| 175 Keyword.VOID, |
| 176 Keyword.WHILE |
| 177 ]; |
| 178 |
| 179 static const List<Keyword> STMT_START_IN_LOOP_OUTSIDE_CLASS = const [ |
| 180 Keyword.ASSERT, |
| 181 Keyword.BREAK, |
144 Keyword.CONTINUE, | 182 Keyword.CONTINUE, |
145 Keyword.DO, | 183 Keyword.DO, |
146 Keyword.FINAL, | 184 Keyword.FINAL, |
147 Keyword.FOR, | 185 Keyword.FOR, |
148 Keyword.IF, | 186 Keyword.IF, |
149 Keyword.NEW, | 187 Keyword.NEW, |
150 Keyword.RETHROW, | 188 Keyword.RETHROW, |
151 Keyword.RETURN, | 189 Keyword.RETURN, |
152 Keyword.SWITCH, | 190 Keyword.SWITCH, |
153 Keyword.THROW, | 191 Keyword.THROW, |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 expect(computeFast(), isTrue); | 570 expect(computeFast(), isTrue); |
533 assertSuggestKeywords([Keyword.THIS]); | 571 assertSuggestKeywords([Keyword.THIS]); |
534 } | 572 } |
535 | 573 |
536 test_constructor_param2() { | 574 test_constructor_param2() { |
537 addTestSource('class A { A(t^) {});}'); | 575 addTestSource('class A { A(t^) {});}'); |
538 expect(computeFast(), isTrue); | 576 expect(computeFast(), isTrue); |
539 assertSuggestKeywords([Keyword.THIS]); | 577 assertSuggestKeywords([Keyword.THIS]); |
540 } | 578 } |
541 | 579 |
| 580 test_do_break_continue() { |
| 581 addTestSource('main() {do {^} while (true);}'); |
| 582 expect(computeFast(), isTrue); |
| 583 assertSuggestKeywords(STMT_START_IN_LOOP_OUTSIDE_CLASS, |
| 584 relevance: DART_RELEVANCE_KEYWORD); |
| 585 } |
| 586 |
| 587 test_do_break_continue2() { |
| 588 addTestSource('class A {foo() {do {^} while (true);}}'); |
| 589 expect(computeFast(), isTrue); |
| 590 assertSuggestKeywords(STMT_START_IN_LOOP_IN_CLASS, |
| 591 relevance: DART_RELEVANCE_KEYWORD); |
| 592 } |
| 593 |
542 test_empty() { | 594 test_empty() { |
543 addTestSource('^'); | 595 addTestSource('^'); |
544 expect(computeFast(), isTrue); | 596 expect(computeFast(), isTrue); |
545 assertSuggestKeywords(DIRECTIVE_DECLARATION_AND_LIBRARY_KEYWORDS, | 597 assertSuggestKeywords(DIRECTIVE_DECLARATION_AND_LIBRARY_KEYWORDS, |
546 relevance: DART_RELEVANCE_HIGH); | 598 relevance: DART_RELEVANCE_HIGH); |
547 } | 599 } |
548 | 600 |
| 601 test_for_break_continue() { |
| 602 addTestSource('main() {for (int x in myList) {^}}'); |
| 603 expect(computeFast(), isTrue); |
| 604 assertSuggestKeywords(STMT_START_IN_LOOP_OUTSIDE_CLASS, |
| 605 relevance: DART_RELEVANCE_KEYWORD); |
| 606 } |
| 607 |
| 608 test_for_break_continue2() { |
| 609 addTestSource('class A {foo() {for (int x in myList) {^}}}'); |
| 610 expect(computeFast(), isTrue); |
| 611 assertSuggestKeywords(STMT_START_IN_LOOP_IN_CLASS, |
| 612 relevance: DART_RELEVANCE_KEYWORD); |
| 613 } |
| 614 |
549 test_for_expression_in() { | 615 test_for_expression_in() { |
550 addTestSource('main() {for (int x i^)}'); | 616 addTestSource('main() {for (int x i^)}'); |
551 expect(computeFast(), isTrue); | 617 expect(computeFast(), isTrue); |
552 assertSuggestKeywords([Keyword.IN], relevance: DART_RELEVANCE_HIGH); | 618 assertSuggestKeywords([Keyword.IN], relevance: DART_RELEVANCE_HIGH); |
553 } | 619 } |
554 | 620 |
555 test_for_expression_in2() { | 621 test_for_expression_in2() { |
556 addTestSource('main() {for (int x in^)}'); | 622 addTestSource('main() {for (int x in^)}'); |
557 expect(computeFast(), isTrue); | 623 expect(computeFast(), isTrue); |
558 assertSuggestKeywords([Keyword.IN], relevance: DART_RELEVANCE_HIGH); | 624 assertSuggestKeywords([Keyword.IN], relevance: DART_RELEVANCE_HIGH); |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 addTestSource( | 1019 addTestSource( |
954 'library bar; import "zoo.dart"; imp^ import "package:foo/foo.dart";'); | 1020 'library bar; import "zoo.dart"; imp^ import "package:foo/foo.dart";'); |
955 expect(computeFast(), isTrue); | 1021 expect(computeFast(), isTrue); |
956 // TODO(danrubel) should not suggest declaration keywords | 1022 // TODO(danrubel) should not suggest declaration keywords |
957 assertSuggestKeywords(DIRECTIVE_AND_DECLARATION_KEYWORDS, | 1023 assertSuggestKeywords(DIRECTIVE_AND_DECLARATION_KEYWORDS, |
958 relevance: DART_RELEVANCE_HIGH); | 1024 relevance: DART_RELEVANCE_HIGH); |
959 expect(request.replacementOffset, 32); | 1025 expect(request.replacementOffset, 32); |
960 expect(request.replacementLength, 3); | 1026 expect(request.replacementLength, 3); |
961 } | 1027 } |
962 | 1028 |
| 1029 test_is_expression() { |
| 1030 addTestSource('main() {if (x is^)}'); |
| 1031 expect(computeFast(), isTrue); |
| 1032 assertSuggestKeywords([Keyword.IS], relevance: DART_RELEVANCE_HIGH); |
| 1033 } |
| 1034 |
963 test_library() { | 1035 test_library() { |
964 addTestSource('library foo;^'); | 1036 addTestSource('library foo;^'); |
965 expect(computeFast(), isTrue); | 1037 expect(computeFast(), isTrue); |
966 assertSuggestKeywords(DIRECTIVE_AND_DECLARATION_KEYWORDS, | 1038 assertSuggestKeywords(DIRECTIVE_AND_DECLARATION_KEYWORDS, |
967 relevance: DART_RELEVANCE_HIGH); | 1039 relevance: DART_RELEVANCE_HIGH); |
968 } | 1040 } |
969 | 1041 |
970 test_library_declaration() { | 1042 test_library_declaration() { |
971 addTestSource('library ^'); | 1043 addTestSource('library ^'); |
972 expect(computeFast(), isTrue); | 1044 expect(computeFast(), isTrue); |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1284 expect(computeFast(), isTrue); | 1356 expect(computeFast(), isTrue); |
1285 assertSuggestKeywords(STMT_START_IN_SWITCH_OUTSIDE_CLASS); | 1357 assertSuggestKeywords(STMT_START_IN_SWITCH_OUTSIDE_CLASS); |
1286 } | 1358 } |
1287 | 1359 |
1288 test_switch_statement2() { | 1360 test_switch_statement2() { |
1289 addTestSource('class A{foo() {switch(1) {case 1:^}}}'); | 1361 addTestSource('class A{foo() {switch(1) {case 1:^}}}'); |
1290 expect(computeFast(), isTrue); | 1362 expect(computeFast(), isTrue); |
1291 assertSuggestKeywords(STMT_START_IN_SWITCH_IN_CLASS); | 1363 assertSuggestKeywords(STMT_START_IN_SWITCH_IN_CLASS); |
1292 } | 1364 } |
1293 | 1365 |
| 1366 test_while_break_continue() { |
| 1367 addTestSource('main() {while (true) {^}}'); |
| 1368 expect(computeFast(), isTrue); |
| 1369 assertSuggestKeywords(STMT_START_IN_LOOP_OUTSIDE_CLASS, |
| 1370 relevance: DART_RELEVANCE_KEYWORD); |
| 1371 } |
| 1372 |
| 1373 test_while_break_continue2() { |
| 1374 addTestSource('class A {foo() {while (true) {^}}}'); |
| 1375 expect(computeFast(), isTrue); |
| 1376 assertSuggestKeywords(STMT_START_IN_LOOP_IN_CLASS, |
| 1377 relevance: DART_RELEVANCE_KEYWORD); |
| 1378 } |
| 1379 |
1294 void _appendCompletions( | 1380 void _appendCompletions( |
1295 StringBuffer msg, Iterable<String> completions, Iterable<String> other) { | 1381 StringBuffer msg, Iterable<String> completions, Iterable<String> other) { |
1296 List<String> sorted = completions.toList(); | 1382 List<String> sorted = completions.toList(); |
1297 sorted.sort((c1, c2) => c1.compareTo(c2)); | 1383 sorted.sort((c1, c2) => c1.compareTo(c2)); |
1298 sorted.forEach( | 1384 sorted.forEach( |
1299 (c) => msg.writeln(' $c, ${other.contains(c) ? '' : '<<<<<<<<<<<'}')); | 1385 (c) => msg.writeln(' $c, ${other.contains(c) ? '' : '<<<<<<<<<<<'}')); |
1300 } | 1386 } |
1301 | 1387 |
1302 bool _equalSets(Iterable<String> iter1, Iterable<String> iter2) { | 1388 bool _equalSets(Iterable<String> iter1, Iterable<String> iter2) { |
1303 if (iter1.length != iter2.length) return false; | 1389 if (iter1.length != iter2.length) return false; |
1304 if (iter1.any((c) => !iter2.contains(c))) return false; | 1390 if (iter1.any((c) => !iter2.contains(c))) return false; |
1305 if (iter2.any((c) => !iter1.contains(c))) return false; | 1391 if (iter2.any((c) => !iter1.contains(c))) return false; |
1306 return true; | 1392 return true; |
1307 } | 1393 } |
1308 } | 1394 } |
OLD | NEW |