]> git.donarmstrong.com Git - perltidy.git/commitdiff
minor improvement in alignment
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 6 Jan 2021 05:32:46 +0000 (21:32 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 6 Jan 2021 05:32:46 +0000 (21:32 -0800)
lib/Perl/Tidy/VerticalAligner.pm
local-docs/BugLog.pod

index e1db520d90e9f5077c8cf5e29bdaae5bf4d4f828..7c50e44e53da4107ec0584979d391f6af107b109 100644 (file)
@@ -1530,9 +1530,12 @@ sub _flush_group_lines {
             ## my $is_isolated_pair = $imax_pair < 0
             ##  && ( $jbeg == 0
             ##    || $rall_lines->[ $jbeg - 1 ]->get_imax_pair() < 0 );
+            my $imax_prev =
+              $jbeg > 0 ? $rall_lines->[ $jbeg - 1 ]->get_imax_pair() : -1;
 
             my ( $is_marginal, $imax_align_fix ) =
-              is_marginal_match( $line_0, $line_1, $grp_level, $imax_align );
+              is_marginal_match( $line_0, $line_1, $grp_level, $imax_align,
+                $imax_prev );
             if ($is_marginal) {
                 combine_fields( $line_0, $line_1, $imax_align_fix );
             }
@@ -3842,7 +3845,7 @@ sub Dump_tree_groups {
 
     sub is_marginal_match {
 
-        my ( $line_0, $line_1, $group_level, $imax_align ) = @_;
+        my ( $line_0, $line_1, $group_level, $imax_align, $imax_prev ) = @_;
 
         # Decide if we should align two lines:
         #   return true if the two lines should not be aligned
@@ -3881,6 +3884,7 @@ sub Dump_tree_groups {
         my $jfirst_bad;
         my $line_ending_fat_comma;    # is last token just a '=>' ?
         my $j0_eq_pad;
+        my $j0_max_pad = 0;
 
         for ( my $j = 0 ; $j < $jmax_1 - 1 ; $j++ ) {
             my ( $raw_tok, $lev, $tag, $tok_count ) =
@@ -3906,6 +3910,9 @@ sub Dump_tree_groups {
                 # Remember the pad at a leading equals
                 if ( $raw_tok eq '=' && $lev == $group_level ) {
                     $j0_eq_pad = $pad;
+                    $j0_max_pad =
+                      0.5 * ( $rfield_lengths_1->[0] + $rfield_lengths_0->[0] );
+                    $j0_max_pad = 4 if ($j0_max_pad < 4);
                 }
             }
 
@@ -4084,24 +4091,32 @@ sub Dump_tree_groups {
 
             if (
 
-                # If there is a following line with leading equals, then let
-                # the sweep align them without restriction.  For example,
-                # the first two lines here are a marginal match, but they
-                # are followed by a line with leading equals, so the sweep-lr
-                # logic can align all of the lines:
-
-                #   $date[1] = $month_to_num{ $date[1] };           # <--line_0
-                #   @xdate = split( /[:\/\s]/, $log->field('t') );  # <--line_1
-                #   $day  = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
-                #   $time = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
-
-                $imax_pair >= 0
-
-                # Experimental logic to allow alignment if there is a small pad.
-                # This works fine but would change some formatting.
-                || (   TEST_MARGINAL_EQ_ALIGNMENT
-                    && $j0_eq_pad >= -4
-                    && $j0_eq_pad <= 4 )
+                # If there is a following line with leading equals, or
+                # preceding line with leading equals, then let the sweep align
+                # them without restriction.  For example, the first two lines
+                # here are a marginal match, but they are followed by a line
+                # with leading equals, so the sweep-lr logic can align all of
+                # the lines:
+
+                #  $date[1] = $month_to_num{ $date[1] };            # <--line_0
+                #  @xdate   = split( /[:\/\s]/, $log->field('t') ); # <--line_1
+                #  $day     = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
+                #  $time    = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
+
+                # Likewise, if we reverse the two pairs we want the same result
+
+                #  $day     = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
+                #  $time    = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
+                #  $date[1] = $month_to_num{ $date[1] };            # <--line_0
+                #  @xdate   = split( /[:\/\s]/, $log->field('t') ); # <--line_1
+
+                (
+                       $imax_pair >= 0
+                    || $imax_prev >= 0
+                    || TEST_MARGINAL_EQ_ALIGNMENT
+                )
+                && $j0_eq_pad >= -$j0_max_pad
+                && $j0_eq_pad <= $j0_max_pad 
               )
             {
 
index 1a8eb9de530c5d310dbbb2404de2f0efd9534da2..8cef02a8439d4373d3a8010af5beaac9b3694525 100644 (file)
@@ -2,6 +2,29 @@
 
 =over 4
 
+=item B<Improve alignment of leading equals in rare situation>
+
+A rare case in which a vertical alignment opportunity of leading equals was
+missed has been fixed. This involved lines with additional varying alignment
+tokens, such as 'unless' and second '=' in lines 1-3 below.  In this example
+lines 4 and 5 were not 'looking' backwards to align their leading equals.
+
+    # OLD:
+    $them = 'localhost' unless ( $them = shift );
+    $cmd  = '!print'    unless ( $cmd  = shift );
+    $port = 2345        unless ( $port = shift );
+    $saddr = 'S n a4 x8';
+    $SIG{'INT'} = 'dokill';
+    
+    # NEW
+    $them       = 'localhost' unless ( $them = shift );
+    $cmd        = '!print'    unless ( $cmd  = shift );
+    $port       = 2345        unless ( $port = shift );
+    $saddr      = 'S n a4 x8';
+    $SIG{'INT'} = 'dokill';
+
+Fixed 5 Jan 2021.
+
 =item B<Moved previous patch to a better location>
 
 The previous patch was moved to a location where it only applies if