📄 indent.c
字号:
*/ char *p1 = s_code; char *p2 = s_code + *dec_ind; while (p2 < e_code) { *p1++ = *p2++; } e_code -= *dec_ind; *e_code = '\0'; } } parser_state_tos->in_stmt = false; /* seeing a label does not * imply we are in a stmt */ for (t_ptr = s_code; *t_ptr; ++t_ptr) { *e_lab++ = *t_ptr; /* turn everything so far into a label */ } e_code = s_code; clear_buf_break_list (); /* This is bullshit for C code, because * normally a label doesn't have breakpoints * at all of course. But in the case of * wrong code, not clearing the list can make * indent core dump. */ *e_lab++ = ':'; set_buf_break (bb_label, paren_target); *e_lab++ = ' '; *e_lab = '\0'; /* parser_state_tos->pcas e will be used by dump_line to decide * how to indent the label. force_nl will force a case n: to be * on a line by itself */ *force_nl = parser_state_tos->pcase = *scase; *scase = false; parser_state_tos->want_blank = false; }}/******************************************************************************/static void handle_token_doublecolon(void){ *e_code++ = ':'; *e_code++ = ':'; parser_state_tos->want_blank = false; parser_state_tos->can_break = bb_doublecolon; parser_state_tos->last_u_d = true; parser_state_tos->saw_double_colon = true;}/******************************************************************************/static void handle_token_semicolon( BOOLEAN * scase, BOOLEAN * force_nl, BOOLEAN * sp_sw, int * dec_ind, BOOLEAN * last_token_ends_sp, exit_values_ty * file_exit_value){ parser_state_tos->in_or_st = 0; parser_state_tos->saw_double_colon = false; *scase = false; squest = 0; /* The following code doesn't seem to do much good. Just because * we've found something like extern int foo(); or int (*foo)(); * doesn't mean we are out of a declaration. Now if it was serving * some purpose we'll have to address that.... if * (parser_state_tos->last_token == rparen) * parser_state_tos->in_parameter_declaration = 0; */ parser_state_tos->cast_mask = 0; parser_state_tos->sizeof_mask = 0; parser_state_tos->block_init = 0; parser_state_tos->block_init_level = 0; parser_state_tos->just_saw_decl--; if (parser_state_tos->in_decl && (s_code == e_code) && !buf_break_used && !parser_state_tos->block_init) { while ((e_code - s_code) < (*dec_ind - 1)) { check_code_size(); set_buf_break (bb_dec_ind, paren_target); *e_code++ = ' '; } parser_state_tos->ind_stmt = false; } *e_code = '\0'; /* null terminate code sect */ /* if we were in a first level structure declaration, * we aren't any more */ parser_state_tos->in_decl = (parser_state_tos->dec_nest > 0) ? true : false; /* If we have a semicolon following an if, while, or for, and the * user wants us to, we should insert a space (to show that there * is a null statement there). */ if (*last_token_ends_sp && settings.space_sp_semicolon) { *e_code++ = ' '; } *e_code++ = ';'; *e_code = '\0'; /* null terminate code sect */ parser_state_tos->want_blank = true; /* we are no longer in the middle of a stmt */ parser_state_tos->in_stmt = (parser_state_tos->p_l_follow > 0); if (!*sp_sw) { /* if not if for (;;) */ do { if (parse (semicolon) != total_success) { *file_exit_value = indent_error; } } while(0); *force_nl = true; /* force newline after a end of stmt */ }}/******************************************************************************/static void handle_token_lbrace( BOOLEAN * force_nl, int * dec_ind, exit_values_ty * file_exit_value){ parser_state_tos->saw_double_colon = false; if (!parser_state_tos->block_init) { *force_nl = true; /* force other stuff on same line as '{' onto * new line */ parser_state_tos->in_stmt = false; /* dont indent the '{' */ } else { /* dont indent the '{' unless it is followed by more code. */ char *p = buf_ptr; for (;;) { p = skip_horiz_space(p); if (*p == EOL || (*p == '/' && p[1] == '/')) { parser_state_tos->in_stmt = false; break; } else if (*p == '/' && p[1] == '*') { p += 2; /* skip over comment */ while (*p && *p != EOL && (*p != '*' || p[1] != '/')) { ++p; } if (!*p || *p == EOL) { parser_state_tos->in_stmt = false; break; } p += 2; if (!*p) { break; } } else { break; } } if (parser_state_tos->block_init_level <= 0) { parser_state_tos->block_init_level = 1; } else { parser_state_tos->block_init_level++; } } if (s_code != e_code && parser_state_tos->block_init != 1) { if ((!parser_state_tos->in_decl && !settings.btype_2) || (parser_state_tos->in_decl && !settings.braces_on_struct_decl_line && !settings.braces_on_func_def_line)) { dump_line (true, &paren_target); parser_state_tos->want_blank = false; } else { if (parser_state_tos->in_parameter_declaration && !parser_state_tos->in_or_st) { parser_state_tos->i_l_follow = 0; if (!settings.braces_on_func_def_line) { dump_line (true, &paren_target); } else { *e_code++ = ' '; } parser_state_tos->want_blank = false; } else { parser_state_tos->want_blank = true; } } } if (parser_state_tos->in_parameter_declaration) { prefix_blankline_requested = 0; } if (s_code == e_code) { parser_state_tos->ind_stmt = false; /* dont put extra indentation on line with '{' */ } if (parser_state_tos->in_decl && parser_state_tos->in_or_st) { /* This is a structure declaration. */ if (parser_state_tos->dec_nest >= di_stack_alloc) { di_stack_alloc *= 2; di_stack = (int *) xrealloc ((char *) di_stack, di_stack_alloc * sizeof (*di_stack)); } di_stack[parser_state_tos->dec_nest++] = *dec_ind; } else { parser_state_tos->in_decl = false; parser_state_tos->decl_on_line = false; /* we cant be in the * middle of a * declaration, so dont * do special * indentation of * comments */ parser_state_tos->in_parameter_declaration = 0; } *dec_ind = 0; /* We are no longer looking for an initializer or structure. Needed * so that the '=' in "enum bar {a = 1" does not get interpreted as * the start of an initializer. */ parser_state_tos->in_or_st = 0; do { if (parse (lbrace) != total_success) { *file_exit_value = indent_error; } } while(0); set_buf_break (bb_lbrace, paren_target); if (parser_state_tos->want_blank && s_code != e_code) { /* put a blank before '{' if '{' is not at start of line */ *e_code++ = ' '; } parser_state_tos->want_blank = false; *e_code++ = '{'; *e_code = '\0'; /* null terminate code sect */ parser_state_tos->just_saw_decl = 0; if (parser_state_tos->block_init && (parser_state_tos->block_init_level >= 2)) { /* Treat the indentation of the second '{' as a '(' * in * struct foo { { bar }, ... } */ if (++parser_state_tos->p_l_follow >= parser_state_tos->paren_indents_size) { parser_state_tos->paren_indents_size *= 2; parser_state_tos->paren_indents = (short *) xrealloc ((char *) parser_state_tos-> paren_indents, parser_state_tos->paren_indents_size * sizeof (short)); } ++parser_state_tos->paren_depth; parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1] = e_code - s_code; } else if (parser_state_tos->block_init && (parser_state_tos->block_init_level == 1)) { /* Put a blank after the first '{' */ parser_state_tos->want_blank = true; }}/******************************************************************************/static void handle_token_rbrace( BOOLEAN * force_nl, int * dec_ind, exit_values_ty * file_exit_value){ /* semicolons can be omitted in declarations */ if (((parser_state_tos->p_stack[parser_state_tos->tos] == decl) && !parser_state_tos->block_init) || /* ANSI C forbids label at end of compound statement, but we don't I guess :/ */ (parser_state_tos->p_stack[parser_state_tos->tos] == casestmt)) { if (parse (semicolon) != total_success) { *file_exit_value = indent_error; } } parser_state_tos->just_saw_decl = 0; parser_state_tos->ind_stmt = false; parser_state_tos->in_stmt = false; if ((parser_state_tos->block_init_level-- == 1) && (s_code != e_code)) { /* Found closing brace of declaration initialisation, with * code on the same line before the brace */ if (parser_state_tos->matching_brace_on_same_line < 0) { /* The matching '{' is not on the same line: * put the '}' on its own line. */ dump_line (true, &paren_target); } else { /* Put a space before the '}' */ set_buf_break (bb_rbrace, paren_target); *e_code++ = ' '; } } *e_code++ = '}'; parser_state_tos->want_blank = true; if (parser_state_tos->block_init && (parser_state_tos->block_init_level > 0)) { /* We were treating this { } as normal ( ) */ --parser_state_tos->paren_depth; if (--parser_state_tos->p_l_follow < 0) { parser_state_tos->p_l_follow = 0; WARNING (_("Extra %c"), (unsigned long) *((unsigned char *) token), 0); } } else if (parser_state_tos->dec_nest > 0) { /* we are in multi-level structure * declaration */ *dec_ind = di_stack[--parser_state_tos->dec_nest]; if ((parser_state_tos->dec_nest == 0) && !parser_state_tos->in_parameter_declaration) { parser_state_tos->just_saw_decl = 2; } parser_state_tos->in_decl = true; } prefix_blankline_requested = 0; if (parse (rbrace) != total_success) { *file_exit_value = indent_error; } parser_state_tos->search_brace = (settings.cuddle_else && (parser_state_tos->p_stack[parser_state_tos->tos] == ifhead)) || (settings.cuddle_do_while && (parser_state_tos->p_stack[parser_state_tos->tos] == dohead)); if (parser_state_tos->p_stack[parser_state_tos->tos] == stmtl) { if ( (parser_state_tos->last_rw != rw_struct_like) && (parser_state_tos->last_rw != rw_enum) && (parser_state_tos->last_rw != rw_decl) ) { *force_nl = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -