]> git.donarmstrong.com Git - perltidy.git/commitdiff
fix issue b1385
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 14 Oct 2022 19:48:53 +0000 (12:48 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 14 Oct 2022 19:48:53 +0000 (12:48 -0700)
dev-bin/run_convergence_tests.pl.data
lib/Perl/Tidy/Formatter.pm

index bfae460c1ddf31950a277fc3d7e8b45fee8e4a30..5a19b930b74a03f998872ec87856b8512e78a13e 100644 (file)
@@ -10245,6 +10245,34 @@ my $formatter = new HTML::FormatPS(
 --want-trailing-commas='b'
 --variable-maximum-line-length
 
+==> b1385.in <==
+(
+    # optional glob
+    {
+        sub    => 'sub26',
+        p      => [ foo => 1, bar => do { local *BAR; *BAR }, ],
+        expect => q{},
+    },
+);
+
+(
+    # optional glob
+    {
+        sub => 'sub26',
+        p   => [
+            foo => 1,
+            bar => do { local *BAR; *BAR }
+        ],
+        expect => q{},
+    },
+);
+
+==> b1385.par <==
+--add-trailing-commas
+--delete-trailing-commas
+--ignore-old-breakpoints
+--want-trailing-commas='m'
+
 ==> b1386.in <==
 # S1
 open( OUT,
index e1c45a96ad6152670e1394effb42b513a321cefa..190297e719e6f4cd177d0cff10457f70dbe44172 100644 (file)
@@ -14422,11 +14422,26 @@ EOM
                 }
                 elsif ($rbrace_follower) {
 
-                    unless ( $rbrace_follower->{$next_nonblank_token} ) {
+                    if ( $rbrace_follower->{$next_nonblank_token} ) {
+
+                        # Fix for b1385: keep break after a comma following a
+                        # 'do' block. This could also be used for other block
+                        # types, but that would cause a significant change in
+                        # existing formatting without much benefit.
+                        if (   $next_nonblank_token eq ','
+                            && $Knnb eq $K_last
+                            && $block_type eq 'do'
+                            && $self->is_trailing_comma($Knnb) )
+                        {
+                            $self->[_rbreak_after_Klast_]->{$K_last} = 1;
+                        }
+                    }
+                    else {
                         $self->end_batch()
                           unless ( $no_internal_newlines
                             || $max_index_to_go < 0 );
                     }
+
                     $rbrace_follower = undef;
                 }
 
@@ -14474,6 +14489,33 @@ EOM
 
 } ## end closure process_line_of_CODE
 
+sub is_trailing_comma {
+    my ( $self, $KK ) = @_;
+
+    # Given:
+    #   $KK - index of a comma in token list
+    # Return:
+    #   true if the comma at index $KK is a trailing comma
+    #   false if not
+
+    my $rLL     = $self->[_rLL_];
+    my $type_KK = $rLL->[$KK]->[_TYPE_];
+    if ( $type_KK ne ',' ) {
+        DEVEL_MODE
+          && Fault("Bad call: expected type ',' but received '$type_KK'\n");
+        return;
+    }
+    my $Knnb = $self->K_next_nonblank($KK);
+    if ( defined($Knnb) ) {
+        my $type_sequence = $rLL->[$Knnb]->[_TYPE_SEQUENCE_];
+        my $type_Knnb     = $rLL->[$Knnb]->[_TYPE_];
+        if ( $type_sequence && $is_closing_type{$type_Knnb} ) {
+            return 1;
+        }
+    }
+    return;
+} ## end sub is_trailing_comma
+
 sub tight_paren_follows {
 
     my ( $self, $K_to_go_0, $K_ic ) = @_;