From 4b52089aa605bf918bea26bc4ad2a2eb4624046e Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 20 Dec 2022 09:17:24 -0800 Subject: [PATCH] add option -bfvt=n, see issue git #110 --- dev-bin/perltidy_random_setup.pl | 2 + lib/Perl/Tidy.pm | 3 + lib/Perl/Tidy/Formatter.pm | 28 ++++- t/snippets/bfvt.in | 34 +++++ t/snippets/bfvt0.par | 1 + t/snippets/bfvt2.par | 1 + t/snippets/expect/bfvt.bfvt0 | 34 +++++ t/snippets/expect/bfvt.bfvt2 | 31 +++++ t/snippets/expect/bfvt.def | 33 +++++ t/snippets/expect/cpb.cpb | 8 ++ t/snippets/expect/cpb.def | 9 ++ t/snippets/packing_list.txt | 5 + t/snippets27.t | 209 ++++++++++++++++++++++++++++++- 13 files changed, 391 insertions(+), 7 deletions(-) create mode 100644 t/snippets/bfvt.in create mode 100644 t/snippets/bfvt0.par create mode 100644 t/snippets/bfvt2.par create mode 100644 t/snippets/expect/bfvt.bfvt0 create mode 100644 t/snippets/expect/bfvt.bfvt2 create mode 100644 t/snippets/expect/bfvt.def create mode 100644 t/snippets/expect/cpb.cpb create mode 100644 t/snippets/expect/cpb.def diff --git a/dev-bin/perltidy_random_setup.pl b/dev-bin/perltidy_random_setup.pl index b82c6ff8..9b46493a 100755 --- a/dev-bin/perltidy_random_setup.pl +++ b/dev-bin/perltidy_random_setup.pl @@ -1129,6 +1129,7 @@ EOM 'paren-tightness' => [ 0, 2 ], 'square-bracket-tightness' => [ 0, 2 ], + 'brace-follower-vertical-tightness' => [ 0, 2 ], 'block-brace-vertical-tightness' => [ 0, 2 ], 'brace-vertical-tightness' => [ 0, 2 ], 'brace-vertical-tightness-closing' => [ 0, 2 ], @@ -1285,6 +1286,7 @@ EOM tee-pod tee-side-comments version + warning-output delete-pod tabs entab-leading-whitespace diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 9f64bd55..e509c8a4 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3256,6 +3256,7 @@ sub generate_options { $add_option->( 'add-newlines', 'anl', '!' ); $add_option->( 'block-brace-vertical-tightness', 'bbvt', '=i' ); $add_option->( 'block-brace-vertical-tightness-list', 'bbvtl', '=s' ); + $add_option->( 'brace-follower-vertical-tightness', 'bfvt', '=i' ); $add_option->( 'brace-vertical-tightness', 'bvt', '=i' ); $add_option->( 'brace-vertical-tightness-closing', 'bvtc', '=i' ); $add_option->( 'cuddled-else', 'ce', '!' ); @@ -3446,6 +3447,7 @@ sub generate_options { 'square-bracket-tightness' => [ 0, 2 ], 'block-brace-vertical-tightness' => [ 0, 2 ], + 'brace-follower-vertical-tightness' => [ 0, 2 ], 'brace-vertical-tightness' => [ 0, 2 ], 'brace-vertical-tightness-closing' => [ 0, 2 ], 'paren-vertical-tightness' => [ 0, 2 ], @@ -3497,6 +3499,7 @@ sub generate_options { block-brace-tightness=0 block-brace-vertical-tightness=0 + brace-follower-vertical-tightness=1 brace-tightness=1 brace-vertical-tightness-closing=0 brace-vertical-tightness=0 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index e34ebbfe..c5a13403 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -176,6 +176,7 @@ my ( $rOpts_blank_lines_after_opening_block, $rOpts_block_brace_tightness, $rOpts_block_brace_vertical_tightness, + $rOpts_brace_follower_vertical_tightness, $rOpts_break_after_labels, $rOpts_break_at_old_attribute_breakpoints, $rOpts_break_at_old_comma_breakpoints, @@ -1852,6 +1853,8 @@ EOM $rOpts_block_brace_tightness = $rOpts->{'block-brace-tightness'}; $rOpts_block_brace_vertical_tightness = $rOpts->{'block-brace-vertical-tightness'}; + $rOpts_brace_follower_vertical_tightness = + $rOpts->{'brace-follower-vertical-tightness'}; $rOpts_break_after_labels = $rOpts->{'break-after-labels'}; $rOpts_break_at_old_attribute_breakpoints = $rOpts->{'break-at-old-attribute-breakpoints'}; @@ -1873,8 +1876,7 @@ 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_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'}; @@ -18461,10 +18463,24 @@ EOM # the indentation of a leading line like 'or do {'. # This doesn't work well with -icb through if ( - $block_type_to_go[$iend_1] eq 'eval' - && !ref( $leading_spaces_to_go[$iend_1] ) - && !$rOpts_indent_closing_brace - && $tokens_to_go[$iend_2] eq '{' + $block_type_to_go[$iend_1] + && $rOpts_brace_follower_vertical_tightness > 0 + && ( + + # -bfvt=1, allow cuddled eval chains [default] + ( + $tokens_to_go[$iend_2] eq '{' + && $block_type_to_go[$iend_1] eq 'eval' + && !ref( $leading_spaces_to_go[$iend_1] ) + && !$rOpts_indent_closing_brace + ) + + # -bfvt=2, allow most brace followers [part of git #110] + || ( $rOpts_brace_follower_vertical_tightness > 1 + && $ibeg_1 == $iend_1 ) + + ) + && ( ( $type_ibeg_2 =~ /^(\&\&|\|\|)$/ ) || ( $type_ibeg_2 eq 'k' diff --git a/t/snippets/bfvt.in b/t/snippets/bfvt.in new file mode 100644 index 00000000..ee5e262e --- /dev/null +++ b/t/snippets/bfvt.in @@ -0,0 +1,34 @@ +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} + or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; + }; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} + or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); +} + || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); +} || ""; diff --git a/t/snippets/bfvt0.par b/t/snippets/bfvt0.par new file mode 100644 index 00000000..55472b35 --- /dev/null +++ b/t/snippets/bfvt0.par @@ -0,0 +1 @@ +-bfvt=0 diff --git a/t/snippets/bfvt2.par b/t/snippets/bfvt2.par new file mode 100644 index 00000000..947852a2 --- /dev/null +++ b/t/snippets/bfvt2.par @@ -0,0 +1 @@ +-bfvt=2 diff --git a/t/snippets/expect/bfvt.bfvt0 b/t/snippets/expect/bfvt.bfvt0 new file mode 100644 index 00000000..91b5b32f --- /dev/null +++ b/t/snippets/expect/bfvt.bfvt0 @@ -0,0 +1,34 @@ +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} + or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; + }; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} + or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } + || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; diff --git a/t/snippets/expect/bfvt.bfvt2 b/t/snippets/expect/bfvt.bfvt2 new file mode 100644 index 00000000..1b556944 --- /dev/null +++ b/t/snippets/expect/bfvt.bfvt2 @@ -0,0 +1,31 @@ +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; +}; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; diff --git a/t/snippets/expect/bfvt.def b/t/snippets/expect/bfvt.def new file mode 100644 index 00000000..9c9cea6a --- /dev/null +++ b/t/snippets/expect/bfvt.def @@ -0,0 +1,33 @@ +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; +}; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} + or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } + || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; diff --git a/t/snippets/expect/cpb.cpb b/t/snippets/expect/cpb.cpb new file mode 100644 index 00000000..648c5f1f --- /dev/null +++ b/t/snippets/expect/cpb.cpb @@ -0,0 +1,8 @@ +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/expect/cpb.def b/t/snippets/expect/cpb.def new file mode 100644 index 00000000..ef9a8553 --- /dev/null +++ b/t/snippets/expect/cpb.def @@ -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/packing_list.txt b/t/snippets/packing_list.txt index 6ca99208..6307b9a1 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -516,3 +516,8 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def +../snippets27.t bfvt.bfvt0 +../snippets27.t bfvt.bfvt2 +../snippets27.t bfvt.def +../snippets27.t cpb.cpb +../snippets27.t cpb.def diff --git a/t/snippets27.t b/t/snippets27.t index beb1ef51..01ed0e43 100644 --- a/t/snippets27.t +++ b/t/snippets27.t @@ -12,6 +12,11 @@ #9 wtc.wtc7 #10 rt144979.def #11 rt144979.rt144979 +#12 bfvt.bfvt0 +#13 bfvt.bfvt2 +#14 bfvt.def +#15 cpb.cpb +#16 cpb.def # To locate test #13 you can search for its name or the string '#13' @@ -29,6 +34,9 @@ BEGIN { # BEGIN SECTION 1: Parameter combinations # ########################################### $rparams = { + 'bfvt0' => "-bfvt=0", + 'bfvt2' => "-bfvt=2", + 'cpb' => "-cpb", 'def' => "", 'dwic' => "-wn -dwic", 'rt144979' => "-xci -ce -lp", @@ -46,6 +54,55 @@ BEGIN { ############################ $rsources = { + 'bfvt' => <<'----------', +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} + or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; + }; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} + or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); +} + || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); +} || ""; +---------- + + 'cpb' => <<'----------', +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; +} + +---------- + 'dwic' => <<'----------', skip_symbols( [ qw( @@ -69,7 +126,7 @@ GetOptions( } return; }, -); +); # part 2 {{{ @@ -645,6 +702,156 @@ GetOptions( #11........... }, + + 'bfvt.bfvt0' => { + source => "bfvt", + params => "bfvt0", + expect => <<'#12...........', +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} + or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; + }; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} + or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } + || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; +#12........... + }, + + 'bfvt.bfvt2' => { + source => "bfvt", + params => "bfvt2", + expect => <<'#13...........', +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; +}; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; +#13........... + }, + + 'bfvt.def' => { + source => "bfvt", + params => "def", + expect => <<'#14...........', +# combines with -bfvt>0 +eval { + require XSLoader; + XSLoader::load( 'Sys::Syslog', $VERSION ); + 1; +} or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Sys::Syslog $VERSION; +}; + +# combines with -bfvt=2 +eval { + ( $line, $cond ) = $self->_normalize_if_elif($line); + 1; +} + or die sprintf "Error at line %d\nLine %d: %s\n%s", + ( $line_info->start_line_num() ) x 2, $line, $@; + +# stable for bfvt<2; combines for bfvt=2; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } + || ""; + +# stays combined for all bfvt; has ci +my $domain = shift + || eval { + require Net::Domain; + Net::Domain::hostfqdn(); + } || ""; +#14........... + }, + + 'cpb.cpb' => { + source => "cpb", + params => "cpb", + expect => <<'#15...........', +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; +} + +#15........... + }, + + 'cpb.def' => { + source => "cpb", + params => "def", + expect => <<'#16...........', +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; +} + +#16........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5