diff --git a/bundles/org.eclipse.jface.text/META-INF/MANIFEST.MF b/bundles/org.eclipse.jface.text/META-INF/MANIFEST.MF index ef7567b50e6..d5048caec25 100644 --- a/bundles/org.eclipse.jface.text/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.jface.text/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jface.text -Bundle-Version: 3.31.100.qualifier +Bundle-Version: 3.32.0.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Export-Package: diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotation.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotation.java index b8d5fd55e78..e38784fa63c 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotation.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotation.java @@ -158,6 +158,27 @@ private void initializeImages(Display display) { } } + /** + * Whether this annotation should be included in the "Collapse All" action of the editor. + * + * @return {@code true} if the annotation participates in "Collapse All" actions + * @since 3.32 + */ + protected boolean includeInCollapseAll() { + return true; + } + + /** + * Whether this annotation should be included in the "Expand All" action of the editor. + * + * @return {@code true} if this annotation participates in "Expand All" actions + * @since 3.32 + */ + protected boolean includeInExpandAll() { + return true; + } + + /** * Returns the state of this annotation. * diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java index 0354964ef44..3aaaa0261d5 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java @@ -116,7 +116,7 @@ public boolean collapseAll(int offset, int length) { Iterator iterator= getAnnotationIterator(); while (iterator.hasNext()) { ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next(); - if (!annotation.isCollapsed()) { + if (!annotation.isCollapsed() && annotation.includeInCollapseAll()) { Position position= getPosition(annotation); if (position != null && position.overlapsWith(offset, length) /* || is a delete at the boundary */ ) { annotation.markCollapsed(); @@ -154,7 +154,7 @@ protected boolean expandAll(int offset, int length, boolean fireModelChanged) { Iterator iterator= getAnnotationIterator(); while (iterator.hasNext()) { ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next(); - if (annotation.isCollapsed()) { + if (annotation.isCollapsed() && annotation.includeInExpandAll()) { Position position= getPosition(annotation); if (position != null && position.overlapsWith(offset, length) /* || is a delete at the boundary */ ) { annotation.markExpanded(); diff --git a/tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/ProjectionViewerTest.java b/tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/ProjectionViewerTest.java index f76cc89eeeb..38e45642408 100644 --- a/tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/ProjectionViewerTest.java +++ b/tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/ProjectionViewerTest.java @@ -15,6 +15,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -22,9 +24,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.TextTransfer; -import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.jface.text.BadLocationException; @@ -77,19 +77,123 @@ public IRegion[] computeProjectionRegions(IDocument document) throws BadLocation public int computeCaptionOffset(IDocument document) throws BadLocationException { return document.get().indexOf('\n') + 1; } + } + + private Shell shell; + @BeforeEach + void setUp() { + shell= new Shell(); + } + + @AfterEach + void cleanUp() { + shell.dispose(); } @Test - public void testCopyPaste() { - Shell shell = new Shell(); - shell.setLayout(new FillLayout()); - ProjectionViewer viewer = new ProjectionViewer(shell, null, null, false, SWT.NONE); - Document document= new Document("/*\n * content\n */"); + void testCollapseAllProjectionAnnotations() { + String content= """ + before + hidden + after\ + """; + TestProjectionViewer viewer= setupWithProjections(content); + ProjectionAnnotation annotation= new ProjectionAnnotation(false); + int start= content.indexOf('\n'); + viewer.getProjectionAnnotationModel().addAnnotation(annotation, new Position(start, content.lastIndexOf('\n') + 1 - start)); + viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); + assertTrue(annotation.isCollapsed()); + assertEquals(""" + before + after\ + """, viewer.getVisibleDocument().get()); + } + + @Test + void testOptOutOfCollapseAllProjectionAnnotations() { + String content= """ + before + hidden + after\ + """; + TestProjectionViewer viewer= setupWithProjections(content); + ProjectionAnnotation annotation= new ProjectionAnnotation(false) { + @Override + protected boolean includeInCollapseAll() { + return false; + } + }; + int start= content.indexOf('\n'); + viewer.getProjectionAnnotationModel().addAnnotation(annotation, new Position(start, content.lastIndexOf('\n') + 1 - start)); + viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); + assertFalse(annotation.isCollapsed()); + assertEquals(""" + before + hidden + after\ + """, viewer.getVisibleDocument().get()); + } + + @Test + void testExpandAllProjectionAnnotations() { + String content= """ + before + hidden + after\ + """; + TestProjectionViewer viewer= setupWithProjections(content); + ProjectionAnnotation annotation= new ProjectionAnnotation(true); + int start= content.indexOf('\n'); + viewer.getProjectionAnnotationModel().addAnnotation(annotation, new Position(start, content.lastIndexOf('\n') + 1 - start)); + viewer.getTextOperationTarget().doOperation(ProjectionViewer.EXPAND_ALL); + assertFalse(annotation.isCollapsed()); + assertEquals(""" + before + hidden + after\ + """, viewer.getVisibleDocument().get()); + } + + @Test + void testOptOutOfExpandAllProjectionAnnotations() { + String content= """ + before + hidden + after\ + """; + TestProjectionViewer viewer= setupWithProjections(content); + ProjectionAnnotation annotation= new ProjectionAnnotation(false) { + @Override + protected boolean includeInExpandAll() { + return false; + } + }; + int start= content.indexOf('\n'); + viewer.getProjectionAnnotationModel().addAnnotation(annotation, new Position(start, content.lastIndexOf('\n') + 1 - start)); + viewer.getTextOperationTarget().doOperation(ProjectionViewer.EXPAND_ALL); + assertFalse(annotation.isCollapsed()); + assertEquals(""" + before + hidden + after\ + """, viewer.getVisibleDocument().get()); + } + + private TestProjectionViewer setupWithProjections(String content) { + TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); + Document document= new Document(content); viewer.setDocument(document, new AnnotationModel()); viewer.enableProjection(); + return viewer; + } + + @Test + public void testCopyPaste() { + ProjectionViewer viewer= setupWithProjections("/*\n * content\n */"); + IDocument document= viewer.getDocument(); + viewer.enableProjection(); viewer.getProjectionAnnotationModel().addAnnotation(new ProjectionAnnotation(false), new ProjectionPosition(document)); - shell.setVisible(true); viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); viewer.getTextOperationTarget().doOperation(ITextOperationTarget.SELECT_ALL); try { @@ -103,8 +207,6 @@ public void testCopyPaste() { @Test public void testVisibleRegionDoesNotChangeWithProjections() { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); ProjectionViewer viewer= new ProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -118,23 +220,16 @@ public void testVisibleRegionDoesNotChangeWithProjections() { viewer.setVisibleRegion(0, regionLength); viewer.enableProjection(); viewer.getProjectionAnnotationModel().addAnnotation(new ProjectionAnnotation(false), new ProjectionPosition(document)); - shell.setVisible(true); - try { - assertEquals(0, viewer.getVisibleRegion().getOffset()); - assertEquals(regionLength, viewer.getVisibleRegion().getLength()); + assertEquals(0, viewer.getVisibleRegion().getOffset()); + assertEquals(regionLength, viewer.getVisibleRegion().getLength()); - viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); - assertEquals(0, viewer.getVisibleRegion().getOffset()); - assertEquals(regionLength, viewer.getVisibleRegion().getLength()); - } finally { - shell.dispose(); - } + viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); + assertEquals(0, viewer.getVisibleRegion().getOffset()); + assertEquals(regionLength, viewer.getVisibleRegion().getLength()); } @Test public void testVisibleRegionProjectionCannotBeExpanded() { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -148,14 +243,9 @@ public void testVisibleRegionProjectionCannotBeExpanded() { int secondLineEnd= documentContent.indexOf('\n', secondLineStart); viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); viewer.enableProjection(); - shell.setVisible(true); - try { - assertEquals("World", viewer.getVisibleDocument().get()); - viewer.getTextOperationTarget().doOperation(ProjectionViewer.EXPAND_ALL); - assertEquals("World", viewer.getVisibleDocument().get()); - } finally { - shell.dispose(); - } + assertEquals("World", viewer.getVisibleDocument().get()); + viewer.getTextOperationTarget().doOperation(ProjectionViewer.EXPAND_ALL); + assertEquals("World", viewer.getVisibleDocument().get()); } @Test @@ -169,8 +259,6 @@ public void testEnableProjectionAddsProjectionAnnotationsIfVisibleRegionEnabled( } private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjectionFirst) { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -183,7 +271,6 @@ private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjection int secondLineStart= documentContent.indexOf("World"); int secondLineEnd= documentContent.indexOf('\n', secondLineStart); - shell.setVisible(true); if (enableProjectionFirst) { viewer.enableProjection(); viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); @@ -192,17 +279,11 @@ private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjection viewer.enableProjection(); } - try { - assertEquals("World", viewer.getVisibleDocument().get().trim()); - } finally { - shell.dispose(); - } + assertEquals("World", viewer.getVisibleDocument().get().trim()); } @Test public void testInsertIntoVisibleRegion() throws BadLocationException { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -215,20 +296,14 @@ public void testInsertIntoVisibleRegion() throws BadLocationException { int secondLineStart= documentContent.indexOf("World"); int secondLineEnd= documentContent.indexOf('\n', secondLineStart); - shell.setVisible(true); - - try { - viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); - viewer.enableProjection(); + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); + viewer.enableProjection(); - assertEquals("World", viewer.getVisibleDocument().get()); + assertEquals("World", viewer.getVisibleDocument().get()); - viewer.getDocument().replace(documentContent.indexOf("rld"), 0, "---"); + viewer.getDocument().replace(documentContent.indexOf("rld"), 0, "---"); - assertEquals("Wo---rld", viewer.getVisibleDocument().get()); - } finally { - shell.dispose(); - } + assertEquals("Wo---rld", viewer.getVisibleDocument().get()); } @Test @@ -243,8 +318,6 @@ public void testReplaceVisibleRegionEnd() throws BadLocationException { private void testReplaceVisibleRegionEnd(String toReplaceWith) throws BadLocationException { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -257,20 +330,14 @@ private void testReplaceVisibleRegionEnd(String toReplaceWith) throws BadLocatio int secondLineStart= documentContent.indexOf("World"); int secondLineEnd= documentContent.indexOf('\n', secondLineStart); - shell.setVisible(true); - - try { - viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); - viewer.enableProjection(); + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); + viewer.enableProjection(); - assertEquals("World", viewer.getVisibleDocument().get()); + assertEquals("World", viewer.getVisibleDocument().get()); - viewer.getDocument().replace(documentContent.indexOf("d\n1"), 3, toReplaceWith); + viewer.getDocument().replace(documentContent.indexOf("d\n1"), 3, toReplaceWith); - assertEquals("Worl" + toReplaceWith, viewer.getVisibleDocument().get()); - } finally { - shell.dispose(); - } + assertEquals("Worl" + toReplaceWith, viewer.getVisibleDocument().get()); } @Test @@ -285,8 +352,6 @@ public void testReplaceVisibleRegionStart() throws BadLocationException { private void testReplaceVisibleRegionStart(String toReplaceWith) throws BadLocationException { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -299,26 +364,18 @@ private void testReplaceVisibleRegionStart(String toReplaceWith) throws BadLocat int secondLineStart= documentContent.indexOf("World"); int secondLineEnd= documentContent.indexOf('\n', secondLineStart); - shell.setVisible(true); - - try { - viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); - viewer.enableProjection(); + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); + viewer.enableProjection(); - assertEquals("World", viewer.getVisibleDocument().get()); + assertEquals("World", viewer.getVisibleDocument().get()); - viewer.getDocument().replace(documentContent.indexOf("o\nW"), 3, toReplaceWith); + viewer.getDocument().replace(documentContent.indexOf("o\nW"), 3, toReplaceWith); - assertEquals(toReplaceWith + "orld", viewer.getVisibleDocument().get()); - } finally { - shell.dispose(); - } + assertEquals(toReplaceWith + "orld", viewer.getVisibleDocument().get()); } @Test public void testVisibleRegionEndsWithWhitespace() { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -332,28 +389,18 @@ public void testVisibleRegionEndsWithWhitespace() { int secondLineTextEnd= documentContent.indexOf('\n', secondLineStart); int secondLineEnd= documentContent.indexOf('\n', secondLineStart); - shell.setVisible(true); - - try { - viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); - viewer.enableProjection(); - - assertEquals("World\t\t", viewer.getVisibleDocument().get()); - - viewer.setVisibleRegion(secondLineStart, secondLineTextEnd - secondLineStart); + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); + viewer.enableProjection(); - assertEquals("World\t\t\n", viewer.getVisibleDocument().get()); + assertEquals("World\t\t", viewer.getVisibleDocument().get()); + viewer.setVisibleRegion(secondLineStart, secondLineTextEnd - secondLineStart); - } finally { - shell.dispose(); - } + assertEquals("World\t\t\n", viewer.getVisibleDocument().get()); } @Test public void testRemoveEntireVisibleRegion() throws BadLocationException { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -367,20 +414,13 @@ public void testRemoveEntireVisibleRegion() throws BadLocationException { int secondLineEnd= documentContent.indexOf('\n', secondLineStart); viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); viewer.enableProjection(); - shell.setVisible(true); - try { - document.replace(secondLineStart, secondLineEnd - secondLineStart, ""); - assertEquals("", viewer.getVisibleDocument().get()); - assertEquals(new Region(secondLineStart, 0), viewer.getVisibleRegion()); - } finally { - shell.dispose(); - } + document.replace(secondLineStart, secondLineEnd - secondLineStart, ""); + assertEquals("", viewer.getVisibleDocument().get()); + assertEquals(new Region(secondLineStart, 0), viewer.getVisibleRegion()); } @Test public void testSetVisibleRegionDoesNotExpandOutsideProjectionRegions() { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -399,19 +439,12 @@ public void testSetVisibleRegionDoesNotExpandOutsideProjectionRegions() { viewer.getProjectionAnnotationModel().addAnnotation(secondAnnotation, new Position(documentContent.indexOf("456"), documentContent.length() - documentContent.indexOf("456"))); viewer.setVisibleRegion(documentContent.indexOf("abc"), documentContent.indexOf("123") - documentContent.indexOf("abc")); - shell.setVisible(true); - try { - assertTrue(firstAnnotation.isCollapsed()); - assertTrue(secondAnnotation.isCollapsed()); - } finally { - shell.dispose(); - } + assertTrue(firstAnnotation.isCollapsed()); + assertTrue(secondAnnotation.isCollapsed()); } @Test public void testSetVisibleRegionExpandsBorderingProjectionRegions() { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -428,20 +461,13 @@ public void testSetVisibleRegionExpandsBorderingProjectionRegions() { viewer.getProjectionAnnotationModel().addAnnotation(secondAnnotation, new Position(documentContent.indexOf("123"), documentContent.length() - documentContent.indexOf("123"))); viewer.setVisibleRegion(documentContent.indexOf("World"), documentContent.indexOf("456") - documentContent.indexOf("World")); - shell.setVisible(true); - try { - assertFalse(firstAnnotation.isCollapsed()); - assertFalse(secondAnnotation.isCollapsed()); - } finally { - shell.dispose(); - } + assertFalse(firstAnnotation.isCollapsed()); + assertFalse(secondAnnotation.isCollapsed()); } @ParameterizedTest @CsvSource({ "true", "false" }) void testRegionEndingWithStartOfLine(boolean enableProjections) { - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ Hello @@ -456,19 +482,12 @@ void testRegionEndingWithStartOfLine(boolean enableProjections) { } viewer.setVisibleRegion(documentContent.indexOf("World"), documentContent.indexOf("123") - documentContent.indexOf("World")); - shell.setVisible(true); - try { - assertEquals("World\n", viewer.getVisibleDocument().get()); - } finally { - shell.dispose(); - } + assertEquals("World\n", viewer.getVisibleDocument().get()); } @Test public void testImageLineStateAfterSettingVisibleRegionsWithProjectionsSetMethodAndClass() throws BadLocationException { // https://github.com/eclipse-platform/eclipse.platform.ui/pull/3456 - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ public class TM { @@ -493,35 +512,28 @@ void c() { addAnnotationBetween(viewer, annotationToCollapse, "\tvoid b()", "\t}"); addAnnotationBetween(viewer, new ProjectionAnnotation(false), "\tvoid c()", "\t}"); - shell.setVisible(true); - try { - viewer.getProjectionAnnotationModel().collapse(annotationToCollapse); + viewer.getProjectionAnnotationModel().collapse(annotationToCollapse); - Position firstMethod= findPositionFromStartAndEndText(viewer, "\tvoid a()", "}"); - viewer.setVisibleRegion(firstMethod.getOffset(), firstMethod.getLength() + 1); - viewer.setVisibleRegion(documentContent.indexOf("class"), documentContent.length() - documentContent.indexOf("class")); + Position firstMethod= findPositionFromStartAndEndText(viewer, "\tvoid a()", "}"); + viewer.setVisibleRegion(firstMethod.getOffset(), firstMethod.getLength() + 1); + viewer.setVisibleRegion(documentContent.indexOf("class"), documentContent.length() - documentContent.indexOf("class")); - IDocumentInformationMapping mapping= ((ProjectionDocument) viewer.getVisibleDocument()).getDocumentInformationMapping(); - // toImageLine should not throw exceptions and yield the correct values - for (int i= 0; i < 5; i++) { - int imageLine= mapping.toImageLine(i);// should not throw exception - assertEquals(i, imageLine); - } - assertEquals(-1, mapping.toImageLine(6), "should still be collapsed"); - for (int i= 7; i < documentContent.split("\n").length; i++) { - int imageLine= mapping.toImageLine(i);// should not throw exception - assertEquals(i - 1, imageLine); - } - } finally { - shell.dispose(); + IDocumentInformationMapping mapping= ((ProjectionDocument) viewer.getVisibleDocument()).getDocumentInformationMapping(); + // toImageLine should not throw exceptions and yield the correct values + for (int i= 0; i < 5; i++) { + int imageLine= mapping.toImageLine(i);// should not throw exception + assertEquals(i, imageLine); + } + assertEquals(-1, mapping.toImageLine(6), "should still be collapsed"); + for (int i= 7; i < documentContent.split("\n").length; i++) { + int imageLine= mapping.toImageLine(i);// should not throw exception + assertEquals(i - 1, imageLine); } } @Test public void testImageLineStateAfterSettingVisibleRegionsWithProjectionsSetDifferentMethods() throws BadLocationException { // https://github.com/eclipse-platform/eclipse.platform.ui/pull/3456 - Shell shell= new Shell(); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); String documentContent= """ public class TM { @@ -546,36 +558,31 @@ void c() { addAnnotationBetween(viewer, annotationToCollapse, "\tvoid b()", "\t}"); addAnnotationBetween(viewer, new ProjectionAnnotation(false), "\tvoid c()", "\t}"); - shell.setVisible(true); - try { - viewer.getProjectionAnnotationModel().collapse(annotationToCollapse); - - Position firstMethod= findPositionFromStartAndEndText(viewer, "\tvoid a()", "}"); - viewer.setVisibleRegion(firstMethod.getOffset(), firstMethod.getLength() + 1); - Position secondMethod= findPositionFromStartAndEndText(viewer, "\tvoid b()", "}"); - viewer.setVisibleRegion(secondMethod.getOffset(), secondMethod.getLength() + 1); - - // the '}' is cut off because this test doesn't include it - assertEquals(""" - void b() { - // ... - } - """, viewer.getVisibleDocument().get()); - - IDocumentInformationMapping mapping= ((ProjectionDocument) viewer.getVisibleDocument()).getDocumentInformationMapping(); - - // there should be no image regions outside of the visible region - - assertEquals(0, mapping.toImageLine(5)); - assertEquals(1, mapping.toImageLine(6)); - assertEquals(2, mapping.toImageLine(7)); - for (int i= 0; i < documentContent.split("\n").length; i++) { - if (i < 5 || i > 7) { - assertEquals(-1, mapping.toImageLine(i)); - } + viewer.getProjectionAnnotationModel().collapse(annotationToCollapse); + + Position firstMethod= findPositionFromStartAndEndText(viewer, "\tvoid a()", "}"); + viewer.setVisibleRegion(firstMethod.getOffset(), firstMethod.getLength() + 1); + Position secondMethod= findPositionFromStartAndEndText(viewer, "\tvoid b()", "}"); + viewer.setVisibleRegion(secondMethod.getOffset(), secondMethod.getLength() + 1); + + // the '}' is cut off because this test doesn't include it + assertEquals(""" + void b() { + // ... + } + """, viewer.getVisibleDocument().get()); + + IDocumentInformationMapping mapping= ((ProjectionDocument) viewer.getVisibleDocument()).getDocumentInformationMapping(); + + // there should be no image regions outside of the visible region + + assertEquals(0, mapping.toImageLine(5)); + assertEquals(1, mapping.toImageLine(6)); + assertEquals(2, mapping.toImageLine(7)); + for (int i= 0; i < documentContent.split("\n").length; i++) { + if (i < 5 || i > 7) { + assertEquals(-1, mapping.toImageLine(i)); } - } finally { - shell.dispose(); } } @@ -594,8 +601,6 @@ private Position findPositionFromStartAndEndText(TestProjectionViewer viewer, St @Test public void testProjectionRegionsShownOnlyInVisibleRegion() { - Shell shell= new Shell(Display.getCurrent()); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL); String documentContent= """ @@ -611,25 +616,19 @@ public void testProjectionRegionsShownOnlyInVisibleRegion() { Document document= new Document(documentContent); viewer.setDocument(document, new AnnotationModel()); ProjectionAnnotation annotation= addVisibleRegionAndProjection(viewer, documentContent); - try { - assertEquals(""" - visible_region_start + assertEquals(""" + visible_region_start - projection_start + projection_start - visible_region_end - """, viewer.getVisibleDocument().get()); + visible_region_end + """, viewer.getVisibleDocument().get()); - annotation.paint(null, null, null); //should exit early and not throw NPE - } finally { - shell.dispose(); - } + annotation.paint(null, null, null); //should exit early and not throw NPE } @Test public void testProjectionRegionsShownWithinVisibleRegion() { - Shell shell= new Shell(Display.getCurrent()); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL); String documentContent= """ @@ -645,21 +644,17 @@ public void testProjectionRegionsShownWithinVisibleRegion() { Document document= new Document(documentContent); viewer.setDocument(document, new AnnotationModel()); ProjectionAnnotation annotation= addVisibleRegionAndProjection(viewer, documentContent); - try { - assertEquals(""" - visible_region_start + assertEquals(""" + visible_region_start - projection_start + projection_start - projection_end + projection_end - visible_region_end - """, viewer.getVisibleDocument().get()); + visible_region_end + """, viewer.getVisibleDocument().get()); - assertThrows(NullPointerException.class, () -> annotation.paint(null, null, null), "expected to run painting logic"); - } finally { - shell.dispose(); - } + assertThrows(NullPointerException.class, () -> annotation.paint(null, null, null), "expected to run painting logic"); } private ProjectionAnnotation addVisibleRegionAndProjection(TestProjectionViewer viewer, String documentContent) { @@ -679,8 +674,6 @@ private ProjectionAnnotation addVisibleRegionAndProjection(TestProjectionViewer @ParameterizedTest @CsvSource({ "true", "false" }) void testDifferentLineEndings(boolean crlf) { - Shell shell= new Shell(Display.getCurrent()); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL); String documentContent= """ // before @@ -703,8 +696,6 @@ void testDifferentLineEndings(boolean crlf) { @Test void testIncludesLastLineIfAdditionalTextPresent() { - Shell shell= new Shell(Display.getCurrent()); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL); String documentContent= """ // before @@ -724,8 +715,6 @@ void testIncludesLastLineIfAdditionalTextPresent() { @Test void testSetVisibleRegionUntilEOF() { - Shell shell= new Shell(Display.getCurrent()); - shell.setLayout(new FillLayout()); TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL); String documentContent= """ // before