MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

codegraph-kernel/src/python.rs:68–140  ·  view source on GitHub ↗
(file_path: &str, source: &str)

Source from the content-addressed store, hash-verified

66}
67
68pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
69 let grammar = crate::langs::grammar_for("python").ok_or("no python grammar")?;
70 let t0 = std::time::Instant::now();
71 let mut parser = Parser::new();
72 parser
73 .set_language(&grammar)
74 .map_err(|e| format!("set_language(python) failed: {e}"))?;
75 let tree = parser
76 .parse(source, None)
77 .ok_or_else(|| "parser returned null tree".to_string())?;
78 if tree.root_node().has_error() {
79 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
80 }
81
82 let mut w = Walker {
83 src: source,
84 file_path,
85 line_starts: util::line_starts(source),
86 arena: Arena::default(),
87 tables: Tables::default(),
88 stack: Vec::new(),
89 node_ids: Vec::new(),
90 defined_fn_names: HashSet::new(),
91 imported_names: HashSet::new(),
92 fn_ref_cands: Vec::new(),
93 fs_values: HashMap::new(),
94 fs_value_counts: HashMap::new(),
95 value_scopes: Vec::new(),
96 };
97
98 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
99 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
100 let mut flags = BoolFlags::default();
101 flags.set(crate::buffers::FLAG_IS_EXPORTED, false);
102 let file_id = w.arena.put(&ids::file_node_id(file_path));
103 let name_ref = w.arena.put(base_name);
104 let qn_ref = w.arena.put(file_path);
105 w.tables.push_node(&NodeRow {
106 kind: node_kind_index("file").unwrap(),
107 visibility: 0,
108 flags,
109 start_line: 1,
110 end_line: line_count,
111 start_column: 0,
112 end_column: 0,
113 name: name_ref,
114 qualified_name: qn_ref,
115 id: file_id,
116 docstring: NONE_STR,
117 signature: NONE_STR,
118 decorators: NONE_STR,
119 type_parameters: NONE_STR,
120 return_type: NONE_STR,
121 extra_json: NONE_STR,
122 });
123 w.node_ids.push(ids::file_node_id(file_path));
124 w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
125

Callers

nothing calls this directly

Calls 14

grammar_forFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
putMethod · 0.80
push_nodeMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
line_startsFunction · 0.70
unwrapMethod · 0.60
setMethod · 0.45
visit_nodeMethod · 0.45

Tested by

no test coverage detected