daipendency_testing/
debugging.rs

1use tree_sitter::Node;
2
3/// Returns a string representation of a tree-sitter node and its children for debugging purposes.
4///
5/// # Arguments
6///
7/// * `node` - The tree-sitter node to debug
8/// * `source_code` - The source code text that the node was parsed from
9///
10/// # Output Format
11///
12/// For each node, prints its kind and the corresponding source code text, with child nodes indented.
13pub fn debug_node(node: &Node, source_code: &str) -> String {
14    debug_node_with_indentation(node, source_code, 0)
15}
16
17fn debug_node_with_indentation(node: &Node, source_code: &str, indent: usize) -> String {
18    let mut result = String::new();
19    let indent_str = " ".repeat(indent);
20
21    result.push_str(&format!(
22        "{}{:?}: {}\n",
23        indent_str,
24        node,
25        node.utf8_text(source_code.as_bytes()).unwrap()
26    ));
27
28    let mut cursor = node.walk();
29    for child in node.children(&mut cursor) {
30        result.push_str(&debug_node_with_indentation(
31            &child,
32            source_code,
33            indent + 2,
34        ));
35    }
36
37    result
38}