Skip to content

Commit 528c75a

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Support folding switch statements/expressions/cases
Fixes Dart-Code/Dart-Code#4506. Change-Id: Idd5924d04e9ffffc49f088ca6884393ea72e9bea Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/298120 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent c4d2d2d commit 528c75a

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

pkg/analysis_server/lib/src/computer/computer_folding.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,44 @@ class _DartUnitFoldingComputerVisitor extends RecursiveAstVisitor<void> {
395395
super.visitSetOrMapLiteral(node);
396396
}
397397

398+
@override
399+
void visitSwitchCase(SwitchCase node) {
400+
_computer._addRegion(node.colon.end, node.end, FoldingKind.BLOCK);
401+
super.visitSwitchCase(node);
402+
}
403+
404+
@override
405+
void visitSwitchDefault(SwitchDefault node) {
406+
_computer._addRegion(node.colon.end, node.end, FoldingKind.BLOCK);
407+
super.visitSwitchDefault(node);
408+
}
409+
410+
@override
411+
void visitSwitchExpression(SwitchExpression node) {
412+
_computer._addRegion(
413+
node.leftBracket.end, node.rightBracket.end, FoldingKind.BLOCK);
414+
super.visitSwitchExpression(node);
415+
}
416+
417+
@override
418+
void visitSwitchExpressionCase(SwitchExpressionCase node) {
419+
_computer._addRegion(node.arrow.end, node.end, FoldingKind.BLOCK);
420+
super.visitSwitchExpressionCase(node);
421+
}
422+
423+
@override
424+
void visitSwitchPatternCase(SwitchPatternCase node) {
425+
_computer._addRegion(node.colon.end, node.end, FoldingKind.BLOCK);
426+
super.visitSwitchPatternCase(node);
427+
}
428+
429+
@override
430+
void visitSwitchStatement(SwitchStatement node) {
431+
_computer._addRegion(
432+
node.leftBracket.end, node.rightBracket.end, FoldingKind.BLOCK);
433+
super.visitSwitchStatement(node);
434+
}
435+
398436
@override
399437
void visitWhileStatement(WhileStatement node) {
400438
var body = node.body;

pkg/analysis_server/test/lsp/folding_test.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,89 @@ void f/*[0*/() {
340340
});
341341
}
342342

343+
Future<void> test_switchExpression() async {
344+
final content = '''
345+
void f(int a) {
346+
var b = switch (a) {/*[0*/
347+
1 => '',
348+
2 =>/*[1*/ '1234567890'
349+
'1234567890'/*1]*/,
350+
_ =>/*[2*/ '1234567890'
351+
'1234567890'
352+
'1234567890'
353+
'1234567890'/*2]*/
354+
}/*0]*/;
355+
}
356+
''';
357+
358+
await computeRanges(content);
359+
expectRanges(
360+
{
361+
0: noFoldingKind,
362+
1: noFoldingKind,
363+
2: noFoldingKind,
364+
},
365+
requireAll: false,
366+
);
367+
}
368+
369+
Future<void> test_switchPattern() async {
370+
final content = '''
371+
void f(int a) {
372+
switch (a) {/*[0*/
373+
case 0:/*[1*/
374+
print('');/*1]*/
375+
case 1:
376+
case 2:/*[2*/
377+
print('');/*2]*/
378+
default:/*[3*/
379+
print('');/*3]*/
380+
}/*0]*/
381+
}
382+
''';
383+
384+
await computeRanges(content);
385+
expectRanges(
386+
{
387+
0: noFoldingKind,
388+
1: noFoldingKind,
389+
2: noFoldingKind,
390+
3: noFoldingKind,
391+
},
392+
requireAll: false,
393+
);
394+
}
395+
396+
Future<void> test_switchStatement() async {
397+
final content = '''
398+
// @dart = 2.19
399+
400+
void f(int a) {
401+
switch (a) {/*[0*/
402+
case 0:/*[1*/
403+
print('');
404+
break;/*1]*/
405+
case 1:
406+
case 2:/*[2*/
407+
print('');/*2]*/
408+
default:/*[3*/
409+
print('');/*3]*/
410+
}/*0]*/
411+
}
412+
''';
413+
414+
await computeRanges(content);
415+
expectRanges(
416+
{
417+
0: noFoldingKind,
418+
1: noFoldingKind,
419+
2: noFoldingKind,
420+
3: noFoldingKind,
421+
},
422+
requireAll: false,
423+
);
424+
}
425+
343426
Future<void> test_whileLoop() async {
344427
final content = '''
345428
f(int i) {

0 commit comments

Comments
 (0)