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

Method extract_variable

codegraph-kernel/src/ccpp/mod.rs:1574–1635  ·  view source on GitHub ↗

extractVariable: C takes the dedicated branch (file-scope declarators, tree-sitter.ts:2795); cpp takes the TS GENERIC fallback (direct identifier children).

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

Source from the content-addressed store, hash-verified

1572 /// tree-sitter.ts:2795); cpp takes the TS GENERIC fallback (direct
1573 /// identifier children).
1574 fn extract_variable(&mut self, node: Node<'t>) {
1575 let is_const = self.variant == Variant::C && self.is_const_declaration(node);
1576 let kind: &'static str = if is_const { "constant" } else { "variable" };
1577 let docstring = preceding_docstring(node, self.src);
1578 // isExported?.() ?? false — EXPLICIT false (tri-state flag set).
1579 let is_exported = Some(false);
1580
1581 if self.variant == Variant::C {
1582 if has_function_ancestor(node) {
1583 return;
1584 }
1585 for i in 0..node.named_child_count() {
1586 let Some(child) = node.named_child(i) else { continue };
1587 if !matches!(
1588 child.kind(),
1589 "init_declarator" | "pointer_declarator" | "array_declarator"
1590 ) {
1591 continue;
1592 }
1593 let Some(name_node) = c_declarator_identifier(child) else { continue };
1594 let name = self.text(name_node);
1595 if name.is_empty() {
1596 continue;
1597 }
1598 let value_node = if child.kind() == "init_declarator" {
1599 child.child_by_field_name("value")
1600 } else {
1601 None
1602 };
1603 let signature = value_node.map(|v| util::init_signature(self.text(v)));
1604 self.create_node(
1605 kind,
1606 name,
1607 child,
1608 Extra {
1609 docstring: docstring.clone(),
1610 signature,
1611 is_exported,
1612 ..Extra::default()
1613 },
1614 );
1615 }
1616 } else {
1617 // Generic fallback: direct identifier children only (`int x;`
1618 // extracts; `int x = 5;` nests in an init_declarator and does not).
1619 for i in 0..node.named_child_count() {
1620 let Some(child) = node.named_child(i) else { continue };
1621 if child.kind() != "identifier" {
1622 continue;
1623 }
1624 let name = self.text(child).to_string();
1625 if !name.is_empty() && name != "<anonymous>" {
1626 self.create_node(
1627 kind,
1628 &name,
1629 child,
1630 Extra { docstring: docstring.clone(), is_exported, ..Extra::default() },
1631 );

Callers 1

dispatch_nodeMethod · 0.45

Calls 8

preceding_docstringFunction · 0.85
has_function_ancestorFunction · 0.85
c_declarator_identifierFunction · 0.85
init_signatureFunction · 0.85
is_const_declarationMethod · 0.80
mapMethod · 0.80
textMethod · 0.65
create_nodeMethod · 0.45

Tested by

no test coverage detected