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

Method extract_variable

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

1114 /// tree-sitter.ts:2795); cpp takes the TS GENERIC fallback (direct
1115 /// identifier children).
1116 fn extract_variable(&mut self, node: Node<'t>) {
1117 let is_const = self.variant == Variant::C && self.is_const_declaration(node);
1118 let kind: &'static str = if is_const { "constant" } else { "variable" };
1119 let docstring = preceding_docstring(node, self.src);
1120 // isExported?.() ?? false — EXPLICIT false (tri-state flag set).
1121 let is_exported = Some(false);
1122
1123 if self.variant == Variant::C {
1124 if has_function_ancestor(node) {
1125 return;
1126 }
1127 for i in 0..node.named_child_count() {
1128 let Some(child) = node.named_child(i) else { continue };
1129 if !matches!(
1130 child.kind(),
1131 "init_declarator" | "pointer_declarator" | "array_declarator"
1132 ) {
1133 continue;
1134 }
1135 let Some(name_node) = c_declarator_identifier(child) else { continue };
1136 let name = self.text(name_node);
1137 if name.is_empty() {
1138 continue;
1139 }
1140 let value_node = if child.kind() == "init_declarator" {
1141 child.child_by_field_name("value")
1142 } else {
1143 None
1144 };
1145 let signature = value_node.map(|v| util::init_signature(self.text(v)));
1146 self.create_node(
1147 kind,
1148 name,
1149 child,
1150 Extra {
1151 docstring: docstring.clone(),
1152 signature,
1153 is_exported,
1154 ..Extra::default()
1155 },
1156 );
1157 }
1158 } else {
1159 // Generic fallback: direct identifier children only (`int x;`
1160 // extracts; `int x = 5;` nests in an init_declarator and does not).
1161 for i in 0..node.named_child_count() {
1162 let Some(child) = node.named_child(i) else { continue };
1163 if child.kind() != "identifier" {
1164 continue;
1165 }
1166 let name = self.text(child).to_string();
1167 if !name.is_empty() && name != "<anonymous>" {
1168 self.create_node(
1169 kind,
1170 &name,
1171 child,
1172 Extra { docstring: docstring.clone(), is_exported, ..Extra::default() },
1173 );

Callers 1

visit_nodeMethod · 0.45

Calls 7

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

Tested by

no test coverage detected