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

Method extract_variable

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

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

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