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

Method extract_import

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

Source from the content-addressed store, hash-verified

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

Callers 1

visit_nodeMethod · 0.45

Calls 8

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
walkMethod · 0.45

Tested by

no test coverage detected