Skip to content

Commit fcb4f27

Browse files
fedejeanneiloveeclipse
authored andcommitted
Only fold inner types when preference is set #3031
Fixes #3031
1 parent c6fffdd commit fcb4f27

3 files changed

Lines changed: 111 additions & 5 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,4 +784,47 @@ public static void main(String[] args) {
784784
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 9, 10); // if
785785
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 11, 13); // else
786786
}
787+
788+
/**
789+
* See <a href="https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/3031">GitHub Issue #3031</a>
790+
*/
791+
@Test
792+
public void testFoldInnerTypesByDefault() throws Exception {
793+
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
794+
store.setValue(PreferenceConstants.EDITOR_FOLDING_INNERTYPES, true);
795+
796+
String str= """
797+
class B { // Should not be folded
798+
static class B1 { // Should be folded
799+
800+
}
801+
}
802+
803+
public class A { // Should not be folded
804+
static class A1 { // Should be folded
805+
}
806+
807+
static class A2 { // Should be folded
808+
public static void main(String[] args) {
809+
Runnable r = new Runnable() { // Should be folded
810+
@Override
811+
public void run() {
812+
813+
}
814+
};
815+
}
816+
817+
}
818+
}
819+
""";
820+
List<FoldingTestUtils.ProjectionRegion> regions= FoldingTestUtils.getProjectionRegionsOfPackage(packageFragment, str);
821+
FoldingTestUtils.assertContainsExpandedRegionUsingStartAndEndLine(regions, str, 0, 4); // class B
822+
FoldingTestUtils.assertContainsCollapsedRegionUsingStartAndEndLine(regions, str, 1, 3); // class B1
823+
FoldingTestUtils.assertContainsExpandedRegionUsingStartAndEndLine(regions, str, 6, 21); // class A
824+
FoldingTestUtils.assertContainsCollapsedRegionUsingStartAndEndLine(regions, str, 7, 8); // class A1
825+
FoldingTestUtils.assertContainsCollapsedRegionUsingStartAndEndLine(regions, str, 10, 20); // class A2
826+
FoldingTestUtils.assertContainsCollapsedRegionUsingStartAndEndLine(regions, str, 12, 17); // Runnable
827+
JavaPlugin.getDefault().getPreferenceStore().setToDefault(PreferenceConstants.EDITOR_FOLDING_INNERTYPES);
828+
829+
}
787830
}

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

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Iterator;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.stream.Collectors;
2728

