Skip to content

Commit f63763e

Browse files
tobias-melcheriloveeclipse
authored andcommitted
Take line spacing into account in code mining drawing logic
1 parent 8a64d23 commit f63763e

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/codemining/AbstractCodeMining.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ public void dispose() {
144144
public Point draw(GC gc, StyledText textWidget, Color color, int x, int y) {
145145
String title= getLabel() != null ? getLabel() : "no command"; //$NON-NLS-1$
146146
gc.drawString(title, x, y, true);
147-
return gc.stringExtent(title);
147+
Point result= gc.stringExtent(title);
148+
result.y+= textWidget.getLineSpacing();
149+
return result;
148150
}
149151

150152
@Override

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/codemining/LineHeaderCodeMining.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static Point draw(String label, GC gc, StyledText textWidget, int x, int y, Call
9292
gc.drawString(line, x, y, true);
9393
Point ext= gc.stringExtent(line);
9494
result.x= Math.max(result.x, ext.x);
95-
result.y+= ext.y;
95+
result.y+= ext.y + textWidget.getLineSpacing();
9696
y+= ext.y + textWidget.getLineSpacing();
9797
}
9898
return result;

tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/codemining/CodeMiningLineHeaderAnnotationTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.eclipse.jface.text.tests.codemining;
1212

13+
import static org.junit.jupiter.api.Assertions.assertEquals;
1314
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1415

1516
import java.util.Arrays;
@@ -21,6 +22,8 @@
2122

2223
import org.eclipse.swt.SWT;
2324
import org.eclipse.swt.custom.StyledText;
25+
import org.eclipse.swt.graphics.GC;
26+
import org.eclipse.swt.graphics.Point;
2427
import org.eclipse.swt.layout.FillLayout;
2528
import org.eclipse.swt.widgets.Display;
2629
import org.eclipse.swt.widgets.Shell;
@@ -72,6 +75,37 @@ public void tearDown() {
7275
fViewer= null;
7376
}
7477

78+
@Test
79+
public void testDrawTakesLineSpacingForSingleLineIntoAccount() throws Exception {
80+
assertDrawTakesLineSpacingIntoAccount("line1\nline2\nline3", "code mining single line");
81+
}
82+
83+
@Test
84+
public void testDrawTakesLineSpacingForMultiLineIntoAccount() throws Exception {
85+
assertDrawTakesLineSpacingIntoAccount("line1\nline2\nline3", "code mining line1\ncode mining line2");
86+
}
87+
88+
private void assertDrawTakesLineSpacingIntoAccount(String source, String codeMiningLabel) throws Exception {
89+
var doc= fViewer.getDocument();
90+
doc.set(source);
91+
var textWidget= fViewer.getTextWidget();
92+
textWidget.setLineSpacing(10);
93+
var mining= new LineHeaderCodeMining(0, doc, null, null) {
94+
@Override
95+
public String getLabel() {
96+
return codeMiningLabel;
97+
}
98+
};
99+
var gc= new GC(textWidget);
100+
try {
101+
Point result= mining.draw(gc, textWidget, null, 0, 0);
102+
String[] codeMiningLabelsByLine= codeMiningLabel.split("\n");
103+
assertEquals(codeMiningLabelsByLine.length * (gc.stringExtent(codeMiningLabelsByLine[0]).y + textWidget.getLineSpacing()), result.y);
104+
} finally {
105+
gc.dispose();
106+
}
107+
}
108+
75109
@Test
76110
public void testGetHeightDoesNotReturnZero() throws Exception {
77111
var cut= new CodeMiningLineHeaderAnnotation(new Position(0, 0), fViewer);

0 commit comments

Comments
 (0)