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

Method extract_import

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

Source from the content-addressed store, hash-verified

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