From d359a6042768ca1f12fafce4f07a38423811f601 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 30 Jan 2021 08:03:32 -0800 Subject: [PATCH] adjust line length and token count tolerances for -wn stability --- lib/Perl/Tidy/Formatter.pm | 21 +++++++++++++++++++-- local-docs/BugLog.pod | 18 +++++++++++++++--- t/snippets/expect/wn6.def | 5 +++-- t/snippets/expect/wn6.wn | 9 +++++---- t/snippets/expect/wn8.def | 3 ++- t/snippets/expect/wn8.wn | 8 +++++--- t/snippets/wn6.in | 5 +++-- t/snippets/wn8.in | 3 ++- t/snippets12.t | 19 +++++++++++-------- t/snippets18.t | 14 +++++++++----- 10 files changed, 74 insertions(+), 31 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index ca5ff27e..fee112f7 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -6653,6 +6653,12 @@ sub weld_nested_containers { # line length and gets broken in a bad spot. my $length_tol = 1; + # Sometimes the total starting indentation can increase at a later stage, + # for example the -bli command will move an opening brace inward one level + # instead of one ci. To avoid blinkers, we add an extra length tolerance. + $length_tol += + abs( $rOpts_indent_columns - $rOpts_continuation_indentation ); + my $excess_length_to_K = sub { my ($K) = @_; @@ -6698,6 +6704,15 @@ sub weld_nested_containers { my $Kouter_closing = $K_closing_container->{$outer_seqno}; my $Kinner_closing = $K_closing_container->{$inner_seqno}; + # RULE: do not weld if inner container has <= 3 tokens unless the next + # token is a heredoc (so we know there will be multiple lines) + if ( $Kinner_closing - $Kinner_opening <= 4 ) { + my $Knext_nonblank = $self->K_next_nonblank($Kinner_opening); + next unless defined($Knext_nonblank); + my $type = $rLL->[$Knext_nonblank]->[_TYPE_]; + next unless ( $type eq 'h' ); + } + my $outer_opening = $rLL->[$Kouter_opening]; my $inner_opening = $rLL->[$Kinner_opening]; my $outer_closing = $rLL->[$Kouter_closing]; @@ -6762,7 +6777,9 @@ sub weld_nested_containers { # (1) the containers are all on one line, and # (2) the line does not exceed the allowable length, and # This flag is used to avoid creating blinkers. - if ( $iline_oo == $iline_oc && $excess_length_to_K->($Klast) <= 0 ) + # For stability, we remove the length tolerance which has been added + if ( $iline_oo == $iline_oc + && $excess_length_to_K->($Klast) <= $length_tol ) { $is_one_line_weld = 1; } @@ -7610,7 +7627,7 @@ sub adjust_container_indentation { my $min_req = 2; # But for -boc we want to see a break at an interior list comma to be - # sure the list stays broken. It is sufficient to require at least two + # sure the list stays broken. It is sufficient to require at least two # non-blank lines within the block. if ($rOpts_break_at_old_comma_breakpoints) { my $iline = $rLL->[$KK]->[_LINE_INDEX_]; diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 74721122..16ebd38a 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,18 @@ =over 4 +=item B + +Most remaining edge cases of blinking states involving the -wn parameter have +been fixed by adjusting some tolerances in sub weld_nested_containers. The +following cases are fixed with this update: + +b156 b157 b186 b196 b454 b520 b527 b530 b532 b533 b534 b612 b614 b625 b627 + +This update has no effect for realistic parameter settings. + +30 Jan 2021. + =item B Some blinking cases produced in random testing were traced to welding in @@ -14,7 +26,7 @@ with the update: b002 b003 b005 b006 b007 b009 b010 b014 b015 b017 b020 b111 b112 b113 b124 b126 b128 b151 b153 b439 b606 -29 Jan 2021. +29 Jan 2021, 33f1f2b. =item B @@ -33,7 +45,7 @@ The following cases were fixed with this update: b025 b075 b091 b109 b110 b152 b154 b155 b162 b168 b176 b422 b423 b424 b425 b426 b565 -29 Jan 2021. +29 Jan 2021, 33f1f2b. =item B @@ -43,7 +55,7 @@ weld_nested_containers. The following cases were fixed with this update: b131 b134 b136 b205 b233 b238 b284 b350 b352 b358 b385 b487 b604 b605 -29 Jan 2021. +29 Jan 2021, 33f1f2b. =item B diff --git a/t/snippets/expect/wn6.def b/t/snippets/expect/wn6.def index dbfdd3a7..7c3fd759 100644 --- a/t/snippets/expect/wn6.def +++ b/t/snippets/expect/wn6.def @@ -13,8 +13,9 @@ ) ); - # do not weld to a one-line block because the function could get separated - # from its opening paren + # OLD: do not weld to a one-line block because the function could + # get separated from its opening paren. + # NEW: (30-jan-2021): keep one-line block together for stability $_[0]->code_handler( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); diff --git a/t/snippets/expect/wn6.wn b/t/snippets/expect/wn6.wn index d40ccd56..213b2ad8 100644 --- a/t/snippets/expect/wn6.wn +++ b/t/snippets/expect/wn6.wn @@ -9,10 +9,11 @@ @{ $coords[0] }, @{ $coords[1] } ) ) ); - # do not weld to a one-line block because the function could get separated - # from its opening paren - $_[0]->code_handler( - sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); + # OLD: do not weld to a one-line block because the function could + # get separated from its opening paren. + # NEW: (30-jan-2021): keep one-line block together for stability + $_[0]->code_handler + ( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); # another example; do not weld because the sub is not broken $wrapped->add_around_modifier( diff --git a/t/snippets/expect/wn8.def b/t/snippets/expect/wn8.def index 98b4d186..9bdccf39 100644 --- a/t/snippets/expect/wn8.def +++ b/t/snippets/expect/wn8.def @@ -15,7 +15,8 @@ _("Cannot delete zone $name: sub-zones or appellations exist.") ); - # fixed RULE 1: this is now a stable state with -wn + # OLD: fixed RULE 1: this is now a stable state with -wn + # NEW (30 jan 2021): do not weld if one interior token $app->FORM->{'appbar1'}->set_status( _("Cannot delete zone $name: sub-zones or appellations exist.") ); diff --git a/t/snippets/expect/wn8.wn b/t/snippets/expect/wn8.wn index 921d5ec5..39e2970b 100644 --- a/t/snippets/expect/wn8.wn +++ b/t/snippets/expect/wn8.wn @@ -11,6 +11,8 @@ _("Cannot delete zone $name: sub-zones or appellations exist.") ); - # fixed RULE 1: this is now a stable state with -wn - $app->FORM->{'appbar1'}->set_status( _( - "Cannot delete zone $name: sub-zones or appellations exist.") ); + # OLD: fixed RULE 1: this is now a stable state with -wn + # NEW (30 jan 2021): do not weld if one interior token + $app->FORM->{'appbar1'}->set_status( + _("Cannot delete zone $name: sub-zones or appellations exist.") + ); diff --git a/t/snippets/wn6.in b/t/snippets/wn6.in index 6ca69a57..356e7b08 100644 --- a/t/snippets/wn6.in +++ b/t/snippets/wn6.in @@ -7,8 +7,9 @@ my $compass = uc( opposite_direction( line_to_canvas_direction( @{ $coords[0] }, @{ $coords[1] } ) ) ); - # do not weld to a one-line block because the function could get separated - # from its opening paren + # OLD: do not weld to a one-line block because the function could + # get separated from its opening paren. + # NEW: (30-jan-2021): keep one-line block together for stability $_[0]->code_handler ( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); diff --git a/t/snippets/wn8.in b/t/snippets/wn8.in index 3e07557f..fe642f0b 100644 --- a/t/snippets/wn8.in +++ b/t/snippets/wn8.in @@ -13,6 +13,7 @@ _("Cannot delete zone $name: sub-zones or appellations exist.") ); - # fixed RULE 1: this is now a stable state with -wn + # OLD: fixed RULE 1: this is now a stable state with -wn + # NEW (30 jan 2021): do not weld if one interior token $app->FORM->{'appbar1'}->set_status(_( "Cannot delete zone $name: sub-zones or appellations exist.")); diff --git a/t/snippets12.t b/t/snippets12.t index e36c9b4b..89172f42 100644 --- a/t/snippets12.t +++ b/t/snippets12.t @@ -166,8 +166,9 @@ use_all_ok( my $compass = uc( opposite_direction( line_to_canvas_direction( @{ $coords[0] }, @{ $coords[1] } ) ) ); - # do not weld to a one-line block because the function could get separated - # from its opening paren + # OLD: do not weld to a one-line block because the function could + # get separated from its opening paren. + # NEW: (30-jan-2021): keep one-line block together for stability $_[0]->code_handler ( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); @@ -512,8 +513,9 @@ use_all_ok( qw{ ) ); - # do not weld to a one-line block because the function could get separated - # from its opening paren + # OLD: do not weld to a one-line block because the function could + # get separated from its opening paren. + # NEW: (30-jan-2021): keep one-line block together for stability $_[0]->code_handler( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); @@ -546,10 +548,11 @@ use_all_ok( qw{ @{ $coords[0] }, @{ $coords[1] } ) ) ); - # do not weld to a one-line block because the function could get separated - # from its opening paren - $_[0]->code_handler( - sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); + # OLD: do not weld to a one-line block because the function could + # get separated from its opening paren. + # NEW: (30-jan-2021): keep one-line block together for stability + $_[0]->code_handler + ( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); # another example; do not weld because the sub is not broken $wrapped->add_around_modifier( diff --git a/t/snippets18.t b/t/snippets18.t index 15866b91..5cd0e2ed 100644 --- a/t/snippets18.t +++ b/t/snippets18.t @@ -273,7 +273,8 @@ my ( $a, $b, $c ) = @_; # test -nsak="my for" _("Cannot delete zone $name: sub-zones or appellations exist.") ); - # fixed RULE 1: this is now a stable state with -wn + # OLD: fixed RULE 1: this is now a stable state with -wn + # NEW (30 jan 2021): do not weld if one interior token $app->FORM->{'appbar1'}->set_status(_( "Cannot delete zone $name: sub-zones or appellations exist.")); ---------- @@ -321,7 +322,8 @@ my ( $a, $b, $c ) = @_; # test -nsak="my for" _("Cannot delete zone $name: sub-zones or appellations exist.") ); - # fixed RULE 1: this is now a stable state with -wn + # OLD: fixed RULE 1: this is now a stable state with -wn + # NEW (30 jan 2021): do not weld if one interior token $app->FORM->{'appbar1'}->set_status( _("Cannot delete zone $name: sub-zones or appellations exist.") ); @@ -345,9 +347,11 @@ my ( $a, $b, $c ) = @_; # test -nsak="my for" _("Cannot delete zone $name: sub-zones or appellations exist.") ); - # fixed RULE 1: this is now a stable state with -wn - $app->FORM->{'appbar1'}->set_status( _( - "Cannot delete zone $name: sub-zones or appellations exist.") ); + # OLD: fixed RULE 1: this is now a stable state with -wn + # NEW (30 jan 2021): do not weld if one interior token + $app->FORM->{'appbar1'}->set_status( + _("Cannot delete zone $name: sub-zones or appellations exist.") + ); #3........... }, -- 2.39.5