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

Method extract_import

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

Source from the content-addressed store, hash-verified

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