]> git.donarmstrong.com Git - perltidy.git/commitdiff
Fix rare loss of vertical alignment in welded containers, c053
authorSteve Hancock <perltidy@users.sourceforge.net>
Mon, 9 Aug 2021 13:11:59 +0000 (06:11 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Mon, 9 Aug 2021 13:11:59 +0000 (06:11 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index 47887f9534854359d49817c82bd50b6330944d7d..a174cc1ee9306b30ed15512123d0a14023295854 100644 (file)
@@ -19471,10 +19471,45 @@ EOM
         # Set flag which tells if this line is contained in a multi-line list
         my $list_seqno = $self->is_list_by_K($Kbeg);
 
+        # The alignment tokens have been marked with nesting_depths, so we need
+        # to pass nesting depths to the vertical aligner. They remain invariant
+        # under welding.  Previously, level values were sent to the aligner.
+        # But they have been altered in welding, and this can lead to
+        # alignement errors.
+        my $nesting_depth_beg = $nesting_depth_to_go[$ibeg];
+        my $nesting_depth_end = $nesting_depth_to_go[$iend];
+        if ( $is_closing_type{ $types_to_go[$ibeg] } ) { $nesting_depth_beg-- }
+        if ( $is_closing_type{ $types_to_go[$iend] } ) { $nesting_depth_end-- }
+
+        # Adjust nesting depths to keep -lp indentation for qw lists.  This is
+        # required because qw lists contained in brackets do not get nesting
+        # depths, but the vertical aligner is watching nesting depth changes to
+        # decide if a -lp block is intact.  Without this patch, qw lists
+        # bracked with angle bracket will not get the correct -lp indentation.
+
+        # Looking for line with isolated qw ...
+        if (   $rOpts_line_up_parentheses
+            && $type_beg eq 'q'
+            && $ibeg == $iend )
+        {
+
+            # ... which is part of a multiline qw
+            my $Km = $self->K_previous_nonblank($Kbeg);
+            my $Kp = $self->K_next_nonblank($Kbeg);
+            if (   defined($Km) && $rLL->[$Km]->[_TYPE_] eq 'q'
+                || defined($Kp) && $rLL->[$Kp]->[_TYPE_] eq 'q' )
+            {
+                $nesting_depth_beg++;
+                $nesting_depth_end++;
+            }
+        }
+
         # send this new line down the pipe
         my $rvalign_hash = {};
-        $rvalign_hash->{level}                     = $lev;
-        $rvalign_hash->{level_end}                 = $level_end;
+        ## $rvalign_hash->{level}                     = $lev;
+        ## $rvalign_hash->{level_end}                 = $level_end;
+        $rvalign_hash->{level}                     = $nesting_depth_beg;
+        $rvalign_hash->{level_end}                 = $nesting_depth_end;
         $rvalign_hash->{level_adj}                 = $level_adj;
         $rvalign_hash->{indentation}               = $indentation;
         $rvalign_hash->{list_seqno}                = $list_seqno;
index 90c55bb3e572afb0da44fbb0dfe19b5fe0b24ccb..fe0bd9bc07ae74e7f619323bb278a9012240b45e 100644 (file)
@@ -2,6 +2,52 @@
 
 =over 4
 
+=item B<Fix rare loss of vertical alignment in welded containers, c053>
+
+This update corrects a rare loss of vertical alignment in welded containers.
+
+To illustrate the issue, the normal formatting of the following snippet is
+
+    # perltidy -sil=1 
+    ( $msg, $defstyle ) = do {
+            $i == 1 ? ( "First", "Color" )
+          : $i == 2 ? ( "Then", "Rarity" )
+          :           ( "Then", "Name" );
+    };
+
+If it appears within a welded container, the alignment of the last line
+was being lost:
+
+    # OLD: perltidy -wn -sil=1
+    { {
+
+        ( $msg, $defstyle ) = do {
+                $i == 1 ? ( "First", "Color" )
+              : $i == 2 ? ( "Then",  "Rarity" )
+              : ( "Then", "Name" );
+        };
+    } }
+
+The corrected result is
+
+    # NEW: perltidy -wn -sil=1
+    { {
+
+        ( $msg, $defstyle ) = do {
+                $i == 1 ? ( "First", "Color" )
+              : $i == 2 ? ( "Then", "Rarity" )
+              :           ( "Then", "Name" );
+        };
+    } }
+
+Several other minor vertical alignment issues are fixed with this updated.  The
+problem was that two slightly different measures of the depth of indentation
+were being compared in the vertical aligner.
+
+This fixes case c053.
+
+8 Aug 2021.
+
 =item B<Fix edge case of formatting instability, b1189>.
 
 Testing with random parameters produced a case of unstable formatting involving