token indent new flags and end of block handler

This commit is contained in:
Allen Webster 2016-09-12 21:58:32 -04:00
parent 8bd7c4611a
commit 894a06d7ef
7 changed files with 132 additions and 65 deletions

File diff suppressed because one or more lines are too long

View File

@ -91,25 +91,36 @@ make_batch_from_indent_marks(Application_Links *app, Partition *part, Buffer_Sum
buffer_find_hard_start(app, buffer, line_start, opts.tab_width);
int32_t correct_indentation = indent_marks[line_i];
if (hard_start.all_whitespace && opts.empty_blank_lines) correct_indentation = 0;
if (correct_indentation == -1) correct_indentation = hard_start.indent_pos;
if (hard_start.all_whitespace && opts.empty_blank_lines){
correct_indentation = 0;
}
if (correct_indentation == -1){
correct_indentation = hard_start.indent_pos;
}
// TODO(allen): Only replace spaces if we are using space based indentation.
// TODO(allen): See if the first clause can just be removed because it's dumb.
if (!hard_start.all_space || correct_indentation != hard_start.indent_pos){
Buffer_Edit new_edit;
new_edit.str_start = str_size;
str_size += correct_indentation;
char *str = push_array(part, char, correct_indentation);
int32_t j = 0;
if (opts.use_tabs){
int32_t i = 0;
for (; i + opts.tab_width <= correct_indentation; i += opts.tab_width) str[j++] = '\t';
for (; i < correct_indentation; ++i) str[j++] = ' ';
for (; i + opts.tab_width <= correct_indentation; i += opts.tab_width){
str[j++] = '\t';
}
for (; i < correct_indentation; ++i){
str[j++] = ' ';
}
}
else{
for (; j < correct_indentation; ++j) str[j] = ' ';
for (; j < correct_indentation; ++j){
str[j] = ' ';
}
}
new_edit.len = j;
new_edit.start = line_start;
new_edit.end = hard_start.char_pos;
@ -258,13 +269,15 @@ struct Indent_Parse_State{
int32_t paren_nesting;
int32_t paren_anchor_indent[16];
int32_t comment_shift;
int32_t previous_comment_indent;
};
static int32_t
compute_this_indent(Application_Links *app, Buffer_Summary *buffer, Indent_Parse_State indent,
Cpp_Token T, Cpp_Token prev_token, int32_t line_i, int32_t tab_width){
compute_this_indent(Application_Links *app, Buffer_Summary *buffer, Indent_Parse_State *indent,
Cpp_Token T, Cpp_Token prev_token, int32_t line_i, bool32 this_line_gets_indented,
bool32 exact_align, int32_t tab_width){
int32_t previous_indent = indent.previous_line_indent;
int32_t previous_indent = indent->previous_line_indent;
int32_t this_indent = 0;
int32_t this_line_start = buffer_get_line_start(app, buffer, line_i);
@ -277,28 +290,41 @@ compute_this_indent(Application_Links *app, Buffer_Summary *buffer, Indent_Parse
if (prev_token.type == CPP_TOKEN_COMMENT){
Hard_Start_Result hard_start = buffer_find_hard_start(app, buffer, this_line_start, tab_width);
if (exact_align){
this_indent = indent->previous_comment_indent;
}
else{
if (hard_start.all_whitespace){
this_indent = previous_indent;
did_special_behavior = true;
}
else{
int32_t line_pos = hard_start.char_pos - this_line_start;
this_indent = line_pos + indent.comment_shift;
this_indent = line_pos + indent->comment_shift;
if (this_indent < 0){
this_indent = 0;
}
did_special_behavior = true;
}
}
if (!hard_start.all_whitespace){
if (this_line_gets_indented){
indent->previous_comment_indent = this_indent;
}
else{
indent->previous_comment_indent = hard_start.indent_pos;
}
}
did_special_behavior = true;
}
else if (prev_token.type == CPP_TOKEN_STRING_CONSTANT){
this_indent = previous_indent;
did_special_behavior = true;
}
}
if (!did_special_behavior){
this_indent = indent.current_indent;
this_indent = indent->current_indent;
if (T.start < next_line_start){
if (T.flags & CPP_TFLAG_PP_DIRECTIVE){
this_indent = 0;
@ -310,9 +336,9 @@ compute_this_indent(Application_Links *app, Buffer_Summary *buffer, Indent_Parse
case CPP_TOKEN_BRACE_OPEN: break;
default:
if (indent.current_indent > 0){
if (!(prev_token.flags & CPP_TFLAG_PP_BODY ||
prev_token.flags & CPP_TFLAG_PP_DIRECTIVE)){
if (indent->current_indent > 0){
if (!(prev_token.flags & CPP_TFLAG_PP_BODY) &&
!(prev_token.flags & CPP_TFLAG_PP_DIRECTIVE)){
switch (prev_token.type){
case CPP_TOKEN_BRACKET_OPEN:
case CPP_TOKEN_BRACE_OPEN: case CPP_TOKEN_BRACE_CLOSE:
@ -328,21 +354,23 @@ compute_this_indent(Application_Links *app, Buffer_Summary *buffer, Indent_Parse
if (this_indent < 0) this_indent = 0;
}
if (indent.paren_nesting > 0){
if (indent->paren_nesting > 0){
if (prev_token.type != CPP_TOKEN_PARENTHESE_OPEN){
int32_t level = indent.paren_nesting-1;
if (level >= ArrayCount(indent.paren_anchor_indent)){
level = ArrayCount(indent.paren_anchor_indent)-1;
int32_t level = indent->paren_nesting-1;
if (level >= ArrayCount(indent->paren_anchor_indent)){
level = ArrayCount(indent->paren_anchor_indent)-1;
}
this_indent = indent.paren_anchor_indent[level];
this_indent = indent->paren_anchor_indent[level];
}
}
return(this_indent);
}
static int32_t*
get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *buffer,
Cpp_Token_Array tokens, int32_t line_start, int32_t line_end, int32_t tab_width){
Cpp_Token_Array tokens, int32_t line_start, int32_t line_end,
bool32 exact_align, int32_t tab_width){
int32_t indent_mark_count = line_end - line_start;
int32_t *indent_marks = push_array(part, int32_t, indent_mark_count);
@ -372,8 +400,7 @@ get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *b
indent.previous_line_indent = indent.current_indent;
for (;line_index < line_end;){
Cpp_Token prev_token = *token_ptr;
Cpp_Token token;
Cpp_Token prev_token = *token_ptr, token = {0};
++token_ptr;
if (token_ptr < tokens.tokens + tokens.count){
@ -388,8 +415,12 @@ get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *b
for (;token.start >= next_line_start_pos && line_index < line_end;){
next_line_start_pos = buffer_get_line_start(app, buffer, line_index+1);
// TODO(allen): Flatten this function back in, as it is no longer
// leaving the indent state unchanged and it's only called here.
int32_t this_indent =
compute_this_indent(app, buffer, indent, token, prev_token, line_index, tab_width);
compute_this_indent(app, buffer, &indent, token, prev_token,
line_index, line_index >= line_start,
exact_align, tab_width);
// NOTE(allen): Rebase the paren anchor if the first token
// after an open paren is on the next line.
@ -424,6 +455,7 @@ get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *b
int32_t start = buffer_get_line_start(app, buffer, line);
indent.comment_shift = (indent.current_indent - (token.start - start));
indent.previous_comment_indent = (token.start - start);
}break;
case CPP_TOKEN_PARENTHESE_OPEN:
@ -527,10 +559,8 @@ buffer_auto_indent(Application_Links *app, Partition *part, Buffer_Summary *buff
// 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;
int32_t line_start = 0, line_end = 0;
if (do_whole_tokens){
if (flags & AutoIndent_FullTokens){
get_indent_lines_whole_tokens(app, buffer, tokens, start, end, &line_start, &line_end);
}
else{
@ -541,7 +571,8 @@ buffer_auto_indent(Application_Links *app, Partition *part, Buffer_Summary *buff
// 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);
get_indentation_marks(app, part, buffer, tokens, line_start, line_end,
(flags & AutoIndent_ExactAlignBlock), tab_width);
// Stage 4: Set the Line Indents
Indent_Options opts = {0};

View File

@ -960,7 +960,7 @@ CUSTOM_COMMAND_SIG(auto_tab_line_at_cursor){
buffer_auto_indent(app, &buffer,
view.cursor.pos, view.cursor.pos,
DEF_TAB_WIDTH,
DEFAULT_INDENT_FLAGS);
DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
move_past_lead_whitespace(app, &view, &buffer);
}
@ -972,7 +972,7 @@ CUSTOM_COMMAND_SIG(auto_tab_whole_file){
buffer_auto_indent(app, &buffer,
0, buffer.size,
DEF_TAB_WIDTH,
DEFAULT_INDENT_FLAGS);
DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
}
CUSTOM_COMMAND_SIG(auto_tab_range){
@ -984,13 +984,22 @@ CUSTOM_COMMAND_SIG(auto_tab_range){
buffer_auto_indent(app, &buffer,
range.min, range.max,
DEF_TAB_WIDTH,
DEFAULT_INDENT_FLAGS);
DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
move_past_lead_whitespace(app, &view, &buffer);
}
CUSTOM_COMMAND_SIG(write_and_auto_tab){
exec_command(app, write_character);
exec_command(app, auto_tab_line_at_cursor);
uint32_t access = AccessOpen;
View_Summary view = app->get_active_view(app, access);
Buffer_Summary buffer = app->get_buffer(app, view.buffer_id, access);
buffer_auto_indent(app, &buffer,
view.cursor.pos, view.cursor.pos,
DEF_TAB_WIDTH,
DEFAULT_INDENT_FLAGS | AutoIndent_ExactAlignBlock);
move_past_lead_whitespace(app, &view, &buffer);
}
CUSTOM_COMMAND_SIG(clean_all_lines){
@ -1847,7 +1856,7 @@ long_braces(Application_Links *app, char *text, int32_t size){
buffer_auto_indent(app, &buffer,
pos, pos + size,
DEF_TAB_WIDTH,
DEFAULT_INDENT_FLAGS);
DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
move_past_lead_whitespace(app, &view, &buffer);
}
@ -1921,7 +1930,7 @@ CUSTOM_COMMAND_SIG(if0_off){
buffer_auto_indent(app, &buffer,
range.min, range.max,
DEF_TAB_WIDTH,
DEFAULT_INDENT_FLAGS);
DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
move_past_lead_whitespace(app, &view, &buffer);
}
}

View File

@ -268,7 +268,13 @@ ENUM(uint32_t, Auto_Indent_Flag){
/* DOC(If AutoIndent_UseTab is set, then when putting in leading whitespace to align
code, as many tabs will be used as possible until the fine grained control of spaces
is needed to finish the alignment.) */
AutoIndent_UseTab = 0x2
AutoIndent_UseTab = 0x2,
/* DOC(If AutoIndent_ExactAlignBlock is set, then block comments are indented by putting
the first non-whitespace character of the line in line with the beginning of the comment.) */
AutoIndent_ExactAlignBlock = 0x4,
/* DOC(If AutoIndent_FullTokens is set, then the set of lines that are indented is
automatically expanded so that any token spanning multiple lines gets entirely indented.) */
AutoIndent_FullTokens = 0x8,
};
/* DOC(A Set_Buffer_Flag field specifies the behavior of an operation that sets the buffer of a view.) */

39
4ed.cpp
View File

@ -1072,6 +1072,45 @@ app_hardcode_styles(Models *models){
style->main.file_info_style = file_info_style;
++style;
/////////////////
style_set_name(style, make_lit_string("Strange"));
style->main.back_color = 0xFF161616;
style->main.margin_color = 0xFF606590;
style->main.margin_hover_color = 0xFF606590;
style->main.margin_active_color = 0xFF9a99e7;
style->main.cursor_color = 0xFFd96e26;
style->main.at_cursor_color = style->main.back_color;
style->main.mark_color = 0xFF808080;
style->main.highlight_color = 0xFF703419;
style->main.at_highlight_color = 0xFFCDAA7D;
style->main.default_color = 0xFFFFFFFF;
style->main.comment_color = 0xFF505f89;
style->main.keyword_color = 0xFFaa8da7;
style->main.str_constant_color = 0xFF9a99e7;
style->main.char_constant_color = style->main.str_constant_color;
style->main.int_constant_color = style->main.str_constant_color;
style->main.float_constant_color = style->main.str_constant_color;
style->main.bool_constant_color = style->main.str_constant_color;
style->main.include_color = 0xFF9a99e7;
style->main.preproc_color = 0xFF606590;
style->main.special_character_color = 0xFFFF0000;
style->main.paste_color = 0xFFFFBB00;
style->main.undo_color = 0xFF80005D;
style->main.highlight_junk_color = 0xFF3A0000;
style->main.highlight_white_color = 0xFF003A3A;
file_info_style.bar_color = 0xFF9a99e7;
file_info_style.bar_active_color = 0xFF9a99e7;
file_info_style.base_color = 0xFF000000;
file_info_style.pop1_color = 0xFF03CF0C;
file_info_style.pop2_color = 0xFFFF0000;
style->main.file_info_style = file_info_style;
++style;
/////////////////
models->styles.count = (i32)(style - styles);
models->styles.max = ArrayCount(models->styles.styles);
style_copy(main_style(models), models->styles.styles + 1);

View File

@ -87,11 +87,12 @@
; BEFORE I SHIP
;
; [X] use strange theme
; [X] tokens in the custom API
; [X] token seeking on custom side
; [X] auto indent on the custom side
; [X] indent whole comments
; [] inserting lines at end of block comment
; [X] 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
; [] occasionally missing the (!) mark on files on windows

View File

@ -1,21 +0,0 @@
//
Theme_Color ThemeColors[] = {
{Stag_Bar, 0x9a99e7},
{Stag_Bar_Active, 0x9a99e7},
{Stag_Margin, 0x606590},
{Stag_Margin_Hover, 0x9a99e7},
{Stag_Margin_Active, 0x9a99e7},
{Stag_Comment, 0x505f89},
{Stag_Keyword, 0xaa8da7},
{Stag_Default, 0xffffff},
{Stag_Cursor, 0xd96e26},
{Stag_Int_Constant, 0x9a99e7},
{Stag_Float_Constant, 0x9a99e7},
{Stag_Bool_Constant, 0x9a99e7},
{Stag_Str_Constant, 0x9a99e7},
{Stag_Char_Constant, 0x9a99e7},
{Stag_Preproc, 0x606590},
{Stag_Include, 0x9a99e7},
};