Skip to content

Commit

Permalink
Selection implementation cleanup (#916)
Browse files Browse the repository at this point in the history
* Removed unneeded code
* Improved variable names
  • Loading branch information
Jugen authored Mar 19, 2020
1 parent c5a7d66 commit 88d104a
Showing 1 changed file with 38 additions and 56 deletions.
94 changes: 38 additions & 56 deletions richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java
Original file line number Diff line number Diff line change
@@ -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<PS, SEG, S> 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(

0 comments on commit 88d104a

Please sign in to comment.