Skip to content

Commit d2c0f0a

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Support folding try/catch/finally blocks
Fixes Dart-Code/Dart-Code#5201 Change-Id: I1790d3aa624d80ad0233216ca650d497f31c632b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/431281 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 33daa43 commit d2c0f0a

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class DartUnitFoldingComputer {
3131
//
3232
// class Foo { [...] }
3333
//
34-
// For if statements, they may have else/elseIfs which would result in long
35-
// lines like:
34+
// For if/try statements, they may have else/elseIfs which would result in
35+
// long lines like:
3636
//
3737
// if (cond) { [...] } else { [...] }
3838
//
@@ -523,6 +523,19 @@ class _DartUnitFoldingComputerVisitor extends RecursiveAstVisitor<void> {
523523
super.visitSwitchStatement(node);
524524
}
525525

526+
@override
527+
void visitTryStatement(TryStatement node) {
528+
_computer.addRegionForConditionalBlock(node.body);
529+
for (var catchNode in node.catchClauses) {
530+
_computer.addRegionForConditionalBlock(catchNode.body);
531+
}
532+
if (node.finallyBlock case var finallyNode?) {
533+
_computer.addRegionForConditionalBlock(finallyNode);
534+
}
535+
536+
super.visitTryStatement(node);
537+
}
538+
526539
@override
527540
void visitWhileStatement(WhileStatement node) {
528541
var body = node.body;

pkg/analysis_server/test/lsp/folding_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,67 @@ void f(int a) {
470470
}, requireAll: false);
471471
}
472472

473+
/// Even without lineFolding, try/catch/finally folding regions end
474+
/// on the last statement of each block (matching if/else) to avoid long
475+
/// lines when folded.
476+
Future<void> test_tryCatchFinally() async {
477+
var content = '''
478+
void f() {
479+
try {/*[0*/
480+
print('');
481+
print('');/*0]*/
482+
} on ArgumentError catch (_) {/*[1*/
483+
print('');
484+
print('');/*1]*/
485+
} catch (e) {/*[2*/
486+
print('');
487+
print('');/*2]*/
488+
} finally {/*[3*/
489+
print('');
490+
print('');/*3]*/
491+
}
492+
}
493+
''';
494+
495+
await computeRanges(content);
496+
expectRangesContain({
497+
0: noFoldingKind,
498+
1: noFoldingKind,
499+
2: noFoldingKind,
500+
3: noFoldingKind,
501+
});
502+
}
503+
504+
Future<void> test_tryCatchFinally_lineFoldingOnly() async {
505+
lineFoldingOnly = true;
506+
507+
var content = '''
508+
void f() {
509+
try {/*[0*/
510+
print('');
511+
print('');/*0]*/
512+
} on ArgumentError catch (_) {/*[1*/
513+
print('');
514+
print('');/*1]*/
515+
} catch (e) {/*[2*/
516+
print('');
517+
print('');/*2]*/
518+
} finally {/*[3*/
519+
print('');
520+
print('');/*3]*/
521+
}
522+
}
523+
''';
524+
525+
await computeRanges(content);
526+
expectRangesContain({
527+
0: noFoldingKind,
528+
1: noFoldingKind,
529+
2: noFoldingKind,
530+
3: noFoldingKind,
531+
});
532+
}
533+
473534
Future<void> test_whileLoop() async {
474535
var content = '''
475536
f(int i) {

pkg/analysis_server/test/src/computer/folding_computer_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,35 @@ void f() {}
10401040
expectNoRegions();
10411041
}
10421042

1043+
Future<void> test_tryCatchFinally() async {
1044+
var content = '''
1045+
void f/*[0*/() {
1046+
try {/*[1*/
1047+
print('');
1048+
print('');/*1]*/
1049+
} on ArgumentError catch (_) {/*[2*/
1050+
print('');
1051+
print('');/*2]*/
1052+
} catch (e) {/*[3*/
1053+
print('');
1054+
print('');/*3]*/
1055+
} finally {/*[4*/
1056+
print('');
1057+
print('');/*4]*/
1058+
}
1059+
}/*0]*/
1060+
''';
1061+
1062+
await _computeRegions(content);
1063+
expectRegions({
1064+
0: FoldingKind.FUNCTION_BODY,
1065+
1: FoldingKind.BLOCK,
1066+
2: FoldingKind.BLOCK,
1067+
3: FoldingKind.BLOCK,
1068+
4: FoldingKind.BLOCK,
1069+
});
1070+
}
1071+
10431072
Future<void> _computeRegions(String sourceContent) async {
10441073
code = TestCode.parse(normalizeSource(sourceContent));
10451074
var file = newFile(sourcePath, code.code);

0 commit comments

Comments
 (0)