Skip to content

Commit ac95931

Browse files
authored
Added SwitchStatments and SwitchExpression to the new folding (#2084)
1 parent 7797bff commit ac95931

File tree

2 files changed

+147
-2
lines changed

2 files changed

+147
-2
lines changed

org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/folding/FoldingTest.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,94 @@ void x() { //here should be an annotation
575575
try { //here should not be an annotation
576576
} catch (Exception e) { //here should not be an annotation
577577
}
578+
int zaehler = 0;
579+
switch (zaehler) { //here should be an annotation
580+
case 0: //here should not be an annotation
581+
break;
582+
default: //here should not be an annotation
583+
break;
584+
}
578585
}
579586
public void bar() { //here should not be an annotation
580587
}
581588
}
582589
""";
583-
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 1);
590+
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
591+
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
592+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 20); // Method
593+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 15, 19); // switch
594+
}
595+
596+
@Test
597+
public void testSwitchExpression() throws Exception {
598+
assumeTrue("Only doable with the new folding", newFoldingActive);
599+
String str= """
600+
package org.example.test;
601+
class Outer {
602+
void a() { //here should be an annotation
603+
int b = 0;
604+
int c = switch (b) { //here should be an annotation
605+
case 1 -> //here should be an annotation
606+
607+
1;
608+
case 2 -> //here should not be an annotation
609+
1;
610+
case 3 -> //here should be an annotation
611+
612+
break;
613+
1;
614+
case 4 -> { //here should be an annotation
615+
b = 2;
616+
yield 3;
617+
}
618+
case 5 -> { //here should not be an annotation
619+
yield 3;
620+
}
621+
default -> //here should be an annotation
622+
623+
0;
624+
};
625+
}
626+
}
627+
""";
628+
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 6);
629+
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
630+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 24); // method
631+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 4, 23); // switch
632+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 5, 6); // 1. case
633+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 10, 11); // 3. case
634+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 14, 15); // 4. case
635+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 21, 22); // default
636+
}
637+
638+
@Test
639+
public void testSwitchStatment() throws Exception {
640+
assumeTrue("Only doable with the new folding", newFoldingActive);
641+
String str= """
642+
package org.example.test;
643+
class Outer {
644+
void a() { //here should be an annotation
645+
int b = 0;
646+
switch (b) { //here should be an annotation
647+
case 0: //here should be an annotation
648+
649+
break;
650+
b = 1;
651+
case 1: //here should not be an annotation
652+
break;
653+
654+
default: //here should be an annotation
655+
656+
break;
657+
}
658+
}
659+
}
660+
""";
661+
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 4);
584662
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
585-
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 13); // Method
663+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 15); // method
664+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 4, 14); // switch
665+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 5, 6); // case
666+
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 12, 13); // default
586667
}
587668
}

org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/text/folding/DefaultJavaFoldingStructureProvider.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import org.eclipse.jdt.core.dom.ASTVisitor;
7878
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
7979
import org.eclipse.jdt.core.dom.Block;
80+
import org.eclipse.jdt.core.dom.BreakStatement;
8081
import org.eclipse.jdt.core.dom.CatchClause;
8182
import org.eclipse.jdt.core.dom.Comment;
8283
import org.eclipse.jdt.core.dom.CompilationUnit;
@@ -90,10 +91,14 @@
9091
import org.eclipse.jdt.core.dom.LambdaExpression;
9192
import org.eclipse.jdt.core.dom.MethodDeclaration;
9293
import org.eclipse.jdt.core.dom.Statement;
94+
import org.eclipse.jdt.core.dom.SwitchCase;
95+
import org.eclipse.jdt.core.dom.SwitchExpression;
96+
import org.eclipse.jdt.core.dom.SwitchStatement;
9397
import org.eclipse.jdt.core.dom.SynchronizedStatement;
9498
import org.eclipse.jdt.core.dom.TryStatement;
9599
import org.eclipse.jdt.core.dom.TypeDeclaration;
96100
import org.eclipse.jdt.core.dom.WhileStatement;
101+
import org.eclipse.jdt.core.dom.YieldStatement;
97102

98103
import org.eclipse.jdt.ui.PreferenceConstants;
99104

@@ -488,6 +493,65 @@ public boolean visit(Initializer node) {
488493
return true;
489494
}
490495

496+
@Override
497+
public boolean visit(SwitchStatement node) {
498+
createFoldingRegionForStatement(node);
499+
createFoldingRegionForSwitch(node, node.statements());
500+
return false;
501+
}
502+
503+
@Override
504+
public boolean visit(SwitchExpression node) {
505+
createFoldingRegionForStatement(node);
506+
createFoldingRegionForSwitch(node, node.statements());
507+
return false;
508+
}
509+
510+
private void createFoldingRegionForSwitch(ASTNode switchNode, List<?> statements) {
511+
SwitchCase previousCase= null;
512+
513+
for (Object obj : statements) {
514+
Statement stmt= (Statement) obj;
515+
516+
if (stmt instanceof SwitchCase currentCase) {
517+
if (previousCase != null) {
518+
int start= previousCase.getStartPosition();
519+
int end= currentCase.getStartPosition();
520+
createFoldingRegion(start, end - start, ctx.collapseMembers());
521+
}
522+
previousCase= currentCase;
523+
524+
} else if (stmt instanceof BreakStatement || stmt instanceof YieldStatement) {
525+
if (previousCase != null) {
526+
int start= previousCase.getStartPosition();
527+
int end= stmt.getStartPosition();
528+
createFoldingRegion(start, end - start, ctx.collapseMembers());
529+
previousCase= null;
530+
}
531+
} else if (stmt instanceof Block block) {
532+
List<?> innerStatements= block.statements();
533+
for (Object innerObj : innerStatements) {
534+
Statement innerStmt= (Statement) innerObj;
535+
536+
if (innerStmt instanceof BreakStatement || innerStmt instanceof YieldStatement) {
537+
if (previousCase != null) {
538+
int start= previousCase.getStartPosition();
539+
int end= innerStmt.getStartPosition();
540+
createFoldingRegion(start, end - start, ctx.collapseMembers());
541+
previousCase= null;
542+
}
543+
}
544+
}
545+
}
546+
}
547+
548+
if (previousCase != null) {
549+
int start= previousCase.getStartPosition();
550+
int end= switchNode.getStartPosition() + switchNode.getLength();
551+
createFoldingRegion(start, end - start, ctx.collapseMembers());
552+
}
553+
}
554+
491555
private void createFoldingRegion(ASTNode node, boolean collapse) {
492556
createFoldingRegion(node.getStartPosition(), node.getLength(), collapse);
493557
}

0 commit comments

Comments
 (0)