diff --git a/4coder_auto_indent.cpp b/4coder_auto_indent.cpp index ba791130..a98397e9 100644 --- a/4coder_auto_indent.cpp +++ b/4coder_auto_indent.cpp @@ -458,6 +458,56 @@ get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *b return(indent_marks); } +static void +get_indent_lines_minimum(Application_Links *app, Buffer_Summary *buffer, + int32_t start_pos, int32_t end_pos, + int32_t *line_start_out, int32_t *line_end_out){ + int32_t line_start = buffer_get_line_index(app, buffer, start_pos); + int32_t line_end = buffer_get_line_index(app, buffer, end_pos) + 1; + + *line_start_out = line_start; + *line_end_out = line_end; +} + +static void +get_indent_lines_whole_tokens(Application_Links *app, Buffer_Summary *buffer, Cpp_Token_Array tokens, + int32_t start_pos, int32_t end_pos, + int32_t *line_start_out, int32_t *line_end_out){ + int32_t line_start = buffer_get_line_index(app, buffer, start_pos); + int32_t line_end = buffer_get_line_index(app, buffer, end_pos); + + for (;line_start > 0;){ + int32_t line_start_pos = 0; + Cpp_Token *token = get_first_token_at_line(app, buffer, tokens, line_start, &line_start_pos); + if (token->start < line_start_pos){ + line_start = buffer_get_line_index(app, buffer, token->start); + } + else{ + break; + } + } + + for (;line_end+1 < buffer->line_count;){ + int32_t next_line_start_pos = 0; + Cpp_Token *token = get_first_token_at_line(app, buffer, tokens, line_end+1, &next_line_start_pos); + if (token && token->start < next_line_start_pos){ + line_end = buffer_get_line_index(app, buffer, token->start+token->size); + } + else{ + break; + } + } + if (line_end >= buffer->line_count){ + line_end = buffer->line_count; + } + else{ + line_end += 1; + } + + *line_start_out = line_start; + *line_end_out = line_end; +} + static bool32 buffer_auto_indent(Application_Links *app, Partition *part, Buffer_Summary *buffer, int32_t start, int32_t end, int32_t tab_width, Auto_Indent_Flag flags){ @@ -468,25 +518,32 @@ buffer_auto_indent(Application_Links *app, Partition *part, Buffer_Summary *buff Temp_Memory temp = begin_temp_memory(part); - // Stage 1: Setup - // Read the tokens to be used for indentation. - // Get the first and last lines to indent. + // Stage 1: Read the tokens to be used for indentation. Cpp_Token_Array tokens; tokens.count = app->buffer_token_count(app, buffer); tokens.max_count = tokens.count; tokens.tokens = push_array(part, Cpp_Token, tokens.count); app->buffer_read_tokens(app, buffer, 0, tokens.count, tokens.tokens); - int32_t line_start = buffer_get_line_index(app, buffer, start); - int32_t line_end = buffer_get_line_index(app, buffer, end) + 1; + // Stage 2: Decide where the first and last lines are. + // The lines in the range [line_start,line_end) will be indented. + int32_t do_whole_tokens = 1; - // Stage 2: Decide Indent Amounts - // Get an array representing how much each line in [line_start,line_end] - // should be indented. + int32_t line_start = 0, line_end = 0; + if (do_whole_tokens){ + get_indent_lines_whole_tokens(app, buffer, tokens, start, end, &line_start, &line_end); + } + else{ + get_indent_lines_minimum(app, buffer, start, end, &line_start, &line_end); + } + + // Stage 3: Decide Indent Amounts + // Get an array representing how much each line in + // the range [line_start,line_end) should be indented. int32_t *indent_marks = get_indentation_marks(app, part, buffer, tokens, line_start, line_end, tab_width); - // Stage 3: Set the Line Indents + // Stage 4: Set the Line Indents Indent_Options opts = {0}; opts.empty_blank_lines = (flags & AutoIndent_ClearLine); opts.use_tabs = (flags & AutoIndent_UseTab); diff --git a/4coder_default_bindings.cpp b/4coder_default_bindings.cpp index c92be080..65480ad2 100644 --- a/4coder_default_bindings.cpp +++ b/4coder_default_bindings.cpp @@ -282,7 +282,7 @@ default_keys(Bind_Helper *context){ bind(context, key_f2, MDFR_NONE, toggle_mouse); bind(context, key_page_up, MDFR_CTRL, toggle_fullscreen); - bind(context, key_f4, MDFR_ALT, exit_4coder); + bind(context, 'E', MDFR_ALT, exit_4coder); end_map(context); diff --git a/4coder_default_include.cpp b/4coder_default_include.cpp index f41463e1..7869cad3 100644 --- a/4coder_default_include.cpp +++ b/4coder_default_include.cpp @@ -571,7 +571,8 @@ buffer_get_line_index(Application_Links *app, Buffer_Summary *buffer, int32_t po } static Cpp_Token* -get_first_token_at_line(Application_Links *app, Buffer_Summary *buffer, Cpp_Token_Array tokens, int32_t line){ +get_first_token_at_line(Application_Links *app, Buffer_Summary *buffer, Cpp_Token_Array tokens, int32_t line, + int32_t *line_start_out = 0){ int32_t line_start = buffer_get_line_start(app, buffer, line); Cpp_Get_Token_Result get_token = cpp_get_token(&tokens, line_start); @@ -579,7 +580,14 @@ get_first_token_at_line(Application_Links *app, Buffer_Summary *buffer, Cpp_Toke get_token.token_index += 1; } - Cpp_Token *result = tokens.tokens + get_token.token_index; + if (line_start_out){ + *line_start_out = line_start; + } + + Cpp_Token *result = 0; + if (get_token.token_index < tokens.count){ + result = tokens.tokens + get_token.token_index; + } return(result); } diff --git a/4ed.cpp b/4ed.cpp index 9c52cb70..a4a06428 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -2505,7 +2505,7 @@ App_Step_Sig(app_step){ "-4coder now supports proper, borderless, fullscreen with the flag -F\n" " and fullscreen can be toggled with .\n" " (This sometimes causes artifacts on the Windows task bar)\n" - "- to exit\n" + "- to exit\n" "-hook on exit for the customization system\n" "-tokens now exposed in customization system\n" "-mouse release events in customization system\n" diff --git a/TODO.txt b/TODO.txt index 7708cf84..1b792d3a 100644 --- a/TODO.txt +++ b/TODO.txt @@ -90,7 +90,7 @@ ; [X] tokens in the custom API ; [X] token seeking on custom side ; [X] auto indent on the custom side -; [] indent whole comments +; [X] indent whole comments ; [] inserting lines at end of block comment ; [] clean up and comment the auto indent code to allow for customizations ; [] more built in options for auto indenting