From 71147d16242e8150ea16c5680c66b6278300ae58 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Thu, 14 Jul 2016 13:18:40 -0400 Subject: [PATCH] upgrade to indent rule for handling comment spacing --- 4coder_default_include.cpp | 48 +++++++++++++++++++------------------- 4ed_file_view.cpp | 33 ++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/4coder_default_include.cpp b/4coder_default_include.cpp index 6471c609..d07e2b86 100644 --- a/4coder_default_include.cpp +++ b/4coder_default_include.cpp @@ -1295,37 +1295,37 @@ CUSTOM_COMMAND_SIG(if0_off){ CUSTOM_COMMAND_SIG(backspace_word){ unsigned int access = AccessOpen; - View_Summary view; - Buffer_Summary buffer; - int pos2, pos1; + View_Summary view = app->get_active_view(app, access); + Buffer_Summary buffer = app->get_buffer(app, view.buffer_id, access); - view = app->get_active_view(app, access); - - pos2 = view.cursor.pos; - exec_command(app, seek_alphanumeric_left); - refresh_view(app, &view); - pos1 = view.cursor.pos; - - buffer = app->get_buffer(app, view.buffer_id, access); - app->buffer_replace_range(app, &buffer, pos1, pos2, 0, 0); + if (buffer.exists){ + int pos2 = 0, pos1 = 0; + + pos2 = view.cursor.pos; + exec_command(app, seek_alphanumeric_left); + refresh_view(app, &view); + pos1 = view.cursor.pos; + + app->buffer_replace_range(app, &buffer, pos1, pos2, 0, 0); + } } CUSTOM_COMMAND_SIG(delete_word){ unsigned int access = AccessOpen; - View_Summary view; - Buffer_Summary buffer; - int pos2, pos1; + View_Summary view = app->get_active_view(app, access); + Buffer_Summary buffer = app->get_buffer(app, view.buffer_id, access); - view = app->get_active_view(app, access); - - pos1 = view.cursor.pos; - exec_command(app, seek_alphanumeric_right); - refresh_view(app, &view); - pos2 = view.cursor.pos; - - buffer = app->get_buffer(app, view.buffer_id, access); - app->buffer_replace_range(app, &buffer, pos1, pos2, 0, 0); + if (buffer.exists){ + int pos2 = 0, pos1 = 0; + + pos1 = view.cursor.pos; + exec_command(app, seek_alphanumeric_right); + refresh_view(app, &view); + pos2 = view.cursor.pos; + + app->buffer_replace_range(app, &buffer, pos1, pos2, 0, 0); + } } CUSTOM_COMMAND_SIG(snipe_token_or_word){ diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 6d426ff9..8528fac8 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -2674,6 +2674,7 @@ struct Indent_Parse_State{ i32 previous_line_indent; i32 paren_nesting; i32 paren_anchor_indent[16]; + i32 comment_shift; }; internal i32 @@ -2693,13 +2694,27 @@ compute_this_indent(Buffer *buffer, Indent_Parse_State indent, next_line_start = buffer_size(buffer); } - if ((prev_token.type == CPP_TOKEN_COMMENT || - prev_token.type == CPP_TOKEN_STRING_CONSTANT) && - prev_token.start <= this_line_start && + b32 did_special_behavior = false; + + if (prev_token.start <= this_line_start && prev_token.start + prev_token.size > this_line_start){ - this_indent = previous_indent; + if (prev_token.type == CPP_TOKEN_COMMENT){ + Hard_Start_Result hard_start = buffer_find_hard_start(buffer, this_line_start, tab_width); + i32 line_pos = hard_start.char_pos - this_line_start; + this_indent = line_pos + indent.comment_shift; + if (this_indent < 0){ + this_indent = 0; + } + did_special_behavior = true; + } + else if (prev_token.type == CPP_TOKEN_STRING_CONSTANT){ + this_indent = previous_indent; + did_special_behavior = true; + } } - else{ + + + if (!did_special_behavior){ this_indent = indent.current_indent; if (T.start < next_line_start){ if (T.flags & CPP_TFLAG_PP_DIRECTIVE){ @@ -2898,6 +2913,14 @@ get_line_indentation_marks(Partition *part, Buffer *buffer, Cpp_Token_Stack toke case CPP_TOKEN_BRACE_OPEN: indent.current_indent += 4; break; case CPP_TOKEN_BRACE_CLOSE: indent.current_indent -= 4; break; + case CPP_TOKEN_COMMENT: + { + i32 line = buffer_get_line_index(buffer, T.start); + i32 start = buffer->line_starts[line]; + + indent.comment_shift = (indent.current_indent - (T.start - start)); + }break; + case CPP_TOKEN_PARENTHESE_OPEN: if (!(T.flags & CPP_TFLAG_PP_BODY)){ if (indent.paren_nesting < ArrayCount(indent.paren_anchor_indent)){