From: Steve Hancock Date: Mon, 29 Mar 2021 02:16:51 +0000 (-0700) Subject: Follow user requests better to break before operators X-Git-Tag: 20210402~6 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=94f0877582f9f2119cc170367abc94f01f0f5f15;p=perltidy.git Follow user requests better to break before operators --- diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index e7e3a1d3..2c2a8ba9 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -200,6 +200,8 @@ my ( %is_closing_sequence_token, %is_container_label_type, + @all_operators, + # Initialized in check_options. These are constants and could # just as well be initialized in a BEGIN block. %is_do_follower, @@ -546,6 +548,13 @@ BEGIN { @q = qw(&& || and or : ? . + - * /); @is_chain_operator{@q} = (1) x scalar(@q); + # Operators that the user can request break before or after. + # Note that some are keywords + @all_operators = qw(% + - * / x != == >= <= =~ !~ < > | & + = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x= + . : ? && || and or err xor + ); + # We can remove semicolons after blocks preceded by these keywords @q = qw(BEGIN END CHECK INIT AUTOLOAD DESTROY UNITCHECK continue if elsif else @@ -1230,11 +1239,6 @@ EOM } # implement user break preferences - my @all_operators = qw(% + - * / x != == >= <= =~ !~ < > | & - = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x= - . : ? && || and or err xor - ); - my $break_after = sub { my @toks = @_; foreach my $tok (@toks) { @@ -11162,7 +11166,12 @@ sub compare_indentation_levels { my %break_before_or_after_token; BEGIN { - my @q = qw( = . : ? and or xor && || ); + + # Updated to use all operators. This fixes case b1054 + # Here is the previous simplified version: + ## my @q = qw( . : ? and or xor && || ); + my @q = @all_operators; + push @q, ','; @break_before_or_after_token{@q} = (1) x scalar(@q); } @@ -11211,16 +11220,18 @@ sub compare_indentation_levels { return if ( $self->weld_len_right_to_go($i) ); my $token = $tokens_to_go[$i]; + my $type = $types_to_go[$i]; # For certain tokens, use user settings to decide if we break before or # after it - # qw( = . : ? and or xor && || ) - if ( $break_before_or_after_token{$token} ) { + if ( $break_before_or_after_token{$token} + && ( $type eq $token || $type eq 'k' ) ) + { if ( $want_break_before{$token} && $i >= 0 ) { $i-- } } # breaks are forced before 'if' and 'unless' - elsif ( $is_if_unless{$token} ) { $i-- } + elsif ( $is_if_unless{$token} && $type eq 'k' ) { $i-- } if ( $i >= 0 && $i <= $max_index_to_go ) { my $i_nonblank = ( $types_to_go[$i] ne 'b' ) ? $i : $i - 1; diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 9ce87071..6795b6be 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,27 @@ =over 4 +=item B + +Random testing produced some cases in which user requests to break before selected +operators were not being followed. For example + + # OLD: perltidy -wbb='.=' + $value .= + ( grep /\s/, ( $value, $next ) ) + ? " $next" + : $next; + + # FIXED: perltidy -wbb='.=' + $value + .= ( grep /\s/, ( $value, $next ) ) + ? " $next" + : $next; + +This fixes case b1054. + +28 Mar 2021. + =item B This is an correction to the update of 13 Mar 2021, 71adc77. Random testing @@ -13,7 +34,7 @@ This update fixes these old cases: b1021 b1023 and these new cases: b1034 b1048 b1049 b1050 b1056 b1058 -27 Mar 2021 +27 Mar 2021, cc94623. =item B