Skip to content

Commit 7ef4a31

Browse files
Merge pull request #702 from JordanMartinez/fixRegression
Use absolute replacements for undo and redo
2 parents 07c475c + 1bffad3 commit 7ef4a31

2 files changed

Lines changed: 30 additions & 34 deletions

File tree

richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/MultiChangeTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public void committing_relative_change_works() {
4545
});
4646
}
4747

48+
@Test
49+
public void committing_relative_change_backToFront_works() {
50+
interact(() -> {
51+
String text = area.getText();
52+
String hello = "hello";
53+
String world = "world";
54+
area.createMultiChange(2)
55+
.insertText(6, world)
56+
.insertText(0, hello)
57+
.commit();
58+
59+
assertEquals(hello + text + world, area.getText());
60+
61+
area.undo();
62+
assertEquals(text, area.getText());
63+
});
64+
}
65+
4866
@Test
4967
public void committing_absolute_change_works() {
5068
interact(() -> {

richtextfx/src/main/java/org/fxmisc/richtext/util/UndoUtils.java

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTex
8282
GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory, Duration preventMergeDelay) {
8383
return factory.createMultiChangeUM(area.multiRichChanges(),
8484
TextChange::invert,
85-
undoMultiRichTextChange(area),
86-
redoMultiRichTextChange(area),
85+
applyMultiRichTextChange(area),
86+
applyMultiRichTextChange(area),
8787
TextChange::mergeWith,
8888
TextChange::isIdentity,
8989
preventMergeDelay);
@@ -128,8 +128,8 @@ public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManag
128128
GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory, Duration preventMergeDelay) {
129129
return factory.createMultiChangeUM(area.multiPlainChanges(),
130130
TextChange::invert,
131-
undoMultiPlainTextChange(area),
132-
redoMultiPlainTextChange(area),
131+
applyMultiPlainTextChange(area),
132+
applyMultiPlainTextChange(area),
133133
TextChange::mergeWith,
134134
TextChange::isIdentity,
135135
preventMergeDelay);
@@ -160,48 +160,26 @@ public static <PS, SEG, S> Consumer<RichTextChange<PS, SEG, S>> applyRichTextCha
160160
return change -> area.replace(change.getPosition(), change.getRemovalEnd(), change.getInserted());
161161
}
162162

163-
public static <PS, SEG, S> Consumer<List<PlainTextChange>> undoMultiPlainTextChange(
164-
GenericStyledArea<PS, SEG, S> area) {
165-
return changeList -> {
166-
MultiChangeBuilder<PS, SEG, S> builder = area.createMultiChange(changeList.size());
167-
for (PlainTextChange c : changeList) {
168-
builder.replaceText(c.getPosition(), c.getRemovalEnd(), c.getInserted());
169-
}
170-
builder.commit();
171-
};
172-
}
173-
174-
public static <PS, SEG, S> Consumer<List<PlainTextChange>> redoMultiPlainTextChange(
175-
GenericStyledArea<PS, SEG, S> area) {
176-
return changeList -> {
177-
MultiChangeBuilder<PS, SEG, S> builder = area.createMultiChange(changeList.size());
178-
for (PlainTextChange c : changeList) {
179-
builder.replaceTextAbsolutely(c.getPosition(), c.getRemovalEnd(), c.getInserted());
180-
}
181-
builder.commit();
182-
};
183-
}
184-
185163
/**
186-
* Undoes a list of {@link RichTextChange} to the given area when the {@link UndoManager}'s change stream emits an event
187-
* by {@code area.replace(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
164+
* Applies a list of {@link PlainTextChange}s to the given area when the {@link UndoManager}'s change stream emits
165+
* an event by {@code area.replaceAbsolutely(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
188166
*/
189-
public static <PS, SEG, S> Consumer<List<RichTextChange<PS, SEG, S>>> undoMultiRichTextChange(
167+
public static <PS, SEG, S> Consumer<List<PlainTextChange>> applyMultiPlainTextChange(
190168
GenericStyledArea<PS, SEG, S> area) {
191169
return changeList -> {
192170
MultiChangeBuilder<PS, SEG, S> builder = area.createMultiChange(changeList.size());
193-
for (RichTextChange<PS, SEG, S> c : changeList) {
194-
builder.replace(c.getPosition(), c.getRemovalEnd(), c.getInserted());
171+
for (PlainTextChange c : changeList) {
172+
builder.replaceTextAbsolutely(c.getPosition(), c.getRemovalEnd(), c.getInserted());
195173
}
196174
builder.commit();
197175
};
198176
}
199177

200178
/**
201-
* Redoes a list of {@link RichTextChange} to the given area when the {@link UndoManager}'s change stream emits an event
202-
* by {@code area.replaceAbsolutely(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
179+
* Applies a list of {@link RichTextChange} to the given area when the {@link UndoManager}'s change stream emits
180+
* an event by {@code area.replaceAbsolutely(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
203181
*/
204-
public static <PS, SEG, S> Consumer<List<RichTextChange<PS, SEG, S>>> redoMultiRichTextChange(
182+
public static <PS, SEG, S> Consumer<List<RichTextChange<PS, SEG, S>>> applyMultiRichTextChange(
205183
GenericStyledArea<PS, SEG, S> area) {
206184
return changeList -> {
207185
MultiChangeBuilder<PS, SEG, S> builder = area.createMultiChange(changeList.size());

0 commit comments

Comments
 (0)