Collapse process query match logic back into tree_sitter_code_index_update_process_query_match

This commit is contained in:
Peter Slattery 2025-08-03 00:00:24 -07:00
parent 30ac43ce66
commit 6d77862b78
2 changed files with 54 additions and 111 deletions

View File

@ -544,38 +544,37 @@ tree_sitter_code_index_update_process_query_match(
Code_Index_Scope_Delim* delim = code_index_new_scope_delim( Code_Index_Scope_Delim* delim = code_index_new_scope_delim(
state->index, &state->index_arena, kind, type_range state->index, &state->index_arena, kind, type_range
); );
code_index_scope_delim_insert(&state->index->scope_delim_list, state->last_delim, delim);
zdll_push_back(state->index->scope_delim_list.first, state->index->scope_delim_list.last, delim); state->last_delim = delim;
state->index->scope_delim_list.count += 1;
} }
} }
else if (string_match(capture_name, SCu8("definition.class"))) else if (string_match(capture_name, SCu8("definition.class")))
{ {
state->last_note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Type, type_range, state->nest_stack_last); Code_Index_Note* note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Type, type_range, state->nest_stack_last);
state->last_note_match_id = query_match.id; state->last_note_match_id = query_match.id;
sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); code_index_note_insert(&state->index->note_list, state->last_note, note);
state->index->note_list.count += 1; state->last_note = note;
} }
else if (string_match(capture_name, SCu8("definition.function"))) else if (string_match(capture_name, SCu8("definition.function")))
{ {
state->last_note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Function, type_range, state->nest_stack_last); Code_Index_Note* note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Function, type_range, state->nest_stack_last);
state->last_note_match_id = query_match.id; state->last_note_match_id = query_match.id;
sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); code_index_note_insert(&state->index->note_list, state->last_note, note);
state->index->note_list.count += 1; state->last_note = note;
} }
else if (string_match(capture_name, SCu8("definition.method"))) else if (string_match(capture_name, SCu8("definition.method")))
{ {
state->last_note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Function, type_range, state->nest_stack_last);; Code_Index_Note* note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Function, type_range, state->nest_stack_last);;
state->last_note_match_id = query_match.id; state->last_note_match_id = query_match.id;
sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); code_index_note_insert(&state->index->note_list, state->last_note, note);
state->index->note_list.count += 1; state->last_note = note;
} }
else if (string_match(capture_name, SCu8("definition.type"))) else if (string_match(capture_name, SCu8("definition.type")))
{ {
state->last_note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Type, type_range, state->nest_stack_last); Code_Index_Note* note = code_index_new_note(state->index, &state->index_arena, CodeIndexNote_Type, type_range, state->nest_stack_last);
state->last_note_match_id = query_match.id; state->last_note_match_id = query_match.id;
sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); code_index_note_insert(&state->index->note_list, state->last_note, note);
state->index->note_list.count += 1; state->last_note = note;
} }
else if (string_match(capture_name, SCu8("name"))) else if (string_match(capture_name, SCu8("name")))
{ {
@ -777,6 +776,9 @@ tree_sitter_code_index_update_tick(Application_Links* app)
Buffer_Tree_Sitter_Data* tree_data = scope_attachment(app, buffer_scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data); Buffer_Tree_Sitter_Data* tree_data = scope_attachment(app, buffer_scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data);
if (!tree_data || !tree_data->tree) continue; if (!tree_data || !tree_data->tree) continue;
Buffer_ID out_buffer = get_buffer_by_name(app, string_u8_litexpr("*tree*"), Access_Always);
buffer_replace_range(app, out_buffer, Ii64(0,buffer_get_size(app, out_buffer)), SCu8(""));
Range_i64 old_range = tree_data->last_update_old_range; Range_i64 old_range = tree_data->last_update_old_range;
Range_i64 new_range = tree_data->last_update_new_range; Range_i64 new_range = tree_data->last_update_new_range;
if (old_range == new_range) continue; if (old_range == new_range) continue;
@ -798,8 +800,9 @@ tree_sitter_code_index_update_tick(Application_Links* app)
TSNode last_node = first_node; TSNode last_node = first_node;
bool would_reparse_entire_file = ts_node_eq(first_node, root); bool would_reparse_entire_file = ts_node_eq(first_node, root);
int query_count = 1; int query_count = 0;
Tree_Sitter_Query_Cursor queries[2]; Tree_Sitter_Query_Cursor queries[2];
Range_i64 edit_range;
if (would_reparse_entire_file) if (would_reparse_entire_file)
{ {
@ -825,8 +828,8 @@ tree_sitter_code_index_update_tick(Application_Links* app)
if (!ts_node_is_null(prev_sibling) && !ts_node_is_null(next_sibling)) if (!ts_node_is_null(prev_sibling) && !ts_node_is_null(next_sibling))
{ {
first_node = prev_sibling; edit_range.min = (i64)ts_node_start_byte(prev_sibling);
last_node = next_sibling; edit_range.max = (i64)ts_node_end_byte(next_sibling);
query_count = 2; query_count = 2;
queries[0] = tree_sitter_query_init( queries[0] = tree_sitter_query_init(
app, app,
@ -843,8 +846,8 @@ tree_sitter_code_index_update_tick(Application_Links* app)
} }
else if (!ts_node_is_null(prev_sibling)) else if (!ts_node_is_null(prev_sibling))
{ {
first_node = prev_sibling; edit_range.min = (i64)ts_node_start_byte(prev_sibling);
last_node = prev_sibling; edit_range.max = (i64)ts_node_end_byte(prev_sibling);
queries[0] = tree_sitter_query_init( queries[0] = tree_sitter_query_init(
app, app,
state.buffer_id, state.buffer_id,
@ -854,8 +857,8 @@ tree_sitter_code_index_update_tick(Application_Links* app)
} }
else if (!ts_node_is_null(next_sibling)) else if (!ts_node_is_null(next_sibling))
{ {
first_node = next_sibling; edit_range.min = (i64)ts_node_start_byte(next_sibling);
last_node = next_sibling; edit_range.max = (i64)ts_node_end_byte(next_sibling);
queries[0] = tree_sitter_query_init( queries[0] = tree_sitter_query_init(
app, app,
state.buffer_id, state.buffer_id,
@ -865,8 +868,8 @@ tree_sitter_code_index_update_tick(Application_Links* app)
} }
else else
{ {
first_node = root; edit_range.min = (i64)ts_node_start_byte(root);
last_node = root; edit_range.max = (i64)ts_node_end_byte(root);
queries[0] = tree_sitter_query_init( queries[0] = tree_sitter_query_init(
app, app,
state.buffer_id, state.buffer_id,
@ -877,36 +880,34 @@ tree_sitter_code_index_update_tick(Application_Links* app)
} }
else else
{ {
TSNode node_at = first_node;
TSNode parent = ts_node_parent(first_node); TSNode parent = ts_node_parent(first_node);
while (!ts_node_has_error(parent) && !ts_node_eq(parent, root)) while (!ts_node_has_error(parent) && !ts_node_eq(parent, root))
{ {
first_node = parent; node_at = parent;
parent = ts_node_parent(first_node); parent = ts_node_parent(node_at);
} }
last_node = first_node; edit_range.min = (i64)ts_node_start_byte(node_at);
edit_range.max = (i64)ts_node_end_byte(node_at);
queries[0] = tree_sitter_query_init( queries[0] = tree_sitter_query_init(
app, app,
state.buffer_id, state.buffer_id,
state.language->queries.ptr[Tree_Sitter_Language_Query_Tags], state.language->queries.ptr[Tree_Sitter_Language_Query_Tags],
first_node node_at
); );
} }
Range_i64 edit_range;
edit_range.min = (i64)ts_node_start_byte(first_node);
edit_range.max = (i64)ts_node_end_byte(last_node);
tree_data->last_update_node_range = edit_range; // TODO(PS): TEMP - remove me once debugging is done tree_data->last_update_node_range = edit_range; // TODO(PS): TEMP - remove me once debugging is done
// Free Scope Delimiters & Notes that fall within old_range // Free Scope Delimiters & Notes that fall within old_range
Code_Index_Scope_Delim* delim = state.index->scope_delim_list.first; Code_Index_Scope_Delim* delim = state.index->scope_delim_list.first;
Code_Index_Scope_Delim* before_range = 0; Code_Index_Scope_Delim* delim_before_range = 0;
Code_Index_Scope_Delim* after_range = 0; Code_Index_Scope_Delim* after_range = 0;
while (delim) while (delim)
{ {
Code_Index_Scope_Delim* next = delim->next; Code_Index_Scope_Delim* next = delim->next;
if (delim->pos.min < edit_range.min && delim->pos.max <= edit_range.min) if (delim->pos.min < edit_range.min && delim->pos.max <= edit_range.min)
{ {
before_range = delim; delim_before_range = delim;
} }
if (range_overlap(delim->pos, edit_range)) if (range_overlap(delim->pos, edit_range))
{ {
@ -918,21 +919,21 @@ tree_sitter_code_index_update_tick(Application_Links* app)
); );
code_index_free_scope_delim(state.index, delim); code_index_free_scope_delim(state.index, delim);
} }
if (delim->pos.min >= edit_range.max) if (delim->pos.min >= edit_range.max) break;
{
after_range = delim;
break;
}
delim = next; delim = next;
} }
state.last_delim = delim_before_range;
Code_Index_Note* note = state.index->note_list.first; Code_Index_Note* note = state.index->note_list.first;
Code_Index_Note* prev = 0; Code_Index_Note* prev = 0;
Code_Index_Note* note_before_range = 0; Code_Index_Note* note_before_range = 0;
Code_Index_Note* note_after_range = 0;
while (note) while (note)
{ {
Code_Index_Note* next = note->next; Code_Index_Note* next = note->next;
if (note->pos.min < edit_range.min && note->pos.max <= edit_range.min)
{
note_before_range = note;
}
if (range_overlap(note->pos, edit_range)) if (range_overlap(note->pos, edit_range))
{ {
Code_Index_Note* new_first = state.index->note_list.first; Code_Index_Note* new_first = state.index->note_list.first;
@ -958,11 +959,12 @@ tree_sitter_code_index_update_tick(Application_Links* app)
{ {
prev = note; prev = note;
} }
if (note->pos.min >= edit_range.max) break;
note = next; note = next;
} }
state.last_note = note_before_range;
if (state.index->note_list.count == 0) Assert(state.index->note_list.first == 0 && state.index->note_list.first == 0); if (state.index->note_list.count == 0) Assert(state.index->note_list.first == 0 && state.index->note_list.first == 0);
Code_Index_Scope_Delim* delim_prev = before_range;
for (int i = 0; i < query_count; i++) for (int i = 0; i < query_count; i++)
{ {
Tree_Sitter_Query_Cursor query = queries[i]; Tree_Sitter_Query_Cursor query = queries[i];
@ -970,80 +972,20 @@ tree_sitter_code_index_update_tick(Application_Links* app)
u32 capture_index; u32 capture_index;
while (state.ok && tree_sitter_query_continue(&query, &query_match, &capture_index)) while (state.ok && tree_sitter_query_continue(&query, &query_match, &capture_index))
{ {
// TODO(PS): @Collapse - the entire body of this while loop used to be in tree_sitter_code_index_update_process_query_match(
// tree_sitter_code_index_update_process_query_match &state, query, query_match, capture_index, scratch
// After we're done, see what we can do to collapse );
TSQueryCapture type_capture = query_match.captures[capture_index];
TSNode type_node = type_capture.node;
Range_i64 type_range = tree_sitter_node_to_range(type_node);
u32 length;
const char* tmp = ts_query_capture_name_for_id(query.query, type_capture.index, &length);
String_Const_u8 capture_name = SCu8((char*)tmp, length);
if (string_match(capture_name, SCu8("scope_begin")) || string_match(capture_name, SCu8("scope_end")))
{
Code_Index_Scope_Delim_Kind kind;
String_Const_u8 delim_char = string_substring(state.buffer_contents, type_range);
bool skip = false;
if (delim_char.str[0] == '{') kind = CodeIndexScopeDelim_ScopeOpen;
else if (delim_char.str[0] == '}') kind = CodeIndexScopeDelim_ScopeClose;
else if (delim_char.str[0] == '(') kind = CodeIndexScopeDelim_ParenOpen;
else if (delim_char.str[0] == ')') kind = CodeIndexScopeDelim_ParenClose;
else if (delim_char.str[0] == '[') kind = CodeIndexScopeDelim_BracketOpen;
else if (delim_char.str[0] == ']') kind = CodeIndexScopeDelim_BracketClose;
else skip = true;
if (!skip)
{
Code_Index_Scope_Delim* delim = code_index_new_scope_delim(
state.index, &state.index_arena, kind, type_range
);
code_index_scope_delim_insert(&state.index->scope_delim_list, delim_prev, delim);
delim_prev = delim;
}
}
else if (string_match(capture_name, SCu8("definition.class")))
{
Code_Index_Note* note = code_index_new_note(state.index, &state.index_arena, CodeIndexNote_Type, type_range, state.nest_stack_last);
state.last_note_match_id = query_match.id;
code_index_note_insert(&state.index->note_list, state.last_note, note);
state.last_note = note;
}
else if (string_match(capture_name, SCu8("definition.function")))
{
Code_Index_Note* note = code_index_new_note(state.index, &state.index_arena, CodeIndexNote_Function, type_range, state.nest_stack_last);
state.last_note_match_id = query_match.id;
code_index_note_insert(&state.index->note_list, state.last_note, note);
state.last_note = note;
}
else if (string_match(capture_name, SCu8("definition.method")))
{
Code_Index_Note* note = code_index_new_note(state.index, &state.index_arena, CodeIndexNote_Function, type_range, state.nest_stack_last);;
state.last_note_match_id = query_match.id;
code_index_note_insert(&state.index->note_list, state.last_note, note);
state.last_note = note;
}
else if (string_match(capture_name, SCu8("definition.type")))
{
Code_Index_Note* note = code_index_new_note(state.index, &state.index_arena, CodeIndexNote_Type, type_range, state.nest_stack_last);
state.last_note_match_id = query_match.id;
code_index_note_insert(&state.index->note_list, state.last_note, note);
state.last_note = note;
}
else if (string_match(capture_name, SCu8("name")))
{
if (state.last_note != 0 && state.last_note_match_id == query_match.id)
{
state.last_note->pos = Ii64_size(type_range.start, type_range.end - type_range.start);
}
}
} }
tree_sitter_query_end(&query); tree_sitter_query_end(&query);
} }
tree_sitter_code_index_update_complete(app, &state, scratch, true); tree_sitter_code_index_update_complete(app, &state, scratch, true);
// @Report
for (Code_Index_Note* note = state.index->note_list.first; note != 0; note = note->next)
{
String_Const_u8 string = push_stringf(scratch, "Note: '%.*s'\n", string_expand(note->text));
buffer_replace_range(app, out_buffer, Ii64(buffer_get_size(app, out_buffer)), string);
}
} }
buffer_modified_set_clear(); buffer_modified_set_clear();

View File

@ -95,6 +95,7 @@ struct Tree_Sitter_Code_Index_Update_State
Code_Index_Nest_Stack* nest_stack_first = 0; Code_Index_Nest_Stack* nest_stack_first = 0;
Code_Index_Nest_Stack* nest_stack_last = 0; Code_Index_Nest_Stack* nest_stack_last = 0;
Code_Index_Scope_Delim* last_delim = 0;
Code_Index_Note* last_note = 0; Code_Index_Note* last_note = 0;
u32 last_note_match_id = max_u32; u32 last_note_match_id = max_u32;