2829
import org.eclipse.jface.text.IRegion;
2930
import org.eclipse.jface.text.Position;
@@ -41,17 +42,33 @@
4142
public final class FoldingTestUtils {
4243
private record StartEnd(int start, int end) {}
4344

45+
public record ProjectionRegion(IRegion region, boolean collapsed) {}
46+
4447
private FoldingTestUtils() {
4548
}
4649

4750
public static List<IRegion> getProjectionRangesOfPackage(IPackageFragment packageFragment, String code) throws Exception {
4851
ICompilationUnit cu= packageFragment.createCompilationUnit("A.java", code, true, null);
4952
JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(cu);
50-
ProjectionAnnotationModel model= editor.getAdapter(ProjectionAnnotationModel.class);
53+
try {
54+
ProjectionAnnotationModel model= editor.getAdapter(ProjectionAnnotationModel.class);
5155

52-
List<IRegion> regions= extractRegions(model);
53-
editor.close(false);
54-
return regions;
56+
return extractRegions(model);
57+
} finally {
58+
editor.close(false);
59+
}
60+
}
61+
62+
public static List<ProjectionRegion> getProjectionRegionsOfPackage(IPackageFragment packageFragment, String code) throws Exception {
63+
ICompilationUnit cu= packageFragment.createCompilationUnit("A.java", code, true, null);
64+
JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(cu);
65+
try {
66+
ProjectionAnnotationModel model= editor.getAdapter(ProjectionAnnotationModel.class);
67+
68+
return extractProjectionRegions(model);
69+
} finally {
70+
editor.close(false);
71+
}
5572
}
5673

5774
public static List<IRegion> extractRegions(ProjectionAnnotationModel model) {
@@ -69,6 +86,22 @@ public static List<IRegion> extractRegions(ProjectionAnnotationModel model) {
6986
return regions;
7087
}
7188

89+
public static List<ProjectionRegion> extractProjectionRegions(ProjectionAnnotationModel model) {
90+
List<ProjectionRegion> regions= new ArrayList<>();
91+
Iterator<Annotation> it= model.getAnnotationIterator();
92+
while (it.hasNext()) {
93+
Annotation a= it.next();
94+
if (a instanceof ProjectionAnnotation projectionAnnotation) {
95+
Position p= model.getPosition(a);
96+
regions.add(new ProjectionRegion(new Region(p.getOffset(), p.getLength()), projectionAnnotation.isCollapsed()));
97+
}
98+
}
99+
List<IRegion> projectionRanges= regions.stream().map(ProjectionRegion::region).collect(Collectors.toList());
100+
assertNoDuplicatedRegions(projectionRanges);
101+
assertNoRegionsStartInTheSameOffset(projectionRanges);
102+
return regions;
103+
}
104+
72105
private static void assertNoDuplicatedRegions(List<IRegion> regions) {
73106
long distinctRegions= regions.stream()
74107
.map(r -> Map.entry(r.getOffset(), r.getLength())) // map to offset-length pairs
@@ -113,6 +146,35 @@ public static void assertContainsRegionUsingStartAndEndLine(List<IRegion> projec
113146
assertContainsRegionWithOffsetAndLength(projectionRanges, startLine, endLine, startEnd.start(), startEnd.end());
114147
}
115148

149+
public static void assertContainsCollapsedRegionUsingStartAndEndLine(List<ProjectionRegion> projectionRanges, String input, int startLine, int endLine) {
150+
assertContainsRegionWithCollapsedState(projectionRanges, input, startLine, endLine, true);
151+
}
152+
153+
public static void assertContainsExpandedRegionUsingStartAndEndLine(List<ProjectionRegion> projectionRanges, String input, int startLine, int endLine) {
154+
assertContainsRegionWithCollapsedState(projectionRanges, input, startLine, endLine, false);
155+
}
156+
157+
private static void assertContainsRegionWithCollapsedState(List<ProjectionRegion> projectionRanges, String input, int startLine, int endLine, boolean collapsed) {
158+
StartEnd startEnd= getStartEnd(input, startLine, endLine);
159+
int expectedRegionBegin= startEnd.start();
160+
int expectedRegionLength= startEnd.end() - startEnd.start() + 1;
161+
162+
for (ProjectionRegion projectionRegion : projectionRanges) {
163+
IRegion region= projectionRegion.region();
164+
if (region.getOffset() == expectedRegionBegin && region.getLength() == expectedRegionLength) {
165+
assertEquals(collapsed, projectionRegion.collapsed(),
166+
"incorrect collapsed state for region from line " + startLine + " to line " + endLine + " (offset: " + expectedRegionBegin
167+
+ ", length: " + expectedRegionLength + ")");
168+
return;
169+
}
170+
}
171+
172+
fail(
173+
"missing region from line " + startLine + " to line " + endLine + " (offset: " + expectedRegionBegin
174+
+ ", length: " + expectedRegionLength + ")" +
175+
", actual regions: " + sorted(projectionRanges.stream().map(ProjectionRegion::region).collect(Collectors.toList())));
176+
}
177+
116178
private static StartEnd getStartEnd(String input, int startLine, int endLine) {
117179
assertTrue(startLine <= endLine, "start line must be smaller or equal to end line");
118180
int startLineBegin= findLineStartIndex(input, startLine);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,8 @@ protected void computeFoldingStructure(IJavaElement element, FoldingStructureCom
12561256
collapse= ctx.collapseImportContainer();
12571257
break;
12581258
case IJavaElement.TYPE:
1259-
collapse= ctx.collapseInnerTypes();
1259+
// only inner types may be collapsed
1260+
collapse= ctx.collapseInnerTypes() && isInnerType((IType) element);
12601261
break;
12611262
case IJavaElement.METHOD:
12621263
case IJavaElement.FIELD:

0 commit comments

Comments
 (0)