|  | 
|  | 1 | +/******************************************************************************* | 
|  | 2 | + * Copyright (c) 2025 Daniel Schmid and others. | 
|  | 3 | + * | 
|  | 4 | + * This program and the accompanying materials | 
|  | 5 | + * are made available under the terms of the Eclipse Public License 2.0 | 
|  | 6 | + * which accompanies this distribution, and is available at | 
|  | 7 | + * https://www.eclipse.org/legal/epl-2.0/ | 
|  | 8 | + * | 
|  | 9 | + * SPDX-License-Identifier: EPL-2.0 | 
|  | 10 | + * | 
|  | 11 | + * Contributors: | 
|  | 12 | + *     Daniel Schmid - initial API and implementation | 
|  | 13 | + *******************************************************************************/ | 
|  | 14 | +package org.eclipse.jdt.text.tests.folding; | 
|  | 15 | + | 
|  | 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 17 | + | 
|  | 18 | +import java.util.List; | 
|  | 19 | + | 
|  | 20 | +import org.junit.After; | 
|  | 21 | +import org.junit.Before; | 
|  | 22 | +import org.junit.Rule; | 
|  | 23 | +import org.junit.Test; | 
|  | 24 | + | 
|  | 25 | +import org.eclipse.jdt.testplugin.JavaProjectHelper; | 
|  | 26 | + | 
|  | 27 | +import org.eclipse.core.runtime.CoreException; | 
|  | 28 | + | 
|  | 29 | +import org.eclipse.jface.preference.IPreferenceStore; | 
|  | 30 | + | 
|  | 31 | +import org.eclipse.jface.text.IDocument; | 
|  | 32 | +import org.eclipse.jface.text.IRegion; | 
|  | 33 | +import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; | 
|  | 34 | + | 
|  | 35 | +import org.eclipse.jdt.core.ICompilationUnit; | 
|  | 36 | +import org.eclipse.jdt.core.IJavaProject; | 
|  | 37 | +import org.eclipse.jdt.core.IPackageFragment; | 
|  | 38 | +import org.eclipse.jdt.core.IPackageFragmentRoot; | 
|  | 39 | + | 
|  | 40 | +import org.eclipse.jdt.ui.PreferenceConstants; | 
|  | 41 | +import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup; | 
|  | 42 | + | 
|  | 43 | +import org.eclipse.jdt.internal.ui.JavaPlugin; | 
|  | 44 | +import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; | 
|  | 45 | +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; | 
|  | 46 | + | 
|  | 47 | +public class FoldingWithShowSelectedElementTests { | 
|  | 48 | +	@Rule | 
|  | 49 | +	public ProjectTestSetup projectSetup= new ProjectTestSetup(); | 
|  | 50 | + | 
|  | 51 | +	private IJavaProject jProject; | 
|  | 52 | + | 
|  | 53 | +	private IPackageFragmentRoot sourceFolder; | 
|  | 54 | + | 
|  | 55 | +	private IPackageFragment packageFragment; | 
|  | 56 | + | 
|  | 57 | +	@Before | 
|  | 58 | +	public void setUp() throws CoreException { | 
|  | 59 | +		jProject= projectSetup.getProject(); | 
|  | 60 | +		sourceFolder= jProject.findPackageFragmentRoot(jProject.getResource().getFullPath().append("src")); | 
|  | 61 | +		if (sourceFolder == null) { | 
|  | 62 | +			sourceFolder= JavaProjectHelper.addSourceContainer(jProject, "src"); | 
|  | 63 | +		} | 
|  | 64 | +		packageFragment= sourceFolder.createPackageFragment("org.example.test", false, null); | 
|  | 65 | +		IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); | 
|  | 66 | +		store.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, true); | 
|  | 67 | +	} | 
|  | 68 | + | 
|  | 69 | +	@After | 
|  | 70 | +	public void tearDown() throws CoreException { | 
|  | 71 | +		JavaProjectHelper.delete(jProject); | 
|  | 72 | +		JavaPlugin.getDefault().getPreferenceStore().setToDefault(PreferenceConstants.EDITOR_SHOW_SEGMENTS); | 
|  | 73 | +		JavaPlugin.getDefault().getPreferenceStore().setToDefault(PreferenceConstants.EDITOR_FOLDING_CUSTOM_REGIONS_ENABLED); | 
|  | 74 | +	} | 
|  | 75 | + | 
|  | 76 | +	@Test | 
|  | 77 | +	public void testFoldingActive() throws Exception { | 
|  | 78 | +		String str= """ | 
|  | 79 | +				package org.example.test; | 
|  | 80 | +				public class A { | 
|  | 81 | +					void someMethod() { | 
|  | 82 | +						// this method should be folded | 
|  | 83 | +					} | 
|  | 84 | +				} | 
|  | 85 | +				"""; | 
|  | 86 | +		List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "B.java", str); | 
|  | 87 | +		assertEquals(1, regions.size()); | 
|  | 88 | +		FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 3); | 
|  | 89 | +	} | 
|  | 90 | + | 
|  | 91 | +	@Test | 
|  | 92 | +	public void testInsertText() throws Exception { | 
|  | 93 | +		JavaPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.EDITOR_FOLDING_CUSTOM_REGIONS_ENABLED, true); | 
|  | 94 | +		String str= """ | 
|  | 95 | +				package org.example.test; | 
|  | 96 | +				public class A { | 
|  | 97 | +					// region | 
|  | 98 | +					void someMethod() { | 
|  | 99 | +						// content here | 
|  | 100 | +					} | 
|  | 101 | +					// endregion | 
|  | 102 | +				} | 
|  | 103 | +				"""; | 
|  | 104 | + | 
|  | 105 | +		ICompilationUnit cu= packageFragment.createCompilationUnit("A.java", str, true, null); | 
|  | 106 | +		JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(cu); | 
|  | 107 | + | 
|  | 108 | +		try { | 
|  | 109 | +			editor.setSelection(cu.getElementAt(str.indexOf("someMethod"))); | 
|  | 110 | + | 
|  | 111 | +			ProjectionAnnotationModel model= editor.getAdapter(ProjectionAnnotationModel.class); | 
|  | 112 | + | 
|  | 113 | +			assertEquals(""" | 
|  | 114 | +						// region | 
|  | 115 | +						void someMethod() { | 
|  | 116 | +							// content here | 
|  | 117 | +						} | 
|  | 118 | +					""", editor.getViewer().getTextWidget().getText()); | 
|  | 119 | + | 
|  | 120 | +			List<IRegion> regions= FoldingTestUtils.extractRegions(model); | 
|  | 121 | + | 
|  | 122 | +			assertEquals(2, regions.size()); | 
|  | 123 | + | 
|  | 124 | +			IDocument document= editor.getViewer().getDocument(); | 
|  | 125 | +			FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 6); | 
|  | 126 | +			FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); | 
|  | 127 | + | 
|  | 128 | +			document.replace(str.indexOf("content"), 0, "method "); | 
|  | 129 | + | 
|  | 130 | +			regions = FoldingTestUtils.extractRegions(model); | 
|  | 131 | +			str = document.get(); | 
|  | 132 | + | 
|  | 133 | +			assertEquals(2, regions.size()); | 
|  | 134 | + | 
|  | 135 | +			FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 6); | 
|  | 136 | +			FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); | 
|  | 137 | +		} finally { | 
|  | 138 | +			editor.close(false); | 
|  | 139 | +		} | 
|  | 140 | +	} | 
|  | 141 | + | 
|  | 142 | +	@Test | 
|  | 143 | +	public void testWithTrailingWhitespaceAfterClosingBrace() throws Exception { | 
|  | 144 | +		JavaPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.EDITOR_FOLDING_CUSTOM_REGIONS_ENABLED, true); | 
|  | 145 | +		String str= """ | 
|  | 146 | +				package org.example.test; | 
|  | 147 | +				public class A { | 
|  | 148 | +					void someMethod() { | 
|  | 149 | +						// content here | 
|  | 150 | +					}\t\t\t\t | 
|  | 151 | +				} | 
|  | 152 | +				"""; | 
|  | 153 | + | 
|  | 154 | +		ICompilationUnit cu= packageFragment.createCompilationUnit("A.java", str, true, null); | 
|  | 155 | +		JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(cu); | 
|  | 156 | + | 
|  | 157 | +		try { | 
|  | 158 | +			editor.setSelection(cu.getElementAt(str.indexOf("someMethod"))); | 
|  | 159 | + | 
|  | 160 | +			assertEquals(""" | 
|  | 161 | +						void someMethod() { | 
|  | 162 | +							// content here | 
|  | 163 | +						}\t\t\t\t | 
|  | 164 | +					""", editor.getViewer().getTextWidget().getText()); | 
|  | 165 | + | 
|  | 166 | +		} finally { | 
|  | 167 | +			editor.close(false); | 
|  | 168 | +		} | 
|  | 169 | +	} | 
|  | 170 | +} | 
0 commit comments