]> git.madduck.net Git - etc/vim.git/blob - tests/data/miscellaneous/debug_visitor.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Improve performance by skipping unnecessary normalisation (#3751)
[etc/vim.git] / tests / data / miscellaneous / debug_visitor.py
1 @dataclass
2 class DebugVisitor(Visitor[T]):
3     tree_depth: int = 0
4
5     def visit_default(self, node: LN) -> Iterator[T]:
6         indent = ' ' * (2 * self.tree_depth)
7         if isinstance(node, Node):
8             _type = type_repr(node.type)
9             out(f'{indent}{_type}', fg='yellow')
10             self.tree_depth += 1
11             for child in node.children:
12                 yield from self.visit(child)
13
14             self.tree_depth -= 1
15             out(f'{indent}/{_type}', fg='yellow', bold=False)
16         else:
17             _type = token.tok_name.get(node.type, str(node.type))
18             out(f'{indent}{_type}', fg='blue', nl=False)
19             if node.prefix:
20                 # We don't have to handle prefixes for `Node` objects since
21                 # that delegates to the first child anyway.
22                 out(f' {node.prefix!r}', fg='green', bold=False, nl=False)
23             out(f' {node.value!r}', fg='blue', bold=False)
24
25     @classmethod
26     def show(cls, code: str) -> None:
27         """Pretty-prints a given string of `code`.
28
29         Convenience method for debugging.
30         """
31         v: DebugVisitor[None] = DebugVisitor()
32         list(v.visit(lib2to3_parse(code)))