From: Steve Hancock Date: Sun, 18 Dec 2022 05:31:01 +0000 (-0800) Subject: add --cuddeled-paren-brace, suggested in git #110 X-Git-Tag: 20221112.02~3 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=ff4a9dfb9b4532dd220596f3335dadbda3b933e5;p=perltidy.git add --cuddeled-paren-brace, suggested in git #110 --- diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 65eb4b74..9f64bd55 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3262,6 +3262,7 @@ sub generate_options { $add_option->( 'cuddled-block-list', 'cbl', '=s' ); $add_option->( 'cuddled-block-list-exclusive', 'cblx', '!' ); $add_option->( 'cuddled-break-option', 'cbo', '=i' ); + $add_option->( 'cuddled-paren-brace', 'cpb', '!' ); $add_option->( 'delete-old-newlines', 'dnl', '!' ); $add_option->( 'opening-brace-always-on-right', 'bar', '!' ); $add_option->( 'opening-brace-on-new-line', 'bl', '!' ); diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index f55c91f6..e34ebbfe 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -189,6 +189,7 @@ my ( $rOpts_closing_side_comment_maximum_text, $rOpts_comma_arrow_breakpoints, $rOpts_continuation_indentation, + $rOpts_cuddled_paren_brace, $rOpts_delete_closing_side_comments, $rOpts_delete_old_whitespace, $rOpts_delete_side_comments, @@ -1872,6 +1873,8 @@ EOM $rOpts->{'closing-side-comment-maximum-text'}; $rOpts_comma_arrow_breakpoints = $rOpts->{'comma-arrow-breakpoints'}; $rOpts_continuation_indentation = $rOpts->{'continuation-indentation'}; + $rOpts_cuddled_paren_brace = + $rOpts->{'cuddled-paren-brace'}; $rOpts_delete_closing_side_comments = $rOpts->{'delete-closing-side-comments'}; $rOpts_delete_old_whitespace = $rOpts->{'delete-old-whitespace'}; @@ -17941,16 +17944,33 @@ EOM DEBUG_RECOMBINE > 1 && do { print STDERR -"RECOMBINE: n=$n imid=$iend_1 if=$ibeg_1 type=$type_ibeg_1 =$tokens_to_go[$ibeg_1] next_type=$type_ibeg_2 next_tok=$tokens_to_go[$ibeg_2]\n"; +"RECOMBINE: n=$n nmax=$nmax imid=$iend_1 if=$ibeg_1 type=$type_ibeg_1 =$tokens_to_go[$ibeg_1] next_type=$type_ibeg_2 next_tok=$tokens_to_go[$ibeg_2]\n"; }; # If line $n is the last line, we set some flags and # do any special checks for it if ( $n == $nmax ) { - # a terminal '{' should stay where it is - # unless preceded by a fat comma - next if ( $type_ibeg_2 eq '{' && $type_iend_1 ne '=>' ); + if ( $type_ibeg_2 eq '{' ) { + + # join isolated ')' and '{' if requested (git #110) + if ( $rOpts_cuddled_paren_brace + && $type_iend_1 eq '}' + && $iend_1 == $ibeg_1 + && $ibeg_2 == $iend_2 ) + { + if ( $tokens_to_go[$iend_1] eq ')' + && $tokens_to_go[$ibeg_2] eq '{' ) + { + $n_best = $n; + last; + } + } + + # otherwise, a terminal '{' should stay where it is + # unless preceded by a fat comma + next if ( $type_iend_1 ne '=>' ); + } if ( $type_iend_2 eq '#' && $iend_2 - $ibeg_2 >= 2 diff --git a/t/snippets/cpb.in b/t/snippets/cpb.in new file mode 100644 index 00000000..ef9a8553 --- /dev/null +++ b/t/snippets/cpb.in @@ -0,0 +1,9 @@ +foreach my $dir ( + '05_lexer', '07_token', '08_regression', '11_util', + '13_data', '15_transform' + ) +{ + my @perl = find_files( catdir( 't', 'data', $dir ) ); + push @files, @perl; +} + diff --git a/t/snippets/cpb.par b/t/snippets/cpb.par new file mode 100644 index 00000000..5aa8bd70 --- /dev/null +++ b/t/snippets/cpb.par @@ -0,0 +1 @@ +-cpb