diff --git a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java index d250917b2..c8b404b07 100644 --- a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java +++ b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java @@ -159,6 +159,7 @@ public void end_position_is_correct_when_change_occurs_after_position() { }); } + @Test public void deletion_which_includes_selection_and_which_occurs_at_end_of_area_moves_selection_to_new_area_end() { interact(() -> { selection.selectRange(area.getLength(), area.getLength()); @@ -167,4 +168,16 @@ public void deletion_which_includes_selection_and_which_occurs_at_end_of_area_mo assertEquals(area.getLength(), selection.getEndPosition()); }); } + + @Test + public void anchor_updates_correctly_with_listener_attached() { + interact(() -> { + area.clear(); + area.anchorProperty().addListener( (ob,ov,nv) -> nv++ ); + area.appendText("asdf"); + area.selectRange(1,2); + assertEquals("s",area.getSelectedText()); + assertEquals(1,area.getAnchor()); + }); + } } diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java index 525e5ec65..a2290b096 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java @@ -431,6 +431,10 @@ public void displaceCaret(int position) { public void displaceSelection(int startPosition, int endPosition) { doUpdate(() -> { delegateSelection.selectRange(startPosition, endPosition); + + if ( startPosition < endPosition && internalStartedByAnchor.getValue() ) { + internalStartedByAnchor.setValue( false ); // See #874 + } internalStartedByAnchor.setValue(startPosition < endPosition); }); } @@ -449,6 +453,10 @@ public void dispose() { private void doSelect(int startPosition, int endPosition, boolean anchorIsStart) { doUpdate(() -> { delegateSelection.selectRange(startPosition, endPosition); + + if ( anchorIsStart && internalStartedByAnchor.getValue() ) { + internalStartedByAnchor.setValue( false ); // See #874 + } internalStartedByAnchor.setValue(anchorIsStart); delegateCaret.moveTo(anchorIsStart ? endPosition : startPosition);