From 51a28ab89428e927121436b078b73e0c3495a730 Mon Sep 17 00:00:00 2001 From: Tobias Melcher Date: Mon, 16 Nov 2026 15:05:24 +0100 Subject: [PATCH] Keep compare borders aligned when line heights change Preserve the per-viewer line-height cache and discard it when widget metrics or the cached prefix height no longer match SWT. Use current SWT line positions where caching is unsuitable so borders, connectors and mouse hit detection stay aligned after scrolling. Fixes #3076 --- .../contentmergeviewer/TextMergeViewer.java | 143 ++++++++----- .../tests/ui/TextMergeViewerGeometryTest.java | 197 ++++++++++++++++++ .../TextMergeViewerScrollRegressionTest.java | 106 ++++++++++ 3 files changed, 391 insertions(+), 55 deletions(-) create mode 100644 team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerGeometryTest.java create mode 100644 team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerScrollRegressionTest.java diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java index 7a41d53b058..b7fabb17111 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java @@ -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; @@ -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> lineHeightsByViewer = new HashMap<>(); + private final Map 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. @@ -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(); @@ -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();) { @@ -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) { @@ -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; @@ -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();) { @@ -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) { @@ -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(); } @@ -4543,43 +4566,53 @@ private int getHeightBetweenLines(MergeSourceViewer tp, int fromLine, int toLine if (fromLine == toLine) { return 0; } - StyledText w = tp.getSourceViewer().getTextWidget(); - List 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 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); } } @@ -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(); @@ -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(); @@ -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) { @@ -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(); @@ -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 iterator = fMerger.changesIterator(); iterator.hasNext();) { @@ -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) { @@ -4853,6 +4888,7 @@ private boolean allOutgoing() { } private void paint(PaintEvent event, MergeSourceViewer tp) { + validateLineHeightCache(tp); if (! fHighlightRanges) { return; @@ -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); @@ -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) { diff --git a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerGeometryTest.java b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerGeometryTest.java new file mode 100644 index 00000000000..914e138603d --- /dev/null +++ b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerGeometryTest.java @@ -0,0 +1,197 @@ +package org.eclipse.team.tests.ui; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.eclipse.compare.CompareConfiguration; +import org.eclipse.compare.contentmergeviewer.TextMergeViewer; +import org.eclipse.compare.internal.MergeSourceViewer; +import org.eclipse.compare.tests.ReflectionUtils; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.TextPresentation; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.events.PaintEvent; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Shell; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TextMergeViewerGeometryTest { + private Shell shell; + private TextMergeViewer viewer; + private MergeSourceViewer sourceViewer; + private StyledText textWidget; + + @BeforeEach + public void setUp() throws Exception { + shell = new Shell(Display.getDefault()); + shell.setLayout(new FillLayout()); + shell.setSize(800, 400); + viewer = new TextMergeViewer(shell, SWT.NONE, new CompareConfiguration()); + sourceViewer = (MergeSourceViewer) ReflectionUtils.getField(viewer, "fLeft", true); + sourceViewer.getSourceViewer().setDocument(new Document("unchanged context\n".repeat(100))); + textWidget = sourceViewer.getSourceViewer().getTextWidget(); + shell.open(); + } + + @AfterEach + public void tearDown() { + shell.dispose(); + } + + @Test + public void testLineHeightChangesWithoutDocumentChanges() throws Exception { + textWidget.setLineSpacing(50); + int initialHeight = heightBetweenLines(0, 80); + Object initialCache = caches().get(sourceViewer); + Font originalFont = textWidget.getFont(); + FontData[] fontData = originalFont.getFontData(); + for (FontData data : fontData) { + data.setHeight(data.getHeight() + 8); + } + Font largerFont = new Font(textWidget.getDisplay(), fontData); + try { + textWidget.setFont(largerFont); + textWidget.setTopIndex(70); + int expectedHeight = textWidget.getLinePixel(80) - textWidget.getLinePixel(0); + assertTrue(expectedHeight > initialHeight); + assertEquals(expectedHeight, heightBetweenLines(0, 80)); + assertNotSame(initialCache, caches().get(sourceViewer)); + assertEquals(textWidget.getLinePixel(81) - textWidget.getLinePixel(80), heightBetweenLines(80, 81)); + } finally { + textWidget.setFont(originalFont); + largerFont.dispose(); + } + } + + @Test + public void testLineSpacingAndIndentChangesWhileScrolled() throws Exception { + textWidget.setLineSpacing(50); + heightBetweenLines(0, 80); + textWidget.setLineSpacing(0); + textWidget.setLineSpacingProvider(line -> line % 3 == 0 ? 7 : 0); + textWidget.setLineVerticalIndent(75, 13); + textWidget.setTopIndex(70); + assertEquals(textWidget.getLinePixel(80) - textWidget.getLinePixel(0), heightBetweenLines(0, 80)); + assertEquals(textWidget.getLinePixel(76) - textWidget.getLinePixel(75), heightBetweenLines(75, 76)); + assertEquals(0, heightBetweenLines(80, 80)); + assertEquals(textWidget.getLinePixel(textWidget.getLineCount()) - textWidget.getLinePixel(99), + heightBetweenLines(99, textWidget.getLineCount())); + assertNull(caches().get(sourceViewer)); + } + + @Test + public void testOutOfDocumentHeightKeepsExistingEstimate() throws Exception { + textWidget.setLineVerticalIndent(0, 13); + for (int spacing : new int[] { 0, 50 }) { + textWidget.setLineSpacing(spacing); + int lastLine = textWidget.getLineCount(); + int expectedHeight = 3 * (textWidget.getLineHeight() + textWidget.getLineVerticalIndent(0)) + spacing; + assertEquals(expectedHeight, heightBetweenLines(lastLine, lastLine + 3)); + assertEquals(0, heightBetweenLines(lastLine + 3, lastLine + 3)); + } + } + + @Test + public void testCacheReusedWithUnchangedMetrics() throws Exception { + textWidget.setLineSpacing(50); + int initialHeight = heightBetweenLines(0, 80); + Object initialCache = caches().get(sourceViewer); + textWidget.setTopIndex(70); + assertEquals(initialHeight, heightBetweenLines(0, 80)); + assertSame(initialCache, caches().get(sourceViewer)); + } + + @Test + public void testCachedIndentAndSpacingChanges() throws Exception { + textWidget.setLineSpacing(50); + int initialHeight = heightBetweenLines(0, 80); + textWidget.setLineVerticalIndent(75, 13); + assertEquals(initialHeight + 13, heightBetweenLines(0, 80)); + textWidget.setLineSpacing(60); + assertEquals(textWidget.getLinePixel(80) - textWidget.getLinePixel(0), heightBetweenLines(0, 80)); + } + + @Test + public void testCacheInvalidatedByDocumentChanges() throws Exception { + textWidget.setLineSpacing(50); + heightBetweenLines(0, 80); + Object initialCache = caches().get(sourceViewer); + sourceViewer.getSourceViewer().getDocument().replace(0, 0, "inserted line\n"); + assertEquals(textWidget.getLinePixel(80) - textWidget.getLinePixel(0), heightBetweenLines(0, 80)); + assertNotSame(initialCache, caches().get(sourceViewer)); + } + + @Test + public void testWordWrapBypassesCache() throws Exception { + textWidget.setLineSpacing(50); + heightBetweenLines(0, 80); + textWidget.setWordWrap(true); + assertEquals(textWidget.getLinePixel(80) - textWidget.getLinePixel(0), heightBetweenLines(0, 80)); + assertNull(caches().get(sourceViewer)); + } + + @Test + public void testCacheInvalidatedByPresentationChanges() throws Exception { + textWidget.setLineSpacing(50); + heightBetweenLines(0, 80); + Object initialCache = caches().get(sourceViewer); + TextPresentation presentation = new TextPresentation(); + presentation.addStyleRange(new StyleRange(0, 4, null, null, SWT.BOLD)); + sourceViewer.getSourceViewer().changeTextPresentation(presentation, true); + assertEquals(textWidget.getLinePixel(80) - textWidget.getLinePixel(0), heightBetweenLines(0, 80)); + assertNotSame(initialCache, caches().get(sourceViewer)); + } + + @Test + public void testCheckpointOnlyUsesPopulatedPrefix() throws Exception { + textWidget.setLineSpacing(50); + int initialHeight = heightBetweenLines(0, 20); + Object initialCache = caches().get(sourceViewer); + assertEquals(20, ReflectionUtils.getField(initialCache, "prefixLines")); + int[] heights = (int[]) ReflectionUtils.getField(initialCache, "heights"); + assertEquals(-1, heights[20]); + textWidget.setLineVerticalIndent(10, 13); + assertEquals(initialHeight + 13, heightBetweenLines(0, 20)); + Object refreshedCache = caches().get(sourceViewer); + assertNotSame(initialCache, refreshedCache); + assertEquals(20, ReflectionUtils.getField(refreshedCache, "prefixLines")); + assertEquals(-1, ((int[]) ReflectionUtils.getField(refreshedCache, "heights"))[20]); + } + + private Map caches() throws Exception { + return (Map) ReflectionUtils.getField(viewer, "lineHeightsByViewer", true); + } + + private int heightBetweenLines(int fromLine, int toLine) throws Exception { + Method paint = TextMergeViewer.class.getDeclaredMethod("paint", PaintEvent.class, MergeSourceViewer.class); + paint.setAccessible(true); + GC gc = new GC(textWidget); + try { + Event event = new Event(); + event.widget = textWidget; + event.gc = gc; + paint.invoke(viewer, new PaintEvent(event), sourceViewer); + } finally { + gc.dispose(); + } + Method method = TextMergeViewer.class.getDeclaredMethod("getHeightBetweenLines", MergeSourceViewer.class, + int.class, int.class); + method.setAccessible(true); + return (int) method.invoke(viewer, sourceViewer, fromLine, toLine); + } +} diff --git a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerScrollRegressionTest.java b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerScrollRegressionTest.java new file mode 100644 index 00000000000..52c4aab5bd3 --- /dev/null +++ b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/TextMergeViewerScrollRegressionTest.java @@ -0,0 +1,106 @@ +package org.eclipse.team.tests.ui; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Method; + +import org.eclipse.compare.CompareConfiguration; +import org.eclipse.compare.contentmergeviewer.TextMergeViewer; +import org.eclipse.compare.internal.MergeSourceViewer; +import org.eclipse.compare.tests.ReflectionUtils; +import org.eclipse.jface.text.Document; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.PaintEvent; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Shell; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TextMergeViewerScrollRegressionTest { + private Shell shell; + private TextMergeViewer viewer; + private MergeSourceViewer sourceViewer; + private StyledText textWidget; + + @BeforeEach + public void setUp() throws Exception { + shell = new Shell(Display.getDefault()); + shell.setLayout(new FillLayout()); + shell.setSize(800, 400); + viewer = new TextMergeViewer(shell, SWT.NONE, new CompareConfiguration()); + sourceViewer = (MergeSourceViewer) ReflectionUtils.getField(viewer, "fLeft", true); + sourceViewer.getSourceViewer().setDocument(new Document("unchanged context\n".repeat(100))); + textWidget = sourceViewer.getSourceViewer().getTextWidget(); + textWidget.setLineSpacing(50); + shell.open(); + } + + @AfterEach + public void tearDown() { + shell.dispose(); + } + + @Test + public void testRepaintAfterFontHeightChangesWhileScrolled() throws Exception { + int initialHeight = heightBetweenLines(0, 80); + Font originalFont = textWidget.getFont(); + FontData[] fontData = originalFont.getFontData(); + for (FontData data : fontData) { + data.setHeight(data.getHeight() + 8); + } + Font largerFont = new Font(textWidget.getDisplay(), fontData); + try { + textWidget.setFont(largerFont); + textWidget.setTopIndex(70); + repaint(); + int expectedHeight = textWidget.getLinePixel(80) - textWidget.getLinePixel(0); + assertTrue(expectedHeight > initialHeight); + assertEquals(expectedHeight, heightBetweenLines(0, 80)); + } finally { + textWidget.setFont(originalFont); + largerFont.dispose(); + } + } + + @Test + public void testRepaintDetectsStalePrefixWithUnchangedBaseMetrics() throws Exception { + int initialHeight = heightBetweenLines(0, 80); + int baseHeight = textWidget.getLineHeight(); + textWidget.setLineVerticalIndent(75, 13); + textWidget.setTopIndex(70); + assertEquals(baseHeight, textWidget.getLineHeight()); + assertEquals(initialHeight, heightBetweenLines(0, 80)); + repaint(); + assertEquals(initialHeight + 13, heightBetweenLines(0, 80)); + assertEquals(textWidget.getLinePixel(80) - textWidget.getLinePixel(0), heightBetweenLines(0, 80)); + } + + private int heightBetweenLines(int fromLine, int toLine) throws Exception { + Method method = TextMergeViewer.class.getDeclaredMethod("getHeightBetweenLines", MergeSourceViewer.class, + int.class, int.class); + method.setAccessible(true); + return (int) method.invoke(viewer, sourceViewer, fromLine, toLine); + } + + private void repaint() throws Exception { + Method method = TextMergeViewer.class.getDeclaredMethod("paint", PaintEvent.class, MergeSourceViewer.class); + method.setAccessible(true); + GC gc = new GC(textWidget); + try { + Event event = new Event(); + event.widget = textWidget; + event.gc = gc; + method.invoke(viewer, new PaintEvent(event), sourceViewer); + } finally { + gc.dispose(); + } + } +}