Skip to content

Commit ff6074e

Browse files
Merge pull request #541 from JordanMartinez/cleanupCaretSelection
Cleanup caret selection
2 parents 18c583d + 58232a2 commit ff6074e

7 files changed

Lines changed: 49 additions & 49 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/BoundedSelection.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* An object for encapsulating a selection in a given area that is bound to an underlying caret. In other words,
8-
* {@link #selectRange(int, int) selecting some range in the area} will move a caret in the same call.
8+
* {@link #selectRangeExpl(int, int) selecting some range in the area} will move a caret in the same call.
99
*
1010
* <p>
1111
* <b>"Position"</b> refers to the place in-between characters. In other words, every {@code "|"} in
@@ -25,13 +25,13 @@
2525
*
2626
* <p>
2727
* The selection is typically made using the {@link #getAnchorPosition() anchor's position} and
28-
* the underlying {@link Caret#getPosition() caret's position}. Hence, {@link #selectRange(int, int)}
29-
* is the typical method to use, although {@link #selectRange0(int, int)} can also be used.
28+
* the underlying {@link Caret#getPosition() caret's position}. Hence, {@link #selectRangeExpl(int, int)}
29+
* is the typical method to use, although {@link #selectRange(int, int)} can also be used.
3030
* </p>
3131
* <p>
3232
* Be careful about calling the underlying {@link Caret#moveTo(int)} method. This will displace the caret
3333
* from the selection bounds and may lead to undesirable/unexpected behavior. If this is done, a
34-
* {@link #selectRange(int, int)} call will reposition the caret, so that it is either the start or end
34+
* {@link #selectRangeExpl(int, int)} call will reposition the caret, so that it is either the start or end
3535
* bound of this selection.
3636
* </p>
3737
*
@@ -78,16 +78,13 @@ default BoundedSelection asBoundedSelection() {
7878
/**
7979
* Positions the anchor and caretPosition explicitly,
8080
* effectively creating a selection.
81-
*
82-
* <p><b>Caution:</b> see {@link org.fxmisc.richtext.model.TextEditingArea#getAbsolutePosition(int, int)}
83-
* to know how the column index argument can affect the returned position.</p>
8481
*/
85-
void selectRange(int anchorParagraph, int anchorColumn, int caretParagraph, int caretColumn);
82+
void selectRangeExpl(int anchorParagraph, int anchorColumn, int caretParagraph, int caretColumn);
8683

8784
/**
8885
* Positions the anchor and caretPosition explicitly,
8986
* effectively creating a selection.
9087
*/
91-
void selectRange(int anchorPosition, int caretPosition);
88+
void selectRangeExpl(int anchorPosition, int caretPosition);
9289

9390
}

richtextfx/src/main/java/org/fxmisc/richtext/BoundedSelectionImpl.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ final class BoundedSelectionImpl<PS, SEG, S> implements BoundedSelection<PS, SEG
124124
}
125125

126126
@Override
127-
public void selectRange(int anchorParagraph, int anchorColumn, int caretParagraph, int caretColumn) {
128-
selectRange(textPosition(anchorParagraph, anchorColumn), textPosition(caretParagraph, caretColumn));
127+
public void selectRangeExpl(int anchorParagraph, int anchorColumn, int caretParagraph, int caretColumn) {
128+
selectRangeExpl(textPosition(anchorParagraph, anchorColumn), textPosition(caretParagraph, caretColumn));
129129
}
130130

131131
@Override
132-
public void selectRange(int anchorPosition, int caretPosition) {
132+
public void selectRangeExpl(int anchorPosition, int caretPosition) {
133133
if (anchorPosition <= caretPosition) {
134134
doSelect(anchorPosition, caretPosition, true);
135135
} else {
@@ -138,34 +138,34 @@ public void selectRange(int anchorPosition, int caretPosition) {
138138
}
139139

140140
@Override
141-
public void selectRange0(int startPosition, int endPosition) {
141+
public void selectRange(int startPosition, int endPosition) {
142142
doSelect(startPosition, endPosition, anchorIsStart());
143143
}
144144

145145
@Override
146-
public void selectRange0(int startParagraphIndex, int startColPosition, int endParagraphIndex, int endColPosition) {
147-
selectRange0(textPosition(startParagraphIndex, startColPosition), textPosition(endParagraphIndex, endColPosition));
146+
public void selectRange(int startParagraphIndex, int startColPosition, int endParagraphIndex, int endColPosition) {
147+
selectRange(textPosition(startParagraphIndex, startColPosition), textPosition(endParagraphIndex, endColPosition));
148148
}
149149

150150
@Override
151151
public void moveStartBy(int amount, Direction direction) {
152152
int updatedStart = direction == Direction.LEFT
153153
? getStartPosition() - amount
154154
: getStartPosition() + amount;
155-
selectRange0(updatedStart, getEndPosition());
155+
selectRange(updatedStart, getEndPosition());
156156
}
157157

158158
@Override
159159
public void moveEndBy(int amount, Direction direction) {
160160
int updatedEnd = direction == Direction.LEFT
161161
? getEndPosition() - amount
162162
: getEndPosition() + amount;
163-
selectRange0(getStartPosition(), updatedEnd);
163+
selectRange(getStartPosition(), updatedEnd);
164164
}
165165

166166
@Override
167167
public void moveStartTo(int position) {
168-
selectRange0(position, getEndPosition());
168+
selectRange(position, getEndPosition());
169169
}
170170

171171
@Override
@@ -175,7 +175,7 @@ public void moveStartTo(int paragraphIndex, int columnPosition) {
175175

176176
@Override
177177
public void moveEndTo(int position) {
178-
selectRange0(getStartPosition(), position);
178+
selectRange(getStartPosition(), position);
179179
}
180180

181181
@Override
@@ -190,7 +190,7 @@ public void dispose() {
190190

191191
private void doSelect(int startPosition, int endPosition, boolean anchorIsStart) {
192192
Runnable updateRange = () -> {
193-
delegate.selectRange0(startPosition, endPosition);
193+
delegate.selectRange(startPosition, endPosition);
194194
internalStartedByAnchor.setValue(anchorIsStart);
195195

196196
caret.moveTo(anchorIsStart ? endPosition : startPosition);

richtextfx/src/main/java/org/fxmisc/richtext/CaretImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ final class CaretImpl implements Caret {
8383
}
8484

8585
CaretImpl(GenericStyledArea<?, ?, ?> area, int startingPosition) {
86-
this(area, area.beingUpdatedProperty(), 0);
86+
this(area, area.beingUpdatedProperty(), startingPosition);
8787
}
8888

8989
CaretImpl(GenericStyledArea<?, ?, ?> area, SuspendableNo dependentBeingUpdated, int startingPosition) {
@@ -102,20 +102,20 @@ final class CaretImpl implements Caret {
102102
// when content is updated by an area, update the caret of all the other
103103
// clones that also display the same document
104104
manageSubscription(area.plainTextChanges(), (plainTextChange -> {
105-
int changeLength = plainTextChange.getInserted().length() - plainTextChange.getRemoved().length();
106-
if (changeLength != 0) {
105+
int netLength = plainTextChange.getNetLength();
106+
if (netLength != 0) {
107107
int indexOfChange = plainTextChange.getPosition();
108108
// in case of a replacement: "hello there" -> "hi."
109-
int endOfChange = indexOfChange + Math.abs(changeLength);
109+
int endOfChange = indexOfChange + Math.abs(netLength);
110110

111111
int caretPosition = getPosition();
112112
if (indexOfChange < caretPosition) {
113113
// if caret is within the changed content, move it to indexOfChange
114-
// otherwise offset it by changeLength
114+
// otherwise offset it by netLength
115115
moveTo(
116116
caretPosition < endOfChange
117117
? indexOfChange
118-
: caretPosition + changeLength
118+
: caretPosition + netLength
119119
);
120120
}
121121
}

richtextfx/src/main/java/org/fxmisc/richtext/UnboundedSelection.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/**
1212
* An object for encapsulating a selection in a given area that is not bound to any caret. In other words,
13-
* {@link #selectRange0(int, int) selecting some range in the area} will not also move a caret in the same call.
13+
* {@link #selectRange(int, int) selecting some range in the area} will not also move a caret in the same call.
1414
*
1515
* <p>
1616
* <b>"Position"</b> refers to the place in-between characters. In other words, every {@code "|"} in
@@ -132,19 +132,17 @@ default BoundedSelection asBoundedSelection() {
132132

133133

134134
/**
135-
* Selects the given range. Note: this method's "0" suffix distinguishes it's signature from
136-
* {@link BoundedSelection#selectRange(int, int, int, int)}.
135+
* Selects the given range.
137136
*
138137
* <p><b>Caution:</b> see {@link org.fxmisc.richtext.model.TextEditingArea#getAbsolutePosition(int, int)} to
139138
* know how the column index argument can affect the returned position.</p>
140139
*/
141-
void selectRange0(int startParagraphIndex, int startColPosition, int endParagraphIndex, int endColPosition);
140+
void selectRange(int startParagraphIndex, int startColPosition, int endParagraphIndex, int endColPosition);
142141

143142
/**
144-
* Selects the given range. Note: this method's "0" suffix distinguishes it's signature from
145-
* {@link BoundedSelection#selectRange(int, int)}.
143+
* Selects the given range.
146144
*/
147-
void selectRange0(int startPosition, int endPosition);
145+
void selectRange(int startPosition, int endPosition);
148146

149147
void moveStartBy(int amount, Direction direction);
150148

richtextfx/src/main/java/org/fxmisc/richtext/UnboundedSelectionImpl.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,38 +157,38 @@ public UnboundedSelectionImpl(GenericStyledArea<PS, SEG, S> area, SuspendableNo
157157
);
158158

159159
manageSubscription(area.plainTextChanges(), plainTextChange -> {
160-
int changeLength = plainTextChange.getInserted().length() - plainTextChange.getRemoved().length();
161-
if (changeLength != 0) {
160+
int netLength = plainTextChange.getNetLength();
161+
if (netLength != 0) {
162162
int indexOfChange = plainTextChange.getPosition();
163163
// in case of a replacement: "hello there" -> "hi."
164-
int endOfChange = indexOfChange + Math.abs(changeLength);
164+
int endOfChange = indexOfChange + Math.abs(netLength);
165165

166166
if (getLength() != 0) {
167167
int selectionStart = getStartPosition();
168168
int selectionEnd = getEndPosition();
169169

170170
// if start/end is within the changed content, move it to indexOfChange
171-
// otherwise, offset it by changeLength
171+
// otherwise, offset it by netLength
172172
// Note: if both are moved to indexOfChange, selection is empty.
173173
if (indexOfChange < selectionStart) {
174174
selectionStart = selectionStart < endOfChange
175175
? indexOfChange
176-
: selectionStart + changeLength;
176+
: selectionStart + netLength;
177177
}
178178
if (indexOfChange < selectionEnd) {
179179
selectionEnd = selectionEnd < endOfChange
180180
? indexOfChange
181-
: selectionEnd + changeLength;
181+
: selectionEnd + netLength;
182182
}
183-
selectRange0(selectionStart, selectionEnd);
183+
selectRange(selectionStart, selectionEnd);
184184
} else {
185185
// force-update internalSelection in case empty selection is
186186
// at the end of area and a character was deleted
187187
// (prevents a StringIndexOutOfBoundsException because
188188
// end is one char farther than area's length).
189189

190190
if (getLength() < getEndPosition()) {
191-
selectRange0(getLength(), getLength());
191+
selectRange(getLength(), getLength());
192192
}
193193
}
194194
}
@@ -217,12 +217,12 @@ public UnboundedSelectionImpl(GenericStyledArea<PS, SEG, S> area, SuspendableNo
217217
}
218218

219219
@Override
220-
public void selectRange0(int startParagraphIndex, int startColPosition, int endParagraphIndex, int endColPosition) {
221-
selectRange0(textPosition(startParagraphIndex, startColPosition), textPosition(endParagraphIndex, endColPosition));
220+
public void selectRange(int startParagraphIndex, int startColPosition, int endParagraphIndex, int endColPosition) {
221+
selectRange(textPosition(startParagraphIndex, startColPosition), textPosition(endParagraphIndex, endColPosition));
222222
}
223223

224224
@Override
225-
public void selectRange0(int startPosition, int endPosition) {
225+
public void selectRange(int startPosition, int endPosition) {
226226
selectRange(new IndexRange(startPosition, endPosition));
227227
}
228228

@@ -252,22 +252,22 @@ direction, amount, getEndPosition(),
252252

253253
@Override
254254
public void moveStartTo(int position) {
255-
selectRange0(position, getEndPosition());
255+
selectRange(position, getEndPosition());
256256
}
257257

258258
@Override
259259
public void moveStartTo(int paragraphIndex, int columnPosition) {
260-
selectRange0(textPosition(paragraphIndex, columnPosition), getEndPosition());
260+
selectRange(textPosition(paragraphIndex, columnPosition), getEndPosition());
261261
}
262262

263263
@Override
264264
public void moveEndTo(int position) {
265-
selectRange0(getStartPosition(), position);
265+
selectRange(getStartPosition(), position);
266266
}
267267

268268
@Override
269269
public void moveEndTo(int paragraphIndex, int columnPosition) {
270-
selectRange0(getStartPosition(), textPosition(paragraphIndex, columnPosition));
270+
selectRange(getStartPosition(), textPosition(paragraphIndex, columnPosition));
271271
}
272272

273273
@Override

richtextfx/src/main/java/org/fxmisc/richtext/model/TextChange.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public TextChange(int position, S removed, S inserted) {
2222
public int getRemovalEnd() { return position + removedLength(); }
2323
public int getInsertionEnd() { return position + insertedLength(); }
2424

25+
/**
26+
* Gets the net length of this change (i.e., {@code insertedLength() - removedLength()})
27+
*/
28+
public int getNetLength() { return insertedLength() - removedLength(); }
29+
2530
protected abstract int removedLength();
2631
protected abstract int insertedLength();
2732
protected abstract S concat(S a, S b);

richtextfx/src/main/java/org/fxmisc/richtext/model/TextEditingArea.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ default StyledDocument<PS, SEG, S> subDocument(int startParagraph, int startColu
220220
* effectively creating a selection.
221221
*/
222222
default void selectRange(int anchor, int caretPosition) {
223-
getMainSelection().selectRange(anchor, caretPosition);
223+
getMainSelection().selectRangeExpl(anchor, caretPosition);
224224
}
225225

226226
/**

0 commit comments

Comments
 (0)