Skip to content

Commit eb6d4bf

Browse files
Merge pull request #487 from JordanMartinez/fixChangeType
Limit TextChange's type's possibilities by only using it for merging
2 parents 1e96f94 + 4990bc1 commit eb6d4bf

3 files changed

Lines changed: 24 additions & 53 deletions

File tree

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,10 @@
33

44
public class PlainTextChange extends TextChange<String, PlainTextChange> {
55

6-
private final ChangeType type;
7-
86
public PlainTextChange(int position, String removed, String inserted) {
97
super(position, removed, inserted);
10-
if (insertedLength() == 0) {
11-
if (removedLength() == 0) {
12-
throw new IllegalStateException(String.format("Cannot get the type of a change that neither inserts nor deletes anything." +
13-
"removed=%s inserted=%s", removed, inserted));
14-
} else {
15-
type = ChangeType.DELETION;
16-
}
17-
} else if (removedLength() == 0) {
18-
type = ChangeType.INSERTION;
19-
} else {
20-
type = ChangeType.REPLACEMENT;
21-
}
228
}
239

24-
@Override
25-
public final ChangeType getType() { return type; }
26-
2710
@Override
2811
protected int removedLength() {
2912
return removed.length();

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,10 @@
22

33
public class RichTextChange<PS, SEG, S> extends TextChange<StyledDocument<PS, SEG, S>, RichTextChange<PS, SEG, S>> {
44

5-
private final ChangeType type;
6-
75
public RichTextChange(int position, StyledDocument<PS, SEG, S> removed, StyledDocument<PS, SEG, S> inserted) {
86
super(position, removed, inserted);
9-
if (insertedLength() == 0) {
10-
if (removedLength() == 0) {
11-
if (!removed.equals(inserted)) {
12-
type = ChangeType.RESTYLED_PARAGRAPH;
13-
} else {
14-
throw new IllegalStateException(String.format("Cannot get the type of a change that neither inserts nor deletes anything." +
15-
"removed=%s inserted=%s", removed, inserted));
16-
}
17-
} else {
18-
type = ChangeType.DELETION;
19-
}
20-
} else if (removedLength() == 0) {
21-
type = ChangeType.INSERTION;
22-
} else {
23-
type = ChangeType.REPLACEMENT;
24-
}
257
}
268

27-
@Override
28-
public final ChangeType getType() { return type; }
29-
309
@Override
3110
protected int removedLength() {
3211
return removed.length();

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,39 @@
55

66
public abstract class TextChange<S, Self extends TextChange<S, Self>> {
77

8-
public static enum ChangeType {
8+
/**
9+
* Indicates whether this change can be merged with another change if they are both {@link #INSERTION} or
10+
* both {@link #DELETION}.
11+
*/
12+
public static enum MergeType {
913
/** Indicates that the change will insert something but not remove anything */
1014
INSERTION,
1115
/** Indicates that the change will delete something but not insert anything */
1216
DELETION,
13-
/** Indicates that the change will remove something and insert something as its replacement.
14-
* For {@link PlainTextChange}s, the replacement will only be content.
15-
* For {@link RichTextChange}s, the replacement may only be the style, only the content, or a mix of both.
16-
* Style changes may change the style of the paragraph's content or a non-empty {@link Paragraph}
17-
* ({@code p.length() > 0}) */
18-
REPLACEMENT,
19-
/** Indicates that the paragraph style of an empty {@link Paragraph} ({@code p.length() == 0}) was changed */
20-
RESTYLED_PARAGRAPH
17+
/** Indicates that the change is a style change, a replacement or something other than the other two types */
18+
NONE,
2119
}
2220

2321

2422
protected final int position;
2523
protected final S removed;
2624
protected final S inserted;
25+
protected final MergeType mergeType;
2726

2827
public TextChange(int position, S removed, S inserted) {
2928
this.position = position;
3029
this.removed = removed;
3130
this.inserted = inserted;
31+
32+
if (insertedLength() == 0) {
33+
mergeType = removedLength() != 0
34+
? MergeType.DELETION
35+
: MergeType.NONE;
36+
} else if (removedLength() == 0) {
37+
mergeType = MergeType.INSERTION;
38+
} else {
39+
mergeType = MergeType.NONE;
40+
}
3241
}
3342

3443
public int getPosition() { return position; };
@@ -37,25 +46,25 @@ public TextChange(int position, S removed, S inserted) {
3746
public Self invert() { return create(position, inserted, removed); }
3847
public int getRemovalEnd() { return position + removedLength(); }
3948
public int getInsertionEnd() { return position + insertedLength(); }
49+
public final MergeType getMergeType() { return mergeType; };
4050

41-
public abstract ChangeType getType();
4251
protected abstract int removedLength();
4352
protected abstract int insertedLength();
4453
protected abstract S concat(S a, S b);
4554
protected abstract S sub(S s, int from, int to);
4655
protected abstract Self create(int position, S removed, S inserted);
4756

4857
/**
49-
* Merges this change with the given change only if the end of this change's inserted text
50-
* equals the latter's position and both are either insertion or deletion changes.
58+
* Merges this change with the given change only if the end of this change's inserted text equals the
59+
* latter's position and both are either {@link MergeType#INSERTION} or {@link MergeType#DELETION} changes.
5160
*
5261
* @param latter change to merge with this change.
5362
* @return a new merged change if changes can be merged,
5463
* {@code null} otherwise.
5564
*/
5665
public Optional<Self> mergeWith(Self latter) {
57-
if((this.getType() == ChangeType.INSERTION || this.getType() == ChangeType.DELETION)
58-
&& this.getType() == latter.getType()
66+
if((this.mergeType == MergeType.INSERTION || this.mergeType == MergeType.DELETION)
67+
&& this.mergeType == latter.mergeType
5968
&& this.getInsertionEnd() == latter.position) {
6069
S removedText = concat(this.removed, latter.removed);
6170
S addedText = concat(this.inserted, latter.inserted);
@@ -87,7 +96,7 @@ public final String toString() {
8796
return
8897
this.getClass().getSimpleName() + "{\n" +
8998
"\tposition: " + position + "\n" +
90-
"\ttype: " + getType() + "\n" +
99+
"\tmergeType: " + mergeType + "\n" +
91100
"\tremoved: " + removed + "\n" +
92101
"\tinserted: " + inserted + "\n" +
93102
"}";

0 commit comments

Comments
 (0)