Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public Paragraph<PS, SEG, S> restyle(int from, int to, S style) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public int getSpanCount() {
public StyleSpan<S> getStyleSpan(int index) {
return spans.get(index);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StyleSpans(length=").append(length())
.append(" spanCount=").append(getSpanCount())
.append(" spans=").append(spans)
.append(")");
return sb.toString();
}
}

static <S> StyleSpans<S> overlay(
Expand Down Expand Up @@ -276,6 +286,20 @@ public StyleSpan<S> getStyleSpan(int index) {
return original.getStyleSpan(firstIdxInOrig + index);
}
}

@Override
public String toString() {
ArrayList<StyleSpan<S>> spans = new ArrayList<>(spanCount);
for (int i = 0; i < spanCount; i++) {
spans.add(getStyleSpan(i));
}
StringBuilder sb = new StringBuilder();
sb.append("SubSpans(length=").append(length)
.append(" spanCount=").append(getSpanCount())
.append(" spans=").append(spans)
.append(")");
return sb.toString();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,19 @@ public void concatEmptyParagraphsTest() {
assertEquals(Boolean.TRUE, p.getStyleAtPosition(0));
}

// Relates to #345 and #505: calling `EditableStyledDocument::setStyleSpans` when the style spans
// would style an empty paragraph would throw an exception
@Test
public void restylingEmptyParagraphViaStyleSpansWorks() {
TextOps<StyledText<Boolean>, Boolean> segOps = StyledText.textOps();
Paragraph<Void, StyledText<Boolean>, Boolean> p = new Paragraph<>(null, segOps, segOps.createEmpty());

StyleSpansBuilder<Boolean> builder = new StyleSpansBuilder<>();
builder.add(true, 2);
StyleSpans<Boolean> spans = builder.create();
Paragraph<Void, StyledText<Boolean>, Boolean> restyledP = p.restyle(0, spans);

assertEquals(p, restyledP);
}

}