Remove listlevel internal state
This commit is contained in:
parent
5b61521e30
commit
f97c852c8a
@ -39,12 +39,6 @@ public interface MarkwonVisitor extends Visitor {
|
|||||||
|
|
||||||
boolean hasNext(@NonNull Node node);
|
boolean hasNext(@NonNull Node node);
|
||||||
|
|
||||||
void incrementListLevel();
|
|
||||||
|
|
||||||
void decrementListLevel();
|
|
||||||
|
|
||||||
int listLevel();
|
|
||||||
|
|
||||||
void ensureNewLine();
|
void ensureNewLine();
|
||||||
|
|
||||||
void forceNewLine();
|
void forceNewLine();
|
||||||
@ -52,4 +46,7 @@ public interface MarkwonVisitor extends Visitor {
|
|||||||
int length();
|
int length();
|
||||||
|
|
||||||
void setSpans(int start, @Nullable Object spans);
|
void setSpans(int start, @Nullable Object spans);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
<N extends Node> NodeVisitor<N> nodeVisitor(@NonNull Class<N> node);
|
||||||
}
|
}
|
||||||
|
@ -43,8 +43,6 @@ class MarkwonVisitorImpl implements MarkwonVisitor {
|
|||||||
|
|
||||||
private final SpannableBuilder builder = new SpannableBuilder();
|
private final SpannableBuilder builder = new SpannableBuilder();
|
||||||
|
|
||||||
private int listLevel;
|
|
||||||
|
|
||||||
private MarkwonVisitorImpl(
|
private MarkwonVisitorImpl(
|
||||||
@NonNull MarkwonConfiguration configuration,
|
@NonNull MarkwonConfiguration configuration,
|
||||||
@NonNull Map<Class<? extends Node>, NodeVisitor<? extends Node>> nodes) {
|
@NonNull Map<Class<? extends Node>, NodeVisitor<? extends Node>> nodes) {
|
||||||
@ -215,21 +213,6 @@ class MarkwonVisitorImpl implements MarkwonVisitor {
|
|||||||
return node.getNext() != null;
|
return node.getNext() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void incrementListLevel() {
|
|
||||||
listLevel += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void decrementListLevel() {
|
|
||||||
listLevel -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int listLevel() {
|
|
||||||
return listLevel;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void ensureNewLine() {
|
public void ensureNewLine() {
|
||||||
if (builder.length() > 0
|
if (builder.length() > 0
|
||||||
@ -253,6 +236,13 @@ class MarkwonVisitorImpl implements MarkwonVisitor {
|
|||||||
SpannableBuilder.setSpans(builder, spans, start, builder.length());
|
SpannableBuilder.setSpans(builder, spans, start, builder.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public <N extends Node> NodeVisitor<N> nodeVisitor(@NonNull Class<N> node) {
|
||||||
|
//noinspection unchecked
|
||||||
|
return (NodeVisitor<N>) nodes.get(node);
|
||||||
|
}
|
||||||
|
|
||||||
static class BuilderImpl implements Builder {
|
static class BuilderImpl implements Builder {
|
||||||
|
|
||||||
private final Map<Class<? extends Node>, NodeVisitor<? extends Node>> nodes =
|
private final Map<Class<? extends Node>, NodeVisitor<? extends Node>> nodes =
|
||||||
|
@ -15,8 +15,6 @@ public class ListItemNodeVisitor implements MarkwonVisitor.NodeVisitor<ListItem>
|
|||||||
|
|
||||||
final int length = visitor.length();
|
final int length = visitor.length();
|
||||||
|
|
||||||
visitor.incrementListLevel();
|
|
||||||
|
|
||||||
final Node parent = listItem.getParent();
|
final Node parent = listItem.getParent();
|
||||||
if (parent instanceof OrderedList) {
|
if (parent instanceof OrderedList) {
|
||||||
|
|
||||||
@ -33,14 +31,24 @@ public class ListItemNodeVisitor implements MarkwonVisitor.NodeVisitor<ListItem>
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
visitor.visitChildren(listItem);
|
visitor.visitChildren(listItem);
|
||||||
visitor.setSpans(length, visitor.factory().bulletListItem(visitor.theme(), visitor.listLevel() - 1));
|
visitor.setSpans(length, visitor.factory().bulletListItem(visitor.theme(), listLevel(listItem)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
visitor.decrementListLevel();
|
|
||||||
|
|
||||||
if (visitor.hasNext(listItem)) {
|
if (visitor.hasNext(listItem)) {
|
||||||
visitor.ensureNewLine();
|
visitor.ensureNewLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int listLevel(@NonNull Node node) {
|
||||||
|
int level = 0;
|
||||||
|
Node parent = node.getParent();
|
||||||
|
while (parent != null) {
|
||||||
|
if (parent instanceof ListItem) {
|
||||||
|
level += 1;
|
||||||
|
}
|
||||||
|
parent = parent.getParent();
|
||||||
|
}
|
||||||
|
return level;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user