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

Method extract_variable

codegraph-kernel/src/go.rs:613–677  ·  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

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

Callers 1

visit_nodeMethod · 0.45

Calls 6

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

Tested by

no test coverage detected