From a8b625981912300995303958aaffb0d0305baaa2 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 3 Apr 2021 07:38:02 -0700 Subject: [PATCH] Fix rare convergence problem with -wn --- bin/perltidy | 4 ++++ lib/Perl/Tidy/Formatter.pm | 23 +++++++++++++++++------ local-docs/BugLog.pod | 10 ++++++++++ t/snippets/expect/wn6.wn | 6 +++--- t/snippets/packing_list.txt | 1 - t/snippets12.t | 6 +++--- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index f08820c6..990f46d8 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -4127,6 +4127,10 @@ Space before and after the curly braces is optional. For a specific example, the following line + oneliner { --maximum-line-length=0 --noadd-newlines } + +or equivalently with abbreviations + oneliner { -l=0 -nanl } could be placed in a F<.perltidyrc> file to temporarily override the maximum diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 32e09911..1f09c210 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -7035,6 +7035,7 @@ 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; @@ -7050,7 +7051,17 @@ sub weld_nested_containers { # Add a tolerance for welds over multiple lines to avoid blinkers my $iline_K = $rLL->[$K]->[_LINE_INDEX_]; - my $tol = ( $iline_K > $iline_outer_opening ) ? $multiline_tol : 0; + 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 $excess_length = $starting_indent + $length + $tol - $rOpts_maximum_line_length; @@ -7207,11 +7218,11 @@ EOM $Kref <= 0 ? 0 : $rLL->[ $Kref - 1 ]->[_CUMULATIVE_LENGTH_]; $starting_indent = 0; - my $level = $rLL->[$Kref]->[_LEVEL_]; + $starting_level = $rLL->[$Kref]->[_LEVEL_]; my $ci_level = $rLL->[$Kref]->[_CI_LEVEL_]; if ( !$rOpts_variable_maximum_line_length ) { - $starting_indent = $rOpts_indent_columns * $level + + $starting_indent = $rOpts_indent_columns * $starting_level + $ci_level * $rOpts_continuation_indentation; } @@ -7300,7 +7311,7 @@ EOM } } } - } + } ## end starting new weld sequence # DO-NOT-WELD RULE 2: # Do not weld an opening paren to an inner one line brace block @@ -7443,9 +7454,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 ) { - my $level = $inner_opening->[_LEVEL_]; - $starting_indent = $rOpts_indent_columns * $level; + $starting_indent = $rOpts_indent_columns * $starting_level; } if (DEBUG_WELD) { diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 1cdcb759..3f46c9b6 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -3,6 +3,16 @@ =over 4 +=item B + +Random testing produced some cases in which unusual parameter combinations +caused lack of convergence for the -wn flag. The problem was fixed by +adjusting a tolerance in the line length calculation. + +This fixes cases b1041 b1055. + +2 Apr 2021. + =item B Random testing produced a case with the combination -bli and -xci which did not diff --git a/t/snippets/expect/wn6.wn b/t/snippets/expect/wn6.wn index b037744b..93b80378 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/snippets/packing_list.txt b/t/snippets/packing_list.txt index 6bcd97e3..8eabb077 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -315,7 +315,6 @@ ../snippets24.t fpva.fpva2 ../snippets24.t lpxl.def ../snippets24.t lpxl.lpxl1 -../snippets24.t lpxl.lpxl2 ../snippets24.t lpxl.lpxl3 ../snippets24.t lpxl.lpxl4 ../snippets24.t lpxl.lpxl5 diff --git a/t/snippets12.t b/t/snippets12.t index 76958dac..a9798f4d 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