From ad8870b2867f14328679a8cc47c660f34e195e55 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 8 May 2021 19:08:19 -0700 Subject: [PATCH] Revise weld tolerances, simplify code, fix welded ci --- lib/Perl/Tidy/Formatter.pm | 58 ++++++++++++++++++++++---------------- local-docs/BugLog.pod | 33 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 53f96838..d205dcd6 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -7386,26 +7386,6 @@ sub weld_nested_containers { my $multiline_tol = 1 + max( $rOpts_indent_columns, $rOpts_continuation_indentation ); - my $excess_length_to_K = sub { - my ($K) = @_; - - # Return estimated excess line length from the line start to token $K - my $length = - $self->cumulative_length_before_K($K) - $starting_lentot + 1; - - # 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 $excess_length = $length + $tol - $maximum_text_length; - - DEBUG_WELD && print <{$seqno}; @@ -7501,6 +7481,11 @@ EOM my $iline_oc = $outer_closing->[_LINE_INDEX_]; my $token_oo = $outer_opening->[_TOKEN_]; + my $is_multiline_weld = + $iline_oo == $iline_io + && $iline_ic == $iline_oc + && $iline_io != $iline_ic; + if (DEBUG_WELD) { my $token_io = $rLL->[$Kinner_opening]->[_TOKEN_]; my $len_oo = $rLL->[$Kouter_opening]->[_CUMULATIVE_LENGTH_]; @@ -7687,14 +7672,29 @@ EOM } } - my $excess = $excess_length_to_K->($K_for_length); + # Use a tolerance for welds over multiple lines to avoid blinkers. + # We can use zero tolerance if it looks like we are working on an + # existing weld. + my $tol = + $is_one_line_weld || $is_multiline_weld + ? 0 + : $multiline_tol; + + # By how many characters does this exceed the text window? + my $excess = + $self->cumulative_length_before_K($K_for_length) - + $starting_lentot + 1 + $tol - + $maximum_text_length; + + # Old patch: Use '>=0' instead of '> 0' here to fix cases b995 b998 + # b1000 b1001 b1007 b1008 b1009 b1010 b1011 b1012 b1016 b1017 b1018 + # Revised patch: New tolerance definition allows going back to '> 0' + # here. This fixes case b1124. See also cases b1087 and b1087a. + if ( $excess > 0 ) { $do_not_weld_rule = 3 } - # Use '>=' instead of '=' here to fix cases b995 b998 b1000 - # b1001 b1007 b1008 b1009 b1010 b1011 b1012 b1016 b1017 b1018 - if ( $excess >= 0 ) { $do_not_weld_rule = 3 } if (DEBUG_WELD) { $Msg .= -"RULE 3 test: excess length to K=$Kinner_opening is $excess ( > 0 ?) \n"; +"RULE 3 test: excess length to K=$Kinner_opening is $excess > 0 with tol= $tol ?) \n"; } } @@ -7818,6 +7818,14 @@ EOM for ( my $KK = $Kstart ; $KK <= $Kstop ; $KK++ ) { $rLL->[$KK]->[_LEVEL_] += $dlevel; } + + # Copy opening ci level to help break at = for -lp mode (case b1124) + $rLL->[$Kinner_opening]->[_CI_LEVEL_] = + $rLL->[$Kouter_opening]->[_CI_LEVEL_]; + + # But do not copy the closing ci level ... it can give poor results + ## $rLL->[$Kinner_closing]->[_CI_LEVEL_] = + ## $rLL->[$Kouter_closing]->[_CI_LEVEL_]; } } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 60da9170..d9d47371 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,39 @@ =over 4 +=item B + +The welding process uses a tolerance to keep results stable. Basically, a zero +tolerance is used if it looks like it is welding an existing weld, while a +finite tolerance is used otherwise. The effect is to reject a few marginal +welds to gain stability. The coding to do this was simplified and the tolerance +was made more precise to fix case b1124. + +Another change with this update is that at welded containers, the value of the +-ci flag of an outer opening token is transferred to the inner opening token. +This may improve some indentation in a few cases if the -lp flag is also used. +It has no effect if -lp is not used. + + # OLD: perltidy -wn -gnu + emit_symbols([qw( + ctermid + get_sysinfo + Perl_OS2_init + ... + CroakWinError + )]); + + # NEW: perltidy -wn -gnu + emit_symbols([qw( + ctermid + get_sysinfo + Perl_OS2_init + ... + CroakWinError + )]); + +9 May 2021. + =item B This is a generalization of commit 7d3bf4 in which the tokenizer sends a signal -- 2.39.5