All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 from dataclasses import dataclass
2 from typing import Iterator, TypeVar, Union
4 from black.nodes import Visitor
5 from black.output import out
6 from black.parsing import lib2to3_parse
7 from blib2to3.pgen2 import token
8 from blib2to3.pytree import Leaf, Node, type_repr
10 LN = Union[Leaf, Node]
15 class DebugVisitor(Visitor[T]):
18 def visit_default(self, node: LN) -> Iterator[T]:
19 indent = " " * (2 * self.tree_depth)
20 if isinstance(node, Node):
21 _type = type_repr(node.type)
22 out(f"{indent}{_type}", fg="yellow")
24 for child in node.children:
25 yield from self.visit(child)
28 out(f"{indent}/{_type}", fg="yellow", bold=False)
30 _type = token.tok_name.get(node.type, str(node.type))
31 out(f"{indent}{_type}", fg="blue", nl=False)
33 # We don't have to handle prefixes for `Node` objects since
34 # that delegates to the first child anyway.
35 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
36 out(f" {node.value!r}", fg="blue", bold=False)
39 def show(cls, code: Union[str, Leaf, Node]) -> None:
40 """Pretty-print the lib2to3 AST of a given string of `code`.
42 Convenience method for debugging.
44 v: DebugVisitor[None] = DebugVisitor()
45 if isinstance(code, str):
46 code = lib2to3_parse(code)