Skip to content

Commit

Permalink
add custom folding regions
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Feb 1, 2025
1 parent fbdc89d commit 83afaa3
Show file tree
Hide file tree
Showing 11 changed files with 963 additions and 53 deletions.
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,189 @@
/*******************************************************************************
* 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
}

}
Loading

0 comments on commit 83afaa3

Please sign in to comment.