Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -473,7 +473,30 @@ public ColorPalette(RGB background, String registryKey, RGB defaultRGB) {
// calling w.getLineHeight(line) + w.getLineSpacing() +
// w.getLineVerticalIndent(line); again and again for the same line is too slow
// we therefore introduce a line height cache by viewer
private final Map<MergeSourceViewer, List<Integer>> lineHeightsByViewer = new HashMap<>();
private final Map<MergeSourceViewer, LineHeightCache> lineHeightsByViewer = new HashMap<>();

private static final class LineHeightCache {
private final Font font;
private final int baseHeight;
private final int spacing;
private final int[] heights;
private int prefixLines;
private int prefixHeight;

private LineHeightCache(StyledText textWidget) {
font = textWidget.getFont();
baseHeight = textWidget.getLineHeight();
spacing = textWidget.getLineSpacing();
heights = new int[textWidget.getLineCount()];
Arrays.fill(heights, -1);
}

private boolean matches(StyledText textWidget) {
return !font.isDisposed() && font.equals(textWidget.getFont())
&& baseHeight == textWidget.getLineHeight() && spacing == textWidget.getLineSpacing()
&& heights.length == textWidget.getLineCount();
}
}

/**
* Preference key for highlighting current line.
Expand Down Expand Up @@ -2401,6 +2424,7 @@ private void setCurrentDiff2(Diff diff, boolean reveal) {
}

private Diff handleMouseInSides(Canvas canvas, MergeSourceViewer tp, int my) {
validateLineHeightCache(tp);

int visibleHeight= tp.getViewportHeight();

Expand All @@ -2409,8 +2433,6 @@ private Diff handleMouseInSides(Canvas canvas, MergeSourceViewer tp, int my) {
}

if (fMerger.hasChanges()) {
int shift = calculateShift(tp) + (2 - LW);

Point region= new Point(0, 0);
char leg = getLeg(tp);
for (Iterator<?> iterator = fMerger.changesIterator(); iterator.hasNext();) {
Expand All @@ -2421,7 +2443,7 @@ private Diff handleMouseInSides(Canvas canvas, MergeSourceViewer tp, int my) {

tp.getLineRange(diff.getPosition(leg), region);
region.x -= tp.getDocumentRegionOffset();
int y = getHeightBetweenLines(tp, 0, region.x) + shift;
int y = getLineY(tp, region.x) + (2 - LW);
int h = getHeightBetweenLines(tp, region.x, region.x + region.y);

if (y+h < 0) {
Expand All @@ -2440,6 +2462,8 @@ private Diff handleMouseInSides(Canvas canvas, MergeSourceViewer tp, int my) {
}

private Diff getDiffUnderMouse(Canvas canvas, int mx, int my, Rectangle r) {
validateLineHeightCache(fLeft);
validateLineHeightCache(fRight);

if (! fSynchronizedScrolling) {
return null;
Expand All @@ -2455,9 +2479,6 @@ private Diff getDiffUnderMouse(Canvas canvas, int mx, int my, Rectangle r) {
}

if (fMerger.hasChanges()) {
int lshift = calculateShift(fLeft);
int rshift = calculateShift(fRight);

Point region= new Point(0, 0);

for (Iterator<?> iterator = fMerger.changesIterator(); iterator.hasNext();) {
Expand All @@ -2468,12 +2489,12 @@ private Diff getDiffUnderMouse(Canvas canvas, int mx, int my, Rectangle r) {

fLeft.getLineRange(diff.getPosition(LEFT_CONTRIBUTOR), region);
region.x -= fLeft.getDocumentRegionOffset();
int ly = getHeightBetweenLines(fLeft, 0, region.x) + lshift;
int ly = getLineY(fLeft, region.x);
int lh = getHeightBetweenLines(fLeft, region.x, region.x + region.y);

fRight.getLineRange(diff.getPosition(RIGHT_CONTRIBUTOR), region);
region.x -= fRight.getDocumentRegionOffset();
int ry = getHeightBetweenLines(fRight, 0, region.x) + rshift;
int ry = getLineY(fRight, region.x);
int rh = getHeightBetweenLines(fRight, region.x, region.x + region.y);

if (Math.max(ly+lh, ry+rh) < 0) {
Expand Down Expand Up @@ -2818,8 +2839,10 @@ private MergeSourceViewer createPart(Composite parent) {
createSourceViewer(parent, getDirection()),
getResourceBundle(), getCompareConfiguration().getContainer());
final StyledText te= viewer.getSourceViewer().getTextWidget();
viewer.getSourceViewer().addTextListener(_ -> lineHeightsByViewer.remove(viewer));
viewer.getSourceViewer().addTextPresentationListener(_ -> lineHeightsByViewer.remove(viewer));
// Clear cached line heights upon zoom changes, as they are no longer valid
te.addListener(SWT.ZoomChanged, _ -> resetCachedLineHeights(viewer));
te.addListener(SWT.ZoomChanged, _ -> lineHeightsByViewer.remove(viewer));
if (!fConfirmSave) {
viewer.hideSaveAction();
}
Expand Down Expand Up @@ -4543,43 +4566,53 @@ private int getHeightBetweenLines(MergeSourceViewer tp, int fromLine, int toLine
if (fromLine == toLine) {
return 0;
}
StyledText w = tp.getSourceViewer().getTextWidget();
List<Integer> lineHeights = this.lineHeightsByViewer.get(tp);
if (lineHeights == null) {
lineHeights = new ArrayList<>();
lineHeights.addAll(Collections.nCopies(w.getLineCount(), null));
this.lineHeightsByViewer.put(tp, lineHeights);
tp.getSourceViewer().addTextListener(_ -> resetCachedLineHeights(tp));
}

int lineSpacing = w.getLineSpacing();

StyledText textWidget = tp.getSourceViewer().getTextWidget();
// If the linenumber is higher than the number of lines, we should prevent an
// IndexOutOfBoundsException
if (toLine > w.getLineCount()){
int lineHeight = tp.getSourceViewer().getTextWidget().getLineHeight();
return (toLine - fromLine) * (lineHeight + w.getLineVerticalIndent(0)) + lineSpacing;
if (toLine > textWidget.getLineCount()) {
int lineHeight = textWidget.getLineHeight();
return (toLine - fromLine) * (lineHeight + textWidget.getLineVerticalIndent(0))
+ textWidget.getLineSpacing();
}

int height = 0;
for (int i = fromLine; i < toLine; i++) {
Integer heightAtLine = lineHeights.get(i);
if (heightAtLine == null) {
int lineOffset = w.getOffsetAtLine(i);
heightAtLine = w.getLineHeight(lineOffset) + lineSpacing + w.getLineVerticalIndent(i);
lineHeights.set(i, heightAtLine);
if (canCacheLineHeights(textWidget)) {
LineHeightCache cache = lineHeightsByViewer.get(tp);
if (cache == null || !cache.matches(textWidget)) {
cache = new LineHeightCache(textWidget);
lineHeightsByViewer.put(tp, cache);
}
int height = 0;
for (int line = fromLine; line < toLine; line++) {
if (cache.heights[line] < 0) {
cache.heights[line] = textWidget.getLineHeight(textWidget.getOffsetAtLine(line))
+ cache.spacing + textWidget.getLineVerticalIndent(line);
}
height += cache.heights[line];
}
while (cache.prefixLines < cache.heights.length && cache.heights[cache.prefixLines] >= 0) {
cache.prefixHeight += cache.heights[cache.prefixLines++];
}
if (cache.matches(textWidget)) {
return height;
}
height += heightAtLine;
}
return height;
lineHeightsByViewer.remove(tp);
return textWidget.getLinePixel(toLine) - textWidget.getLinePixel(fromLine);
}

private boolean canCacheLineHeights(StyledText textWidget) {
return textWidget.getLineSpacing() > 0 && !textWidget.getWordWrap();
}

private void resetCachedLineHeights(MergeSourceViewer mergeViewer) {
StyledText styledText = mergeViewer.getSourceViewer().getTextWidget();
List<Integer> lineHeightsList = lineHeightsByViewer.get(mergeViewer);
if (lineHeightsList != null) {
lineHeightsList.clear();
lineHeightsList.addAll(Collections.nCopies(styledText.getLineCount(), null));
private void validateLineHeightCache(MergeSourceViewer viewer) {
LineHeightCache cache = lineHeightsByViewer.get(viewer);
if (cache == null) {
return;
}
StyledText textWidget = viewer.getSourceViewer().getTextWidget();
if (!canCacheLineHeights(textWidget) || !cache.matches(textWidget)
|| cache.prefixHeight != textWidget.getLinePixel(cache.prefixLines) - textWidget.getLinePixel(0)
|| !cache.matches(textWidget)) {
lineHeightsByViewer.remove(viewer);
}
}

Expand Down Expand Up @@ -4632,6 +4665,8 @@ private void paintCenter(Canvas canvas, GC g) {
// simply ignore it in this case.
return;
}
validateLineHeightCache(fLeft);
validateLineHeightCache(fRight);

Display display= canvas.getDisplay();

Expand Down Expand Up @@ -4660,9 +4695,6 @@ private void paintCenter(Canvas canvas, GC g) {
}

if (fMerger.hasChanges()) {
int lshift = calculateShift(fLeft);
int rshift = calculateShift(fRight);

Point region= new Point(0, 0);

boolean allOutgoing = allOutgoing();
Expand All @@ -4675,13 +4707,13 @@ private void paintCenter(Canvas canvas, GC g) {
fLeft.getLineRange(diff.getPosition(LEFT_CONTRIBUTOR), region);
region.x -= fLeft.getDocumentRegionOffset();
int diffLines = region.y;
int ly = getHeightBetweenLines(fLeft, 0, region.x) + lshift;
int ly = getLineY(fLeft, region.x);
int lh = getHeightBetweenLines(fLeft, region.x, region.x + region.y);

fRight.getLineRange(diff.getPosition(RIGHT_CONTRIBUTOR), region);
region.x -= fRight.getDocumentRegionOffset();
diffLines = Math.max(diffLines, region.y);
int ry = getHeightBetweenLines(fRight, 0, region.x) + rshift;
int ry = getLineY(fRight, region.x);
int rh = getHeightBetweenLines(fRight, region.x, region.x + region.y);

if (Math.max(ly+lh, ry+rh) < 0) {
Expand Down Expand Up @@ -4770,11 +4802,16 @@ private double[] buildBaseCenterCurve(int width) {
return curve;
}

private int calculateShift(MergeSourceViewer tp) {
return tp.getSourceViewer().getTopInset() - tp.getSourceViewer().getTextWidget().getTopPixel();
private int getLineY(MergeSourceViewer tp, int line) {
StyledText textWidget = tp.getSourceViewer().getTextWidget();
if (canCacheLineHeights(textWidget)) {
return getHeightBetweenLines(tp, 0, line) + tp.getSourceViewer().getTopInset() - textWidget.getTopPixel();
}
return textWidget.getLinePixel(line) - textWidget.getTopMargin() + tp.getSourceViewer().getTopInset();
}

private void paintSides(GC g, MergeSourceViewer tp, Canvas canvas, boolean right) {
validateLineHeightCache(tp);

Display display= canvas.getDisplay();

Expand Down Expand Up @@ -4802,8 +4839,6 @@ private void paintSides(GC g, MergeSourceViewer tp, Canvas canvas, boolean right

if (fMerger.hasChanges()) {
boolean allOutgoing = allOutgoing();
int shift = calculateShift(tp) + (2 - LW);

Point region= new Point(0, 0);
char leg = getLeg(tp);
for (Iterator<Diff> iterator = fMerger.changesIterator(); iterator.hasNext();) {
Expand All @@ -4814,7 +4849,7 @@ private void paintSides(GC g, MergeSourceViewer tp, Canvas canvas, boolean right

tp.getLineRange(diff.getPosition(leg), region);
region.x -= tp.getDocumentRegionOffset();
int y = getHeightBetweenLines(tp, 0, region.x) + shift;
int y = getLineY(tp, region.x) + (2 - LW);
int h = getHeightBetweenLines(tp, region.x, region.x + region.y);

if (y+h < 0) {
Expand Down Expand Up @@ -4853,6 +4888,7 @@ private boolean allOutgoing() {
}

private void paint(PaintEvent event, MergeSourceViewer tp) {
validateLineHeightCache(tp);

if (! fHighlightRanges) {
return;
Expand All @@ -4865,11 +4901,8 @@ private void paint(PaintEvent event, MergeSourceViewer tp) {
GC g= event.gc;

int w= canvas.getSize().x;
int shift = calculateShift(tp) + (2 - LW);
int maxh= event.y+event.height; // visibleHeight

shift += fTopInset;

Point range= new Point(0, 0);

char leg = getLeg(tp);
Expand All @@ -4882,7 +4915,7 @@ private void paint(PaintEvent event, MergeSourceViewer tp) {

tp.getLineRange(diff.getPosition(leg), range);
range.x -= tp.getDocumentRegionOffset();
int y = getHeightBetweenLines(tp, 0, range.x) + shift;
int y = getLineY(tp, range.x) + (2 - LW) + fTopInset;
int h = getHeightBetweenLines(tp, range.x, range.x + range.y);

if (y+h < event.y) {
Expand Down
Loading
Loading