From d05a3f6a0d1fb117c8d24c782f37d49817d9514b Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Fri, 7 Feb 2020 17:37:10 -0800 Subject: [PATCH] Show whitespace --- custom/4coder_config.cpp | 30 +++++++++++++++++----- custom/4coder_default_hooks.cpp | 23 +++++++++++++---- custom/4coder_draw.cpp | 45 +++++++++++++++++++++++++++++++++ ship_files/changes.txt | 2 ++ 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/custom/4coder_config.cpp b/custom/4coder_config.cpp index 67a47569..aef8a748 100644 --- a/custom/4coder_config.cpp +++ b/custom/4coder_config.cpp @@ -1639,15 +1639,31 @@ CUSTOM_DOC("Parse the current buffer as a theme file and add the theme to the th String_Const_u8 error_text = config_stringize_errors(app, scratch, config); print_message(app, error_text); - String_Const_u8 name = string_front_of_path(file_name); - if (string_match(string_postfix(name, 7), string_u8_litexpr(".4coder"))){ - name = string_chop(name, 7); + u64 problem_score = 0; + if (color_table.count < defcolor_line_numbers_text){ + problem_score = defcolor_line_numbers_text - color_table.count; + } + for (u32 i = 0; i < color_table.count; i += 1){ + if (color_table.arrays[i].count == 0){ + problem_score += 1; + } } - save_theme(color_table, name); - Color_Table_Node *node = global_theme_list.last; - if (node != 0 && string_match(node->name, name)){ - active_color_table = node->table; + if (error_text.size > 0 || problem_score >= 10){ + String_Const_u8 string = push_u8_stringf(scratch, "There appears to be a problem parsing %.*s; no theme change applied\n", string_expand(file_name)); + print_message(app, string); + } + else{ + String_Const_u8 name = string_front_of_path(file_name); + if (string_match(string_postfix(name, 7), string_u8_litexpr(".4coder"))){ + name = string_chop(name, 7); + } + save_theme(color_table, name); + + Color_Table_Node *node = global_theme_list.last; + if (node != 0 && string_match(node->name, name)){ + active_color_table = node->table; + } } } } diff --git a/custom/4coder_default_hooks.cpp b/custom/4coder_default_hooks.cpp index a5d9f36c..3b452788 100644 --- a/custom/4coder_default_hooks.cpp +++ b/custom/4coder_default_hooks.cpp @@ -288,6 +288,13 @@ default_render_buffer(Application_Links *app, View_ID view_id, Face_ID face_id, b32 is_active_view = (active_view == view_id); Rect_f32 prev_clip = draw_set_clip(app, rect); + Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id); + + // NOTE(allen): Cursor shape + Face_Metrics metrics = get_face_metrics(app, face_id); + f32 cursor_roundness = (metrics.normal_advance*0.5f)*0.9f; + f32 mark_thickness = 2.f; + // NOTE(allen): Token colorizing Token_Array token_array = get_token_array_from_buffer(app, buffer); if (token_array.tokens != 0){ @@ -304,7 +311,6 @@ default_render_buffer(Application_Links *app, View_ID view_id, Face_ID face_id, } } else{ - Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id); paint_text_color_fcolor(app, text_layout_id, visible_range, fcolor_id(defcolor_text_default)); } @@ -349,10 +355,17 @@ default_render_buffer(Application_Links *app, View_ID view_id, Face_ID face_id, fcolor_id(defcolor_highlight_cursor_line)); } - // NOTE(allen): Cursor shape - Face_Metrics metrics = get_face_metrics(app, face_id); - f32 cursor_roundness = (metrics.normal_advance*0.5f)*0.9f; - f32 mark_thickness = 2.f; + // NOTE(allen): Whitespace highlight + b64 show_whitespace = false; + view_get_setting(app, view_id, ViewSetting_ShowWhitespace, &show_whitespace); + if (show_whitespace){ + if (token_array.tokens == 0){ + draw_whitespace_highlight(app, buffer, text_layout_id, cursor_roundness); + } + else{ + draw_whitespace_highlight(app, text_layout_id, &token_array, cursor_roundness); + } + } // NOTE(allen): Cursor switch (fcoder_mode){ diff --git a/custom/4coder_draw.cpp b/custom/4coder_draw.cpp index 4121ae13..8923dd76 100644 --- a/custom/4coder_draw.cpp +++ b/custom/4coder_draw.cpp @@ -520,6 +520,51 @@ draw_cpp_token_colors(Application_Links *app, Text_Layout_ID text_layout_id, Tok } } +function void +draw_whitespace_highlight(Application_Links *app, Text_Layout_ID text_layout_id, Token_Array *array, f32 roundness){ + Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id); + i64 first_index = token_index_from_pos(array, visible_range.first); + Token_Iterator_Array it = token_iterator_index(0, array, first_index); + for (;;){ + Token *token = token_it_read(&it); + if (token->pos >= visible_range.one_past_last){ + break; + } + if (token->kind == TokenBaseKind_Whitespace){ + Range_i64 range = Ii64(token); + draw_character_block(app, text_layout_id, range, roundness, + fcolor_id(defcolor_highlight_white)); + } + if (!token_it_inc_all(&it)){ + break; + } + } +} + +function void +draw_whitespace_highlight(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, f32 roundness){ + Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id); + for (i64 i = visible_range.first; i < visible_range.one_past_last;){ + u8 c = buffer_get_char(app, buffer, i); + if (character_is_whitespace(c)){ + i64 s = i; + i += 1; + for (; i < visible_range.one_past_last; i += 1){ + c = buffer_get_char(app, buffer, i); + if (!character_is_whitespace(c)){ + break; + } + } + Range_i64 range = Ii64(s, i); + draw_character_block(app, text_layout_id, range, roundness, + fcolor_id(defcolor_highlight_white)); + } + else{ + i += 1; + } + } +} + function void draw_comment_highlights(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, Token_Array *array, Comment_Highlight_Pair *pairs, i32 pair_count){ diff --git a/ship_files/changes.txt b/ship_files/changes.txt index d87bc237..f982b113 100644 --- a/ship_files/changes.txt +++ b/ship_files/changes.txt @@ -4,9 +4,11 @@ + clipboard is only read when the requests to, or when the collect-all mode is started via the command 'begin_clipboard_collection_mode' + 'clear_clipboard' command + in config.4coder the variable virtual_whitespace_regular_indent determines the number of space-widths to use as the regular indentation in a virtual whitespace layout + + show whitespace mode implemented in 'default_render_buffer' + Fix: tabs are measured with the correct amount of width for the user's settings + Fix: virtual whitespace toggling works when the config initially diabled virtual whitespace + Fix: never miss the most recent post to the clipboard on windows + + Fix: command `load_theme_current_buffer` gaurds against destroying the active color palette when bad files are loaded 4.1.3 + Unkillable buffer setting