|
18 | 18 | import org.eclipse.swt.dnd.Clipboard;
|
19 | 19 | import org.eclipse.swt.dnd.TextTransfer;
|
20 | 20 | import org.eclipse.swt.layout.FillLayout;
|
| 21 | +import org.eclipse.swt.widgets.Composite; |
21 | 22 | import org.eclipse.swt.widgets.Shell;
|
22 | 23 |
|
23 | 24 | import org.eclipse.jface.text.BadLocationException;
|
|
29 | 30 | import org.eclipse.jface.text.Position;
|
30 | 31 | import org.eclipse.jface.text.Region;
|
31 | 32 | import org.eclipse.jface.text.source.AnnotationModel;
|
| 33 | +import org.eclipse.jface.text.source.IOverviewRuler; |
| 34 | +import org.eclipse.jface.text.source.IVerticalRuler; |
32 | 35 | import org.eclipse.jface.text.source.projection.IProjectionPosition;
|
33 | 36 | import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
|
34 | 37 | import org.eclipse.jface.text.source.projection.ProjectionViewer;
|
35 | 38 |
|
36 | 39 | public class ProjectionViewerTest {
|
37 | 40 |
|
| 41 | + /** |
| 42 | + * A {@link ProjectionViewer} that provides access to {@link #getVisibleDocument()}. |
| 43 | + */ |
| 44 | + private final class TestProjectionViewer extends ProjectionViewer { |
| 45 | + private TestProjectionViewer(Composite parent, IVerticalRuler ruler, IOverviewRuler overviewRuler, boolean showsAnnotationOverview, int styles) { |
| 46 | + super(parent, ruler, overviewRuler, showsAnnotationOverview, styles); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public IDocument getVisibleDocument() { |
| 51 | + return super.getVisibleDocument(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
38 | 55 | private static final class ProjectionPosition extends Position implements IProjectionPosition {
|
39 | 56 |
|
40 | 57 | public ProjectionPosition(IDocument document) {
|
@@ -75,4 +92,159 @@ public void testCopyPaste() {
|
75 | 92 | shell.dispose();
|
76 | 93 | }
|
77 | 94 | }
|
| 95 | + |
| 96 | + @Test |
| 97 | + public void testVisibleRegionDoesNotChangeWithProjections() { |
| 98 | + Shell shell= new Shell(); |
| 99 | + shell.setLayout(new FillLayout()); |
| 100 | + ProjectionViewer viewer= new ProjectionViewer(shell, null, null, false, SWT.NONE); |
| 101 | + String documentContent= """ |
| 102 | + Hello |
| 103 | + World |
| 104 | + 123 |
| 105 | + 456 |
| 106 | + """; |
| 107 | + Document document= new Document(documentContent); |
| 108 | + viewer.setDocument(document, new AnnotationModel()); |
| 109 | + int regionLength= documentContent.indexOf('\n'); |
| 110 | + viewer.setVisibleRegion(0, regionLength); |
| 111 | + viewer.enableProjection(); |
| 112 | + viewer.getProjectionAnnotationModel().addAnnotation(new ProjectionAnnotation(false), new ProjectionPosition(document)); |
| 113 | + shell.setVisible(true); |
| 114 | + try { |
| 115 | + assertEquals(0, viewer.getVisibleRegion().getOffset()); |
| 116 | + assertEquals(regionLength, viewer.getVisibleRegion().getLength()); |
| 117 | + |
| 118 | + viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); |
| 119 | + assertEquals(0, viewer.getVisibleRegion().getOffset()); |
| 120 | + assertEquals(regionLength, viewer.getVisibleRegion().getLength()); |
| 121 | + } finally { |
| 122 | + shell.dispose(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testVisibleRegionProjectionCannotBeExpanded() { |
| 128 | + Shell shell= new Shell(); |
| 129 | + shell.setLayout(new FillLayout()); |
| 130 | + TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); |
| 131 | + String documentContent= """ |
| 132 | + Hello |
| 133 | + World |
| 134 | + 123 |
| 135 | + 456 |
| 136 | + """; |
| 137 | + Document document= new Document(documentContent); |
| 138 | + viewer.setDocument(document, new AnnotationModel()); |
| 139 | + int secondLineStart= documentContent.indexOf("World"); |
| 140 | + int secondLineEnd= documentContent.indexOf('\n', secondLineStart); |
| 141 | + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); |
| 142 | + viewer.enableProjection(); |
| 143 | + shell.setVisible(true); |
| 144 | + try { |
| 145 | + assertEquals("World", viewer.getVisibleDocument().get()); |
| 146 | + viewer.getTextOperationTarget().doOperation(ProjectionViewer.EXPAND_ALL); |
| 147 | + assertEquals("World", viewer.getVisibleDocument().get()); |
| 148 | + } finally { |
| 149 | + shell.dispose(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testVisibleRegionAddsProjectionAnnotationsIfProjectionsEnabled() { |
| 155 | + testProjectionAnnotationsFromVisibleRegion(true); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testEnableProjectionAddsProjectionAnnotationsIfVisibleRegionEnabled() { |
| 160 | + testProjectionAnnotationsFromVisibleRegion(false); |
| 161 | + } |
| 162 | + |
| 163 | + private void testProjectionAnnotationsFromVisibleRegion(boolean enableProjectionFirst) { |
| 164 | + Shell shell= new Shell(); |
| 165 | + shell.setLayout(new FillLayout()); |
| 166 | + TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); |
| 167 | + String documentContent= """ |
| 168 | + Hello |
| 169 | + World |
| 170 | + 123 |
| 171 | + 456 |
| 172 | + """; |
| 173 | + Document document= new Document(documentContent); |
| 174 | + viewer.setDocument(document, new AnnotationModel()); |
| 175 | + int secondLineStart= documentContent.indexOf("World"); |
| 176 | + int secondLineEnd= documentContent.indexOf('\n', secondLineStart); |
| 177 | + |
| 178 | + shell.setVisible(true); |
| 179 | + if (enableProjectionFirst) { |
| 180 | + viewer.enableProjection(); |
| 181 | + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); |
| 182 | + } else { |
| 183 | + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); |
| 184 | + viewer.enableProjection(); |
| 185 | + } |
| 186 | + |
| 187 | +// boolean startAnnotationFound= false; |
| 188 | +// boolean endAnnotationFound= false; |
| 189 | +// int annotationCount= 0; |
| 190 | + |
| 191 | + try { |
| 192 | + assertEquals("World", viewer.getVisibleDocument().get().trim()); |
| 193 | + |
| 194 | +// for (Iterator<Annotation> it= viewer.getProjectionAnnotationModel().getAnnotationIterator(); it.hasNext();) { |
| 195 | +// Annotation annotation= it.next(); |
| 196 | +// assertEquals("org.eclipse.jface.text.source.projection.ProjectionViewer.InvisibleCollapsedProjectionAnnotation", annotation.getClass().getCanonicalName()); |
| 197 | +// Position position= viewer.getProjectionAnnotationModel().getPosition(annotation); |
| 198 | +// |
| 199 | +// if (position.getOffset() == 0) { |
| 200 | +// assertEquals("org.eclipse.jface.text.source.projection.ProjectionViewer.ExactRegionProjectionPosition", position.getClass().getCanonicalName()); |
| 201 | +// assertEquals("Hello\n".length(), position.getLength()); |
| 202 | +// startAnnotationFound= true; |
| 203 | +// } else { |
| 204 | +// assertEquals(secondLineEnd + 2, position.getOffset()); |
| 205 | +// assertEquals(7, position.getLength()); |
| 206 | +// endAnnotationFound= true; |
| 207 | +// } |
| 208 | +// annotationCount++; |
| 209 | +// } |
| 210 | +// assertEquals(2, annotationCount); |
| 211 | +// assertTrue(startAnnotationFound); |
| 212 | +// assertTrue(endAnnotationFound); |
| 213 | + } finally { |
| 214 | + shell.dispose(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void testInsertIntoVisibleRegion() throws BadLocationException { |
| 220 | + Shell shell= new Shell(); |
| 221 | + shell.setLayout(new FillLayout()); |
| 222 | + TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); |
| 223 | + String documentContent= """ |
| 224 | + Hello |
| 225 | + World |
| 226 | + 123 |
| 227 | + 456 |
| 228 | + """; |
| 229 | + Document document= new Document(documentContent); |
| 230 | + viewer.setDocument(document, new AnnotationModel()); |
| 231 | + int secondLineStart= documentContent.indexOf("World"); |
| 232 | + int secondLineEnd= documentContent.indexOf('\n', secondLineStart); |
| 233 | + |
| 234 | + shell.setVisible(true); |
| 235 | + |
| 236 | + try { |
| 237 | + viewer.setVisibleRegion(secondLineStart, secondLineEnd - secondLineStart); |
| 238 | + viewer.enableProjection(); |
| 239 | + |
| 240 | + assertEquals("World", viewer.getVisibleDocument().get()); |
| 241 | + |
| 242 | + viewer.getDocument().replace(documentContent.indexOf("rld"), 0, "---"); |
| 243 | + |
| 244 | + assertEquals("Wo---rld", viewer.getVisibleDocument().get()); |
| 245 | + } finally { |
| 246 | + shell.dispose(); |
| 247 | + } |
| 248 | + } |
| 249 | + |
78 | 250 | }
|
0 commit comments