2017-01-23 06:19:43 +00:00
|
|
|
/*
|
|
|
|
4coder_auto_indent.cpp - Commands for auto-indentation of C++ code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
internal Batch_Edit*
|
2019-06-01 23:58:28 +00:00
|
|
|
make_batch_from_indent_marks(Application_Links *app, Arena *arena, Buffer_ID buffer,
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 first_line, i64 one_past_last_line, i64 *indent_marks,
|
2019-02-25 23:42:13 +00:00
|
|
|
Indent_Options opts){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 *shifted_indent_marks = indent_marks - first_line;
|
2016-09-09 01:02:51 +00:00
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
Batch_Edit *batch_first = 0;
|
|
|
|
Batch_Edit *batch_last = 0;
|
2016-09-09 01:02:51 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
for (i64 line_number = first_line;
|
2018-05-09 07:10:07 +00:00
|
|
|
line_number < one_past_last_line;
|
|
|
|
++line_number){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_start_pos = get_line_start_pos(app, buffer, line_number);
|
2019-06-14 22:57:22 +00:00
|
|
|
Indent_Info hard_start = get_indent_info_line_start(app, buffer, line_start_pos, opts.tab_width);
|
2016-09-09 01:02:51 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 correct_indentation = shifted_indent_marks[line_number];
|
2019-06-14 22:57:22 +00:00
|
|
|
if (hard_start.is_blank && opts.empty_blank_lines){
|
2016-09-13 01:58:32 +00:00
|
|
|
correct_indentation = 0;
|
|
|
|
}
|
|
|
|
if (correct_indentation == -1){
|
|
|
|
correct_indentation = hard_start.indent_pos;
|
|
|
|
}
|
2016-09-09 01:02:51 +00:00
|
|
|
|
2018-05-09 07:10:07 +00:00
|
|
|
if (correct_indentation != hard_start.indent_pos){
|
2019-06-01 23:58:28 +00:00
|
|
|
umem str_size = 0;
|
2019-09-02 18:59:36 +00:00
|
|
|
u8 *str = 0;
|
2018-05-09 07:10:07 +00:00
|
|
|
if (opts.use_tabs){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 tab_count = correct_indentation/opts.tab_width;
|
|
|
|
i64 indent = tab_count*opts.tab_width;
|
|
|
|
i64 space_count = correct_indentation - indent;
|
2019-06-01 23:58:28 +00:00
|
|
|
str_size = tab_count + space_count;
|
2019-09-02 18:59:36 +00:00
|
|
|
str = push_array(arena, u8, str_size);
|
2019-06-01 23:58:28 +00:00
|
|
|
block_fill_u8(str, tab_count, '\t');
|
2019-06-14 22:57:22 +00:00
|
|
|
block_fill_u8(str + tab_count, space_count, ' ');
|
2016-09-09 01:02:51 +00:00
|
|
|
}
|
|
|
|
else{
|
2019-06-01 23:58:28 +00:00
|
|
|
str_size = correct_indentation;
|
2019-09-02 18:59:36 +00:00
|
|
|
str = push_array(arena, u8, str_size);
|
2019-06-01 23:58:28 +00:00
|
|
|
block_fill_u8(str, str_size, ' ');
|
2016-09-09 01:02:51 +00:00
|
|
|
}
|
2016-09-13 01:58:32 +00:00
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
Batch_Edit *batch = push_array(arena, Batch_Edit, 1);
|
|
|
|
sll_queue_push(batch_first, batch_last, batch);
|
|
|
|
batch->edit.text = SCu8(str, str_size);
|
|
|
|
batch->edit.range = Ii64(line_start_pos, hard_start.first_char_pos);
|
2016-09-09 01:02:51 +00:00
|
|
|
}
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
return(batch_first);
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 21:05:57 +00:00
|
|
|
internal void
|
2019-06-20 23:43:27 +00:00
|
|
|
set_line_indents(Application_Links *app, Arena *arena, Buffer_ID buffer, i64 first_line, i64 one_past_last_line, i64 *indent_marks, Indent_Options opts){
|
2019-09-02 18:59:36 +00:00
|
|
|
Batch_Edit *batch = make_batch_from_indent_marks(app, arena, buffer, first_line, one_past_last_line, indent_marks, opts);
|
|
|
|
if (batch != 0){
|
|
|
|
buffer_batch_edit(app, buffer, batch);
|
2016-09-09 01:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 05:31:35 +00:00
|
|
|
internal Token*
|
|
|
|
seek_matching_token_backwards(Token_Array tokens, Token *token,
|
|
|
|
Token_Base_Kind open, Token_Base_Kind close){
|
2016-09-08 00:32:31 +00:00
|
|
|
if (token <= tokens.tokens){
|
|
|
|
token = tokens.tokens;
|
|
|
|
}
|
|
|
|
else{
|
2019-02-26 23:08:42 +00:00
|
|
|
i32 nesting_level = 0;
|
2016-09-08 00:32:31 +00:00
|
|
|
for (; token > tokens.tokens; --token){
|
2019-09-04 05:31:35 +00:00
|
|
|
if (!HasFlag(token->flags, TokenBaseFlag_PreProcessorBody)){
|
|
|
|
if (token->kind == close){
|
2016-09-08 00:32:31 +00:00
|
|
|
++nesting_level;
|
|
|
|
}
|
2019-09-04 05:31:35 +00:00
|
|
|
else if (token->kind == open){
|
2016-09-08 00:32:31 +00:00
|
|
|
if (nesting_level == 0){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
--nesting_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(token);
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:05:57 +00:00
|
|
|
internal Indent_Anchor_Position
|
2019-09-04 05:31:35 +00:00
|
|
|
find_anchor_token(Application_Links *app, Buffer_ID buffer, Token_Array tokens, i64 line_start, i32 tab_width){
|
2018-11-20 08:18:54 +00:00
|
|
|
Indent_Anchor_Position anchor = {};
|
2018-05-07 02:47:22 +00:00
|
|
|
if (tokens.count > 0){
|
2019-09-04 05:31:35 +00:00
|
|
|
Token *first_invalid_token = get_first_token_from_line(app, buffer, tokens, line_start);
|
2018-05-07 02:47:22 +00:00
|
|
|
if (first_invalid_token <= tokens.tokens){
|
2018-05-09 07:10:07 +00:00
|
|
|
anchor.token = tokens.tokens;
|
2018-05-07 02:47:22 +00:00
|
|
|
}
|
|
|
|
else{
|
2019-02-26 23:08:42 +00:00
|
|
|
i32 stack[256];
|
|
|
|
i32 top = -1;
|
2019-09-04 05:31:35 +00:00
|
|
|
Token *token_it = tokens.tokens;
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 highest_checked_line_number = -1;
|
2018-05-07 02:47:22 +00:00
|
|
|
for (; token_it < first_invalid_token; ++token_it){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_number = get_line_number_from_pos(app, buffer, token_it->start);
|
2018-05-07 02:47:22 +00:00
|
|
|
if (highest_checked_line_number < line_number){
|
|
|
|
highest_checked_line_number = line_number;
|
|
|
|
if (top == -1){
|
2018-05-09 07:10:07 +00:00
|
|
|
anchor.token = token_it;
|
2018-05-07 02:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (token_it->type){
|
|
|
|
case CPP_TOKEN_BRACE_OPEN:
|
|
|
|
case CPP_TOKEN_BRACKET_OPEN:
|
|
|
|
case CPP_TOKEN_PARENTHESE_OPEN:
|
|
|
|
{
|
|
|
|
top += 1;
|
|
|
|
stack[top] = token_it->type;
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_PARENTHESE_CLOSE:
|
|
|
|
{
|
|
|
|
for (;top >= 0;){
|
2019-02-26 23:08:42 +00:00
|
|
|
i32 index = top;
|
2018-05-07 02:47:22 +00:00
|
|
|
top -= 1;
|
|
|
|
if (stack[index] == CPP_TOKEN_PARENTHESE_OPEN){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_BRACE_CLOSE:
|
|
|
|
{
|
|
|
|
for (;top >= 0;){
|
2019-02-26 23:08:42 +00:00
|
|
|
i32 index = top;
|
2018-05-07 02:47:22 +00:00
|
|
|
if (stack[index] == CPP_TOKEN_PARENTHESE_OPEN){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
top -= 1;
|
|
|
|
if (stack[index] == CPP_TOKEN_BRACE_OPEN){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_BRACKET_CLOSE:
|
|
|
|
{
|
|
|
|
for (;top >= 0;){
|
2019-02-26 23:08:42 +00:00
|
|
|
i32 index = top;
|
2018-05-07 02:47:22 +00:00
|
|
|
if (stack[index] == CPP_TOKEN_PARENTHESE_OPEN ||
|
|
|
|
stack[index] == CPP_TOKEN_BRACE_OPEN){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
top -= 1;
|
|
|
|
if (stack[index] == CPP_TOKEN_BRACKET_OPEN){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 07:10:07 +00:00
|
|
|
return(anchor);
|
2016-09-09 01:02:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
internal i64*
|
2019-06-01 23:58:28 +00:00
|
|
|
get_indentation_marks(Application_Links *app, Arena *arena, Buffer_ID buffer,
|
2019-09-04 05:31:35 +00:00
|
|
|
Token_Array tokens, i64 first_line, i64 one_past_last_line,
|
2019-02-26 23:08:42 +00:00
|
|
|
b32 exact_align, i32 tab_width){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 indent_mark_count = one_past_last_line - first_line;
|
|
|
|
i64 *indent_marks = push_array(arena, i64, indent_mark_count);
|
2016-09-13 13:52:54 +00:00
|
|
|
// Shift the array so line_index works correctly.
|
2018-05-09 07:10:07 +00:00
|
|
|
indent_marks -= first_line;
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2016-09-09 01:02:51 +00:00
|
|
|
// Decide where to start indentation parsing.
|
2018-05-09 07:10:07 +00:00
|
|
|
Indent_Anchor_Position anchor = find_anchor_token(app, buffer, tokens, first_line, tab_width);
|
2019-09-04 05:31:35 +00:00
|
|
|
Token *token_ptr = anchor.token;
|
2018-11-20 08:18:54 +00:00
|
|
|
Indent_Parse_State indent = {};
|
2018-05-09 07:10:07 +00:00
|
|
|
indent.current_indent = anchor.indentation;
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2016-09-13 23:59:48 +00:00
|
|
|
if (token_ptr == 0){
|
2019-06-20 23:43:27 +00:00
|
|
|
for (i64 line_index = first_line; line_index < one_past_last_line; ++line_index){
|
2016-09-13 23:59:48 +00:00
|
|
|
indent_marks[line_index] = 0;
|
|
|
|
}
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
2016-09-13 23:59:48 +00:00
|
|
|
else{
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_number = get_line_number_from_pos(app, buffer, token_ptr->start);
|
2018-05-09 07:10:07 +00:00
|
|
|
if (line_number > first_line){
|
|
|
|
line_number = first_line;
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
2016-09-13 23:59:48 +00:00
|
|
|
|
2017-06-27 16:02:22 +00:00
|
|
|
if (token_ptr == tokens.tokens){
|
|
|
|
indent.current_indent = 0;
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 next_line_start_pos = get_line_start_pos(app, buffer, line_number);
|
2016-09-13 23:59:48 +00:00
|
|
|
indent.previous_line_indent = indent.current_indent;
|
2019-09-04 05:31:35 +00:00
|
|
|
Token prev_token = {};
|
|
|
|
Token token = {};
|
2017-11-28 20:49:40 +00:00
|
|
|
if (token_ptr < tokens.tokens + tokens.count){
|
|
|
|
token = *token_ptr;
|
|
|
|
}
|
2017-11-28 23:31:10 +00:00
|
|
|
|
|
|
|
// Back up and consume this token too IF it is a scope opener.
|
|
|
|
if (token.type == CPP_TOKEN_BRACE_OPEN || token.type == CPP_TOKEN_BRACKET_OPEN){
|
|
|
|
--token_ptr;
|
|
|
|
}
|
2016-09-13 23:59:48 +00:00
|
|
|
|
2017-11-29 23:00:14 +00:00
|
|
|
// LOOP OVER TOKENS
|
|
|
|
for (;;){
|
2018-05-09 07:10:07 +00:00
|
|
|
if (line_number >= one_past_last_line){
|
2017-11-29 23:00:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-27 16:02:22 +00:00
|
|
|
prev_token = token;
|
2016-09-13 23:59:48 +00:00
|
|
|
++token_ptr;
|
2019-06-14 21:07:17 +00:00
|
|
|
if (token_ptr < tokens.tokens + tokens.count){
|
2016-09-13 23:59:48 +00:00
|
|
|
token = *token_ptr;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
token.type = CPP_TOKEN_EOF;
|
2019-06-19 02:31:59 +00:00
|
|
|
token.start = (i32)buffer_get_size(app, buffer);
|
2016-09-13 23:59:48 +00:00
|
|
|
token.flags = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:10:07 +00:00
|
|
|
for (;token.start >= next_line_start_pos && line_number < one_past_last_line;){
|
2019-06-02 03:07:57 +00:00
|
|
|
next_line_start_pos = get_line_start_pos(app, buffer, line_number + 1);
|
2016-09-13 13:52:54 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 this_indent = 0;
|
|
|
|
i64 previous_indent = indent.previous_line_indent;
|
2017-11-29 23:00:14 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 this_line_start = get_line_start_pos(app, buffer, line_number);
|
|
|
|
i64 next_line_start = next_line_start_pos;
|
2017-11-29 23:00:14 +00:00
|
|
|
|
2019-02-26 23:08:42 +00:00
|
|
|
b32 did_multi_line_behavior = false;
|
2017-11-29 23:00:14 +00:00
|
|
|
|
|
|
|
// 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){
|
2019-06-14 22:57:22 +00:00
|
|
|
Indent_Info hard_start = get_indent_info_line_start(app, buffer, this_line_start, tab_width);
|
2017-11-29 23:00:14 +00:00
|
|
|
|
|
|
|
if (exact_align){
|
|
|
|
this_indent = indent.previous_comment_indent;
|
|
|
|
}
|
|
|
|
else{
|
2019-06-14 22:57:22 +00:00
|
|
|
if (hard_start.is_blank){
|
2017-11-29 23:00:14 +00:00
|
|
|
this_indent = previous_indent;
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_pos = hard_start.first_char_pos - this_line_start;
|
2017-11-29 23:00:14 +00:00
|
|
|
this_indent = line_pos + indent.comment_shift;
|
|
|
|
if (this_indent < 0){
|
|
|
|
this_indent = 0;
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 23:00:14 +00:00
|
|
|
|
2019-06-14 22:57:22 +00:00
|
|
|
if (!hard_start.is_blank){
|
2018-05-09 07:10:07 +00:00
|
|
|
if (line_number >= first_line){
|
2017-11-29 23:00:14 +00:00
|
|
|
indent.previous_comment_indent = this_indent;
|
2016-09-13 23:59:48 +00:00
|
|
|
}
|
|
|
|
else{
|
2017-11-29 23:00:14 +00:00
|
|
|
indent.previous_comment_indent = hard_start.indent_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
did_multi_line_behavior = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!did_multi_line_behavior){
|
|
|
|
this_indent = indent.current_indent;
|
|
|
|
if (token.start < next_line_start){
|
|
|
|
if (token.flags & CPP_TFLAG_PP_DIRECTIVE){
|
|
|
|
this_indent = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch (token.type){
|
2019-02-25 23:42:13 +00:00
|
|
|
case CPP_TOKEN_BRACKET_CLOSE:
|
|
|
|
{
|
|
|
|
this_indent -= tab_width;
|
|
|
|
}break;
|
|
|
|
case CPP_TOKEN_BRACE_CLOSE:
|
|
|
|
{
|
|
|
|
this_indent -= tab_width;
|
|
|
|
}break;
|
|
|
|
case CPP_TOKEN_BRACE_OPEN:
|
|
|
|
{}break;
|
2017-11-29 23:00:14 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (indent.current_indent > 0){
|
2019-02-26 23:08:42 +00:00
|
|
|
b32 statement_complete = false;
|
2016-09-13 23:59:48 +00:00
|
|
|
|
2019-09-04 05:31:35 +00:00
|
|
|
Token *prev_usable_token_ptr = token_ptr - 1;
|
|
|
|
Token prev_usable_token = {};
|
2017-11-29 23:00:14 +00:00
|
|
|
if (prev_usable_token_ptr >= tokens.tokens){
|
|
|
|
prev_usable_token = *prev_usable_token_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan backwards for the previous token that actually tells us about the statement.
|
2019-02-26 23:08:42 +00:00
|
|
|
b32 has_prev_usable_token = true;
|
2017-11-28 23:31:10 +00:00
|
|
|
#define NotUsable(T) \
|
2017-11-29 23:00:14 +00:00
|
|
|
(((T).flags&CPP_TFLAG_PP_BODY) || ((T).flags&CPP_TFLAG_PP_DIRECTIVE) || ((T).type == CPP_TOKEN_COMMENT))
|
|
|
|
if (NotUsable(prev_usable_token)){
|
|
|
|
has_prev_usable_token = false;
|
|
|
|
|
|
|
|
for (--prev_usable_token_ptr;
|
|
|
|
prev_usable_token_ptr >= tokens.tokens;
|
|
|
|
--prev_usable_token_ptr){
|
2017-11-28 23:31:10 +00:00
|
|
|
|
2017-11-29 23:00:14 +00:00
|
|
|
prev_usable_token = *prev_usable_token_ptr;
|
|
|
|
if (!NotUsable(prev_usable_token)){
|
|
|
|
has_prev_usable_token = true;
|
|
|
|
break;
|
2016-09-13 23:59:48 +00:00
|
|
|
}
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
2017-11-29 23:00:14 +00:00
|
|
|
}
|
2017-11-28 23:31:10 +00:00
|
|
|
#undef NotUsable
|
2017-11-29 23:00:14 +00:00
|
|
|
|
|
|
|
if (!has_prev_usable_token){
|
|
|
|
statement_complete = true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (prev_usable_token.type == CPP_TOKEN_BRACKET_OPEN ||
|
|
|
|
prev_usable_token.type == CPP_TOKEN_BRACE_OPEN ||
|
|
|
|
prev_usable_token.type == CPP_TOKEN_BRACE_CLOSE ||
|
|
|
|
prev_usable_token.type == CPP_TOKEN_SEMICOLON ||
|
|
|
|
prev_usable_token.type == CPP_TOKEN_COLON ||
|
|
|
|
prev_usable_token.type == CPP_TOKEN_COMMA){
|
2017-11-28 23:31:10 +00:00
|
|
|
statement_complete = true;
|
|
|
|
}
|
2017-11-29 23:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!statement_complete){
|
|
|
|
this_indent += tab_width;
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-16 23:38:22 +00:00
|
|
|
}
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
2016-09-13 23:59:48 +00:00
|
|
|
}
|
2019-02-25 23:42:13 +00:00
|
|
|
if (this_indent < 0){
|
|
|
|
this_indent = 0;
|
|
|
|
}
|
2017-11-29 23:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (indent.paren_nesting > 0){
|
|
|
|
if (prev_token.type != CPP_TOKEN_PARENTHESE_OPEN){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 level = indent.paren_nesting - 1;
|
|
|
|
level = clamp_top(level, ArrayCount(indent.paren_anchor_indent) - 1);
|
2017-11-29 23:00:14 +00:00
|
|
|
this_indent = indent.paren_anchor_indent[level];
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 23:59:48 +00:00
|
|
|
// Rebase the paren anchor if the first token
|
|
|
|
// after the open paren is on the next line.
|
2016-09-13 13:52:54 +00:00
|
|
|
if (indent.paren_nesting > 0){
|
2016-09-13 23:59:48 +00:00
|
|
|
if (prev_token.type == CPP_TOKEN_PARENTHESE_OPEN){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 level = indent.paren_nesting - 1;
|
|
|
|
level = clamp_top(level, ArrayCount(indent.paren_anchor_indent) - 1);
|
2016-09-13 23:59:48 +00:00
|
|
|
indent.paren_anchor_indent[level] = this_indent;
|
2016-09-13 13:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 23:59:48 +00:00
|
|
|
|
2018-05-09 07:10:07 +00:00
|
|
|
if (line_number >= first_line){
|
2017-11-28 20:16:53 +00:00
|
|
|
indent_marks[line_number] = this_indent;
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
2017-11-28 20:16:53 +00:00
|
|
|
++line_number;
|
2016-09-13 23:59:48 +00:00
|
|
|
|
|
|
|
indent.previous_line_indent = this_indent;
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 23:59:48 +00:00
|
|
|
// Update indent state.
|
|
|
|
switch (token.type){
|
2017-06-27 15:49:27 +00:00
|
|
|
case CPP_TOKEN_BRACKET_OPEN: indent.current_indent += tab_width; break;
|
|
|
|
case CPP_TOKEN_BRACKET_CLOSE: indent.current_indent -= tab_width; break;
|
|
|
|
case CPP_TOKEN_BRACE_OPEN: indent.current_indent += tab_width; break;
|
|
|
|
case CPP_TOKEN_BRACE_CLOSE: indent.current_indent -= tab_width; break;
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2016-09-13 23:59:48 +00:00
|
|
|
case CPP_TOKEN_COMMENT:
|
2017-12-02 22:17:06 +00:00
|
|
|
case CPP_TOKEN_STRING_CONSTANT:
|
2016-09-13 23:59:48 +00:00
|
|
|
{
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line = get_line_number_from_pos(app, buffer, token.start);
|
|
|
|
i64 start = get_line_start_pos(app, buffer, line);
|
2019-06-14 22:57:22 +00:00
|
|
|
Indent_Info hard_start = get_indent_info_line_start(app, buffer, start, tab_width);
|
2017-03-23 19:15:33 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 old_dist_to_token = (token.start - start);
|
|
|
|
i64 old_indent = hard_start.indent_pos;
|
|
|
|
i64 token_start_inset = old_dist_to_token - old_indent;
|
|
|
|
i64 new_dist_to_token = indent.current_indent + token_start_inset;
|
2017-12-02 22:17:06 +00:00
|
|
|
|
|
|
|
indent.comment_shift = (new_dist_to_token - old_dist_to_token);
|
|
|
|
indent.previous_comment_indent = old_indent;
|
2016-09-13 23:59:48 +00:00
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_PARENTHESE_OPEN:
|
2017-06-27 16:02:22 +00:00
|
|
|
{
|
|
|
|
if (!(token.flags & CPP_TFLAG_PP_BODY)){
|
|
|
|
if (indent.paren_nesting < ArrayCount(indent.paren_anchor_indent)){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line = get_line_number_from_pos(app, buffer, token.start);
|
|
|
|
i64 start = get_line_start_pos(app, buffer, line);
|
|
|
|
i64 char_pos = token.start - start;
|
2017-06-27 16:02:22 +00:00
|
|
|
|
2019-06-14 22:57:22 +00:00
|
|
|
Indent_Info hard_start = get_indent_info_line_start(app, buffer, start, tab_width);
|
2017-06-27 16:02:22 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_pos = hard_start.first_char_pos - start;
|
2017-06-27 16:02:22 +00:00
|
|
|
|
|
|
|
indent.paren_anchor_indent[indent.paren_nesting] = char_pos - line_pos + indent.previous_line_indent + 1;
|
|
|
|
}
|
|
|
|
++indent.paren_nesting;
|
2016-09-13 23:59:48 +00:00
|
|
|
}
|
2017-06-27 16:02:22 +00:00
|
|
|
}break;
|
2016-09-13 23:59:48 +00:00
|
|
|
|
|
|
|
case CPP_TOKEN_PARENTHESE_CLOSE:
|
2017-06-27 16:02:22 +00:00
|
|
|
{
|
|
|
|
if (!(token.flags & CPP_TFLAG_PP_BODY)){
|
|
|
|
if (indent.paren_nesting > 0){
|
2019-06-16 23:38:22 +00:00
|
|
|
--indent.paren_nesting;
|
2017-06-27 16:02:22 +00:00
|
|
|
}
|
2016-09-23 04:56:47 +00:00
|
|
|
}
|
2017-06-27 16:02:22 +00:00
|
|
|
}break;
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 01:02:51 +00:00
|
|
|
// Unshift the indent_marks array.
|
2018-05-09 07:10:07 +00:00
|
|
|
indent_marks += first_line;
|
2016-09-08 00:32:31 +00:00
|
|
|
return(indent_marks);
|
|
|
|
}
|
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
// TODO(allen): replace these with new range operators.
|
2019-06-09 21:05:57 +00:00
|
|
|
internal void
|
2019-06-20 23:43:27 +00:00
|
|
|
get_indent_lines_minimum(Application_Links *app, Buffer_ID buffer, i64 start_pos, i64 end_pos, i64 *line_start_out, i64 *line_end_out){
|
|
|
|
i64 line_start = get_line_number_from_pos(app, buffer, start_pos);
|
|
|
|
i64 line_end = get_line_number_from_pos(app, buffer, end_pos) + 1;
|
2016-09-10 15:22:25 +00:00
|
|
|
*line_start_out = line_start;
|
|
|
|
*line_end_out = line_end;
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:05:57 +00:00
|
|
|
internal void
|
2019-09-04 05:31:35 +00:00
|
|
|
get_indent_lines_whole_tokens(Application_Links *app, Buffer_ID buffer, Token_Array tokens, i64 start_pos, i64 end_pos, i64 *line_start_out, i64 *line_end_out){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_start = get_line_number_from_pos(app, buffer, start_pos);
|
|
|
|
i64 line_end = get_line_number_from_pos(app, buffer, end_pos);
|
2016-09-10 15:22:25 +00:00
|
|
|
|
2017-06-27 15:49:27 +00:00
|
|
|
for (;line_start > 1;){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_start_pos = get_line_start_pos(app, buffer, line_start);
|
2019-09-04 05:31:35 +00:00
|
|
|
Token *token = get_first_token_from_pos(tokens, line_start_pos);
|
2019-06-02 03:07:57 +00:00
|
|
|
if (token != 0 && token->start < line_start_pos){
|
|
|
|
line_start = get_line_number_from_pos(app, buffer, token->start);
|
2016-09-10 15:22:25 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_count = buffer_get_line_count(app, buffer);
|
2019-04-04 08:25:16 +00:00
|
|
|
|
|
|
|
for (;line_end < line_count;){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 next_line_start_pos = get_line_start_pos(app, buffer, line_end + 1);
|
2019-09-04 05:31:35 +00:00
|
|
|
Token *token = get_first_token_from_pos(tokens, next_line_start_pos);
|
2019-04-04 08:25:16 +00:00
|
|
|
if (token != 0 && token->start < next_line_start_pos){
|
2019-06-02 03:07:57 +00:00
|
|
|
line_end = get_line_number_from_pos(app, buffer, token->start + token->size);
|
2016-09-10 15:22:25 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 15:49:27 +00:00
|
|
|
|
2019-06-02 03:07:57 +00:00
|
|
|
line_end = clamp_top(line_end, line_count);
|
|
|
|
line_end += 1;
|
2016-09-10 15:22:25 +00:00
|
|
|
|
|
|
|
*line_start_out = line_start;
|
|
|
|
*line_end_out = line_end;
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:05:57 +00:00
|
|
|
internal b32
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_auto_indent(Application_Links *app, Buffer_ID buffer, i64 start, i64 end, i32 tab_width, Auto_Indent_Flag flags){
|
2019-02-26 23:08:42 +00:00
|
|
|
b32 result = false;
|
2019-09-04 05:31:35 +00:00
|
|
|
|
|
|
|
Managed_Scope scope = buffer_get_managed_scope(app, buffer);
|
|
|
|
|
|
|
|
Token_Array *tokens_ptr = scope_attachment(app, scope, attachment_tokens, Token_Array);
|
|
|
|
if (tokens_ptr != 0 && tokens_ptr->count != 0){
|
2019-06-20 23:43:27 +00:00
|
|
|
Scratch_Block scratch(app);
|
2019-09-04 05:31:35 +00:00
|
|
|
Token_Array tokens = *token_ptr;
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 line_start = 0;
|
|
|
|
i64 line_end = 0;
|
2019-06-19 02:31:59 +00:00
|
|
|
if (HasFlag(flags, AutoIndent_FullTokens)){
|
2016-09-10 15:22:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 *indent_marks = get_indentation_marks(app, scratch, buffer, tokens, line_start, line_end, (flags & AutoIndent_ExactAlignBlock), tab_width);
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2018-11-20 08:18:54 +00:00
|
|
|
Indent_Options opts = {};
|
2016-09-09 01:02:51 +00:00
|
|
|
opts.empty_blank_lines = (flags & AutoIndent_ClearLine);
|
|
|
|
opts.use_tabs = (flags & AutoIndent_UseTab);
|
|
|
|
opts.tab_width = tab_width;
|
2016-09-08 00:32:31 +00:00
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
set_line_indents(app, scratch, buffer, line_start, line_end, indent_marks, opts);
|
2019-09-04 05:31:35 +00:00
|
|
|
|
|
|
|
result = true;
|
2016-09-08 00:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
//
|
|
|
|
// Commands
|
|
|
|
//
|
|
|
|
|
2018-05-28 05:30:31 +00:00
|
|
|
#if !defined(DEFAULT_INDENT_FLAGS)
|
|
|
|
# define DEFAULT_INDENT_FLAGS ((global_config.indent_with_tabs)?(AutoIndent_UseTab):(0))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(DEF_TAB_WIDTH)
|
|
|
|
# define DEF_TAB_WIDTH global_config.indent_width
|
|
|
|
#endif
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(auto_tab_whole_file)
|
|
|
|
CUSTOM_DOC("Audo-indents the entire current buffer.")
|
|
|
|
{
|
2019-06-19 02:31:59 +00:00
|
|
|
View_ID view = get_active_view(app, AccessOpen);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, AccessOpen);
|
|
|
|
i32 buffer_size = (i32)buffer_get_size(app, buffer);
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_auto_indent(app, buffer, 0, buffer_size, DEF_TAB_WIDTH, DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(auto_tab_line_at_cursor)
|
|
|
|
CUSTOM_DOC("Auto-indents the line on which the cursor sits.")
|
|
|
|
{
|
2019-06-19 02:31:59 +00:00
|
|
|
View_ID view = get_active_view(app, AccessOpen);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, AccessOpen);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
|
|
|
buffer_auto_indent(app, buffer, pos, pos, DEF_TAB_WIDTH, DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
|
2019-04-06 21:13:49 +00:00
|
|
|
move_past_lead_whitespace(app, view, buffer);
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(auto_tab_range)
|
|
|
|
CUSTOM_DOC("Auto-indents the range between the cursor and the mark.")
|
|
|
|
{
|
2019-06-19 02:31:59 +00:00
|
|
|
View_ID view = get_active_view(app, AccessOpen);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, AccessOpen);
|
2019-06-20 23:43:27 +00:00
|
|
|
Range_i64 range = get_view_range(app, view);
|
|
|
|
buffer_auto_indent(app, buffer, range.min, range.max, DEF_TAB_WIDTH, DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
|
2019-04-06 21:13:49 +00:00
|
|
|
move_past_lead_whitespace(app, view, buffer);
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(write_and_auto_tab)
|
|
|
|
CUSTOM_DOC("Inserts a character and auto-indents the line on which the cursor sits.")
|
|
|
|
{
|
2019-04-06 21:13:49 +00:00
|
|
|
write_character(app);
|
2019-06-19 02:31:59 +00:00
|
|
|
View_ID view = get_active_view(app, AccessOpen);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, AccessOpen);
|
2019-02-26 23:08:42 +00:00
|
|
|
u32 flags = DEFAULT_INDENT_FLAGS;
|
2017-12-02 22:17:06 +00:00
|
|
|
User_Input in = get_command_input(app);
|
2018-11-20 08:18:54 +00:00
|
|
|
if (in.key.character == '\n'){
|
2017-12-02 22:17:06 +00:00
|
|
|
flags |= AutoIndent_ExactAlignBlock;
|
|
|
|
}
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
|
|
|
buffer_auto_indent(app, buffer, pos, pos, DEF_TAB_WIDTH, flags);
|
2019-04-06 21:13:49 +00:00
|
|
|
move_past_lead_whitespace(app, view, buffer);
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|