]> git.donarmstrong.com Git - perltidy.git/commitdiff
Revise weld tolerances, simplify code, fix welded ci
authorSteve Hancock <perltidy@users.sourceforge.net>
Sun, 9 May 2021 02:08:19 +0000 (19:08 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sun, 9 May 2021 02:08:19 +0000 (19:08 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index 53f96838c4c451f8c56a3b839c9b6369a1261f33..d205dcd6bf2a3192f06b5691f6d860b94bf3d67d 100644 (file)
@@ -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 <<EOM;
-at index $K excess length to K is $excess_length, tol=$tol, length=$length, starting_lentot=$starting_lentot, maximum_text_length=$maximum_text_length, line(K)=$iline_K , line_start = $iline_outer_opening
-EOM
-
-        return ($excess_length);
-    };
-
     my $length_to_opening_seqno = sub {
         my ($seqno) = @_;
         my $KK      = $K_opening_container->{$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_];
         }
     }
 
index 60da917044388a716e0fc73c18cca4e5682e7767..d9d47371ba319ab0670579fb32b0a29e4dfda5e2 100644 (file)
@@ -2,6 +2,39 @@
 
 =over 4
 
+=item B<Revise weld tolerances, simplify code, fix welded ci>
+
+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<Correct brace types mismarked by tokenizer, update>
 
 This is a generalization of commit 7d3bf4 in which the tokenizer sends a signal