Initializer lists wern't being properly detected as block initializers and thus were being formatted with a newline forced after the opening brace.
The newly added -sar option will add spaces between the braces and the inner part of the initializer for those who prefer it. I've alwo added this to the K&R style, as this style can be clearly seen in The C Programming Language 2nd Ed. --- ChangeLog | 3 +++ doc/indent.texi | 46 ++++++++++++++++++++++++++++++++++++++++++++++ regression/TEST | 3 ++- src/args.c | 6 +++++- src/handletoken.c | 19 +++++++++++++++++-- src/indent.h | 1 + 6 files changed, 74 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3807fe2..facc512 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2015-06-15 Tim Hentenaar <t...@hentenaar.com> * Added -par to -kr. + * Fixed detection of struct initializer lists as block_init. + * Added an option (-sar/--spaces-around-initializers) to add a space + after the lbrace and before the rbrace of struct initializers. 2015-06-14 Tim Hentenaar <t...@hentenaar.com> * Added an option (-as/--align-with-spaces) which if indenting with diff --git a/doc/indent.texi b/doc/indent.texi index 68ea82d..858c632 100644 --- a/doc/indent.texi +++ b/doc/indent.texi @@ -1214,6 +1214,45 @@ int one(void) @end group @end example +@noindent +@kindex -sar +@kindex --spaces-around-initializers + +The @option{-sar} option affects how @command{indent} will render initializer +lists. Without @option{-sar} they are formatted like this: + +@example +@group +int a[] = @{1, 2, 3, 4@}; + +struct s @{ + const char *name; + int x; +@} a[] = @{ + @{"name", 0@}, + @{"a", 1@} +@}; +@end group +@end example + +@noindent +With @option{-sar} they are formatted like this, with spaces inside the +braces: + +@example +@group +int a[] = @{ 1, 2, 3, 4 @}; + +struct s @{ + const char *name; + int x; +@} a[] = @{ + @{ "name", 0 @}, + @{ "a", 1 @} +@}; +@end group +@end example + @kindex -ut @kindex --use-tabs @kindex -nut @@ -2211,6 +2250,11 @@ Put a space after each @code{for}.@* Put a space after each @code{if}.@* @xref{Statements}. +@item -sar +@itemx --spaces-around-initializers +Put a space after the @samp{@{} and before the @samp{@}} in initializers.@* +@xref{Declarations}. + @item -saw @itemx --space-after-while Put a space after each @code{while}.@* @@ -2367,6 +2411,7 @@ the corresponding short option. \line{ --space-after-procedure-calls \leaderfill -pcs\ } \line{ --space-after-while \leaderfill -saw\ } \line{ --space-special-semicolon \leaderfill -ss\ \ } +\line{ --spaces-around-initializers \leaderfill -sar\ \ } \line{ --standard-output \leaderfill -st\ \ } \line{ --start-left-side-of-comments \leaderfill -sc\ \ } \line{ --struct-brace-indentation \leaderfill -sbi@var{n}} @@ -2460,6 +2505,7 @@ the corresponding short option. --space-after-procedure-calls -pcs --space-after-while -saw --space-special-semicolon -ss +--spaces-around-initializers -sar --standard-output -st --start-left-side-of-comments -sc --struct-brace-indentation -sbi@var{n} diff --git a/regression/TEST b/regression/TEST index 53eb374..e532967 100755 --- a/regression/TEST +++ b/regression/TEST @@ -68,7 +68,7 @@ SPECIALS="typedef-bug.c bug-hp.c bug-di.c newlined-parms.c indent.pro.c \ bad-comment.c unknown-type.c unknown-type-npsl.c bug-npsl.c bug-psl.c do-cdw.c \ label.c goto-1.c goto-2.c line-count.c decl_block.c pointer-pal.c else-comment-2-br.c else-comment-2-bl.c \ else-comment-2-br-ce.c preserve-newline-after-right-brace.c gettext-strings.c single-line-conditionals.c \ - align-with-spaces.c no-space-after-fp-name.c" + align-with-spaces.c no-space-after-fp-name.c spaces-around-initializers.c" ARGS="-bad" $INDENT -npro $ARGS input/bad-break.c -o output/bad-break.c @@ -178,6 +178,7 @@ ARGS="" $INDENT -npro -slc input/single-line-conditionals.c -o output/single-line-conditionals.c $INDENT -npro -br -ut -i8 -l70 -ts8 -as input/align-with-spaces.c -o output/align-with-spaces.c $INDENT -npro -npcs input/fp.c -o output/no-space-after-fp-name.c +$INDENT -npro -sar input/spaces-around-initializers.c -o output/spaces-around-initializers.c ARGS="-kr -cp0 -l132 -lps -br -psl" $INDENT -npro $ARGS input/const.c -o output/const.c diff --git a/src/args.c b/src/args.c index baadefc..853c535 100644 --- a/src/args.c +++ b/src/args.c @@ -83,7 +83,7 @@ RCSTAG_CC ("$Id$"); "-nbad\0-bap\0-nbc\0-bbo\0-hnl\0-br\0-brs\0-c33\0-cd33\0" \ "-ncdb\0-ce\0-ci4\0-cli0\0-d0\0-di1\0-nfc1\0-i4\0-ip0\0-l75\0-lp\0" \ "-npcs\0-nprs\0-npsl\0-sai\0-saf\0-saw\0-cs\0-nsc\0-nsob\0-nfca\0-cp33\0"\ - "-nss\0-par\0" + "-nss\0-par\0-sar\0" #define GNU_SETTINGS_STRING (int *) \ "-nbad\0-bap\0-bbo\0-hnl\0-nbc\0-bl\0-bls\0-ncdb\0-cs\0-nce\0" \ @@ -201,6 +201,7 @@ static int exp_version = 0; static int exp_par = 0; static int exp_slc = 0; static int exp_as = 0; +static int exp_sar = 0; /** * The following structure is controlled by command line parameters and @@ -367,6 +368,7 @@ const pro_ty pro[] = {"par", PRO_BOOL, true, ON, &settings.pointer_align_right, &exp_par}, {"slc", PRO_BOOL, false, ON, &settings.allow_single_line_conditionals, &exp_slc}, {"as", PRO_BOOL, false, ON, &settings.align_with_spaces, &exp_as}, + {"sar", PRO_BOOL, false, ON, &settings.spaces_around_initializers, &exp_sar}, /* Signify end of structure. */ {0, PRO_IGN, 0, ONOFF_NA, 0, 0} @@ -494,6 +496,7 @@ const pro_ty pro[] = {"par", PRO_BOOL, true, ON, &settings.pointer_align_right, &exp_par}, {"slc", PRO_BOOL, false, ON, &settings.allow_single_line_conditionals, &exp_slc}, {"as", PRO_BOOL, false, ON, &settings.align_with_spaces, &exp_as}, + {"sar", PRO_BOOL, false, ON, &settings.spaces_around_initializers, &exp_sar}, /* Signify end of structure. */ {0, PRO_IGN, 0, ONOFF_NA, 0, 0} @@ -631,6 +634,7 @@ const long_option_conversion_ty option_conversions[] = {"pointer-align-left", "pal"}, {"single-line-conditionals", "slc"}, {"align-with-spaces", "as"}, + {"spaces-around-initializers", "sar"}, /* Signify end of structure. */ {0, 0}, diff --git a/src/handletoken.c b/src/handletoken.c index 5250cc0..6effe59 100644 --- a/src/handletoken.c +++ b/src/handletoken.c @@ -1023,6 +1023,12 @@ static void handle_token_lbrace( { parser_state_tos->saw_double_colon = false; + /* If the last token was a binary_op (probably =) then we're + * likely starting an initializer or initializer list. + */ + if (parser_state_tos->last_token == binary_op) + parser_state_tos->block_init = 1; + if (!parser_state_tos->block_init) { *force_nl = true; /* force other stuff on same line as '{' onto @@ -1132,7 +1138,6 @@ static void handle_token_lbrace( 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; @@ -1207,6 +1212,8 @@ static void handle_token_lbrace( ++parser_state_tos->paren_depth; parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1] = e_code - s_code; + if (settings.spaces_around_initializers) + parser_state_tos->want_blank = true; } else if (parser_state_tos->block_init && (parser_state_tos->block_init_level == 1)) @@ -1246,8 +1253,9 @@ static void handle_token_rbrace( parser_state_tos->just_saw_decl = 0; parser_state_tos->ind_stmt = false; parser_state_tos->in_stmt = false; + parser_state_tos->block_init_level--; - if ((parser_state_tos->block_init_level-- == 1) + if ((parser_state_tos->block_init_level == 0) && (s_code != e_code)) { /* Found closing brace of declaration initialisation, with @@ -1267,6 +1275,13 @@ static void handle_token_rbrace( *(e_code++) = ' '; } } + else if (parser_state_tos->block_init_level == 1 + && settings.spaces_around_initializers) + { + /* Put a space before the '}' */ + set_buf_break (bb_rbrace, paren_target); + *(e_code++) = ' '; + } *(e_code++) = '}'; parser_state_tos->want_blank = true; diff --git a/src/indent.h b/src/indent.h index 12c4e04..82c5e49 100644 --- a/src/indent.h +++ b/src/indent.h @@ -329,6 +329,7 @@ typedef struct user_options_st int gettext_strings; /*!< true: _("...") is a string, false: it's a function */ int allow_single_line_conditionals; /*!< Don't indent the body of an unbraced if, else, etc. */ int align_with_spaces; /*!< Align with spaces if indenting with tabs. */ + int spaces_around_initializers; /*!< Place spaces after { and before } in initializers. */ } user_options_ty; extern user_options_ty settings; -- 2.3.6 _______________________________________________ bug-indent mailing list bug-indent@gnu.org https://lists.gnu.org/mailman/listinfo/bug-indent