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

Method extract_import

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

Source from the content-addressed store, hash-verified

478 }
479
480 fn extract_import(&mut self, node: Node<'t>) {
481 let import_text = self.text(node).trim().to_string();
482 let imports_kind = edge_kind_index("imports").unwrap();
483
484 if node.kind() == "import_from_statement" {
485 // Hook path: module_name field → import node + module ref, then
486 // per-name binding refs (emitPyFromImportRefs).
487 let Some(module_node) = node.child_by_field_name("module_name") else { return };
488 let module_name = self.text(module_node).to_string();
489 if module_name.is_empty() {
490 return;
491 }
492 self.create_node(
493 "import",
494 &module_name,
495 node,
496 Extra { signature: Some(import_text), ..Extra::default() },
497 );
498 let parent = self.top_row();
499 self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
500
501 // emitPyFromImportRefs: one `imports` ref per imported name.
502 for i in 0..node.named_child_count() {
503 let Some(child) = node.named_child(i) else { continue };
504 if child.start_byte() == module_node.start_byte()
505 && child.end_byte() == module_node.end_byte()
506 {
507 continue;
508 }
509 if child.kind() == "wildcard_import" {
510 continue;
511 }
512 let name_node = match child.kind() {
513 "aliased_import" => child
514 .child_by_field_name("alias")
515 .or_else(|| child.child_by_field_name("name"))
516 .or_else(|| child.named_child(0)),
517 "dotted_name" => Some(child),
518 _ => None,
519 };
520 let Some(name_node) = name_node else { continue };
521 let raw = self.text(name_node);
522 let local = raw.rsplit('.').next().unwrap_or("");
523 if local.is_empty() {
524 continue;
525 }
526 self.push_ref_at(parent, &local.to_string(), imports_kind, name_node);
527 }
528 return;
529 }
530
531 // import_statement: `import a.b, x as y` — one import node + module ref
532 // per dotted name (the python multi-import branch).
533 let parent = self.top_row();
534 for i in 0..node.named_child_count() {
535 let Some(child) = node.named_child(i) else { continue };
536 if child.kind() == "dotted_name" {
537 let name = self.text(child).to_string();

Callers 1

visit_nodeMethod · 0.45

Calls 6

edge_kind_indexFunction · 0.85
unwrapMethod · 0.60
textMethod · 0.45
create_nodeMethod · 0.45
top_rowMethod · 0.45
push_ref_atMethod · 0.45

Tested by

no test coverage detected