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 59078bc5a88..0a5234b3ae7 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 @@ -507,7 +507,8 @@ public void keyReleased(KeyEvent e) { @Override public void caretMoved(CaretEvent event) { - if (event.caretOffset == 0) { + int offsetEndPosition = sourceViewer.getTextWidget().getText().length(); + if (event.caretOffset == 0 || event.caretOffset == offsetEndPosition) { return; } Display.getDefault().asyncExec(() -> { 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 1dece650d71..06129dcea7e 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 @@ -69,7 +69,7 @@ public void setup() { hoverColor = new Color(1, 1, 1); backgroundColor = new Color(2, 2, 2); separatorColor = new Color(3, 3, 3); - StickyScrollingControlSettings settings = new StickyScrollingControlSettings(5, lineNumberColor, hoverColor, + StickyScrollingControlSettings settings = new StickyScrollingControlSettings(2, lineNumberColor, hoverColor, backgroundColor, separatorColor, true); stickyScrollingControl = new StickyScrollingControl(sourceViewer, ruler, settings, null); } @@ -143,7 +143,7 @@ public void testCopyStyleRanges() { } @Test - public void testWithoutVeticalRuler() { + public void testWithoutVerticalRuler() { sourceViewer = new SourceViewer(shell, null, SWT.None); StickyScrollingControlSettings settings = new StickyScrollingControlSettings(5, lineNumberColor, hoverColor, backgroundColor, separatorColor, true); @@ -267,6 +267,77 @@ public void limitStickyLinesToTextWidgetHeight() { assertEquals("", stickyLineText.getText()); } + @Test + public void testListenForCarretAfterKeyDown() { + // set height to 10 so that scrolling is needed + sourceViewer.getTextWidget().setBounds(0, 0, 200, 10); + String text = """ + line 1 + line 2 + line 3 + line 4 + line 5 + line 6 + line 7 + line 8 + line 9"""; + sourceViewer.setInput(new Document(text)); + assertEquals(0, sourceViewer.getTextWidget().getTopPixel()); + + sourceViewer.getTextWidget().notifyListeners(SWT.KeyDown, new Event()); + sourceViewer.getTextWidget().setCaretOffset(42); + + drainDisplayEventQueue(); + assertThat(sourceViewer.getTextWidget().getTopPixel(), greaterThan(0)); + } + + @Test + public void testDontListenForCaretWithoutKeyDown() { + // set height to 10 so that scrolling is needed + sourceViewer.getTextWidget().setBounds(0, 0, 200, 10); + String text = """ + line 1 + line 2 + line 3 + line 4 + line 5 + line 6 + line 7 + line 8 + line 9"""; + sourceViewer.setInput(new Document(text)); + assertEquals(0, sourceViewer.getTextWidget().getTopPixel()); + + sourceViewer.getTextWidget().setCaretOffset(42); + + drainDisplayEventQueue(); + assertEquals(0, sourceViewer.getTextWidget().getTopPixel()); + } + + @Test + public void testDontMoveCaretAtDocumentEnd() { + // set height to 10 so that scrolling is needed + sourceViewer.getTextWidget().setBounds(0, 0, 200, 10); + String text = """ + line 1 + line 2 + line 3 + line 4 + line 5 + line 6 + line 7 + line 8 + line 9"""; + sourceViewer.setInput(new Document(text)); + assertEquals(0, sourceViewer.getTextWidget().getTopPixel()); + + sourceViewer.getTextWidget().notifyListeners(SWT.KeyDown, new Event()); + sourceViewer.getTextWidget().setCaretOffset(62); + + drainDisplayEventQueue(); + assertEquals(0, sourceViewer.getTextWidget().getTopPixel()); + } + private Canvas getStickyControlCanvas(Composite composite) { for (Control control : composite.getChildren()) { if (control instanceof Canvas canvas) { @@ -298,4 +369,9 @@ private Composite getStickyLineSeparator() { return (Composite) canvas.getChildren()[2]; } + private void drainDisplayEventQueue() { + while (Display.getDefault().readAndDispatch()) { + } + } + }