|
13 | 13 | import java.io.ByteArrayInputStream; |
14 | 14 | import java.text.SimpleDateFormat; |
15 | 15 | import java.util.Date; |
| 16 | +import java.util.HashMap; |
16 | 17 | import java.util.concurrent.Callable; |
17 | 18 |
|
18 | 19 | import org.junit.After; |
|
41 | 42 | import org.eclipse.jface.text.BadLocationException; |
42 | 43 | import org.eclipse.jface.text.IDocument; |
43 | 44 | import org.eclipse.jface.text.ITextViewer; |
| 45 | +import org.eclipse.jface.text.Position; |
| 46 | +import org.eclipse.jface.text.source.Annotation; |
44 | 47 | import org.eclipse.jface.text.source.ISourceViewer; |
45 | 48 | import org.eclipse.jface.text.source.ISourceViewerExtension5; |
| 49 | +import org.eclipse.jface.text.source.projection.ProjectionAnnotation; |
| 50 | +import org.eclipse.jface.text.source.projection.ProjectionViewer; |
46 | 51 |
|
47 | 52 | import org.eclipse.ui.IEditorPart; |
48 | 53 | import org.eclipse.ui.IWorkbenchPage; |
@@ -100,6 +105,60 @@ public void run(IProgressMonitor monitor) throws CoreException { |
100 | 105 | }, new NullProgressMonitor()); |
101 | 106 | } |
102 | 107 |
|
| 108 | + @Test |
| 109 | + public void testInlinedAnnotationSupportIsInLinesReturnsValidResultAfterDocumentChange() throws Exception { |
| 110 | + IFile file = project.getFile("test.testprojectionviewer"); |
| 111 | + if (file.exists()) { |
| 112 | + file.delete(true, new NullProgressMonitor()); |
| 113 | + } |
| 114 | + String source = "first\nsecond\nthird\n"; |
| 115 | + file.create(new ByteArrayInputStream(source.getBytes("UTF-8")), true, new NullProgressMonitor()); |
| 116 | + CodeMiningTestProvider.provideHeaderMiningAtLine = 2; |
| 117 | + CodeMiningTestProvider.lineHeaderMiningText = " first line header\n secone line header\n third line header"; |
| 118 | + int offset = source.indexOf("second") + "second".length(); |
| 119 | + IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file); |
| 120 | + drainEventQueue(); |
| 121 | + ISourceViewer viewer = (ISourceViewer) editor.getAdapter(ITextViewer.class); |
| 122 | + StyledText widget = viewer.getTextWidget(); |
| 123 | + |
| 124 | + var annotationModel = ((ProjectionViewer) viewer).getProjectionAnnotationModel(); |
| 125 | + var deletionsArray = new Annotation[] {}; |
| 126 | + var additions = new HashMap<Annotation, Position>(); |
| 127 | + ProjectionAnnotation annot = new ProjectionAnnotation(); |
| 128 | + additions.put(annot, new Position(0, source.length())); |
| 129 | + annotationModel.modifyAnnotations(deletionsArray, additions, null); |
| 130 | + |
| 131 | + Assert.assertTrue("Line header code mining above 3rd line not drawn", |
| 132 | + waitForCondition(widget.getDisplay(), 2000, new Callable<Boolean>() { |
| 133 | + @Override |
| 134 | + public Boolean call() throws Exception { |
| 135 | + try { |
| 136 | + return existsPixelWithNonBackgroundColorAtLine(viewer, 2); |
| 137 | + } catch (BadLocationException e) { |
| 138 | + e.printStackTrace(); |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + })); |
| 143 | + |
| 144 | + IDocument doc = viewer.getDocument(); |
| 145 | + widget.setCaretOffset(offset); |
| 146 | + doc.replace(offset, 0, "\n insert text"); |
| 147 | + drainEventQueue(); |
| 148 | + Assert.assertTrue("Line header code mining above 4th line after inserting text not drawn", |
| 149 | + waitForCondition(widget.getDisplay(), 2000, new Callable<Boolean>() { |
| 150 | + @Override |
| 151 | + public Boolean call() throws Exception { |
| 152 | + try { |
| 153 | + return existsPixelWithNonBackgroundColorAtLine(viewer, 3); |
| 154 | + } catch (BadLocationException e) { |
| 155 | + e.printStackTrace(); |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + })); |
| 160 | + } |
| 161 | + |
103 | 162 | @Test |
104 | 163 | public void testCodeMiningOnEmptyLine() throws Exception { |
105 | 164 | IFile file = project.getFile("test.txt"); |
@@ -208,12 +267,17 @@ private static boolean existsPixelWithNonBackgroundColorAtLine(ITextViewer viewe |
208 | 267 | lineLength = 0; |
209 | 268 | } |
210 | 269 | int verticalScroolBarWidth = viewer.getTextWidget().getVerticalBar().getThumbBounds().width; |
211 | | - Rectangle lineBounds = widget.getTextBounds(document.getLineOffset(line), |
212 | | - document.getLineOffset(line) + lineLength); |
| 270 | + int lineOffset = document.getLineOffset(line); |
| 271 | + Rectangle lineBounds = widget.getTextBounds(lineOffset, lineOffset + lineLength); |
| 272 | + String lineStr = document.get(lineOffset, lineLength); |
213 | 273 | Image image = new Image(widget.getDisplay(), widget.getSize().x, widget.getSize().y); |
214 | 274 | try { |
215 | 275 | GC gc = new GC(widget); |
216 | 276 | gc.copyArea(image, 0, 0); |
| 277 | + Point textExtent = gc.textExtent(lineStr); |
| 278 | + if (lineBounds.height - textExtent.y > textExtent.y) { |
| 279 | + lineBounds.height -= textExtent.y; |
| 280 | + } |
217 | 281 | gc.dispose(); |
218 | 282 | ImageData imageData = image.getImageData(); |
219 | 283 | for (int x = lineBounds.x + 1; x < image.getBounds().width - verticalScroolBarWidth |
|
0 commit comments