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

Method extract_variable

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

extractVariable's Go branch: var/const specs + short_var_declaration. Returns the initializers it walked under their specs' symbols, for the declaration scan to skip.

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

Source from the content-addressed store, hash-verified

688 /// Returns the initializers it walked under their specs' symbols, for the
689 /// declaration scan to skip.
690 fn extract_variable(&mut self, node: Node<'t>) -> Vec<usize> {
691 let mut walked = Vec::new();
692 let docstring = self.docstring_of(node);
693 let is_const_decl = node.kind() == "const_declaration";
694
695 // A `var ( … )` group wraps its specs in a `var_spec_list`; a
696 // `const ( … )` group holds them directly.
697 let mut specs: Vec<Node<'t>> = Vec::new();
698 for i in 0..node.named_child_count() {
699 let Some(child) = node.named_child(i) else { continue };
700 if child.kind() == "var_spec_list" {
701 specs.extend((0..child.named_child_count()).filter_map(|j| child.named_child(j)));
702 } else {
703 specs.push(child);
704 }
705 }
706 for spec in specs {
707 if !matches!(spec.kind(), "var_spec" | "const_spec") {
708 continue;
709 }
710 let mut var_row: Option<u32> = None;
711 if let Some(name_node) = spec.named_child(0) {
712 if name_node.kind() == "identifier" {
713 let name = self.text(name_node).to_string();
714 let value_node = if spec.named_child_count() > 1 {
715 spec.named_child(spec.named_child_count() - 1)
716 } else {
717 None
718 };
719 let signature = value_node.map(|v| util::init_signature(self.text(v)));
720 // A member of a `const ( … )` or `var ( … )` group has its
721 // own doc comment beside it in the parentheses; one without
722 // keeps the group's.
723 let doc = self.docstring_of(spec).or_else(|| docstring.clone());
724 // Exported by its own name's case, read from the spec (the
725 // declaration around it has no name).
726 let is_exported = Some(self.is_exported(spec));
727 var_row = self.create_node(
728 if is_const_decl { "constant" } else { "variable" },
729 &name,
730 spec,
731 Extra { docstring: doc, signature, is_exported, ..Extra::default() },
732 );
733 }
734 }
735 // Walk the initializer ATTRIBUTED to the declared symbol (#693).
736 if let Some(value_field) = spec.child_by_field_name("value") {
737 if let Some(row) = var_row {
738 let name = self.nodes_meta[row as usize].name.clone();
739 self.stack.push(Scope { row, kind: "variable", name });
740 self.visit_function_body(value_field);
741 self.stack.pop();
742 walked.push(value_field.id());
743 } else {
744 self.visit_function_body(value_field);
745 }
746 }
747 }

Callers 1

visit_nodeMethod · 0.45

Calls 10

init_signatureFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
idMethod · 0.80
textMethod · 0.65
docstring_ofMethod · 0.45
is_exportedMethod · 0.45
create_nodeMethod · 0.45
visit_function_bodyMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected