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

Method extract_variable

codegraph-kernel/src/go.rs:608–672  ·  view source on GitHub ↗

extractVariable's Go branch: var/const specs + short_var_declaration.

(&mut self, node: Node<'t>)

Source from the content-addressed store, hash-verified

606
607 /// extractVariable's Go branch: var/const specs + short_var_declaration.
608 fn extract_variable(&mut self, node: Node<'t>) {
609 let docstring = preceding_docstring(node, self.src);
610 let is_const_decl = node.kind() == "const_declaration";
611
612 for i in 0..node.named_child_count() {
613 let Some(spec) = node.named_child(i) else { continue };
614 if !matches!(spec.kind(), "var_spec" | "const_spec") {
615 continue;
616 }
617 let mut var_row: Option<u32> = None;
618 if let Some(name_node) = spec.named_child(0) {
619 if name_node.kind() == "identifier" {
620 let name = self.text(name_node).to_string();
621 let value_node = if spec.named_child_count() > 1 {
622 spec.named_child(spec.named_child_count() - 1)
623 } else {
624 None
625 };
626 let signature = value_node.map(|v| util::init_signature(self.text(v)));
627 var_row = self.create_node(
628 if is_const_decl { "constant" } else { "variable" },
629 &name,
630 spec,
631 Extra { docstring: docstring.clone(), signature, ..Extra::default() },
632 );
633 }
634 }
635 // Walk the initializer ATTRIBUTED to the declared symbol (#693).
636 if let Some(value_field) = spec.child_by_field_name("value") {
637 if let Some(row) = var_row {
638 let name = self.nodes_meta[row as usize].name.clone();
639 self.stack.push(Scope { row, kind: "variable", name });
640 self.visit_function_body(value_field);
641 self.stack.pop();
642 } else {
643 self.visit_function_body(value_field);
644 }
645 }
646 }
647
648 if node.kind() == "short_var_declaration" {
649 let left = node.child_by_field_name("left");
650 let right = node.child_by_field_name("right");
651 if let Some(left) = left {
652 let identifiers: Vec<Node> = if left.kind() == "expression_list" {
653 (0..left.named_child_count())
654 .filter_map(|i| left.named_child(i))
655 .filter(|c| c.kind() == "identifier")
656 .collect()
657 } else {
658 vec![left]
659 };
660 for id in identifiers {
661 let name = self.text(id).to_string();
662 let signature = right.map(|r| util::init_signature(self.text(r)));
663 self.create_node(
664 "variable",
665 &name,

Callers 1

visit_nodeMethod · 0.45

Calls 6

preceding_docstringFunction · 0.85
init_signatureFunction · 0.85
textMethod · 0.45
create_nodeMethod · 0.45
visit_function_bodyMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected