Skip to content

Commit

Permalink
Don't scroll on caret movement to the end of editor
Browse files Browse the repository at this point in the history
When the caret is moved to the end of the editor, the sticky scrolling does not need to adapt the visible source lines.
  • Loading branch information
Christopher-Hermann authored and BeckerWdf committed Jul 12, 2024
1 parent 42f74bb commit db983ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -298,4 +369,9 @@ private Composite getStickyLineSeparator() {
return (Composite) canvas.getChildren()[2];
}

private void drainDisplayEventQueue() {
while (Display.getDefault().readAndDispatch()) {
}
}

}

0 comments on commit db983ef

Please sign in to comment.