MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / walk_tree

Method walk_tree

atomic-semantic/src/parsers/python.rs:39–103  ·  view source on GitHub ↗

Walk the AST and extract entities.

(
        &self,
        node: &Node,
        source: &str,
        file_path: &str,
        entities: &mut Vec<Entity>,
        in_class: Option<&str>,
    )

Source from the content-addressed store, hash-verified

37
38 /// Walk the AST and extract entities.
39 fn walk_tree(
40 &self,
41 node: &Node,
42 source: &str,
43 file_path: &str,
44 entities: &mut Vec<Entity>,
45 in_class: Option<&str>,
46 ) {
47 match node.kind() {
48 "function_definition" => {
49 if let Some(entity) = self.extract_function(node, source, file_path, in_class) {
50 entities.push(entity);
51 }
52 }
53 "class_definition" => {
54 if let Some(entity) = self.extract_class(node, source, file_path) {
55 let class_name = entity.name.clone();
56 entities.push(entity);
57
58 // Walk class body for methods
59 if let Some(body) = node.child_by_field_name("body") {
60 let mut cursor = body.walk();
61 for child in body.children(&mut cursor) {
62 self.walk_tree(&child, source, file_path, entities, Some(&class_name));
63 }
64 }
65 return; // Don't recurse into class again below
66 }
67 }
68 // Module-level assignments: `X = 42` or `X: int = 42`
69 "expression_statement" if in_class.is_none() => {
70 if let Some(child) = node.child(0) {
71 if child.kind() == "assignment" || child.kind() == "augmented_assignment" {
72 if let Some(entity) = self.extract_assignment(&child, source, file_path) {
73 entities.push(entity);
74 }
75 }
76 }
77 }
78 "import_statement" | "import_from_statement" => {
79 if let Some(entity) = self.extract_import(node, source, file_path) {
80 entities.push(entity);
81 }
82 }
83 "decorated_definition" => {
84 // Decorated functions/classes — walk into the inner definition
85 let mut cursor = node.walk();
86 for child in node.children(&mut cursor) {
87 if child.kind() == "function_definition" || child.kind() == "class_definition" {
88 self.walk_tree(&child, source, file_path, entities, in_class);
89 }
90 }
91 return; // Don't double-recurse
92 }
93 _ => {}
94 }
95
96 // Recurse into children (but not class bodies — handled above)

Callers 1

extractMethod · 0.45

Calls 10

walkMethod · 0.80
childrenMethod · 0.80
is_noneMethod · 0.80
extract_assignmentMethod · 0.80
kindMethod · 0.45
extract_functionMethod · 0.45
pushMethod · 0.45
extract_classMethod · 0.45
cloneMethod · 0.45
extract_importMethod · 0.45

Tested by

no test coverage detected