From e274bb9120a25b487327aa4604ae9c9f18db46c1 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Fri, 14 Jun 2019 15:57:22 -0700 Subject: [PATCH] Simplify and rename 'hard starts' stuff for indentation --- 4coder_api_transition_30_31_helpers.cpp | 15 +++++- 4coder_auto_indent.cpp | 71 +++++-------------------- 4coder_auto_indent.h | 7 --- 4coder_combined_write_commands.cpp | 8 ++- 4coder_generated/command_metadata.h | 16 +++--- 4coder_helper.cpp | 43 +++++++++++++-- 4coder_helper.h | 7 +++ things_ive_broken.txt | 2 + 8 files changed, 86 insertions(+), 83 deletions(-) diff --git a/4coder_api_transition_30_31_helpers.cpp b/4coder_api_transition_30_31_helpers.cpp index 3337684f..74020d06 100644 --- a/4coder_api_transition_30_31_helpers.cpp +++ b/4coder_api_transition_30_31_helpers.cpp @@ -436,11 +436,22 @@ mark_enclosures(Application_Links *app, Managed_Scope render_scope, Buffer_Summa } } +struct Hard_Start_Result{ + i32 char_pos; + i32 indent_pos; + i32 all_whitespace; + i32 all_space; +}; + static Hard_Start_Result buffer_find_hard_start(Application_Links *app, Buffer_Summary *buffer, i32 line_start, i32 tab_width){ Hard_Start_Result result = {}; if (buffer != 0){ - buffer_find_hard_start(app, buffer->buffer_id, line_start, tab_width); + Indent_Info info = get_indent_info_line_start(app, buffer->buffer_id, line_start, tab_width); + result.char_pos = info.first_char_pos; + result.indent_pos = info.indent_pos; + result.all_whitespace = info.is_blank; + result.all_space = info.all_space; } return(result); } @@ -667,7 +678,7 @@ execute_standard_build(Application_Links *app, View_Summary *view, Buffer_ID act static b32 post_buffer_range_to_clipboard(Application_Links *app, i32 clipboard_index, Buffer_Summary *buffer, i32 first, i32 one_past_last){ - return(post_buffer_range_to_clipboard(app, clipboard_index, buffer==0?0:buffer->buffer_id, first, one_past_last)); + return(post_buffer_range_to_clipboard(app, clipboard_index, buffer==0?0:buffer->buffer_id, first, one_past_last)); } static void diff --git a/4coder_auto_indent.cpp b/4coder_auto_indent.cpp index b38bf0ac..f2b7dbb5 100644 --- a/4coder_auto_indent.cpp +++ b/4coder_auto_indent.cpp @@ -4,51 +4,6 @@ // TOP -internal Hard_Start_Result -buffer_find_hard_start(Application_Links *app, Buffer_ID buffer, i32 line_start, i32 tab_width){ - i32 tab_additional_width = tab_width - 1; - - Hard_Start_Result result = {}; - result.all_space = true; - result.indent_pos = 0; - result.char_pos = line_start; - - char data_chunk[1024]; - Stream_Chunk stream = {}; - stream.add_null = true; - if (init_stream_chunk(&stream, app, buffer, line_start, data_chunk, sizeof(data_chunk))){ - b32 still_looping = true; - do{ - for (; result.char_pos < stream.end; ++result.char_pos){ - char c = stream.data[result.char_pos]; - - if (c == '\n' || c == 0){ - result.all_whitespace = 1; - goto double_break; - } - - if (c >= '!' && c <= '~'){ - goto double_break; - } - - if (c == '\t'){ - result.indent_pos += tab_additional_width; - } - - if (c != ' '){ - result.all_space = false; - } - - result.indent_pos += 1; - } - still_looping = forward_stream_chunk(&stream); - }while(still_looping); - } - - double_break:; - return(result); -} - internal Buffer_Batch_Edit make_batch_from_indent_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, i32 first_line, i32 one_past_last_line, i32 *indent_marks, @@ -65,10 +20,10 @@ make_batch_from_indent_marks(Application_Links *app, Arena *arena, Buffer_ID buf line_number < one_past_last_line; ++line_number){ i32 line_start_pos = get_line_start_pos(app, buffer, line_number); - Hard_Start_Result hard_start = buffer_find_hard_start(app, buffer, line_start_pos, opts.tab_width); + Indent_Info hard_start = get_indent_info_line_start(app, buffer, line_start_pos, opts.tab_width); i32 correct_indentation = shifted_indent_marks[line_number]; - if (hard_start.all_whitespace && opts.empty_blank_lines){ + if (hard_start.is_blank && opts.empty_blank_lines){ correct_indentation = 0; } if (correct_indentation == -1){ @@ -85,7 +40,7 @@ make_batch_from_indent_marks(Application_Links *app, Arena *arena, Buffer_ID buf str_size = tab_count + space_count; str = push_array(arena, char, str_size); block_fill_u8(str, tab_count, '\t'); - block_fill_u8(str + tab_count, space_count, ' '); + block_fill_u8(str + tab_count, space_count, ' '); } else{ str_size = correct_indentation; @@ -99,7 +54,7 @@ make_batch_from_indent_marks(Application_Links *app, Arena *arena, Buffer_ID buf edits[edit_count].str_start = (i32)str_position; edits[edit_count].len = (i32)str_size; edits[edit_count].start = line_start_pos; - edits[edit_count].end = hard_start.char_pos; + edits[edit_count].end = hard_start.first_char_pos; edit_count += 1; } @@ -302,17 +257,17 @@ get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, // NOTE(allen): Check for multi-line tokens if (prev_token.start <= this_line_start && prev_token.start + prev_token.size > this_line_start){ if (prev_token.type == CPP_TOKEN_COMMENT || prev_token.type == CPP_TOKEN_STRING_CONSTANT){ - Hard_Start_Result hard_start = buffer_find_hard_start(app, buffer, this_line_start, tab_width); + Indent_Info hard_start = get_indent_info_line_start(app, buffer, this_line_start, tab_width); if (exact_align){ this_indent = indent.previous_comment_indent; } else{ - if (hard_start.all_whitespace){ + if (hard_start.is_blank){ this_indent = previous_indent; } else{ - i32 line_pos = hard_start.char_pos - this_line_start; + i32 line_pos = hard_start.first_char_pos - this_line_start; this_indent = line_pos + indent.comment_shift; if (this_indent < 0){ this_indent = 0; @@ -320,7 +275,7 @@ get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, } } - if (!hard_start.all_whitespace){ + if (!hard_start.is_blank){ if (line_number >= first_line){ indent.previous_comment_indent = this_indent; } @@ -400,7 +355,7 @@ get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, this_indent += tab_width; } } - } + } } } if (this_indent < 0){ @@ -450,7 +405,7 @@ get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, { i32 line = get_line_number_from_pos(app, buffer, token.start); i32 start = get_line_start_pos(app, buffer, line); - Hard_Start_Result hard_start = buffer_find_hard_start(app, buffer, start, tab_width); + Indent_Info hard_start = get_indent_info_line_start(app, buffer, start, tab_width); i32 old_dist_to_token = (token.start - start); i32 old_indent = hard_start.indent_pos; @@ -469,9 +424,9 @@ get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, i32 start = get_line_start_pos(app, buffer, line); i32 char_pos = token.start - start; - Hard_Start_Result hard_start = buffer_find_hard_start(app, buffer, start, tab_width); + Indent_Info hard_start = get_indent_info_line_start(app, buffer, start, tab_width); - i32 line_pos = hard_start.char_pos - start; + i32 line_pos = hard_start.first_char_pos - start; indent.paren_anchor_indent[indent.paren_nesting] = char_pos - line_pos + indent.previous_line_indent + 1; } @@ -483,7 +438,7 @@ get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer, { if (!(token.flags & CPP_TFLAG_PP_BODY)){ if (indent.paren_nesting > 0){ - --indent.paren_nesting; + --indent.paren_nesting; } } }break; diff --git a/4coder_auto_indent.h b/4coder_auto_indent.h index 127fb61e..85872a37 100644 --- a/4coder_auto_indent.h +++ b/4coder_auto_indent.h @@ -7,13 +7,6 @@ #if !defined(FCODER_AUTO_INDENT_H) #define FCODER_AUTO_INDENT_H -struct Hard_Start_Result{ - i32 char_pos; - i32 indent_pos; - i32 all_whitespace; - i32 all_space; -}; - struct Indent_Options{ b32 empty_blank_lines; b32 use_tabs; diff --git a/4coder_combined_write_commands.cpp b/4coder_combined_write_commands.cpp index 644f8ace..57f2e038 100644 --- a/4coder_combined_write_commands.cpp +++ b/4coder_combined_write_commands.cpp @@ -113,11 +113,9 @@ static i32 get_start_of_line_at_cursor(Application_Links *app, View_ID view, Buffer_ID buffer){ i32 pos = 0; view_get_cursor_pos(app, view, &pos); - Full_Cursor cursor = {}; - view_compute_cursor(app, view, seek_pos(pos), &cursor); - view_compute_cursor(app, view, seek_line_char(cursor.line, 1), &cursor); - Hard_Start_Result hard_start = buffer_find_hard_start(app, buffer, cursor.pos, DEF_TAB_WIDTH); - return(hard_start.char_pos); + i32 line = get_line_number_from_pos(app, buffer, pos); + pos = get_line_side_pos(app, buffer, line, Side_Min); + return(pos); } static b32 diff --git a/4coder_generated/command_metadata.h b/4coder_generated/command_metadata.h index 9b79e56a..00336934 100644 --- a/4coder_generated/command_metadata.h +++ b/4coder_generated/command_metadata.h @@ -394,10 +394,10 @@ static Command_Metadata fcoder_metacmd_table[234] = { { PROC_LINKS(interactive_new, 0), "interactive_new", 15, "Interactively creates a new file.", 33, "w:\\4ed\\code\\4coder_lists.cpp", 28, 885 }, { PROC_LINKS(interactive_open, 0), "interactive_open", 16, "Interactively opens a file.", 27, "w:\\4ed\\code\\4coder_lists.cpp", 28, 919 }, { PROC_LINKS(command_lister, 0), "command_lister", 14, "Opens an interactive list of all registered commands.", 53, "w:\\4ed\\code\\4coder_lists.cpp", 28, 1004 }, -{ PROC_LINKS(auto_tab_whole_file, 0), "auto_tab_whole_file", 19, "Audo-indents the entire current buffer.", 39, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 606 }, -{ PROC_LINKS(auto_tab_line_at_cursor, 0), "auto_tab_line_at_cursor", 23, "Auto-indents the line on which the cursor sits.", 47, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 619 }, -{ PROC_LINKS(auto_tab_range, 0), "auto_tab_range", 14, "Auto-indents the range between the cursor and the mark.", 55, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 633 }, -{ PROC_LINKS(write_and_auto_tab, 0), "write_and_auto_tab", 18, "Inserts a character and auto-indents the line on which the cursor sits.", 71, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 646 }, +{ PROC_LINKS(auto_tab_whole_file, 0), "auto_tab_whole_file", 19, "Audo-indents the entire current buffer.", 39, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 561 }, +{ PROC_LINKS(auto_tab_line_at_cursor, 0), "auto_tab_line_at_cursor", 23, "Auto-indents the line on which the cursor sits.", 47, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 574 }, +{ PROC_LINKS(auto_tab_range, 0), "auto_tab_range", 14, "Auto-indents the range between the cursor and the mark.", 55, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 588 }, +{ PROC_LINKS(write_and_auto_tab, 0), "write_and_auto_tab", 18, "Inserts a character and auto-indents the line on which the cursor sits.", 71, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 601 }, { PROC_LINKS(list_all_locations, 0), "list_all_locations", 18, "Queries the user for a string and lists all exact case-sensitive matches found in all open buffers.", 99, "w:\\4ed\\code\\4coder_search.cpp", 29, 705 }, { PROC_LINKS(list_all_substring_locations, 0), "list_all_substring_locations", 28, "Queries the user for a string and lists all case-sensitive substring matches found in all open buffers.", 103, "w:\\4ed\\code\\4coder_search.cpp", 29, 712 }, { PROC_LINKS(list_all_locations_case_insensitive, 0), "list_all_locations_case_insensitive", 35, "Queries the user for a string and lists all exact case-insensitive matches found in all open buffers.", 101, "w:\\4ed\\code\\4coder_search.cpp", 29, 719 }, @@ -471,10 +471,10 @@ static Command_Metadata fcoder_metacmd_table[234] = { { PROC_LINKS(write_note, 0), "write_note", 10, "At the cursor, insert a '// NOTE' comment, includes user name if it was specified in config.4coder.", 99, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 94 }, { PROC_LINKS(write_block, 0), "write_block", 11, "At the cursor, insert a block comment.", 38, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 100 }, { PROC_LINKS(write_zero_struct, 0), "write_zero_struct", 17, "At the cursor, insert a ' = {};'.", 33, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 106 }, -{ PROC_LINKS(comment_line, 0), "comment_line", 12, "Insert '//' at the beginning of the line after leading whitespace.", 66, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 135 }, -{ PROC_LINKS(uncomment_line, 0), "uncomment_line", 14, "If present, delete '//' at the beginning of the line after leading whitespace.", 78, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 149 }, -{ PROC_LINKS(comment_line_toggle, 0), "comment_line_toggle", 19, "Turns uncommented lines into commented lines and vice versa for comments starting with '//'.", 92, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 163 }, -{ PROC_LINKS(snippet_lister, 0), "snippet_lister", 14, "Opens a snippet lister for inserting whole pre-written snippets of text.", 72, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 252 }, +{ PROC_LINKS(comment_line, 0), "comment_line", 12, "Insert '//' at the beginning of the line after leading whitespace.", 66, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 133 }, +{ PROC_LINKS(uncomment_line, 0), "uncomment_line", 14, "If present, delete '//' at the beginning of the line after leading whitespace.", 78, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 147 }, +{ PROC_LINKS(comment_line_toggle, 0), "comment_line_toggle", 19, "Turns uncommented lines into commented lines and vice versa for comments starting with '//'.", 92, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 161 }, +{ PROC_LINKS(snippet_lister, 0), "snippet_lister", 14, "Opens a snippet lister for inserting whole pre-written snippets of text.", 72, "w:\\4ed\\code\\4coder_combined_write_commands.cpp", 46, 250 }, { PROC_LINKS(set_bindings_choose, 0), "set_bindings_choose", 19, "Remap keybindings using the 'choose' mapping rule.", 50, "w:\\4ed\\code\\4coder_remapping_commands.cpp", 41, 39 }, { PROC_LINKS(set_bindings_default, 0), "set_bindings_default", 20, "Remap keybindings using the 'default' mapping rule.", 51, "w:\\4ed\\code\\4coder_remapping_commands.cpp", 41, 49 }, { PROC_LINKS(set_bindings_mac_default, 0), "set_bindings_mac_default", 24, "Remap keybindings using the 'mac-default' mapping rule.", 55, "w:\\4ed\\code\\4coder_remapping_commands.cpp", 41, 64 }, diff --git a/4coder_helper.cpp b/4coder_helper.cpp index a21d81bb..2ec75ec8 100644 --- a/4coder_helper.cpp +++ b/4coder_helper.cpp @@ -638,9 +638,9 @@ scan(Application_Links *app, Boundary_Function_List funcs, Buffer_ID buffer, Sca } else{ result = -1; - for (Boundary_Function_Node *node = funcs.first; - node != 0; - node = node->next){ + for (Boundary_Function_Node *node = funcs.first; + node != 0; + node = node->next){ i32 pos = scan(app, node->func, buffer, direction, start_pos); result = Max(result, pos); } @@ -1272,6 +1272,43 @@ get_pos_of_blank_line_grouped(Application_Links *app, Buffer_ID buffer, Scan_Dir return(pos); } +internal Indent_Info +get_indent_info_range(Application_Links *app, Buffer_ID buffer, Range range, i32 tab_width){ + Scratch_Block scratch(app); + String_Const_u8 s = push_buffer_range(app, scratch, buffer, range); + + i32 tab_additional_width = tab_width - 1; + + Indent_Info info = {}; + info.first_char_pos = range.end; + info.is_blank = true; + info.all_space = true; + for (umem i = 0; i < s.size; i += 1){ + u8 c = s.str[i]; + if (!character_is_whitespace(c)){ + info.is_blank = false; + info.all_space = false; + info.first_char_pos = range.start + (i32)i; + break; + } + if (c != ' '){ + info.all_space = false; + } + if (c == '\t'){ + info.indent_pos += tab_additional_width; + } + info.indent_pos += 1; + } + + return(info); +} + +internal Indent_Info +get_indent_info_line_start(Application_Links *app, Buffer_ID buffer, i32 line_start, i32 tab_width){ + i32 end = get_line_side_pos_from_pos(app, buffer, line_start, Side_Max); + return(get_indent_info_range(app, buffer, make_range(line_start, end), tab_width)); +} + //////////////////////////////// static History_Group diff --git a/4coder_helper.h b/4coder_helper.h index faab3deb..dae10c72 100644 --- a/4coder_helper.h +++ b/4coder_helper.h @@ -126,6 +126,13 @@ struct Boundary_Function_List{ typedef Range Enclose_Function(Application_Links *app, Buffer_ID buffer, Range range); +struct Indent_Info{ + i32 first_char_pos; + i32 indent_pos; + i32 is_blank; + i32 all_space; +}; + //////////////////////////////// struct Stream_Chunk{ diff --git a/things_ive_broken.txt b/things_ive_broken.txt index 318fecbf..ee1bc7f8 100644 --- a/things_ive_broken.txt +++ b/things_ive_broken.txt @@ -28,3 +28,5 @@ view_boundary_seek_set_pos view_buffer_boundary_range view_buffer_snipe_range +Hard_Start_Result -> Line_Indent_Info +buffer_find_hard_start -> get_line_indent_info \ No newline at end of file