Skip to content

Commit 2080d82

Browse files
authored
Merge pull request DeusData#1060 from isc-tdyar/fix/semantic-body-tokens
fix(extract): populate body_tokens before MinHash gate; add OS/TS identifier node types
2 parents bb9677f + b9797ec commit 2080d82

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static bool try_append_ident(const char *source, uint32_t s, int len, uint32_t *
8282
/* Walk AST body, collect unique identifier text as space-separated string.
8383
* Returns arena-allocated string or NULL. */
8484
static char *extract_body_ident_tokens(CBMExtractCtx *ctx, TSNode body) {
85-
enum { BT_STACK = 512, BT_BUF = 512, BT_MAX_IDENTS = 40, BT_SEEN = 128, BT_SEEN_MASK = 127 };
85+
enum { BT_STACK = 512, BT_BUF = 2048, BT_MAX_IDENTS = 128, BT_SEEN = 256, BT_SEEN_MASK = 255 };
8686
TSNode bt_stack[BT_STACK];
8787
int bt_top = 0;
8888
bt_stack[bt_top++] = body;
@@ -98,9 +98,13 @@ static char *extract_body_ident_tokens(CBMExtractCtx *ctx, TSNode body) {
9898
if (nc == 0) {
9999
const char *k = ts_node_type(nd);
100100
if (strcmp(k, "identifier") == 0 || strcmp(k, "field_identifier") == 0 ||
101-
strcmp(k, "property_identifier") == 0 ||
101+
strcmp(k, "property_identifier") == 0 || strcmp(k, "type_identifier") == 0 ||
102102
strcmp(k, "objectscript_identifier") == 0 ||
103-
strcmp(k, "identifier_segment_immediate") == 0) {
103+
strcmp(k, "objectscript_identifier_special") == 0 ||
104+
strcmp(k, "identifier_segment_immediate") == 0 ||
105+
strcmp(k, "identifier_segment_immediate_special") == 0 ||
106+
strcmp(k, "class_name") == 0 || strcmp(k, "method_name") == 0 ||
107+
strcmp(k, "routine_name") == 0 || strcmp(k, "quote_permitting_identifier") == 0) {
104108
uint32_t s = ts_node_start_byte(nd);
105109
int len = (int)(ts_node_end_byte(nd) - s);
106110
if (len > 0 && len < CBM_SZ_64 && s < (uint32_t)ctx->source_len) {
@@ -130,6 +134,10 @@ static void compute_fingerprint(CBMExtractCtx *ctx, CBMDefinition *def, TSNode f
130134
if (ts_node_is_null(body)) {
131135
body = func_node;
132136
}
137+
/* Extract raw identifier tokens from body for semantic search — before MinHash gate
138+
* so short functions still get body_tokens even without a fingerprint. */
139+
def->body_tokens = extract_body_ident_tokens(ctx, body);
140+
133141
cbm_minhash_t result;
134142
if (!cbm_minhash_compute(body, ctx->source, (int)ctx->language, &result)) {
135143
return; /* Too short or empty — no fingerprint */
@@ -157,9 +165,6 @@ static void compute_fingerprint(CBMExtractCtx *ctx, CBMDefinition *def, TSNode f
157165
cbm_ast_profile_to_str(&profile, sp_buf, sizeof(sp_buf));
158166
def->structural_profile = cbm_arena_strdup(ctx->arena, sp_buf);
159167
}
160-
161-
/* Extract raw identifier tokens from body for semantic search */
162-
def->body_tokens = extract_body_ident_tokens(ctx, body);
163168
}
164169

165170
// Tree-sitter row is 0-based; lines are 1-based.

‎tests/test_extraction.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,25 @@ TEST(ts_class) {
813813
PASS();
814814
}
815815

816+
TEST(body_tokens_type_identifier) {
817+
CBMFileResult *r = extract("function serialize(obj: MyModel): SerializedResult {\n"
818+
" const result: SerializedResult = new SerializedResult();\n"
819+
" return result;\n"
820+
"}\n",
821+
CBM_LANG_TYPESCRIPT, "t", "serial.ts");
822+
ASSERT_NOT_NULL(r);
823+
ASSERT_FALSE(r->has_error);
824+
for (int i = 0; i < r->defs.count; i++) {
825+
if (strcmp(r->defs.items[i].name, "serialize") == 0) {
826+
ASSERT_NOT_NULL(r->defs.items[i].body_tokens);
827+
ASSERT(strstr(r->defs.items[i].body_tokens, "SerializedResult") != NULL);
828+
break;
829+
}
830+
}
831+
cbm_free_result(r);
832+
PASS();
833+
}
834+
816835
/* --- Lua --- */
817836
TEST(lua_function) {
818837
CBMFileResult *r = extract(
@@ -4627,6 +4646,7 @@ SUITE(extraction) {
46274646
RUN_TEST(js_class);
46284647
RUN_TEST(ts_function);
46294648
RUN_TEST(ts_class);
4649+
RUN_TEST(body_tokens_type_identifier);
46304650
RUN_TEST(lua_function);
46314651
RUN_TEST(bash_function);
46324652
RUN_TEST(perl_function);

0 commit comments

Comments
 (0)