From 1507f414b1efb4029740854a5bf26ef4ed4bf1ee Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Mon, 4 Aug 2025 00:51:19 -0700 Subject: [PATCH] Cleaning up temp code, unused struct members, and using a Scratch_Block to store buffer_contents during parsing --- code/custom/4coder_custom_hooks.cpp | 14 -------- code/custom/4coder_tree_sitter.cpp | 55 +++++++++++++---------------- code/custom/4coder_tree_sitter.h | 5 --- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/code/custom/4coder_custom_hooks.cpp b/code/custom/4coder_custom_hooks.cpp index e021a316..83fd358a 100644 --- a/code/custom/4coder_custom_hooks.cpp +++ b/code/custom/4coder_custom_hooks.cpp @@ -391,20 +391,6 @@ function void custom_render_buffer( // NOTE(allen): put the actual text on the actual screen draw_text_layout_default(app, text_layout_id); - // TEMP - highlight the node range of the last edit - if (use_tree_sitter_token_coloring) { - Managed_Scope scope = buffer_get_managed_scope(app, buffer); - Buffer_Tree_Sitter_Data* tree_data = scope_attachment(app, scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data); - Range_i64 last_update_range = tree_data->last_update_node_range; - Rect_f32 range_min = text_layout_character_on_screen(app, text_layout_id, last_update_range.min); - Rect_f32 range_max = text_layout_character_on_screen(app, text_layout_id, last_update_range.max); - Rect_f32 range; - range.x0 = Min(range_min.x0, range_max.x0); - range.y0 = Min(range_min.y0, range_max.y0); - range.x1 = Max(range_min.x1, range_max.x1); - range.y1 = Max(range_min.y1, range_max.y1); - draw_rectangle(app, range, 0.f, 0x33FF00FF); - } draw_set_clip(app, prev_clip); } diff --git a/code/custom/4coder_tree_sitter.cpp b/code/custom/4coder_tree_sitter.cpp index af0199ab..82e35dfc 100644 --- a/code/custom/4coder_tree_sitter.cpp +++ b/code/custom/4coder_tree_sitter.cpp @@ -247,20 +247,15 @@ function Tree_Sitter_Parse_State tree_sitter_parse_state_create(Application_Links* app, Buffer_ID buffer_id) { Tree_Sitter_Parse_State parse_state = {}; - parse_state.arena = make_arena_system(KB(16)); // TODO(PS): investigate if we need this or if we should just use the scratch block instead parse_state.parser = ts_parser_new(); ts_parser_set_timeout_micros(parse_state.parser, 5000); - Scratch_Block scratch(app); - parse_state.buffer_name = push_buffer_unique_name(app, scratch, buffer_id); - parse_state.lang = tree_sitter_language_for_buffer(app, buffer_id); // TODO(PS): this might not need to be stored on parse_state - parse_state.buffer_contents = push_whole_buffer(app, &parse_state.arena, buffer_id); - Managed_Scope scope = buffer_get_managed_scope(app, buffer_id); parse_state.tree_data = scope_attachment(app, scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data); parse_state.old_tree = tree_sitter_buffer_get_tree_copy(parse_state.tree_data); - bool lang_set = ts_parser_set_language(parse_state.parser, parse_state.lang->language); + Tree_Sitter_Language_Definition* lang = tree_sitter_language_for_buffer(app, buffer_id); + bool lang_set = ts_parser_set_language(parse_state.parser, lang->language); if (!lang_set) { AssertMessageAlways("Failed to set the language for the parser." @@ -276,7 +271,6 @@ tree_sitter_parse_state_destroy(Tree_Sitter_Parse_State* parse_state) { ts_parser_delete(parse_state->parser); ts_tree_delete(parse_state->old_tree); - linalloc_clear(&parse_state->arena); } function TSTree* @@ -296,9 +290,13 @@ tree_sitter_parse_full_file_async(Async_Context* actx, String_Const_u8 data) if (data.size != sizeof(Buffer_ID)) return; Buffer_ID buffer_id = *(Buffer_ID*)data.str; Application_Links *app = actx->app; + Scratch_Block scratch(app); acquire_global_frame_mutex(app); - Tree_Sitter_Parse_State parse_state = tree_sitter_parse_state_create(app, buffer_id); + String_Const_u8 buffer_contents = push_whole_buffer(app, scratch, buffer_id); + Tree_Sitter_Parse_State parse_state = tree_sitter_parse_state_create( + app, buffer_id + ); release_global_frame_mutex(app); // Iterate until we get a tree or we find that we should cancel the parse @@ -306,7 +304,7 @@ tree_sitter_parse_full_file_async(Async_Context* actx, String_Const_u8 data) b32 canceled = false; for (;;) { - new_tree = ts_parser_parse_string(parse_state.parser, parse_state.old_tree, (char *)parse_state.buffer_contents.str, (u32)parse_state.buffer_contents.size); + new_tree = ts_parser_parse_string(parse_state.parser, parse_state.old_tree, (char *)buffer_contents.str, (u32)buffer_contents.size); if (async_check_canceled(actx)) { canceled = true; @@ -332,8 +330,11 @@ tree_sitter_parse_full_file_async(Async_Context* actx, String_Const_u8 data) function void tree_sitter_parse_incremental(Application_Links* app, Buffer_ID buffer_id) { - Tree_Sitter_Parse_State parse_state = tree_sitter_parse_state_create(app, buffer_id); - //Assert(parse_state.old_tree != 0); // if we are incrementally parsing, we expect there to be an old tree + Scratch_Block scratch(app); + String_Const_u8 buffer_contents = push_whole_buffer(app, scratch, buffer_id); + Tree_Sitter_Parse_State parse_state = tree_sitter_parse_state_create( + app, buffer_id + ); TSTree *new_tree = 0; while (!new_tree) @@ -341,8 +342,8 @@ tree_sitter_parse_incremental(Application_Links* app, Buffer_ID buffer_id) new_tree = ts_parser_parse_string( parse_state.parser, parse_state.old_tree, - (char *)parse_state.buffer_contents.str, - (u32)parse_state.buffer_contents.size + (char *)buffer_contents.str, + (u32)buffer_contents.size ); } @@ -479,7 +480,9 @@ tree_sitter_code_index_update_state_create( { state.index_arena = code_index_storage->arena; state.index = code_index_storage->file; - } else { + } + else + { state.index_arena = make_arena_system(KB(16)); state.index = push_array_zero(&state.index_arena, Code_Index_File, 1); string_pool_init(&state.index->string_pool); @@ -494,21 +497,6 @@ tree_sitter_code_index_update_state_create( code_index_note_list_hash_init(&state.new_notes); state.ok = true; -/* @RemoveOldFreeingCode = Freeing all delims is undesired for incremental updates, and unnecessary if - we're working with a wholly new index. The same will be true for notes soon. - if (state.index->scope_delim_list.last) { - state.index->scope_delim_list.last->next = state.index->scope_delim_free; - } - state.index->scope_delim_free = state.index->scope_delim_list.first; - block_zero_struct(&state.index->scope_delim_list); - - - if (state.index->note_list.last) { - state.index->note_list.last->next = state.index->note_free; - } - state.index->note_free = state.index->note_list.first; - block_zero_struct(&state.index->note_list); -*/ return state; } @@ -685,6 +673,12 @@ tree_sitter_code_index_update_complete( } } + // TODO(PS): This should work similarly to how tree-sitter tree copies work + // - when you take the code_index, its ref count gets incremented + // - if you try to free it, decrement ref count. If ref count = 0, free + // - creating a new file means pushing the old files back one + // this way the memory still exists for existing processes to point to + // but the rest of the application can start using the new file if (!update_was_incremental) { code_index_lock(); @@ -883,7 +877,6 @@ tree_sitter_code_index_update_tick(Application_Links* app) Range_i64 edit_range; edit_range.min = (i64)ts_node_start_byte(queries[0].first_node); edit_range.max = (i64)ts_node_end_byte(queries[query_count-1].first_node); - tree_data->last_update_node_range = edit_range; // TODO(PS): TEMP - remove me once debugging is done // Free Scope Delimiters that fall within old_range { diff --git a/code/custom/4coder_tree_sitter.h b/code/custom/4coder_tree_sitter.h index 4b0c7bc8..72fdb6e0 100644 --- a/code/custom/4coder_tree_sitter.h +++ b/code/custom/4coder_tree_sitter.h @@ -72,12 +72,7 @@ struct Code_Index_Nest_Stack struct Tree_Sitter_Parse_State { - String_Const_u8 buffer_name; - Tree_Sitter_Language_Definition* lang; - String_Const_u8 buffer_contents; Buffer_Tree_Sitter_Data* tree_data; - - Arena arena; TSParser* parser; TSTree *old_tree; };