Skip to content

Commit 3b4a7ab

Browse files
tobias-melcherBeckerWdf
authored andcommitted
fix calculation of multiline heigth on windows with consolas size 9
1 parent e24ba9d commit 3b4a7ab

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

bundles/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/codemining/CodeMiningLineHeaderAnnotation.java

+46-9
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,61 @@ public CodeMiningLineHeaderAnnotation(Position position, ISourceViewer viewer) {
7777

7878
@Override
7979
public int getHeight() {
80-
return hasAtLeastOneResolvedMiningNotEmpty() ? getMultilineHeight() : 0;
80+
return hasAtLeastOneResolvedMiningNotEmpty() ? getMultilineHeight(null) : 0;
8181
}
8282

83-
private int getMultilineHeight() {
83+
public int getHeight(GC gc) {
84+
return hasAtLeastOneResolvedMiningNotEmpty() ? getMultilineHeight(gc) : 0;
85+
}
86+
87+
private int getMultilineHeight(GC gc) {
8488
int numLinesOfAllMinings= 0;
85-
for (ICodeMining mining : fMinings) {
89+
StyledText styledText= super.getTextWidget();
90+
boolean ignoreFirstLine= false;
91+
int sumLineHeight= 0;
92+
for (int i= 0; i < fMinings.size(); i++) {
93+
ICodeMining mining= fMinings.get(i);
8694
String label= mining.getLabel();
8795
if (label == null) {
8896
continue;
8997
}
90-
int numLines= label.split("\\r?\\n|\\r").length; //$NON-NLS-1$
91-
if (numLines > 1) {
92-
numLinesOfAllMinings= numLines - 1;
98+
String[] splitted= label.split("\\r?\\n|\\r"); //$NON-NLS-1$
99+
int numLines= splitted.length;
100+
if (ignoreFirstLine) {
101+
numLines--;
93102
}
103+
numLinesOfAllMinings+= numLines;
104+
if (gc != null) {
105+
sumLineHeight= calculateLineHeight(gc, styledText, ignoreFirstLine, sumLineHeight, i, splitted);
106+
}
107+
ignoreFirstLine= true;
94108
}
95-
numLinesOfAllMinings++;
96-
StyledText styledText= super.getTextWidget();
97-
return numLinesOfAllMinings * (styledText.getLineHeight() + styledText.getLineSpacing());
109+
if (gc != null) {
110+
return sumLineHeight;
111+
} else {
112+
int lineHeight= styledText.getLineHeight();
113+
int result= numLinesOfAllMinings * (lineHeight + styledText.getLineSpacing());
114+
return result;
115+
}
116+
}
117+
118+
private int calculateLineHeight(GC gc, StyledText styledText, boolean ignoreFirstLine, int sumLineHeight, int miningIndex, String[] splitted) {
119+
for (int j= 0; j < splitted.length; j++) {
120+
String line= splitted[j];
121+
if (j == 0 && ignoreFirstLine) {
122+
continue;
123+
}
124+
if (j == splitted.length - 1 && miningIndex + 1 < fMinings.size()) { // last line, take first line from next mining
125+
String nextLabel= fMinings.get(miningIndex + 1).getLabel();
126+
if (nextLabel != null) {
127+
String firstFromNext= nextLabel.split("\\r?\\n|\\r")[0]; //$NON-NLS-1$
128+
line+= firstFromNext;
129+
}
130+
}
131+
Point ext= gc.textExtent(line);
132+
sumLineHeight+= ext.y + styledText.getLineSpacing();
133+
}
134+
return sumLineHeight;
98135
}
99136

100137
/**

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.swt.graphics.Rectangle;
2626

2727
import org.eclipse.jface.internal.text.codemining.CodeMiningLineContentAnnotation;
28+
import org.eclipse.jface.internal.text.codemining.CodeMiningLineHeaderAnnotation;
2829

2930
import org.eclipse.jface.text.ITextViewer;
3031
import org.eclipse.jface.text.source.Annotation;
@@ -161,7 +162,12 @@ private static void draw(LineHeaderAnnotation annotation, GC gc, StyledText text
161162
}
162163
if (gc != null) {
163164
// Setting vertical indent first, before computing bounds
164-
int height= annotation.getHeight();
165+
int height;
166+
if (annotation instanceof CodeMiningLineHeaderAnnotation cmlha) {
167+
height= cmlha.getHeight(gc);
168+
} else {
169+
height= annotation.getHeight();
170+
}
165171
if (height != 0) {
166172
if (height != textWidget.getLineVerticalIndent(line)) {
167173
if (annotation.oldLine != -1 && annotation.oldLine < textWidget.getLineCount()) {

examples/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/EmptyLineCodeMiningProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextVi
3535
emptyLineHeaders.add(new LineHeaderCodeMining(line, document, this) {
3636
@Override
3737
public String getLabel() {
38-
return "Next line is empty";
38+
return "Next line is \nempty";
3939
}
4040
});
4141
}

0 commit comments

Comments
 (0)