Skip to content

Commit 6686f8c

Browse files
Merge pull request #509 from JordanMartinez/fixStyleSpansIssue
Fix style spans issue
2 parents 7873e82 + 1653ae0 commit 6686f8c

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public Paragraph<PS, SEG, S> restyle(int from, int to, S style) {
230230

231231
public Paragraph<PS, SEG, S> restyle(int from, StyleSpans<? extends S> styleSpans) {
232232
int len = styleSpans.length();
233-
if(styleSpans.equals(getStyleSpans(from, from + len))) {
233+
if(styleSpans.equals(getStyleSpans(from, from + len)) || length() == 0) {
234234
return this;
235235
}
236236

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public int getSpanCount() {
4040
public StyleSpan<S> getStyleSpan(int index) {
4141
return spans.get(index);
4242
}
43+
44+
@Override
45+
public String toString() {
46+
StringBuilder sb = new StringBuilder();
47+
sb.append("StyleSpans(length=").append(length())
48+
.append(" spanCount=").append(getSpanCount())
49+
.append(" spans=").append(spans)
50+
.append(")");
51+
return sb.toString();
52+
}
4353
}
4454

4555
static <S> StyleSpans<S> overlay(
@@ -276,6 +286,20 @@ public StyleSpan<S> getStyleSpan(int index) {
276286
return original.getStyleSpan(firstIdxInOrig + index);
277287
}
278288
}
289+
290+
@Override
291+
public String toString() {
292+
ArrayList<StyleSpan<S>> spans = new ArrayList<>(spanCount);
293+
for (int i = 0; i < spanCount; i++) {
294+
spans.add(getStyleSpan(i));
295+
}
296+
StringBuilder sb = new StringBuilder();
297+
sb.append("SubSpans(length=").append(length)
298+
.append(" spanCount=").append(getSpanCount())
299+
.append(" spans=").append(spans)
300+
.append(")");
301+
return sb.toString();
302+
}
279303
}
280304

281305

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ public void concatEmptyParagraphsTest() {
2020
assertEquals(Boolean.TRUE, p.getStyleAtPosition(0));
2121
}
2222

23+
// Relates to #345 and #505: calling `EditableStyledDocument::setStyleSpans` when the style spans
24+
// would style an empty paragraph would throw an exception
25+
@Test
26+
public void restylingEmptyParagraphViaStyleSpansWorks() {
27+
TextOps<StyledText<Boolean>, Boolean> segOps = StyledText.textOps();
28+
Paragraph<Void, StyledText<Boolean>, Boolean> p = new Paragraph<>(null, segOps, segOps.createEmpty());
29+
30+
StyleSpansBuilder<Boolean> builder = new StyleSpansBuilder<>();
31+
builder.add(true, 2);
32+
StyleSpans<Boolean> spans = builder.create();
33+
Paragraph<Void, StyledText<Boolean>, Boolean> restyledP = p.restyle(0, spans);
34+
35+
assertEquals(p, restyledP);
36+
}
37+
2338
}

0 commit comments

Comments
 (0)