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

Method extract_import

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

Source from the content-addressed store, hash-verified

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