diff --git a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java index 289f4d2caf4..114aa88d7e4 100644 --- a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java +++ b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java @@ -407,6 +407,9 @@ private void addSourceViewerListeners() { controlListener= new ControlListener() { @Override public void controlResized(ControlEvent e) { + if (areStickyLinesOutDated(textWidget)) { + return; + } limitVisibleStickyLinesToTextWidgetHeight(textWidget); layoutStickyLines(); if (stickyScrollingHandler != null) { @@ -422,6 +425,24 @@ public void controlMoved(ControlEvent e) { textWidget.addControlListener(controlListener); } + /** + * Checks if the sticky lines are out dated. Specifically, it verifies that the + * line number of the last sticky line does not exceed the total line count of + * the source viewer. + * + * This situation can occur, for example, when an editor is opened via the + * search view and "reuse editor" is enabled. In such cases, the text in the + * source viewer is replaced, but the out dated sticky lines associated with the + * previous source code remain in the first call. + */ + private boolean areStickyLinesOutDated(StyledText textWidget) { + if (stickyLines.size() > 0) { + int lastStickyLineNumber = stickyLines.get(stickyLines.size() - 1).getLineNumber(); + return lastStickyLineNumber > textWidget.getLineCount(); + } + return true; + } + private void limitVisibleStickyLinesToTextWidgetHeight(StyledText textWidget) { int lineHeight= textWidget.getLineHeight() + textWidget.getLineSpacing(); int textWidgetHeight= textWidget.getBounds().height; diff --git a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java index 4b8e63506de..04fdfd18012 100644 --- a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java +++ b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java @@ -252,6 +252,25 @@ public void testLayoutStickyLinesCanvasOnResize() { assertEquals(boundsAfterResize.height, boundsBeforeResize.height); } + @Test + public void testDontLayoutOutDatedStickyLinesOnResize() { + sourceViewer.getTextWidget().setBounds(0, 0, 200, 200); + + List stickyLines = List.of(new StickyLineStub("line 10", 10)); + stickyScrollingControl.setStickyLines(stickyLines); + + Canvas stickyControlCanvas = getStickyControlCanvas(shell); + Rectangle boundsBeforeResize = stickyControlCanvas.getBounds(); + + sourceViewer.getTextWidget().setBounds(0, 0, 150, 200); + + stickyControlCanvas = getStickyControlCanvas(shell); + Rectangle boundsAfterResize = stickyControlCanvas.getBounds(); + + // No IllegalArgumentException: Index out of bounds + assertEquals(boundsAfterResize, boundsBeforeResize); + } + @Test public void testNavigateToStickyLine() { String text = """