Skip to content

Commit 836902e

Browse files
authored
Fix Paragraph returning incorrectly styled subsequence (#817)
Fixed subSequence(int) bug
1 parent bc7cab6 commit 836902e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public Paragraph<PS, SEG, S> subSequence(int start) {
272272
} else if (start == length()) {
273273
// in case one is using EitherOps<SegmentOps, SegmentOps>, force the empty segment
274274
// to use the left ops' default empty seg, not the right one's empty seg
275-
return new Paragraph<>(paragraphStyle, segmentOps, segmentOps.createEmptySeg(), styles.subView(0, 0));
275+
return new Paragraph<>(paragraphStyle, segmentOps, segmentOps.createEmptySeg(), styles.subView(start,start));
276276
} else if(start < length()) {
277277
Position pos = navigator.offsetToPosition(start, Forward);
278278
int segIdx = pos.getMajor();

richtextfx/src/test/java/org/fxmisc/richtext/model/ParagraphTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,19 @@ public void restylingEmptyParagraphViaEmptyStyleSpansWorks() {
6262
Paragraph<Void, String, Collection<String>> restyledP = p.restyle(0, spans);
6363
assertEquals( test, restyledP.getStyleSpans().getStyleSpan( 0 ).getStyle() );
6464
}
65+
66+
// Relates to #815 where an undo after deleting a portion of styled text in a multi-
67+
// styled paragraph causes an exception in UndoManager receiving an unexpected change.
68+
@Test
69+
public void multiStyleParagraphReturnsCorrect_subSequenceOfLength() {
70+
71+
Collection<String> test = Collections.singleton("test");
72+
TextOps<String, Collection<String>> segOps = SegmentOps.styledTextOps();
73+
StyleSpansBuilder ssb = new StyleSpansBuilder<>(2);
74+
ssb.add( Collections.EMPTY_LIST, 8 );
75+
ssb.add( test, 8 );
76+
77+
Paragraph<Void, String, Collection<String>> p = new Paragraph<>(null, segOps, "noStyle hasStyle", ssb.create());
78+
assertEquals( test, p.subSequence( p.length() ).getStyleOfChar(0) );
79+
}
6580
}

0 commit comments

Comments
 (0)