|
| 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.ArrayList; |
| 19 | +import java.util.Iterator; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.eclipse.jdt.testplugin.JavaProjectHelper; |
| 28 | + |
| 29 | +import org.eclipse.core.runtime.CoreException; |
| 30 | + |
| 31 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 32 | + |
| 33 | +import org.eclipse.jface.text.IDocument; |
| 34 | +import org.eclipse.jface.text.IRegion; |
| 35 | +import org.eclipse.jface.text.Position; |
| 36 | +import org.eclipse.jface.text.Region; |
| 37 | +import org.eclipse.jface.text.source.Annotation; |
| 38 | +import org.eclipse.jface.text.source.projection.ProjectionAnnotation; |
| 39 | +import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; |
| 40 | + |
| 41 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 42 | +import org.eclipse.jdt.core.IJavaProject; |
| 43 | +import org.eclipse.jdt.core.IPackageFragment; |
| 44 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 45 | + |
| 46 | +import org.eclipse.jdt.ui.PreferenceConstants; |
| 47 | +import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup; |
| 48 | + |
| 49 | +import org.eclipse.jdt.internal.ui.JavaPlugin; |
| 50 | +import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; |
| 51 | +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; |
| 52 | + |
| 53 | +public class FoldingWithShowSelectedElementTests { |
| 54 | + @Rule |
| 55 | + public ProjectTestSetup projectSetup= new ProjectTestSetup(); |
| 56 | + |
| 57 | + private IJavaProject jProject; |
| 58 | + |
| 59 | + private IPackageFragmentRoot sourceFolder; |
| 60 | + |
| 61 | + private IPackageFragment packageFragment; |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws CoreException { |
| 65 | + jProject= projectSetup.getProject(); |
| 66 | + sourceFolder= jProject.findPackageFragmentRoot(jProject.getResource().getFullPath().append("src")); |
| 67 | + if (sourceFolder == null) { |
| 68 | + sourceFolder= JavaProjectHelper.addSourceContainer(jProject, "src"); |
| 69 | + } |
| 70 | + packageFragment= sourceFolder.createPackageFragment("org.example.test", false, null); |
| 71 | + IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); |
| 72 | + store.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, true); |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + public void tearDown() throws CoreException { |
| 77 | + JavaProjectHelper.delete(jProject); |
| 78 | + JavaPlugin.getDefault().getPreferenceStore().setToDefault(PreferenceConstants.EDITOR_SHOW_SEGMENTS); |
| 79 | + JavaPlugin.getDefault().getPreferenceStore().setToDefault(PreferenceConstants.EDITOR_FOLDING_CUSTOM_REGIONS_ENABLED); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testFoldingActive() throws Exception { |
| 84 | + String str= """ |
| 85 | + package org.example.test; |
| 86 | + public class A { |
| 87 | + void someMethod() { |
| 88 | + // this method should be folded |
| 89 | + } |
| 90 | + } |
| 91 | + """; |
| 92 | + List<IRegion> regions= FoldingTestUtils.getProjectionRangesOfFile(packageFragment, "B.java", str); |
| 93 | + assertEquals(1, regions.size()); |
| 94 | + FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 3); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testInsertText() throws Exception { |
| 99 | + JavaPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.EDITOR_FOLDING_CUSTOM_REGIONS_ENABLED, true); |
| 100 | + String str= """ |
| 101 | + package org.example.test; |
| 102 | + public class A { |
| 103 | + // region |
| 104 | + void someMethod() { |
| 105 | + // content here |
| 106 | + } |
| 107 | + // endregion |
| 108 | + } |
| 109 | + """; |
| 110 | + |
| 111 | + ICompilationUnit cu= packageFragment.createCompilationUnit("A.java", str, true, null); |
| 112 | + JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(cu); |
| 113 | + |
| 114 | + try { |
| 115 | + editor.setSelection(cu.getElementAt(str.indexOf("someMethod"))); |
| 116 | + |
| 117 | + ProjectionAnnotationModel model= editor.getAdapter(ProjectionAnnotationModel.class); |
| 118 | + |
| 119 | + assertEquals(""" |
| 120 | + // region |
| 121 | + void someMethod() { |
| 122 | + // content here |
| 123 | + } |
| 124 | + """, editor.getViewer().getTextWidget().getText()); |
| 125 | + |
| 126 | + List<IRegion> regions= extractRegions(model); |
| 127 | + |
| 128 | + assertEquals(2, regions.size()); |
| 129 | + |
| 130 | + IDocument document= editor.getViewer().getDocument(); |
| 131 | + FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 6); |
| 132 | + FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); |
| 133 | + |
| 134 | + document.replace(str.indexOf("content"), 0, "method "); |
| 135 | + |
| 136 | + regions = extractRegions(model); |
| 137 | + str = document.get(); |
| 138 | + |
| 139 | + assertEquals(2, regions.size()); |
| 140 | + |
| 141 | + FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 2, 6); |
| 142 | + FoldingTestUtils.assertContainsRegionUsingStartAndEndLine(regions, str, 3, 4); |
| 143 | + } finally { |
| 144 | + editor.close(false); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testWithTrailingWhitespaceAfterClosingBrace() throws Exception { |
| 150 | + JavaPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.EDITOR_FOLDING_CUSTOM_REGIONS_ENABLED, true); |
| 151 | + String str= """ |
| 152 | + package org.example.test; |
| 153 | + public class A { |
| 154 | + void someMethod() { |
| 155 | + // content here |
| 156 | + }\t\t\t\t |
| 157 | + } |
| 158 | + """; |
| 159 | + |
| 160 | + ICompilationUnit cu= packageFragment.createCompilationUnit("A.java", str, true, null); |
| 161 | + JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(cu); |
| 162 | + |
| 163 | + try { |
| 164 | + editor.setSelection(cu.getElementAt(str.indexOf("someMethod"))); |
| 165 | + |
| 166 | + assertEquals(""" |
| 167 | + void someMethod() { |
| 168 | + // content here |
| 169 | + }\t\t\t\t |
| 170 | + """, editor.getViewer().getTextWidget().getText()); |
| 171 | + |
| 172 | + } finally { |
| 173 | + editor.close(false); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private List<IRegion> extractRegions(ProjectionAnnotationModel model) { |
| 178 | + List<IRegion> regions= new ArrayList<>(); |
| 179 | + Iterator<Annotation> it= model.getAnnotationIterator(); |
| 180 | + while (it.hasNext()) { |
| 181 | + Annotation a= it.next(); |
| 182 | + if (a instanceof ProjectionAnnotation) { |
| 183 | + Position p= model.getPosition(a); |
| 184 | + regions.add(new Region(p.getOffset(), p.getLength())); |
| 185 | + } |
| 186 | + } |
| 187 | + return regions; |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments