Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

extract: one Field per class field (Java, C#, Solidity)

Martin Vogelef0f4807eb1a
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 1001563f5..0f5f8641d 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -221,7 +221,7 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec);
static void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
- const CBMLangSpec *spec);
+ const CBMLangSpec *spec, int fields_from);
static void extract_rust_impl(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
const CBMLangSpec *spec);
@@ -7569,10 +7569,11 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
extract_class_methods(ctx, node, class_qn, spec);
// Extract typed struct/class fields (for cross-file LSP type resolution)
+ int fields_from = ctx->result->defs.count;
extract_class_fields(ctx, node, class_qn, spec);
- // Extract class-level variables (field declarations)
- extract_class_variables(ctx, node, class_qn, spec);
+ // Extract class-level variables (field declarations not already Fields)
+ extract_class_variables(ctx, node, class_qn, spec, fields_from);
if (ctx->language == CBM_LANG_PYTHON) {
extract_py_field_types(ctx, node, class_qn);
@@ -10208,6 +10209,15 @@ static TSNode resolve_field_name_node(TSNode child) {
return null_node;
}
const char *nk = ts_node_type(name_node);
+ /* Java `private static final String PROP = "v";`: the declarator holds the
+ * initializer too, so its text named the Field `PROP = "v"`; its name is
+ * the declarator's own name field */
+ if (strcmp(nk, "variable_declarator") == 0) {
+ TSNode inner = ts_node_child_by_field_name(name_node, TS_FIELD("name"));
+ if (!ts_node_is_null(inner)) {
+ return inner;
+ }
+ }
if (strcmp(nk, "pointer_declarator") == 0 || strcmp(nk, "array_declarator") == 0) {
TSNode inner = ts_node_child_by_field_name(name_node, TS_FIELD("declarator"));
if (!ts_node_is_null(inner)) {
@@ -10606,9 +10616,14 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
member_iter_done(&it);
}
-// Extract class-level variables (field declarations inside class bodies)
+// Extract class-level variables (field declarations inside class bodies).
+// A declaration that extract_class_fields already made a Field (the defs from
+// `fields_from` on) is not minted again: Java, C# and Solidity list the same
+// kind as field and variable, and the second copy was a Variable with the
+// module's qualified name (`demo.query.filters` for field Query.filters),
+// which collided with package paths.
static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
- const CBMLangSpec *spec) {
+ const CBMLangSpec *spec, int fields_from) {
if (!spec->variable_node_types || !spec->variable_node_types[0]) {
return;
}
@@ -10618,6 +10633,10 @@ static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const
return;
}
+ /* the Fields of this body, in body order: walked alongside the children */
+ int fields_to = ctx->result->defs.count;
+ int fi = fields_from;
+
/* Record the declaring class on every variable minted from this body (see
* push_var_def_qn); saved/restored so module-level minting stays bare. */
const char *saved_parent = ctx->var_parent_class;
@@ -10626,9 +10645,19 @@ static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const
member_iter_init(&it, body, ctx->language, true);
TSNode child;
while (member_iter_next(&it, &child)) {
- if (cbm_kind_in_set(child, spec->variable_node_types)) {
- extract_var_names(ctx, child, spec);
+ if (!cbm_kind_in_set(child, spec->variable_node_types)) {
+ continue;
+ }
+ int line = (int)ts_node_start_point(child).row + TS_LINE_OFFSET;
+ while (fi < fields_to && (strcmp(ctx->result->defs.items[fi].label, "Field") != 0 ||
+ (int)ctx->result->defs.items[fi].start_line < line)) {
+ fi++;
+ }
+ if (fi < fields_to && (int)ctx->result->defs.items[fi].start_line == line &&
+ spec->field_node_types && cbm_kind_in_set(child, spec->field_node_types)) {
+ continue; /* already a Field of this class */
}
+ extract_var_names(ctx, child, spec);
}
member_iter_done(&it);
ctx->var_parent_class = saved_parent;
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 5c85441ea..5a0e94c8b 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -586,6 +586,48 @@ TEST(java_class) {
PASS();
}
+/* A class field is one Field. Java, C# and Solidity list the field
+ * declaration as a variable kind too, and the class-body variable pass minted
+ * a second node: a Variable with the module's qualified name (Query.filters
+ * became `demo.query.filters`, the name of a package a documentation code
+ * span then bound). Languages whose class variables are no Field keep them. */
+TEST(class_field_is_one_field_not_also_a_variable) {
+ CBMFileResult *r = extract("package demo.query;\npublic class Query {\n"
+ " private Object filters;\n"
+ " private static final String PROP = \"v\";\n}\n",
+ CBM_LANG_JAVA, "t", "Query.java");
+ ASSERT_NOT_NULL(r);
+ ASSERT_EQ(count_defs_named(r, "Field", "filters"), 1);
+ ASSERT_EQ(count_defs_named(r, "Variable", "filters"), 0);
+ /* an initialized field is named by its declarator's name, not its text
+ * (`PROP = "v"` was the Field's name, so only the duplicate was findable) */
+ ASSERT_EQ(count_defs_named(r, "Field", "PROP"), 1);
+ ASSERT_EQ(count_defs_named(r, "Variable", "PROP"), 0);
+ cbm_free_result(r);
+ r = extract("namespace Demo {\n public class Q {\n private int count;\n }\n}\n",
+ CBM_LANG_CSHARP, "t", "Q.cs");
+ ASSERT_NOT_NULL(r);
+ ASSERT_EQ(count_defs_named(r, "Field", "count"), 1);
+ ASSERT_EQ(count_defs_named(r, "Variable", "count"), 0);
+ cbm_free_result(r);
+ r = extract("pragma solidity ^0.8.0;\ncontract Q {\n uint256 public total;\n}\n",
+ CBM_LANG_SOLIDITY, "t", "Q.sol");
+ ASSERT_NOT_NULL(r);
+ ASSERT_EQ(count_defs_named(r, "Field", "total"), 1);
+ ASSERT_EQ(count_defs_named(r, "Variable", "total"), 0);
+ cbm_free_result(r);
+ /* no Field for these: their class variables stay */
+ r = extract("class Q {\n var count: Int = 0\n}\n", CBM_LANG_SWIFT, "t", "Q.swift");
+ ASSERT_NOT_NULL(r);
+ ASSERT_EQ(count_defs_named(r, "Variable", "count"), 1);
+ cbm_free_result(r);
+ r = extract("extends Node\nclass Inner:\n var x = 1\n", CBM_LANG_GDSCRIPT, "t", "q.gd");
+ ASSERT_NOT_NULL(r);
+ ASSERT_EQ(count_defs_named(r, "Variable", "x"), 1);
+ cbm_free_result(r);
+ PASS();
+}
+
TEST(java_method) {
CBMFileResult *r = extract(
"public class Svc { public void doWork() {} public int compute(int x) { return x; } }",
@@ -10398,6 +10440,7 @@ SUITE(extraction) {
/* OOP */
RUN_TEST(java_class);
RUN_TEST(java_method);
+ RUN_TEST(class_field_is_one_field_not_also_a_variable);
RUN_TEST(java_interface);
RUN_TEST(java_interface_no_duplicate_function_issue1234);
RUN_TEST(java_enum_dedup_preserves_calls_issue1234);