From: Steve Hancock Date: Wed, 12 Jul 2023 03:17:41 +0000 (-0700) Subject: fix b1458 X-Git-Tag: 20230701.01~7 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=1055902ad4580821e5649a3389868c91b942e292;p=perltidy.git fix b1458 --- diff --git a/dev-bin/run_convergence_tests.pl.data b/dev-bin/run_convergence_tests.pl.data index 2cbc5811..0b804bb5 100644 --- a/dev-bin/run_convergence_tests.pl.data +++ b/dev-bin/run_convergence_tests.pl.data @@ -11666,6 +11666,25 @@ BEGIN { --extended-continuation-indentation --extended-line-up-parentheses +==> b1458.in <== + join( '', + grep { defined } + @{ $me->{'mail_hdr_list'} } + ); + + join( '', + grep { defined } + @{ $me->{'mail_hdr_list'} + }, ); + +==> b1458.par <== +--maximum-line-length=38 +--indent-columns=5 +--continuation-indentation=1 +--add-trailing-commas +--want-trailing-commas='b' +--delete-trailing-commas + ==> b146.in <== # State 1 diff --git a/dev-bin/run_convergence_tests.pl.expect b/dev-bin/run_convergence_tests.pl.expect index f19eb819..1a47deee 100644 --- a/dev-bin/run_convergence_tests.pl.expect +++ b/dev-bin/run_convergence_tests.pl.expect @@ -7901,6 +7901,17 @@ BEGIN { } +==> b1458 <== + join( '', + grep { defined } + @{ $me->{'mail_hdr_list'} + }, ); + + join( '', + grep { defined } + @{ $me->{'mail_hdr_list'} + }, ); + ==> b146 <== # State 1 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 223462eb..0591ce66 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -9566,6 +9566,22 @@ sub add_trailing_comma { $self->match_trailing_comma_rule( $KK, $Kfirst, $Kp, $trailing_comma_rule, 1 ); + # b1458 fix method 1: do not add if this would excess line length. + # This is more general than fix method 2, below, but the logic is not + # as clean. So this fix is currently deactivated. + if ( 0 && $match && $rOpts_delete_trailing_commas && $KK > 0 ) { + my $line_index = $rLL->[ $KK - 1 ]->[_LINE_INDEX_]; + my $rlines = $self->[_rlines_]; + my $line_of_tokens = $rlines->[$line_index]; + my $input_line = $line_of_tokens->{_line_text}; + my $len = $self->[_length_function_]->($input_line) - 1; + my $level = $rLL->[$Kfirst]->[_LEVEL_]; + my $max_len = $maximum_line_length_at_level[$level]; + if ( $len >= $max_len ) { + $match = 0; + } + } + # if so, add a comma if ($match) { my $Knew = $self->store_new_token( ',', ',', $Kp ); @@ -9627,6 +9643,31 @@ sub delete_trailing_comma { } } + # b1458 fix method 2: do not remove a comma after a leading brace type 'R' + # since it is under stress and could become unstable. This is a more + # specific fix but the logic is cleaner than method 1. + if ( !$match + && $rOpts_add_trailing_commas + && $rLL->[$Kfirst]->[_TYPE_] eq 'R' ) + { + + # previous old token should be the comma.. + my $Kp_old = $self->K_previous_nonblank( $KK, $rLL ); + if ( defined($Kp_old) + && $Kp_old > $Kfirst + && $rLL->[$Kp_old]->[_TYPE_] eq ',' ) + { + + # if the comma follows the first token of the line .. + my $Kpp_old = $self->K_previous_nonblank( $Kp_old, $rLL ); + if ( defined($Kpp_old) && $Kpp_old eq $Kfirst ) { + + # do not delete it + $match = 1; + } + } + } + # If no match, delete it if ( !$match ) {