From cc11721199a768ddcf93bb6f984c4702607fd3c1 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Mon, 12 Nov 2018 15:09:11 +0200 Subject: [PATCH] Fixes bug #780 After undo (ctrl-Z), text insertion point jumps to the start (#785) --- .../fxmisc/richtext/api/UndoManagerTests.java | 17 +++++++++++++++++ .../java/org/fxmisc/richtext/SelectionImpl.java | 6 +++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/UndoManagerTests.java b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/UndoManagerTests.java index 6ece706f8..2e9c1dfe6 100644 --- a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/UndoManagerTests.java +++ b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/UndoManagerTests.java @@ -16,6 +16,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; @RunWith(NestedRunner.class) public class UndoManagerTests { @@ -40,6 +41,22 @@ public void incoming_change_is_not_merged_after_period_of_user_inactivity() { assertEquals("", area.getText()); } + @Test // After undo, text insertion point jumps to the start of the text area #780 + public void undo_leaves_correct_insertion_point() { + long periodOfUserInactivity = UndoUtils.DEFAULT_PREVENT_MERGE_DELAY.toMillis() + 300L; + + write("abc def "); + sleep(periodOfUserInactivity); + + write("xyz"); + interact(area::undo); + + write('g'); + + sleep(periodOfUserInactivity); + assertTrue(area.getText().endsWith("g")); + } + @Test public void testUndoWithWinNewlines() { String text1 = "abc\r\ndef"; diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java index 7d036fa14..e3f6ff808 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java @@ -269,9 +269,9 @@ public SelectionImpl(String name, GenericStyledArea area, int startP // (prevents a StringIndexOutOfBoundsException because // end is one char farther than area's length). - if (getLength() < getEndPosition()) { - finalStart = getLength(); - finalEnd = getLength(); + if (getEndPosition() > 0) { + finalStart = area.getLength(); + finalEnd = finalStart; } } }