Skip to content

Commit

Permalink
fix calculation of multiline heigth on windows with consolas size 9
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-melcher authored and BeckerWdf committed Jan 2, 2025
1 parent e24ba9d commit 3b4a7ab
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,61 @@ public CodeMiningLineHeaderAnnotation(Position position, ISourceViewer viewer) {

@Override
public int getHeight() {
return hasAtLeastOneResolvedMiningNotEmpty() ? getMultilineHeight() : 0;
return hasAtLeastOneResolvedMiningNotEmpty() ? getMultilineHeight(null) : 0;
}

private int getMultilineHeight() {
public int getHeight(GC gc) {
return hasAtLeastOneResolvedMiningNotEmpty() ? getMultilineHeight(gc) : 0;
}

private int getMultilineHeight(GC gc) {
int numLinesOfAllMinings= 0;
for (ICodeMining mining : fMinings) {
StyledText styledText= super.getTextWidget();
boolean ignoreFirstLine= false;
int sumLineHeight= 0;
for (int i= 0; i < fMinings.size(); i++) {
ICodeMining mining= fMinings.get(i);
String label= mining.getLabel();
if (label == null) {
continue;
}
int numLines= label.split("\\r?\\n|\\r").length; //$NON-NLS-1$
if (numLines > 1) {
numLinesOfAllMinings= numLines - 1;
String[] splitted= label.split("\\r?\\n|\\r"); //$NON-NLS-1$
int numLines= splitted.length;
if (ignoreFirstLine) {
numLines--;
}
numLinesOfAllMinings+= numLines;
if (gc != null) {
sumLineHeight= calculateLineHeight(gc, styledText, ignoreFirstLine, sumLineHeight, i, splitted);
}
ignoreFirstLine= true;
}
numLinesOfAllMinings++;
StyledText styledText= super.getTextWidget();
return numLinesOfAllMinings * (styledText.getLineHeight() + styledText.getLineSpacing());
if (gc != null) {
return sumLineHeight;
} else {
int lineHeight= styledText.getLineHeight();
int result= numLinesOfAllMinings * (lineHeight + styledText.getLineSpacing());
return result;
}
}

private int calculateLineHeight(GC gc, StyledText styledText, boolean ignoreFirstLine, int sumLineHeight, int miningIndex, String[] splitted) {
for (int j= 0; j < splitted.length; j++) {
String line= splitted[j];
if (j == 0 && ignoreFirstLine) {
continue;
}
if (j == splitted.length - 1 && miningIndex + 1 < fMinings.size()) { // last line, take first line from next mining
String nextLabel= fMinings.get(miningIndex + 1).getLabel();
if (nextLabel != null) {
String firstFromNext= nextLabel.split("\\r?\\n|\\r")[0]; //$NON-NLS-1$
line+= firstFromNext;
}
}
Point ext= gc.textExtent(line);
sumLineHeight+= ext.y + styledText.getLineSpacing();
}
return sumLineHeight;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.jface.internal.text.codemining.CodeMiningLineContentAnnotation;
import org.eclipse.jface.internal.text.codemining.CodeMiningLineHeaderAnnotation;

import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.Annotation;
Expand Down Expand Up @@ -161,7 +162,12 @@ private static void draw(LineHeaderAnnotation annotation, GC gc, StyledText text
}
if (gc != null) {
// Setting vertical indent first, before computing bounds
int height= annotation.getHeight();
int height;
if (annotation instanceof CodeMiningLineHeaderAnnotation cmlha) {
height= cmlha.getHeight(gc);
} else {
height= annotation.getHeight();
}
if (height != 0) {
if (height != textWidget.getLineVerticalIndent(line)) {
if (annotation.oldLine != -1 && annotation.oldLine < textWidget.getLineCount()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextVi
emptyLineHeaders.add(new LineHeaderCodeMining(line, document, this) {
@Override
public String getLabel() {
return "Next line is empty";
return "Next line is \nempty";
}
});
}
Expand Down

0 comments on commit 3b4a7ab

Please sign in to comment.