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

Method extract_variable

codegraph-kernel/src/ccpp/mod.rs:1572–1633  ·  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

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

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