Skip to content

Commit bb4f64a

Browse files
committed
add more tests, prevent collapsing of invisible folding regions
1 parent f721012 commit bb4f64a

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionViewer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ private void expandAll() {
582582
IDocument doc= getDocument();
583583
int length= doc == null ? 0 : doc.getLength();
584584
if (isProjectionMode()) {
585+
if (fVisibleRegionDuringProjection != null) {
586+
offset= fVisibleRegionDuringProjection.getOffset();
587+
length= fVisibleRegionDuringProjection.getLength();
588+
}
585589
fProjectionAnnotationModel.expandAll(offset, length);
586590
}
587591
}

tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/ProjectionViewerTest.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.swt.dnd.Clipboard;
2222
import org.eclipse.swt.dnd.TextTransfer;
2323
import org.eclipse.swt.layout.FillLayout;
24+
import org.eclipse.swt.widgets.Composite;
2425
import org.eclipse.swt.widgets.Shell;
2526

2627
import org.eclipse.jface.text.BadLocationException;
@@ -33,12 +34,28 @@
3334
import org.eclipse.jface.text.Region;
3435
import org.eclipse.jface.text.source.Annotation;
3536
import org.eclipse.jface.text.source.AnnotationModel;
37+
import org.eclipse.jface.text.source.IOverviewRuler;
38+
import org.eclipse.jface.text.source.IVerticalRuler;
3639
import org.eclipse.jface.text.source.projection.IProjectionPosition;
3740
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
3841
import org.eclipse.jface.text.source.projection.ProjectionViewer;
3942

4043
public class ProjectionViewerTest {
4144

45+
/**
46+
* A {@link ProjectionViewer} that provides access to {@link #getVisibleDocument()}.
47+
*/
48+
private final class TestProjectionViewer extends ProjectionViewer {
49+
private TestProjectionViewer(Composite parent, IVerticalRuler ruler, IOverviewRuler overviewRuler, boolean showsAnnotationOverview, int styles) {
50+
super(parent, ruler, overviewRuler, showsAnnotationOverview, styles);
51+
}
52+
53+
@Override
54+
public IDocument getVisibleDocument() {
55+
return super.getVisibleDocument();
56+
}
57+
}
58+
4259
private static final class ProjectionPosition extends Position implements IProjectionPosition {
4360

4461
public ProjectionPosition(IDocument document) {
@@ -110,6 +127,33 @@ public void testVisibleRegionDoesNotChangeWithProjections() {
110127
}
111128
}
112129

130+
@Test
131+
public void testVisibleRegionProjectionCannotBeExpanded() {
132+
Shell shell= new Shell();
133+
shell.setLayout(new FillLayout());
134+
TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE);
135+
String documentContent= """
136+
Hello
137+
World
138+
123
139+
456
140+
""";
141+
Document document= new Document(documentContent);
142+
viewer.setDocument(document, new AnnotationModel());
143+
int secondLineStart= documentContent.indexOf("World");
144+
int secondLineEnd= documentContent.indexOf('\n', secondLineStart);
145+
viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart);
146+
viewer.enableProjection();
147+
shell.setVisible(true);
148+
try {
149+
assertEquals("World", viewer.getVisibleDocument().get());
150+
viewer.getTextOperationTarget().doOperation(ProjectionViewer.EXPAND_ALL);
151+
assertEquals("World", viewer.getVisibleDocument().get());
152+
} finally {
153+
shell.dispose();
154+
}
155+
}
156+
113157
@Test
114158
public void testVisibleRegionAddsProjectionAnnotationsIfProjectionsEnabled() {
115159
testProjectionAnnotationsFromVisibleRegion(true);
@@ -123,7 +167,7 @@ public void testEnableProjectionAddsProjectionAnnotationsIfVisibleRegionEnabled(
123167
private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjectionFirst) {
124168
Shell shell= new Shell();
125169
shell.setLayout(new FillLayout());
126-
ProjectionViewer viewer= new ProjectionViewer(shell, null, null, false, SWT.NONE);
170+
TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE);
127171
String documentContent= """
128172
Hello
129173
World
@@ -149,6 +193,8 @@ private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjection
149193
int annotationCount= 0;
150194

151195
try {
196+
assertEquals("World", viewer.getVisibleDocument().get().trim());
197+
152198
for (Iterator<Annotation> it= viewer.getProjectionAnnotationModel().getAnnotationIterator(); it.hasNext();) {
153199
Annotation annotation= it.next();
154200
assertEquals("org.eclipse.jface.text.source.projection.ProjectionViewer.InvisibleCollapsedProjectionAnnotation", annotation.getClass().getCanonicalName());
@@ -172,4 +218,32 @@ private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjection
172218
shell.dispose();
173219
}
174220
}
221+
222+
@Test
223+
public void testInsertIntoVisibleRegion() throws BadLocationException {
224+
Shell shell= new Shell();
225+
shell.setLayout(new FillLayout());
226+
TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE);
227+
String documentContent= """
228+
Hello
229+
World
230+
123
231+
456
232+
""";
233+
Document document= new Document(documentContent);
234+
viewer.setDocument(document, new AnnotationModel());
235+
int secondLineStart= documentContent.indexOf("World");
236+
int secondLineEnd= documentContent.indexOf('\n', secondLineStart);
237+
238+
shell.setVisible(true);
239+
240+
viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart);
241+
viewer.enableProjection();
242+
243+
assertEquals("World", viewer.getVisibleDocument().get());
244+
245+
viewer.getDocument().replace(documentContent.indexOf("rld"), 0, "---");
246+
247+
assertEquals("Wo---rld", viewer.getVisibleDocument().get());
248+
}
175249
}

0 commit comments

Comments
 (0)