Skip to content

Fix folding of 2 consecutive methods #1993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jdt.text.tests.codemining.ParameterNamesCodeMiningTest;
import org.eclipse.jdt.text.tests.contentassist.ContentAssistTestSuite;
import org.eclipse.jdt.text.tests.folding.FoldingTest;
import org.eclipse.jdt.text.tests.folding.FoldingTestNew;
import org.eclipse.jdt.text.tests.semantictokens.SemanticTokensProviderTest;
import org.eclipse.jdt.text.tests.spelling.SpellCheckEngineTestCase;
import org.eclipse.jdt.text.tests.templates.TemplatesTestSuite;
Expand Down Expand Up @@ -71,6 +72,7 @@
JavaElementPrefixPatternMatcherTest.class,
CodeMiningTriggerTest.class,
ParameterNamesCodeMiningTest.class,
FoldingTestNew.class,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully, you will not need another class and you can parameterize FoldingTest (see my comment in the review). In any case, the name FoldingNewTest would have been better. But please let's move this discussion to another PR.

FoldingTest.class,
})
public class JdtTextTestSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@

import org.eclipse.core.runtime.CoreException;

import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.jface.text.IRegion;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;

import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup;

import org.eclipse.jdt.internal.ui.JavaPlugin;

public class FoldingTest {
@Rule
public ProjectTestSetup projectSetup= new ProjectTestSetup();
Expand All @@ -55,9 +50,7 @@ public void setUp() throws CoreException {
sourceFolder= JavaProjectHelper.addSourceContainer(jProject, "src");
}
packageFragment= sourceFolder.createPackageFragment("org.example.test", false, null);
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
store.setValue(PreferenceConstants.EDITOR_NEW_FOLDING_ENABLED, true);
}
}

@After
public void tearDown() throws CoreException {
Expand Down Expand Up @@ -242,15 +235,14 @@ void b() { //here should be an annotation
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 6);
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 5);

List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 1, 3); // 1. Javadoc
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 4, 6); // 2. Javadoc
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 7, 9); // 3. Javadoc
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 12, 14); // 4. Javadoc
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 15, 19); // Methode b()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 16, 18); // 5. Javadoc
}

@Test
Expand Down Expand Up @@ -285,217 +277,20 @@ void a() {
}

@Test
public void testIfStatementFolding() throws Exception {
String str= """
package org.example.test;
public class D {
void x() { //here should be an annotation
if (true) { //here should be an annotation
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 4); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 3); // if
}

@Test
public void testTryStatementFolding() throws Exception {
public void testinnerClassinMethod() throws Exception {
String str= """
package org.example.test;
public class E {
void x() { //here should be an annotation
try { //here should be an annotation
class Outer {
void a() { //here should be an annotation
class Inner2{ //here should be an annotation

} catch (Exception e) { //here should be an annotation

}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 3);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 7); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); // try
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 5, 6); // catch
}

@Test
public void testWhileStatementFolding() throws Exception {
String str= """
package org.example.test;
public class F {
void x() { //here should be an annotation
while (true) { //here should be an annotation
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 4); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 3); // while
}

@Test
public void testForStatementFolding() throws Exception {
String str= """
package org.example.test;
public class G {
void x() { //here should be an annotation
for(int i=0;i<1;i++){ //here should be an annotation
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 4); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 3); // for
}

@Test
public void testEnhancedForStatementFolding() throws Exception {
String str= """
package org.example.test;
public class H {
void x() { //here should be an annotation
for(String s: new String[0]){ //here should be an annotation
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 4); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 3); // for
}

@Test
public void testDoStatementFolding() throws Exception {
String str= """
package org.example.test;
public class I {
void x() { //here should be an annotation
do { //here should be an annotation

} while(false);
}
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 5); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); // do
}

@Test
public void testSynchronizedStatementFolding() throws Exception {
String str= """
package org.example.test;
public class K {
void x() { //here should be an annotation
synchronized(this) { //here should be an annotation
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 4); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 3); // synchronized
}

@Test
public void testLambdaExpressionFolding() throws Exception {
String str= """
package org.example.test;
import java.util.function.Supplier;
public class L {
void x() { //here should be an annotation
Supplier<String> s = () -> { //here should be an annotation
return "";
};
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 6); // 1. Method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 4, 5); // Supplier
}

@Test
public void testAnonymousClassDeclarationFolding() throws Exception {
String str= """
package org.example.test;
public class M {
Object o = new Object(){ //here should be an annotation
void y() { //here should be an annotation

}
};
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 2);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 5); // Object
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); // Method
}

@Test
public void testEnumDeclarationFolding() throws Exception {
String str= """
package org.example.test;
public enum N { //here should be an annotation
A,
B
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 1);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 1, 3); // enum
}

@Test
public void testInitializerFolding() throws Exception {
String str= """
package org.example.test;
public class O {
static { //here should be an annotation
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 1);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 2); // static
}

@Test
public void testNestedFolding() throws Exception {
String str= """
package org.example.test;
public class P {
void x() { //here should be an annotation
if (true) { //here should be an annotation
for(int i=0;i<1;i++){ //here should be an annotation
while(true) { //here should be an annotation
do { //here should be an annotation
} while(false);
}
}
}
}
}
""";
FoldingTestUtils.assertCodeHasRegions(packageFragment, "TestFolding.java", str, 5);
List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "TestFolding.java", str);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 10); // method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 9); // if
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 4, 8); // for
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 5, 7); // while
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 6, 6); // do
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 5); // method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); // inner class
}
}
Loading
Loading