From 6729f8cb3d91a104af658bb23ff365c482daa70f Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 14 Oct 2022 12:48:53 -0700 Subject: [PATCH] fix issue b1385 --- dev-bin/run_convergence_tests.pl.data | 28 +++++++++++++++++ lib/Perl/Tidy/Formatter.pm | 44 ++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/dev-bin/run_convergence_tests.pl.data b/dev-bin/run_convergence_tests.pl.data index bfae460c..5a19b930 100644 --- a/dev-bin/run_convergence_tests.pl.data +++ b/dev-bin/run_convergence_tests.pl.data @@ -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, diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index e1c45a96..190297e7 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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 ) = @_; -- 2.39.5