From 88d104af19cf59e9851d257939b5980806f343b0 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Thu, 19 Mar 2020 13:18:06 +0200 Subject: [PATCH] Selection implementation cleanup (#916) * Removed unneeded code * Improved variable names --- .../org/fxmisc/richtext/SelectionImpl.java | 94 ++++++++----------- 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java index 5e332b11f..54168e24c 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java @@ -21,7 +21,6 @@ import java.util.function.Function; import java.util.function.IntSupplier; -import static org.fxmisc.richtext.GenericStyledArea.EMPTY_RANGE; import static org.fxmisc.richtext.model.TwoDimensional.Bias.Backward; import static org.fxmisc.richtext.model.TwoDimensional.Bias.Forward; import static org.reactfx.EventStreams.invalidationsOf; @@ -222,64 +221,47 @@ public SelectionImpl(String name, GenericStyledArea area, int startP ).suspendable(); manageSubscription(area.multiPlainChanges(), list -> { - int finalStart = getStartPosition(); - int finalEnd = getEndPosition(); + int selectStart = getStartPosition(); + int selectEnd = getEndPosition(); for (PlainTextChange plainTextChange : list) { - int netLength = plainTextChange.getNetLength(); - //if (netLength != 0) Causes IndexOutOfBoundsException in ParagraphText.getRangeShapeSafely issue #689 - // but can be safely reimplemented if this causes other issues. - { - int indexOfChange = plainTextChange.getPosition(); - // in case of a replacement: "hello there" -> "hi." - int endOfChange = indexOfChange + Math.abs(netLength); - - if (getLength() != 0) { - - /* - "->" means add (positive) netLength to position - "<-" means add (negative) netLength to position - "x" means don't update position - - "start / end" means what should be done in each case for each anchor if they differ - - "+a" means one of the anchors was included in the deleted portion of content - "-a" means one of the anchors was not included in the deleted portion of content - Before/At/After means indexOfChange "<" / "==" / ">" position - - | Before +a | Before -a | At | After - -------+---------------+-----------+--------+------ - Add | N/A | -> | -> / x | x - Delete | indexOfChange | <- | x | x - */ - if (indexOfChange == finalStart && netLength > 0) { - finalStart = finalStart + netLength; - } else if (indexOfChange < finalStart) { - finalStart = finalStart < endOfChange - ? indexOfChange - : finalStart + netLength; - } - if (indexOfChange < finalEnd) { - finalEnd = finalEnd < endOfChange - ? indexOfChange - : finalEnd + netLength; - } - if (finalStart > finalEnd) { - finalStart = finalEnd; - } - } else { - // force-update internalSelection in case empty selection is - // at the end of area and a character was deleted - // (prevents a StringIndexOutOfBoundsException because - // end is one char farther than area's length). - - if (getEndPosition() > 0) { - finalStart = area.getLength(); - finalEnd = finalStart; - } - } + int changeLength = plainTextChange.getNetLength(); + int indexOfChange = plainTextChange.getPosition(); + // in case of a replacement: "hello there" -> "hi." + int endOfChange = indexOfChange + Math.abs(changeLength); + + /* + "->" means add (positive) netLength to position + "<-" means add (negative) netLength to position + "x" means don't update position + + "start / end" means what should be done in each case for each anchor if they differ + + "+a" means one of the anchors was included in the deleted portion of content + "-a" means one of the anchors was not included in the deleted portion of content + Before/At/After means indexOfChange "<" / "==" / ">" position + + | Before +a | Before -a | At | After + -------+---------------+-----------+--------+------ + Add | N/A | -> | -> / x | x + Delete | indexOfChange | <- | x | x + */ + if (indexOfChange == selectStart && changeLength > 0) { + selectStart = selectStart + changeLength; + } else if (indexOfChange < selectStart) { + selectStart = selectStart < endOfChange + ? indexOfChange + : selectStart + changeLength; + } + if (indexOfChange < selectEnd) { + selectEnd = selectEnd < endOfChange + ? indexOfChange + : selectEnd + changeLength; + } + if (selectStart > selectEnd) { + selectStart = selectEnd; } } - selectRange(finalStart, finalEnd); + selectRange(selectStart, selectEnd); }); Suspendable omniSuspendable = Suspendable.combine(