From 6d77862b7824116e50647b8319dcd5a9b1877d3e Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Sun, 3 Aug 2025 00:00:24 -0700 Subject: [PATCH] Collapse process query match logic back into tree_sitter_code_index_update_process_query_match --- code/custom/4coder_tree_sitter.cpp | 164 ++++++++++------------------- code/custom/4coder_tree_sitter.h | 1 + 2 files changed, 54 insertions(+), 111 deletions(-) diff --git a/code/custom/4coder_tree_sitter.cpp b/code/custom/4coder_tree_sitter.cpp index 5b52fef6..4c178ad7 100644 --- a/code/custom/4coder_tree_sitter.cpp +++ b/code/custom/4coder_tree_sitter.cpp @@ -544,38 +544,37 @@ tree_sitter_code_index_update_process_query_match( Code_Index_Scope_Delim* delim = code_index_new_scope_delim( state->index, &state->index_arena, kind, type_range ); - - zdll_push_back(state->index->scope_delim_list.first, state->index->scope_delim_list.last, delim); - state->index->scope_delim_list.count += 1; + code_index_scope_delim_insert(&state->index->scope_delim_list, state->last_delim, delim); + state->last_delim = delim; } } 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; - sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); - state->index->note_list.count += 1; + 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"))) { - 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; - sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); - state->index->note_list.count += 1; + 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"))) { - 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; - sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); - state->index->note_list.count += 1; + 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"))) { - 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; - sll_queue_push(state->index->note_list.first, state->index->note_list.last, state->last_note); - state->index->note_list.count += 1; + code_index_note_insert(&state->index->note_list, state->last_note, note); + state->last_note = note; } 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); 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 new_range = tree_data->last_update_new_range; if (old_range == new_range) continue; @@ -798,8 +800,9 @@ tree_sitter_code_index_update_tick(Application_Links* app) TSNode last_node = first_node; 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]; + Range_i64 edit_range; 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)) { - first_node = prev_sibling; - last_node = next_sibling; + edit_range.min = (i64)ts_node_start_byte(prev_sibling); + edit_range.max = (i64)ts_node_end_byte(next_sibling); query_count = 2; queries[0] = tree_sitter_query_init( app, @@ -843,8 +846,8 @@ tree_sitter_code_index_update_tick(Application_Links* app) } else if (!ts_node_is_null(prev_sibling)) { - first_node = prev_sibling; - last_node = prev_sibling; + edit_range.min = (i64)ts_node_start_byte(prev_sibling); + edit_range.max = (i64)ts_node_end_byte(prev_sibling); queries[0] = tree_sitter_query_init( app, state.buffer_id, @@ -854,8 +857,8 @@ tree_sitter_code_index_update_tick(Application_Links* app) } else if (!ts_node_is_null(next_sibling)) { - first_node = next_sibling; - last_node = next_sibling; + edit_range.min = (i64)ts_node_start_byte(next_sibling); + edit_range.max = (i64)ts_node_end_byte(next_sibling); queries[0] = tree_sitter_query_init( app, state.buffer_id, @@ -865,8 +868,8 @@ tree_sitter_code_index_update_tick(Application_Links* app) } else { - first_node = root; - last_node = root; + edit_range.min = (i64)ts_node_start_byte(root); + edit_range.max = (i64)ts_node_end_byte(root); queries[0] = tree_sitter_query_init( app, state.buffer_id, @@ -877,36 +880,34 @@ tree_sitter_code_index_update_tick(Application_Links* app) } else { + TSNode node_at = first_node; TSNode parent = ts_node_parent(first_node); while (!ts_node_has_error(parent) && !ts_node_eq(parent, root)) { - first_node = parent; - parent = ts_node_parent(first_node); + node_at = parent; + 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( app, state.buffer_id, 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 // Free Scope Delimiters & Notes that fall within old_range 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; while (delim) { Code_Index_Scope_Delim* next = delim->next; 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)) { @@ -918,21 +919,21 @@ tree_sitter_code_index_update_tick(Application_Links* app) ); code_index_free_scope_delim(state.index, delim); } - if (delim->pos.min >= edit_range.max) - { - after_range = delim; - break; - } + if (delim->pos.min >= edit_range.max) break; delim = next; } + state.last_delim = delim_before_range; Code_Index_Note* note = state.index->note_list.first; Code_Index_Note* prev = 0; Code_Index_Note* note_before_range = 0; - Code_Index_Note* note_after_range = 0; while (note) { 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)) { 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; } + if (note->pos.min >= edit_range.max) break; 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); - Code_Index_Scope_Delim* delim_prev = before_range; for (int i = 0; i < query_count; i++) { Tree_Sitter_Query_Cursor query = queries[i]; @@ -970,80 +972,20 @@ tree_sitter_code_index_update_tick(Application_Links* app) u32 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 - // 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_code_index_update_process_query_match( + &state, query, query_match, capture_index, scratch + ); } tree_sitter_query_end(&query); } 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(); diff --git a/code/custom/4coder_tree_sitter.h b/code/custom/4coder_tree_sitter.h index 25f3682e..c816a943 100644 --- a/code/custom/4coder_tree_sitter.h +++ b/code/custom/4coder_tree_sitter.h @@ -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_last = 0; + Code_Index_Scope_Delim* last_delim = 0; Code_Index_Note* last_note = 0; u32 last_note_match_id = max_u32;