Skip to content

Commit e487947

Browse files
Merge pull request #470 from JordanMartinez/fixMoveSelectedTextBug
Fix move selected text bug
2 parents cb3850f + 2a0dbd5 commit e487947

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.fxmisc.richtext.model;
22

33
import javafx.scene.control.IndexRange;
4+
import org.fxmisc.richtext.GenericStyledArea;
45

56
/**
67
* Extended edit actions for {@link TextEditingArea}.
@@ -176,7 +177,7 @@ default void replaceSelection(StyledDocument<PS, SEG, S> replacement) {
176177
default void moveSelectedText(int pos) {
177178
IndexRange sel = getSelection();
178179

179-
if(pos >= sel.getStart() && pos <= sel.getEnd()) {
180+
if((pos >= sel.getStart() && pos <= sel.getEnd()) || sel.equals(GenericStyledArea.EMPTY_RANGE)) {
180181
// no move, just position the caret
181182
selectRange(pos, pos);
182183
} else {

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,7 @@ public static enum ChangeType {
1515
}
1616

1717
private ChangeType type;
18-
public final ChangeType getType() {
19-
if (type == null) {
20-
if (insertedLength() == 0) {
21-
if (removedLength() == 0) {
22-
throw new IllegalStateException("Cannot get the type of a change that neither inserts nor deletes anything.");
23-
} else {
24-
type = ChangeType.DELETION;
25-
}
26-
} else if (removedLength() == 0) {
27-
type = ChangeType.INSERTION;
28-
} else {
29-
type = ChangeType.REPLACEMENT;
30-
}
31-
}
32-
return type;
33-
}
18+
public final ChangeType getType() { return type; }
3419

3520
protected final int position;
3621
protected final S removed;
@@ -40,6 +25,18 @@ public TextChange(int position, S removed, S inserted) {
4025
this.position = position;
4126
this.removed = removed;
4227
this.inserted = inserted;
28+
29+
if (insertedLength() == 0) {
30+
if (removedLength() == 0) {
31+
throw new IllegalStateException("Cannot get the type of a change that neither inserts nor deletes anything.");
32+
} else {
33+
type = ChangeType.DELETION;
34+
}
35+
} else if (removedLength() == 0) {
36+
type = ChangeType.INSERTION;
37+
} else {
38+
type = ChangeType.REPLACEMENT;
39+
}
4340
}
4441

4542
public int getPosition() { return position; };
@@ -97,7 +94,7 @@ public final String toString() {
9794
return
9895
this.getClass().getSimpleName() + "{\n" +
9996
"\tposition: " + position + "\n" +
100-
"\ttype: " + getType() + "\n" +
97+
"\ttype: " + type + "\n" +
10198
"\tremoved: " + removed + "\n" +
10299
"\tinserted: " + inserted + "\n" +
103100
"}";

0 commit comments

Comments
 (0)