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

Method extract_import

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

Extract an import statement.

(&self, node: &Node, source: &str, file_path: &str)

Source from the content-addressed store, hash-verified

254
255 /// Extract an import statement.
256 fn extract_import(&self, node: &Node, source: &str, file_path: &str) -> Option<Entity> {
257 let line = node.start_position().row as u32 + 1;
258 let end_line = node.end_position().row as u32 + 1;
259
260 let text = self.node_text(node, source);
261
262 // Extract the module name from `import X` or `from X import Y`
263 let name = if node.kind() == "import_from_statement" {
264 // from X import Y → name is "X"
265 node.child_by_field_name("module_name")
266 .map(|n| self.node_text(&n, source))
267 .unwrap_or_else(|| text.clone())
268 } else {
269 // import X → name is "X"
270 let mut cursor = node.walk();
271 let children: Vec<_> = node.children(&mut cursor).collect();
272 children
273 .iter()
274 .find(|c| c.kind() == "dotted_name")
275 .map(|n| self.node_text(n, source))
276 .unwrap_or_else(|| text.clone())
277 };
278
279 let mut entity = Entity::new(name, EntityKind::Import, file_path, line, end_line);
280 entity = entity.with_signature(text);
281
282 Some(entity)
283 }
284
285 /// Build a function signature string.
286 fn build_function_signature(&self, node: &Node, source: &str) -> Option<String> {

Callers 1

walk_treeMethod · 0.45

Calls 7

walkMethod · 0.80
childrenMethod · 0.80
with_signatureMethod · 0.80
node_textMethod · 0.45
kindMethod · 0.45
cloneMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected