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

Method extract_variable

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

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

Callers 1

dispatch_nodeMethod · 0.45

Calls 9

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
walkMethod · 0.45
create_nodeMethod · 0.45

Tested by

no test coverage detected