Skip to content

Commit c9a002b

Browse files
authored
Fixed RichTextChange reported by ReadOnlyStyledDocument replace (#908)
1 parent a32e5a9 commit c9a002b

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ public void identity_change_works() {
162162
assertEquals(0, plainEmissions.get());
163163
});
164164
}
165+
166+
@Test
167+
public void testForBug904() {
168+
String firstLine = "some text\n";
169+
write( firstLine );
170+
interact( () -> area.setStyle( 5, 9, "-fx-font-weight: bold;" ) );
171+
write( "new line" );
172+
area.getUndoManager().preventMerge();
173+
area.append( area.getContent().subSequence( firstLine.length()-1, area.getLength() ) );
174+
interact( area::undo ); // should not throw Unexpected change received exception
175+
}
165176

166177
}
167178

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ private Tuple3<ReadOnlyStyledDocument<PS, SEG, S>, RichTextChange<PS, SEG, S>, M
419419
return start.map(l0::split).map((l, removed) -> {
420420
ReadOnlyStyledDocument<PS, SEG, S> replacement = f.apply(removed);
421421
ReadOnlyStyledDocument<PS, SEG, S> doc = l.concatR(replacement).concat(r);
422-
RichTextChange<PS, SEG, S> change = new RichTextChange<>(pos, removed, replacement);
422+
// Next we use doc.subSequence instead of replacement because Paragraph.concat's returned paragraph style can vary.
423+
RichTextChange<PS, SEG, S> change = new RichTextChange<>(pos, removed, doc.subSequence(pos, pos+replacement.length()));
423424
List<Paragraph<PS, SEG, S>> addedPars = doc.getParagraphs().subList(start.major, start.major + replacement.getParagraphCount());
424425
MaterializedListModification<Paragraph<PS, SEG, S>> parChange =
425426
MaterializedListModification.create(start.major, removedPars, addedPars);

0 commit comments

Comments
 (0)