From ffef089865a8e7b8c69bc9eda9b23035085d9751 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 5 Apr 2021 07:36:52 -0700 Subject: [PATCH] Improve previous patch of -wn tolerance --- bin/perltidy | 2 ++ lib/Perl/Tidy/Formatter.pm | 48 ++++++++++++++++++++++++-------------- local-docs/BugLog.pod | 7 ++++++ t/snippets/expect/wn6.wn | 6 ++--- t/snippets12.t | 6 ++--- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index 990f46d8..ffa7c527 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2654,10 +2654,12 @@ Here are some additional example strings and their meanings: '^(' - the weld must not start with a paren '.(' - the second and later tokens may not be parens + '.w(' - the second and later tokens may not keyword or function call parens '(' - no parens in a weld '^K(' - exclude a leading paren preceded by a non-keyword '.k(' - exclude a secondary paren preceded by a keyword '[ {' - exclude all brackets and braces + '[ ( ^K{' - exclude everthing except nested structures like do {{ ... }} =item B of non-block curly braces, parentheses, and square brackets. diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 1f09c210..905f0b96 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -7035,7 +7035,6 @@ sub weld_nested_containers { # Variables needed for estimating line lengths my $starting_indent; my $starting_lentot; - my $starting_level; my $iline_outer_opening = -1; my $weld_count_this_start = 0; @@ -7051,17 +7050,7 @@ sub weld_nested_containers { # Add a tolerance for welds over multiple lines to avoid blinkers my $iline_K = $rLL->[$K]->[_LINE_INDEX_]; - my $level_K = $rLL->[$K]->[_LEVEL_]; - my $tol = 0; - if ( - $iline_K > $iline_outer_opening - - # fix for cases b1041 b1055: - || $level_K > $starting_level - ) - { - $tol = $multiline_tol; - } + my $tol = ( $iline_K > $iline_outer_opening ) ? $multiline_tol : 0; my $excess_length = $starting_indent + $length + $tol - $rOpts_maximum_line_length; @@ -7218,12 +7207,34 @@ EOM $Kref <= 0 ? 0 : $rLL->[ $Kref - 1 ]->[_CUMULATIVE_LENGTH_]; $starting_indent = 0; - $starting_level = $rLL->[$Kref]->[_LEVEL_]; + my $level = $rLL->[$Kref]->[_LEVEL_]; my $ci_level = $rLL->[$Kref]->[_CI_LEVEL_]; + if ( !$rOpts_variable_maximum_line_length ) { - $starting_indent = $rOpts_indent_columns * $starting_level + + $starting_indent = $rOpts_indent_columns * $level + $ci_level * $rOpts_continuation_indentation; + + # Switch to using the outer opening token as the reference + # point if a line break before it would make a longer line. + # Fixes case b1055 and is also an alternate fix for b1065. + my $level_oo = $rLL->[$Kouter_opening]->[_LEVEL_]; + if ( $Kref < $Kouter_opening ) { + my $ci_level_oo = $rLL->[$Kouter_opening]->[_CI_LEVEL_]; + my $lentot_oo = + $rLL->[ $Kouter_opening - 1 ]->[_CUMULATIVE_LENGTH_]; + my $starting_indent_oo = $rOpts_indent_columns * $level_oo + + $ci_level_oo * $rOpts_continuation_indentation; + if ( $lentot_oo - $starting_lentot < + $starting_indent_oo - $starting_indent ) + { + $Kref = $Kouter_opening; + $level = $level_oo; + $ci_level = $ci_level_oo; + $starting_lentot = $lentot_oo; + $starting_indent = $starting_indent_oo; + } + } } # Avoid problem areas with the -wn -lp combination. @@ -7454,9 +7465,9 @@ EOM $starting_lentot = $self->cumulative_length_before_K($Kinner_opening); $starting_indent = 0; - $starting_level = $inner_opening->[_LEVEL_]; if ( !$rOpts_variable_maximum_line_length ) { - $starting_indent = $rOpts_indent_columns * $starting_level; + my $level = $inner_opening->[_LEVEL_]; + $starting_indent = $rOpts_indent_columns * $level; } if (DEBUG_WELD) { @@ -8546,7 +8557,10 @@ sub extended_ci { next; } - # Skip if this is a -bli container (this fixes case b1065) + # Skip if this is a -bli container (this fixes case b1065) Note: case + # b1065 is also fixed by the update for b1055, so this update is not + # essential now. But there does not seem to be a good reason to add + # xci and bli together, so the update is retained. if ( $ris_bli_container->{$seqno} ) { next; } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 3f46c9b6..930a08db 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -3,6 +3,13 @@ =over 4 +=item B + +The previous update produced some problems in testing which are corrected +with this update. + +5 Apr 2021. + =item B Random testing produced some cases in which unusual parameter combinations diff --git a/t/snippets/expect/wn6.wn b/t/snippets/expect/wn6.wn index 93b80378..b037744b 100644 --- a/t/snippets/expect/wn6.wn +++ b/t/snippets/expect/wn6.wn @@ -5,9 +5,9 @@ PDL::Graphics::TriD::Scale->new( $sx, $sy, $sz ) ); # but weld this more complex statement - my $compass = uc( opposite_direction( - line_to_canvas_direction( @{ $coords[0] }, @{ $coords[1] } ) - ) ); + my $compass = uc( opposite_direction( line_to_canvas_direction( + @{ $coords[0] }, @{ $coords[1] } + ) ) ); # OLD: do not weld to a one-line block because the function could # get separated from its opening paren. diff --git a/t/snippets12.t b/t/snippets12.t index a9798f4d..76958dac 100644 --- a/t/snippets12.t +++ b/t/snippets12.t @@ -544,9 +544,9 @@ use_all_ok( qw{ PDL::Graphics::TriD::Scale->new( $sx, $sy, $sz ) ); # but weld this more complex statement - my $compass = uc( opposite_direction( - line_to_canvas_direction( @{ $coords[0] }, @{ $coords[1] } ) - ) ); + my $compass = uc( opposite_direction( line_to_canvas_direction( + @{ $coords[0] }, @{ $coords[1] } + ) ) ); # OLD: do not weld to a one-line block because the function could # get separated from its opening paren. -- 2.39.5