mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 22:51:29 +00:00
Support completion in switch expressions
Change-Id: I21df4780eed21043b3d97ea0e55c50285e4c9b28 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282700 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
2026b38fcc
commit
77174eff4a
|
@ -826,6 +826,13 @@ class _KeywordVisitor extends GeneralizingAstVisitor<void> {
|
|||
return super.visitSwitchCase(node);
|
||||
}
|
||||
|
||||
@override
|
||||
void visitSwitchExpression(SwitchExpression node) {
|
||||
if (entity == node.expression) {
|
||||
_addExpressionKeywords(node);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void visitSwitchPatternCase(SwitchPatternCase node) {
|
||||
final entity = this.entity;
|
||||
|
@ -861,22 +868,16 @@ class _KeywordVisitor extends GeneralizingAstVisitor<void> {
|
|||
if (entity == node.expression) {
|
||||
_addExpressionKeywords(node);
|
||||
} else if (entity == node.rightBracket) {
|
||||
if (node.members.isEmpty) {
|
||||
_addSuggestion(Keyword.CASE);
|
||||
_addSuggestion2(DEFAULT_COLON);
|
||||
} else {
|
||||
_addSuggestion(Keyword.CASE);
|
||||
_addSuggestion2(DEFAULT_COLON);
|
||||
_addSuggestion(Keyword.CASE);
|
||||
_addSuggestion2(DEFAULT_COLON);
|
||||
if (node.members.isNotEmpty) {
|
||||
_addStatementKeywords(node);
|
||||
}
|
||||
}
|
||||
if (node.members.contains(entity)) {
|
||||
if (entity == node.members.first) {
|
||||
_addSuggestion(Keyword.CASE);
|
||||
_addSuggestion2(DEFAULT_COLON);
|
||||
} else {
|
||||
_addSuggestion(Keyword.CASE);
|
||||
_addSuggestion2(DEFAULT_COLON);
|
||||
_addSuggestion(Keyword.CASE);
|
||||
_addSuggestion2(DEFAULT_COLON);
|
||||
if (entity != node.members.first) {
|
||||
_addStatementKeywords(node);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import '../../../../client/completion_driver_test.dart';
|
||||
|
||||
void main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(SwitchExpressionTest1);
|
||||
defineReflectiveTests(SwitchExpressionTest2);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class SwitchExpressionTest1 extends AbstractCompletionDriverTest
|
||||
with SwitchExpressionTestCases {
|
||||
@override
|
||||
TestingCompletionProtocol get protocol => TestingCompletionProtocol.version1;
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class SwitchExpressionTest2 extends AbstractCompletionDriverTest
|
||||
with SwitchExpressionTestCases {
|
||||
@override
|
||||
TestingCompletionProtocol get protocol => TestingCompletionProtocol.version2;
|
||||
}
|
||||
|
||||
// mixin SwitchCaseTestCases on AbstractCompletionDriverTest {
|
||||
// }
|
||||
|
||||
mixin SwitchExpressionTestCases on AbstractCompletionDriverTest {
|
||||
Future<void> test_body_afterArrow() async {
|
||||
await computeSuggestions('''
|
||||
int f(Object p01) {
|
||||
return switch (p01) {
|
||||
1 => ^
|
||||
};
|
||||
}
|
||||
''');
|
||||
assertResponse('''
|
||||
suggestions
|
||||
p01
|
||||
kind: parameter
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> test_body_afterComma() async {
|
||||
await computeSuggestions('''
|
||||
int f(Object p01) {
|
||||
return switch (p01) {
|
||||
1 => 2,
|
||||
^
|
||||
};
|
||||
}
|
||||
''');
|
||||
assertResponse('''
|
||||
suggestions
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> test_body_empty() async {
|
||||
await computeSuggestions('''
|
||||
int f(Object p01) {
|
||||
return switch (p01) {
|
||||
^
|
||||
};
|
||||
}
|
||||
''');
|
||||
assertResponse('''
|
||||
suggestions
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> test_expression() async {
|
||||
await computeSuggestions('''
|
||||
int f(Object p01) {
|
||||
return switch (^) {};
|
||||
}
|
||||
''');
|
||||
assertResponse('''
|
||||
suggestions
|
||||
const
|
||||
kind: keyword
|
||||
false
|
||||
kind: keyword
|
||||
null
|
||||
kind: keyword
|
||||
p01
|
||||
kind: parameter
|
||||
switch
|
||||
kind: keyword
|
||||
true
|
||||
kind: keyword
|
||||
''');
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ import 'record_pattern_test.dart' as record_pattern;
|
|||
import 'record_type_annotation_test.dart' as record_type_annotation;
|
||||
import 'relational_pattern_test.dart' as relational_pattern;
|
||||
import 'super_formal_parameter_test.dart' as super_formal_parameter;
|
||||
import 'switch_expression_test.dart' as switch_expression;
|
||||
import 'switch_pattern_case_test.dart' as switch_pattern_case;
|
||||
import 'type_argument_list_test.dart' as type_argument_list;
|
||||
import 'variable_declaration_list_test.dart' as variable_declaration_list;
|
||||
|
@ -51,6 +52,7 @@ void main() {
|
|||
record_type_annotation.main();
|
||||
relational_pattern.main();
|
||||
super_formal_parameter.main();
|
||||
switch_expression.main();
|
||||
switch_pattern_case.main();
|
||||
type_argument_list.main();
|
||||
variable_declaration_list.main();
|
||||
|
|
|
@ -1388,6 +1388,24 @@ class _OpTypeAstVisitor extends GeneralizingAstVisitor<void> {
|
|||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void visitSwitchExpression(SwitchExpression node) {
|
||||
if (identical(entity, node.expression)) {
|
||||
optype.completionLocation = 'SwitchExpression_expression';
|
||||
optype.includeReturnValueSuggestions = true;
|
||||
optype.includeTypeNameSuggestions = true;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void visitSwitchExpressionCase(SwitchExpressionCase node) {
|
||||
if (identical(entity, node.expression)) {
|
||||
optype.completionLocation = 'SwitchExpressionCase_expression';
|
||||
optype.includeReturnValueSuggestions = true;
|
||||
optype.includeTypeNameSuggestions = true;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void visitSwitchPatternCase(SwitchPatternCase node) {
|
||||
if (identical(entity, node.colon)) {
|
||||
|
|
Loading…
Reference in a new issue