From 3cfb743a125ab5239e32a5ac75407d11b6e93b38 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sun, 25 Sep 2016 15:25:27 -0400 Subject: [PATCH] added remeasuring for character starts; fixed issue with inserting characters at the beginning of a line --- 4coder_default_include.cpp | 4 +- 4ed_file_view.cpp | 23 +++++-- buffer/4coder_buffer_abstract.cpp | 111 +++++++++++++++++++++++++++--- 3 files changed, 118 insertions(+), 20 deletions(-) diff --git a/4coder_default_include.cpp b/4coder_default_include.cpp index 9c144d4d..580dcabb 100644 --- a/4coder_default_include.cpp +++ b/4coder_default_include.cpp @@ -731,12 +731,10 @@ CUSTOM_COMMAND_SIG(write_character){ int32_t pos = view.cursor.pos; buffer_replace_range(app, &buffer, pos, pos, &character, 1); - view_set_cursor(app, &view, seek_pos(view.cursor.character_pos + 1), true); + view_set_cursor(app, &view, seek_pos(view.cursor.pos + 1), true); } } - - CUSTOM_COMMAND_SIG(delete_char){ uint32_t access = AccessOpen; View_Summary view = get_active_view(app, access); diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 49ba9110..a879865c 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -17,13 +17,15 @@ get_or_add_map_index(Models *models, i32 mapid){ i32 user_map_count = models->user_map_count; i32 *map_id_table = models->map_id_table; for (result = 0; result < user_map_count; ++result){ - if (map_id_table[result] == mapid) break; + if (map_id_table[result] == mapid){ + break; + } if (map_id_table[result] == -1){ map_id_table[result] = mapid; break; } } - return result; + return(result); } internal i32 @@ -32,13 +34,15 @@ get_map_index(Models *models, i32 mapid){ i32 user_map_count = models->user_map_count; i32 *map_id_table = models->map_id_table; for (result = 0; result < user_map_count; ++result){ - if (map_id_table[result] == mapid) break; + if (map_id_table[result] == mapid){ + break; + } if (map_id_table[result] == 0){ result = user_map_count; break; } } - return result; + return(result); } internal Command_Map* @@ -55,8 +59,12 @@ get_map_base(Models *models, i32 mapid, b32 add){ map = models->user_maps + mapid; } } - else if (mapid == mapid_global) map = &models->map_top; - else if (mapid == mapid_file) map = &models->map_file; + else if (mapid == mapid_global){ + map = &models->map_top; + } + else if (mapid == mapid_file){ + map = &models->map_file; + } return(map); } @@ -2059,7 +2067,8 @@ file_do_single_edit(System_Functions *system, // TODO(allen): write the remeasurement version file_allocate_character_starts_as_needed(general, file); - buffer_measure_character_starts(buffer, file->state.character_starts, 0, VWHITE); + buffer_remeasure_character_starts(buffer, line_start, line_end, line_shift, + file->state.character_starts, 0, VWHITE); file_allocate_wraps_as_needed(general, file); buffer_remeasure_wrap_y(buffer, line_start, line_end, line_shift, diff --git a/buffer/4coder_buffer_abstract.cpp b/buffer/4coder_buffer_abstract.cpp index 4f55bec4..6da65e08 100644 --- a/buffer/4coder_buffer_abstract.cpp +++ b/buffer/4coder_buffer_abstract.cpp @@ -148,6 +148,8 @@ buffer_measure_character_starts(Buffer_Type *buffer, i32 *character_starts, i32 skipping_whitespace = 1; } + stream.use_termination_character = 1; + stream.terminator = '\n'; if (buffer_stringify_loop(&stream, buffer, i, size)){ b32 still_looping = 0; do{ @@ -174,9 +176,6 @@ buffer_measure_character_starts(Buffer_Type *buffer, i32 *character_starts, i32 }while(still_looping); } - ++character_index; - character_starts[line_index++] = character_index; - assert_4tech(line_index-1 == buffer->line_count); } @@ -298,6 +297,96 @@ buffer_remeasure_starts(Buffer_Type *buffer, i32 line_start, i32 line_end, i32 l buffer->line_count = new_line_count; } +internal_4tech void +buffer_remeasure_character_starts(Buffer_Type *buffer, i32 line_start, i32 line_end, i32 line_shift, + i32 *character_starts, i32 mode, i32 virtual_whitespace){ + assert_4tech(mode == 0); + + i32 new_line_count = buffer->line_count; + + assert_4tech(0 <= line_start); + assert_4tech(line_start <= line_end); + assert_4tech(line_end < new_line_count - line_shift); + + ++line_end; + + // Shift + i32 line_count = new_line_count; + i32 new_line_end = line_end; + if (line_shift != 0){ + line_count -= line_shift; + new_line_end += line_shift; + + memmove_4tech(character_starts + line_end + line_shift, character_starts + line_end, + sizeof(i32)*(line_count - line_end + 1)); + } + + // Iteration data (yikes! Need better loop system) + Buffer_Stream_Type stream = {0}; + i32 size = buffer_size(buffer); + i32 char_i = buffer->line_starts[line_start]; + i32 line_i = line_start; + + // Character measurement + i32 last_char_start = character_starts[line_i]; + i32 current_char_start = last_char_start; + + b32 skipping_whitespace = 0; + + if (virtual_whitespace){ + skipping_whitespace = 1; + } + + stream.use_termination_character = 1; + stream.terminator = '\n'; + if (buffer_stringify_loop(&stream, buffer, char_i, size)){ + b32 still_looping = 0; + do{ + for (; char_i < stream.end; ++char_i){ + u8 ch = (u8)stream.data[char_i]; + if (ch == '\n'){ + character_starts[line_i++] = last_char_start; + ++current_char_start; + last_char_start = current_char_start; + if (virtual_whitespace){ + skipping_whitespace = 1; + } + + if (line_i >= new_line_end){ + goto buffer_remeasure_character_starts_end; + } + } + else{ + if (ch != ' ' && ch != '\t'){ + skipping_whitespace = 0; + } + + if (!skipping_whitespace){ + ++current_char_start; + } + } + } + still_looping = buffer_stringify_next(&stream); + }while(still_looping); + } + + assert_4tech(line_i >= new_line_end); + + buffer_remeasure_character_starts_end:; + + // Adjust + if (line_i <= new_line_end){ + i32 character_shift = current_char_start - character_starts[line_i]; + + if (character_shift != 0){ + character_starts += line_i; + for (; line_i <= new_line_count; ++line_i, ++character_starts){ + *character_starts += character_shift; + } + } + } +} + internal_4tech void buffer_remeasure_wrap_y(Buffer_Type *buffer, i32 line_start, i32 line_end, i32 line_shift, f32 *wraps, f32 font_height, f32 *adv, f32 max_width){ @@ -317,7 +406,7 @@ buffer_remeasure_wrap_y(Buffer_Type *buffer, i32 line_start, i32 line_end, i32 l new_line_end += line_shift; memmove_4tech(wraps + line_end + line_shift, wraps + line_end, - sizeof(i32)*(line_count - line_end)); + sizeof(i32)*(line_count - line_end + 1)); } // Iteration data (yikes! Need better loop system) @@ -368,13 +457,15 @@ buffer_remeasure_wrap_y(Buffer_Type *buffer, i32 line_start, i32 line_end, i32 l buffer_remeasure_wraps_end:; - f32 y_shift = current_wrap - wraps[line_i]; - // Adjust - if (y_shift != 0){ - wraps += line_i; - for (; line_i <= new_line_count; ++line_i, ++wraps){ - *wraps += y_shift; + if (line_i <= new_line_end){ + f32 y_shift = current_wrap - wraps[line_i]; + + if (y_shift != 0){ + wraps += line_i; + for (; line_i <= new_line_count; ++line_i, ++wraps){ + *wraps += y_shift; + } } } }