SpannableBuilder

This commit is contained in:
Dimitry Ivanov 2017-11-05 12:15:02 +03:00
parent 1f403abeb0
commit d75590c225
2 changed files with 11 additions and 37 deletions

View File

@ -111,11 +111,14 @@ public class SpannableBuilder {
} }
@NonNull @NonNull
public CharSequence remove(int start, int end) { public CharSequence removeFromEnd(int start) {
// this method is intended to be used only by markdown visitor // this method is not intended to be used by clients
// it's a workaround to allow tables // it's a workaround to support tables
final int end = length();
// as we do not expose builder and do no apply spans to it, we are safe to NOT to convert to String
final SpannableStringBuilderImpl impl = new SpannableStringBuilderImpl(builder.subSequence(start, end)); final SpannableStringBuilderImpl impl = new SpannableStringBuilderImpl(builder.subSequence(start, end));
final Iterator<Span> iterator = spans.iterator(); final Iterator<Span> iterator = spans.iterator();
@ -129,37 +132,7 @@ public class SpannableBuilder {
} }
} }
// SHIFT EXISTING! builder.replace(start, end, "");
if (spans.size() > 0) {
for (Span s : spans) {
// if end < start -> not affected
if (s.end < start) {
continue;
}
// if end between start & end (which is really bad one) -> make end=start
if (s.end >= start && s.end <= end) {
s.end = start;
continue;
}
// if start between start&end -> make start=end
if (s.start >= start && s.start <= end) {
s.start = start;
// shift end by difference
s.end = s.end - (end - start);
continue;
}
// if after, just shift by difference
final int diff = end - start;
s.start = s.start - diff;
s.end = s.end - diff;
}
}
return impl; return impl;
} }
@ -180,7 +153,8 @@ public class SpannableBuilder {
// breaks the order that we intend to use // breaks the order that we intend to use
// so, we will defensively copy builder // so, we will defensively copy builder
final SpannableStringBuilderImpl impl = new SpannableStringBuilderImpl(builder.toString()); // as we do not expose builder and do no apply spans to it, we are safe to NOT to convert to String
final SpannableStringBuilderImpl impl = new SpannableStringBuilderImpl(builder);
for (Span span : spans) { for (Span span : spans) {
impl.setSpan(span.what, span.start, span.end, span.flags); impl.setSpan(span.what, span.start, span.end, span.flags);
@ -215,7 +189,7 @@ public class SpannableBuilder {
} }
} }
private static class Span { static class Span {
final Object what; final Object what;
int start; int start;

View File

@ -364,7 +364,7 @@ public class SpannableMarkdownVisitor extends AbstractVisitor {
pendingTableRow.add(new TableRowSpan.Cell( pendingTableRow.add(new TableRowSpan.Cell(
tableCellAlignment(cell.getAlignment()), tableCellAlignment(cell.getAlignment()),
builder.remove(length, builder.length()) builder.removeFromEnd(length)
)); ));
tableRowIsHeader = cell.isHeader(); tableRowIsHeader = cell.isHeader();