Files
g3/crates/g3-core/examples/inspect_python_ast.rs
Jochen 52f78653b4 add context window monitor
Writes the current context window to logs/current_context_window (uses a symlink to a session ID).

This PR was unfortunately generated by a different LLM and did a ton of superficial reformating, it's actually a fairly small and benign change, but I don't want to roll back everything. Hope that's ok.
2025-11-27 21:00:02 +11:00

57 lines
1.3 KiB
Rust

//! Inspect tree-sitter AST structure for Python code
use tree_sitter::{Language, Parser};
fn print_tree(node: tree_sitter::Node, source: &str, indent: usize) {
let indent_str = " ".repeat(indent);
let node_text = &source[node.byte_range()];
let preview = if node_text.len() > 50 {
format!("{}...", &node_text[..50])
} else {
node_text.to_string()
};
println!(
"{}{} [{}:{}] '{}'",
indent_str,
node.kind(),
node.start_position().row + 1,
node.start_position().column + 1,
preview.replace('\n', "\\n")
);
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
print_tree(child, source, indent + 1);
}
}
fn main() -> anyhow::Result<()> {
let source_code = r#"
def regular_function():
pass
async def async_function():
pass
class MyClass:
def method(self):
pass
"#;
println!("Source code:");
println!("{}", source_code);
println!("\n{}", "=".repeat(80));
println!("AST Structure:");
println!("{}\n", "=".repeat(80));
let mut parser = Parser::new();
let language: Language = tree_sitter_python::LANGUAGE.into();
parser.set_language(&language)?;
let tree = parser.parse(source_code, None).unwrap();
print_tree(tree.root_node(), source_code, 0);
Ok(())
}