Skip to content

Commit

Permalink
Merge pull request #470 from JordanMartinez/fixMoveSelectedTextBug
Browse files Browse the repository at this point in the history
Fix move selected text bug
  • Loading branch information
JordanMartinez committed Mar 25, 2017
2 parents cb3850f + 2a0dbd5 commit e487947
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fxmisc.richtext.model;

import javafx.scene.control.IndexRange;
import org.fxmisc.richtext.GenericStyledArea;

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

if(pos >= sel.getStart() && pos <= sel.getEnd()) {
if((pos >= sel.getStart() && pos <= sel.getEnd()) || sel.equals(GenericStyledArea.EMPTY_RANGE)) {
// no move, just position the caret
selectRange(pos, pos);
} else {
Expand Down
31 changes: 14 additions & 17 deletions richtextfx/src/main/java/org/fxmisc/richtext/model/TextChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,7 @@ public static enum ChangeType {
}

private ChangeType type;
public final ChangeType getType() {
if (type == null) {
if (insertedLength() == 0) {
if (removedLength() == 0) {
throw new IllegalStateException("Cannot get the type of a change that neither inserts nor deletes anything.");
} else {
type = ChangeType.DELETION;
}
} else if (removedLength() == 0) {
type = ChangeType.INSERTION;
} else {
type = ChangeType.REPLACEMENT;
}
}
return type;
}
public final ChangeType getType() { return type; }

protected final int position;
protected final S removed;
Expand All @@ -40,6 +25,18 @@ public TextChange(int position, S removed, S inserted) {
this.position = position;
this.removed = removed;
this.inserted = inserted;

if (insertedLength() == 0) {
if (removedLength() == 0) {
throw new IllegalStateException("Cannot get the type of a change that neither inserts nor deletes anything.");
} else {
type = ChangeType.DELETION;
}
} else if (removedLength() == 0) {
type = ChangeType.INSERTION;
} else {
type = ChangeType.REPLACEMENT;
}
}

public int getPosition() { return position; };
Expand Down Expand Up @@ -97,7 +94,7 @@ public final String toString() {
return
this.getClass().getSimpleName() + "{\n" +
"\tposition: " + position + "\n" +
"\ttype: " + getType() + "\n" +
"\ttype: " + type + "\n" +
"\tremoved: " + removed + "\n" +
"\tinserted: " + inserted + "\n" +
"}";
Expand Down

0 comments on commit e487947

Please sign in to comment.