Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMickael Istria2019-06-27 20:22:43 +0000
committerMickael Istria2019-06-27 20:22:43 +0000
commitd35e475221bdd306056c199b96afc8f53839e214 (patch)
treeb24425673922ce353ecfbb0119186ff3b881951d
parentf6f861584d69ea66be304e03ece940a922bd717a (diff)
downloadeclipse.platform.text-d35e475221bdd306056c199b96afc8f53839e214.tar.gz
eclipse.platform.text-d35e475221bdd306056c199b96afc8f53839e214.tar.xz
eclipse.platform.text-d35e475221bdd306056c199b96afc8f53839e214.zip
Bug 547519 - [code mining] Fix apply on tabulationI20190627-1800
With patch for SWT bug 547519, it's not possible to draw annotations at the right of a tabulation, just by fixing the char width. Change-Id: I1432a40f6bd2c25dfe4d11ba40ec278fbd448fa8 Signed-off-by: Mickael Istria <mistria@redhat.com>
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java6
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java2
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/AnnotationOnTabTest.java92
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/LineContentBoundsDrawingTest.java (renamed from org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/InlineAnnotationTest.java)12
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java6
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java3
6 files changed, 109 insertions, 12 deletions
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java
index 5a492dad97c..3a481804467 100644
--- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java
@@ -27,7 +27,8 @@ import org.eclipse.jface.text.tests.rules.FastPartitionerTest;
import org.eclipse.jface.text.tests.rules.ScannerColumnTest;
import org.eclipse.jface.text.tests.rules.WordRuleTest;
import org.eclipse.jface.text.tests.source.LineNumberRulerColumnTest;
-import org.eclipse.jface.text.tests.source.inlined.InlineAnnotationTest;
+import org.eclipse.jface.text.tests.source.inlined.AnnotationOnTabTest;
+import org.eclipse.jface.text.tests.source.inlined.LineContentBoundsDrawingTest;
import org.eclipse.jface.text.tests.templates.persistence.TemplatePersistenceDataTest;
@@ -58,7 +59,8 @@ import org.eclipse.jface.text.tests.templates.persistence.TemplatePersistenceDat
WordRuleTest.class,
TemplatePersistenceDataTest.class,
- InlineAnnotationTest.class,
+ LineContentBoundsDrawingTest.class,
+ AnnotationOnTabTest.class,
CodeMiningTest.class,
CodeMiningProjectionViewerTest.class
})
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java
index 88bf217ce1b..a8bd472b204 100644
--- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningProjectionViewerTest.java
@@ -50,7 +50,7 @@ import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
-import org.eclipse.jface.text.tests.source.inlined.InlineAnnotationTest.AccessAllAnnoations;
+import org.eclipse.jface.text.tests.source.inlined.LineContentBoundsDrawingTest.AccessAllAnnoations;
import org.eclipse.jface.text.tests.util.DisplayHelper;
public class CodeMiningProjectionViewerTest {
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/AnnotationOnTabTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/AnnotationOnTabTest.java
new file mode 100644
index 00000000000..4cf6795c238
--- /dev/null
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/AnnotationOnTabTest.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Red Hat Inc. and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+package org.eclipse.jface.text.tests.source.inlined;
+
+import java.util.Collections;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Shell;
+
+import org.eclipse.jface.resource.JFaceResources;
+
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.ITextViewerExtension2;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.source.AnnotationModel;
+import org.eclipse.jface.text.source.IAnnotationAccess;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.jface.text.source.SourceViewer;
+import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport;
+import org.eclipse.jface.text.source.inlined.LineContentAnnotation;
+import org.eclipse.jface.text.tests.source.inlined.LineContentBoundsDrawingTest.AccessAllAnnoations;
+import org.eclipse.jface.text.tests.source.inlined.LineContentBoundsDrawingTest.TestAnnotationPainter;
+import org.eclipse.jface.text.tests.util.DisplayHelper;
+
+public class AnnotationOnTabTest {
+
+ private Shell fParent;
+
+ @Before
+ public void setUp() {
+ fParent= new Shell();
+ }
+
+ @After
+ public void tearDown() {
+ fParent.dispose();
+ fParent = null;
+ }
+
+ @Test
+ public void testTextBoundsMatchPaintedArea() {
+ fParent.setLayout(new FillLayout());
+
+ // Create source viewer and initialize the content
+ ISourceViewer sourceViewer = new SourceViewer(fParent,null,
+ SWT.V_SCROLL | SWT.BORDER);
+ sourceViewer.setDocument(new Document("\t\treference\n\t\tannotated"), new AnnotationModel());
+ StyledText textWidget= sourceViewer.getTextWidget();
+ textWidget.setFont(JFaceResources.getTextFont());
+
+ // Initialize inlined annotations support
+ InlinedAnnotationSupport support = new InlinedAnnotationSupport();
+ IAnnotationAccess annotationAccess = new AccessAllAnnoations();
+ TestAnnotationPainter painter = new TestAnnotationPainter(sourceViewer, annotationAccess);
+ ((ITextViewerExtension2) sourceViewer).addPainter(painter);
+ support.install(sourceViewer, painter);
+
+ // add annotations
+ int annotationIndex = sourceViewer.getDocument().get().indexOf("annotated");
+ LineContentAnnotation annotation= new LineContentAnnotation(new Position(annotationIndex, 1), sourceViewer);
+ annotation.setText("a"); // single char, so overall annoation is 3 chars, less than default 4 chars
+ support.updateAnnotations(Collections.singleton(annotation));
+ fParent.open();
+ Assert.assertTrue(new DisplayHelper() {
+ @Override
+ protected boolean condition() {
+ return textWidget.isVisible() && painter.wasPainted();
+ }
+ }.waitForCondition(textWidget.getDisplay(), 2000));
+ DisplayHelper.sleep(textWidget.getDisplay(), 1000);
+ int referenceIndex = textWidget.getText().indexOf("reference");
+ Rectangle referenceBounds = textWidget.getTextBounds(referenceIndex, referenceIndex);
+ Rectangle annotatedCharactedBounds = textWidget.getTextBounds(annotationIndex, annotationIndex);
+ Assert.assertTrue("Annotation didn't shift target character to the right, it most likely replaced the tab instead of expanding it", referenceBounds.x < annotatedCharactedBounds.x);
+ }
+}
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/InlineAnnotationTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/LineContentBoundsDrawingTest.java
index 75e924d2811..0fca4422ed4 100644
--- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/InlineAnnotationTest.java
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/source/inlined/LineContentBoundsDrawingTest.java
@@ -43,8 +43,12 @@ import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport;
import org.eclipse.jface.text.source.inlined.LineContentAnnotation;
import org.eclipse.jface.text.tests.util.DisplayHelper;
+/**
+ * This test verify that the bounds of the text as returned by StyledText.getTextBounds()
+ * actually match what's printed on the widget.
+ */
@RunWith(Parameterized.class)
-public class InlineAnnotationTest {
+public class LineContentBoundsDrawingTest {
@Parameters(name="{0}")
public static String[] contents() {
return new String[] {
@@ -56,7 +60,7 @@ public class InlineAnnotationTest {
private String text;
- public InlineAnnotationTest(String text) {
+ public LineContentBoundsDrawingTest(String text) {
this.text = text;
}
@@ -77,10 +81,10 @@ public class InlineAnnotationTest {
}
}
- private static final class TestAnnotationPainter extends AnnotationPainter {
+ public static final class TestAnnotationPainter extends AnnotationPainter {
private boolean painted;
- private TestAnnotationPainter(ISourceViewer sourceViewer, IAnnotationAccess access) {
+ public TestAnnotationPainter(ISourceViewer sourceViewer, IAnnotationAccess access) {
super(sourceViewer, access);
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
index 038b1e215d5..ec8451dbafb 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
@@ -248,8 +248,8 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
return;
}
if (gc != null) {
- String hostCharacter= textWidget.getText(widgetOffset - 1, widgetOffset - 1);
- int redrawnCharacterWidth= gc.stringExtent(hostCharacter).x;
+ char hostCharacter= textWidget.getText(widgetOffset - 1, widgetOffset - 1).charAt(0);
+ int redrawnCharacterWidth= hostCharacter != '\t' ? gc.getCharWidth(hostCharacter) : textWidget.getTabs() * gc.getCharWidth(' ');
Rectangle charBounds= textWidget.getTextBounds(widgetOffset - 1, widgetOffset - 1);
Rectangle annotationBounds= new Rectangle(charBounds.x + redrawnCharacterWidth, charBounds.y, annotation.getWidth(), charBounds.height);
@@ -299,7 +299,7 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
gc.setForeground(textWidget.getSelectionForeground());
gc.setBackground(textWidget.getSelectionBackground());
}
- gc.drawString(hostCharacter, charBounds.x, charBounds.y, true);
+ gc.drawString(Character.toString(hostCharacter), charBounds.x, charBounds.y, true);
// END TO REMOVE
} else if (style != null && style.metrics != null && style.metrics.width != 0) {
// line content annotation had an , reset it
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
index 6a606360b57..699a92d8545 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
@@ -152,8 +152,7 @@ public class LineContentAnnotation extends AbstractInlinedAnnotation {
boolean drawRightToPreviousChar(int widgetOffset) {
return widgetOffset > 0 &&
- getTextWidget().getLineAtOffset(widgetOffset) == getTextWidget().getLineAtOffset(widgetOffset - 1) &&
- getTextWidget().getText().charAt(widgetOffset - 1) != '\t'; // \t workaround bug 547532
+ getTextWidget().getLineAtOffset(widgetOffset) == getTextWidget().getLineAtOffset(widgetOffset - 1);
}
}

Back to the top