Skip to content
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

custom folding regions #1825

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -20,7 +20,7 @@
import org.eclipse.jdt.text.tests.codemining.CodeMiningTriggerTest;
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.FoldingTestSuite;
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,7 +71,7 @@
JavaElementPrefixPatternMatcherTest.class,
CodeMiningTriggerTest.class,
ParameterNamesCodeMiningTest.class,
FoldingTest.class,
FoldingTestSuite.class,
})
public class JdtTextTestSuite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*******************************************************************************
Copy link
Author

Choose a reason for hiding this comment

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

This test class runs all tests from testCommentsInEmptyBlocks with the new folding introduced in #1562.
The test methods that yield different results (due to the folding being different in some ways, for example #1992) are overridden.

* Copyright (c) 2025 Daniel Schmid and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Daniel Schmid - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.text.tests.folding;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.jface.text.IRegion;

import org.eclipse.jdt.ui.PreferenceConstants;

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

public class CustomFoldingRegionNewFoldingTest extends CustomFoldingRegionTest {

@Before
@Override
public void setUp() throws CoreException {
super.setUp();
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
store.setValue(PreferenceConstants.EDITOR_NEW_FOLDING_ENABLED, true);
}


@After
@Override
public void tearDown() throws CoreException {
super.tearDown();
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
store.setToDefault(PreferenceConstants.EDITOR_NEW_FOLDING_ENABLED);
}

@Test
@Override
public void testCustomFoldingRegionsInMethod() throws Exception {
String str= """
package org.example.test;
public class Test {
void a(){
// region

// endregion
}
}
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(2, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 2, 5);
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 3, 5);
}

@Test
@Override
public void testCustomFoldingRegionsMultipleLevels() throws Exception {
String str= """
package org.example.test;
// region outside class
public class Test {
// region outside method
void a(){
// endregion should be ignored
// region inside method
System.out.println("Hello World");
// endregion inside method
}
// endregion outside method
}
// endregion outside class
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(4, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 1, 12);//outside class
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 3, 10);//outside method
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 4, 8);//void a()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 6, 8);//inside method
}

@Test
@Override
public void testCustomFoldingRegionsUsingSpecialCommentTypes() throws Exception {
String str= """
package org.example.test;

public class Test {
void a(){
/* region multiline
*/
/** region javadoc */
/** endregion javadoc */
/* endregion multiline
*/
}
}
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(5, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 3, 9);//void a()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 4, 5);// multiline (comment)
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 4, 9);// multiline (folding region)
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 6, 7);// javadoc
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 8, 9);// multiline (last comment)
}

@Test
@Override
public void testCustomRegionsWithLocalClass() throws Exception {
String str= """
package org.example.test;

public class Test {
void a(){
// region
int i;

// endregion
class Inner{

}
}
}
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(2, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 3, 10);//void a()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 4, 7);//region
}

@Test
@Override
public void testCustomRegionsAroundFieldAndMethod() throws Exception {
String str= """
package org.example.test;

public class Test {
// region
int a;

void b(){

}
// endregion
}
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(2, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 3, 9);//region
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 6, 7);//void b()
}

@Test
@Override
public void testCommentsInEmptyBlocks() throws Exception {
String str= """
package org.example.test;
public class Test {
void a(){
{/* region 1*/}
System.out.println("Hello World");
{/* endregion 1*/}
}
}
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(2, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 2, 5);//void a()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 3, 5);//region 1
}

@Test
public void testNotFoldedWithinDifferentControlFlowStatements() throws Exception {
String str= """
package org.example.test;
public class Test {
void a() {
// region
for (int i = 0; i < 10; i++) {
// endregion
// region
}
boolean b=false;
// region
while (b) {
// endregion
}
}
}
""";
List<IRegion> projectionRanges= getProjectionRangesOfFile(str);
assertEquals(3, projectionRanges.size());
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 2, 12);//void a()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 4, 6);//void a()
FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(projectionRanges, str, 10, 11);//void a()
}

}
Loading