From 97f02ee6c3dd7b4d82eb9e33d26aa183b5eafe7c Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 9 Aug 2021 06:11:59 -0700 Subject: [PATCH] Fix rare loss of vertical alignment in welded containers, c053 --- lib/Perl/Tidy/Formatter.pm | 39 ++++++++++++++++++++++++++++++-- local-docs/BugLog.pod | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 47887f95..a174cc1e 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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; diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 90c55bb3..fe0bd9bc 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,52 @@ =over 4 +=item B + +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. Testing with random parameters produced a case of unstable formatting involving -- 2.39.5