|
11 | 11 | package org.eclipse.jface.text.tests;
|
12 | 12 |
|
13 | 13 | import static org.junit.Assert.assertEquals;
|
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | + |
| 16 | +import java.util.Iterator; |
14 | 17 |
|
15 | 18 | import org.junit.Test;
|
16 | 19 |
|
17 | 20 | import org.eclipse.swt.SWT;
|
18 | 21 | import org.eclipse.swt.dnd.Clipboard;
|
19 | 22 | import org.eclipse.swt.dnd.TextTransfer;
|
20 | 23 | import org.eclipse.swt.layout.FillLayout;
|
| 24 | +import org.eclipse.swt.widgets.Composite; |
21 | 25 | import org.eclipse.swt.widgets.Shell;
|
22 | 26 |
|
23 | 27 | import org.eclipse.jface.text.BadLocationException;
|
|
28 | 32 | import org.eclipse.jface.text.ITextSelection;
|
29 | 33 | import org.eclipse.jface.text.Position;
|
30 | 34 | import org.eclipse.jface.text.Region;
|
| 35 | +import org.eclipse.jface.text.source.Annotation; |
31 | 36 | import org.eclipse.jface.text.source.AnnotationModel;
|
| 37 | +import org.eclipse.jface.text.source.IOverviewRuler; |
| 38 | +import org.eclipse.jface.text.source.IVerticalRuler; |
32 | 39 | import org.eclipse.jface.text.source.projection.IProjectionPosition;
|
33 | 40 | import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
|
34 | 41 | import org.eclipse.jface.text.source.projection.ProjectionViewer;
|
35 | 42 |
|
36 | 43 | public class ProjectionViewerTest {
|
37 | 44 |
|
| 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 | + |
38 | 59 | private static final class ProjectionPosition extends Position implements IProjectionPosition {
|
39 | 60 |
|
40 | 61 | public ProjectionPosition(IDocument document) {
|
@@ -75,4 +96,154 @@ public void testCopyPaste() {
|
75 | 96 | shell.dispose();
|
76 | 97 | }
|
77 | 98 | }
|
| 99 | + |
| 100 | + @Test |
| 101 | + public void testVisibleRegionDoesNotChangeWithProjections() { |
| 102 | + Shell shell= new Shell(); |
| 103 | + shell.setLayout(new FillLayout()); |
| 104 | + ProjectionViewer viewer= new ProjectionViewer(shell, null, null, false, SWT.NONE); |
| 105 | + String documentContent= """ |
| 106 | + Hello |
| 107 | + World |
| 108 | + 123 |
| 109 | + 456 |
| 110 | + """; |
| 111 | + Document document= new Document(documentContent); |
| 112 | + viewer.setDocument(document, new AnnotationModel()); |
| 113 | + int regionLength= documentContent.indexOf('\n'); |
| 114 | + viewer.setVisibleRegion(0, regionLength); |
| 115 | + viewer.enableProjection(); |
| 116 | + viewer.getProjectionAnnotationModel().addAnnotation(new ProjectionAnnotation(false), new ProjectionPosition(document)); |
| 117 | + shell.setVisible(true); |
| 118 | + try { |
| 119 | + assertEquals(0, viewer.getVisibleRegion().getOffset()); |
| 120 | + assertEquals(regionLength, viewer.getVisibleRegion().getLength()); |
| 121 | + |
| 122 | + viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); |
| 123 | + assertEquals(0, viewer.getVisibleRegion().getOffset()); |
| 124 | + assertEquals(regionLength, viewer.getVisibleRegion().getLength()); |
| 125 | + } finally { |
| 126 | + shell.dispose(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 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 | + |
| 157 | + @Test |
| 158 | + public void testVisibleRegionAddsProjectionAnnotationsIfProjectionsEnabled() { |
| 159 | + testProjectionAnnotationsFromVisibleRegion(true); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testEnableProjectionAddsProjectionAnnotationsIfVisibleRegionEnabled() { |
| 164 | + testProjectionAnnotationsFromVisibleRegion(false); |
| 165 | + } |
| 166 | + |
| 167 | + private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjectionFirst) { |
| 168 | + Shell shell= new Shell(); |
| 169 | + shell.setLayout(new FillLayout()); |
| 170 | + TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); |
| 171 | + String documentContent= """ |
| 172 | + Hello |
| 173 | + World |
| 174 | + 123 |
| 175 | + 456 |
| 176 | + """; |
| 177 | + Document document= new Document(documentContent); |
| 178 | + viewer.setDocument(document, new AnnotationModel()); |
| 179 | + int secondLineStart= documentContent.indexOf("World"); |
| 180 | + int secondLineEnd= documentContent.indexOf('\n', secondLineStart); |
| 181 | + |
| 182 | + shell.setVisible(true); |
| 183 | + if (enableProjectionFirst) { |
| 184 | + viewer.enableProjection(); |
| 185 | + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); |
| 186 | + } else { |
| 187 | + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); |
| 188 | + viewer.enableProjection(); |
| 189 | + } |
| 190 | + |
| 191 | + boolean startAnnotationFound= false; |
| 192 | + boolean endAnnotationFound= false; |
| 193 | + int annotationCount= 0; |
| 194 | + |
| 195 | + try { |
| 196 | + assertEquals("World", viewer.getVisibleDocument().get().trim()); |
| 197 | + |
| 198 | + for (Iterator<Annotation> it= viewer.getProjectionAnnotationModel().getAnnotationIterator(); it.hasNext();) { |
| 199 | + Annotation annotation= it.next(); |
| 200 | + assertEquals("org.eclipse.jface.text.source.projection.ProjectionViewer.InvisibleCollapsedProjectionAnnotation", annotation.getClass().getCanonicalName()); |
| 201 | + Position position= viewer.getProjectionAnnotationModel().getPosition(annotation); |
| 202 | + |
| 203 | + if (position.getOffset() == 0) { |
| 204 | + assertEquals("org.eclipse.jface.text.source.projection.ProjectionViewer.ExactRegionProjectionPosition", position.getClass().getCanonicalName()); |
| 205 | + assertEquals("Hello\n".length(), position.getLength()); |
| 206 | + startAnnotationFound= true; |
| 207 | + } else { |
| 208 | + assertEquals(secondLineEnd, position.getOffset()); |
| 209 | + assertEquals(9, position.getLength()); |
| 210 | + endAnnotationFound= true; |
| 211 | + } |
| 212 | + annotationCount++; |
| 213 | + } |
| 214 | + assertEquals(2, annotationCount); |
| 215 | + assertTrue(startAnnotationFound); |
| 216 | + assertTrue(endAnnotationFound); |
| 217 | + } finally { |
| 218 | + shell.dispose(); |
| 219 | + } |
| 220 | + } |
| 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 | + } |
78 | 249 | }
|
0 commit comments