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

Method extract_import

codegraph-kernel/src/python.rs:619–700  ·  view source on GitHub ↗
(&mut self, node: Node<'t>)

Source from the content-addressed store, hash-verified

617 }
618
619 fn extract_import(&mut self, node: Node<'t>) {
620 let import_text = self.text(node).trim().to_string();
621 let imports_kind = edge_kind_index("imports").unwrap();
622
623 if node.kind() == "import_from_statement" {
624 // Hook path: module_name field → import node + module ref, then
625 // per-name binding refs (emitPyFromImportRefs).
626 let Some(module_node) = node.child_by_field_name("module_name") else { return };
627 let module_name = self.text(module_node).to_string();
628 if module_name.is_empty() {
629 return;
630 }
631 self.create_node(
632 "import",
633 &module_name,
634 node,
635 Extra { signature: Some(import_text), ..Extra::default() },
636 );
637 let parent = self.top_row();
638 self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
639
640 // emitPyFromImportRefs: one `imports` ref per imported name.
641 for i in 0..node.named_child_count() {
642 let Some(child) = node.named_child(i) else { continue };
643 if child.start_byte() == module_node.start_byte()
644 && child.end_byte() == module_node.end_byte()
645 {
646 continue;
647 }
648 if child.kind() == "wildcard_import" {
649 continue;
650 }
651 let name_node = match child.kind() {
652 "aliased_import" => child
653 .child_by_field_name("alias")
654 .or_else(|| child.child_by_field_name("name"))
655 .or_else(|| child.named_child(0)),
656 "dotted_name" => Some(child),
657 _ => None,
658 };
659 let Some(name_node) = name_node else { continue };
660 let raw = self.text(name_node);
661 let local = raw.rsplit('.').next().unwrap_or("");
662 if local.is_empty() {
663 continue;
664 }
665 self.push_ref_at(parent, &local.to_string(), imports_kind, name_node);
666 }
667 return;
668 }
669
670 // import_statement: `import a.b, x as y` — one import node + module ref
671 // per dotted name (the python multi-import branch).
672 let parent = self.top_row();
673 for i in 0..node.named_child_count() {
674 let Some(child) = node.named_child(i) else { continue };
675 if child.kind() == "dotted_name" {
676 let name = self.text(child).to_string();

Callers 1

visit_nodeMethod · 0.45

Calls 7

edge_kind_indexFunction · 0.85
nextMethod · 0.80
textMethod · 0.65
unwrapMethod · 0.60
create_nodeMethod · 0.45
top_rowMethod · 0.45
push_ref_atMethod · 0.45

Tested by

no test coverage detected