From ba2e2c3cd77036e8dfd4057b773ef811d5a27fb0 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 30 Nov 2022 13:44:59 -0800 Subject: [PATCH] fix b1436, b1439 --- .perlcriticrc | 4 +- dev-bin/run_convergence_tests.pl.data | 64 +++++++++++++++++++++++++++ lib/Perl/Tidy/Formatter.pm | 51 ++++++++++++++------- 3 files changed, 102 insertions(+), 17 deletions(-) diff --git a/.perlcriticrc b/.perlcriticrc index e294d6a7..68a7829d 100644 --- a/.perlcriticrc +++ b/.perlcriticrc @@ -77,10 +77,10 @@ short_subroutine_statements = 2 # This policy is very useful in locating complex code which might benefit from # simplification. The max value has to be set rather high here because there # are some routines in Formatter.pm with high mccabe values. In particular, -# sub "check_options" has a value of 126 but is a very straightforward sub. +# sub "check_options" has a value of 133 but is a very straightforward sub. # Nothing would be gained by modifying it. [Subroutines::ProhibitExcessComplexity] -max_mccabe=130 +max_mccabe=140 # This policy can be very helpful for locating complex code, but sometimes # deep nests are the best option, especially in error handling and debug diff --git a/dev-bin/run_convergence_tests.pl.data b/dev-bin/run_convergence_tests.pl.data index b062e2ab..662496d7 100644 --- a/dev-bin/run_convergence_tests.pl.data +++ b/dev-bin/run_convergence_tests.pl.data @@ -11135,6 +11135,70 @@ $last = after ( --keep-old-breakpoints-after=':' --maximum-line-length=34 +==> b1436.in <== + if ( + $included{'gold'} + && $yes_andor =~ /any/ + && $not_andor !~ /all/ + && ( + grep ( { $included{$_} } @COLORS ) + + grep ( { $excluded{$_} } @COLORS ) ) == 5 + ) + { + } + + if ( + $included{'gold'} + && $yes_andor =~ /any/ + && $not_andor !~ /all/ + && ( grep ( { $included{$_} } @COLORS ) + + grep ( { $excluded{$_} } @COLORS ) ) == 5 + ) + { + } + +==> b1436.par <== +--continuation-indentation=8 +--extended-continuation-indentation +--keep-old-breakpoints-after='+' +--variable-maximum-line-length +--want-break-before='+' + +==> b1439.in <== + $pt += + ( 0.1734 - 0.000393 * $t ) * dsin($m) + + 0.0021 * dsin( 2 * $m ) + - 0.4068 * dsin($mprime) + + 0.0161 * dsin( 2 * $mprime ) + - 0.0004 * dsin( 3 * $mprime ) + + 0.0104 * dsin( 2 * $f ) + - 0.0051 * dsin( $m + $mprime ) + - 0.0074 * dsin( $m - $mprime ) + + 0.0004 * dsin( 2 * $f + $m ) + - 0.0004 * dsin( 2 * $f - $m ) + - 0.0006 * dsin( 2 * $f + $mprime ) + + 0.0010 * dsin( 2 * $f - $mprime ) + + 0.0005 * dsin( $m + 2 * $mprime ); + + $pt += + ( 0.1734 - 0.000393 * $t ) * dsin($m) + + 0.0021 * dsin( 2 * $m ) - + 0.4068 * dsin($mprime) + + 0.0161 * dsin( 2 * $mprime ) - + 0.0004 * dsin( 3 * $mprime ) + + 0.0104 * dsin( 2 * $f ) - + 0.0051 * dsin( $m + $mprime ) + - 0.0074 * dsin( $m - $mprime ) + + 0.0004 * dsin( 2 * $f + $m ) - + 0.0004 * dsin( 2 * $f - $m ) + - 0.0006 * dsin( 2 * $f + $mprime ) + + 0.0010 * dsin( 2 * $f - $mprime ) + + 0.0005 * dsin( $m + 2 * $mprime ); + +==> b1439.par <== +--keep-old-breakpoints-after='-' +--want-break-before='-' + ==> b146.in <== # State 1 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 4696e7c6..5ec3cc8d 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -261,6 +261,7 @@ my ( %is_container_label_type, %is_die_confess_croak_warn, %is_my_our_local, + %is_soft_keep_break_type, @all_operators, @@ -745,6 +746,13 @@ BEGIN { push @q, ','; @is_counted_type{@q} = (1) x scalar(@q); + # Tokens where --keep-old-break-xxx flags make soft breaks instead + # of hard breaks. See b1433 and b1436. + # NOTE: $type is used as the hash key for now; if other container tokens + # are added it might be necessary to use a token/type mixture. + @q = qw# -> ? : && || + - / * #; + @is_soft_keep_break_type{@q} = (1) x scalar(@q); + } { ## begin closure to count instances @@ -1799,6 +1807,31 @@ EOM initialize_keep_old_breakpoints( $rOpts->{'keep-old-breakpoints-after'}, 'kba', \%keep_break_after_type ); + # Modify %keep_break_before and %keep_break_after to avoid conflicts + # with %want_break_before; fixes b1436. + # This became necessary after breaks for some tokens were converted + # from hard to soft (see b1433). + # We could do this for all tokens, but to minimize changes to existing + # code we currently only do this for the soft break tokens. + foreach my $key ( keys %keep_break_before_type ) { + if ( defined( $want_break_before{$key} ) + && !$want_break_before{$key} + && $is_soft_keep_break_type{$key} ) + { + $keep_break_after_type{$key} = $keep_break_before_type{$key}; + delete $keep_break_before_type{$key}; + } + } + foreach my $key ( keys %keep_break_after_type ) { + if ( defined( $want_break_before{$key} ) + && $want_break_before{$key} + && $is_soft_keep_break_type{$key} ) + { + $keep_break_before_type{$key} = $keep_break_after_type{$key}; + delete $keep_break_after_type{$key}; + } + } + $controlled_comma_style ||= $keep_break_before_type{','}; $controlled_comma_style ||= $keep_break_after_type{','}; @@ -8861,19 +8894,6 @@ EOM return ( $severe_error, $rqw_lines ); } ## end sub resync_lines_and_tokens -my %is_soft_break_type; - -BEGIN { - - # Soft breaks are needed to avoid conflicts for token types which might be - # treated with special logic for chains. Fixes b1433, 1434, 1435. - # NOTE: $type is used as the hash key for now; if other container tokens - # are added it might be necessary to use a token/type mixture. - my @q = qw# -> ? : && || + - / * #; - @is_soft_break_type{@q} = (1) x scalar(@q); - -} - sub keep_old_line_breaks { # Called once per file to find and mark any old line breaks which @@ -8949,7 +8969,7 @@ sub keep_old_line_breaks { if ( !$seqno ) { my $type = $rLL->[$KK]->[_TYPE_]; if ( $rkeep_break_hash->{$type} ) { - $rbreak_hash->{$KK} = $is_soft_break_type{$type} ? 2 : 1; + $rbreak_hash->{$KK} = $is_soft_keep_break_type{$type} ? 2 : 1; } } @@ -8983,7 +9003,8 @@ sub keep_old_line_breaks { } if ($match) { my $type = $rLL->[$KK]->[_TYPE_]; - $rbreak_hash->{$KK} = $is_soft_break_type{$type} ? 2 : 1; + $rbreak_hash->{$KK} = + $is_soft_keep_break_type{$type} ? 2 : 1; } } } -- 2.39.5