Coverage for src/renaissance/integrations/tree_sitter/visualizer.py: 100%
24 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Render a tree-sitter LST as a Mermaid graph for visualization."""
9from renaissance.integrations.tree_sitter.lst import LST
10from renaissance.utils.text_utils import signature_to_id
13class LstVisualizer:
14 """AI: Render a tree-sitter LST as a Mermaid graph for visualization."""
16 def __init__(self) -> None:
17 """AI: Prepare a visualizer that renders an LST as a Mermaid graph."""
18 self.lines = ["graph TD"]
19 self.counter = 0
20 self.node_ids = {}
22 def _get_node_id(self, node):
23 if node not in self.node_ids:
24 self.counter += 1
25 self.node_ids[node] = f"n{self.counter}"
26 return self.node_ids[node]
28 def _render_node(self, node) -> None:
29 node_id = self._get_node_id(node)
30 label = f"""\
31 {node_id}: {node.semantic_kind} ({node.parser_kind}) {{
32 offset: {node.offset}
33 signature: {signature_to_id(node.signature)}
34 }}"""
35 label = label.replace("\n", "<br>")
36 self.lines.append(f'{node_id}["{label}"]')
37 for child in node.children:
38 self._render_node(child)
39 child_id = self._get_node_id(child)
40 self.lines.append(f"{node_id} --> {child_id}")
42 def render(self, lst: LST):
43 """AI: Render the given LST as a Mermaid graph diagram string."""
44 self._render_node(lst.root)
45 return "\n".join(self.lines)