From: Steve Hancock Date: Tue, 2 Jun 2020 00:04:56 +0000 (-0700) Subject: fixed 2 issues with -kpit option (git #26) X-Git-Tag: 20200619~18 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=869a1942c0b4c23e6141743ab541fe6c8190876b;p=perltidy.git fixed 2 issues with -kpit option (git #26) --- diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 7a4cb880..7adf6f57 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -2222,6 +2222,30 @@ sub set_whitespace_flags { $ws = WS_NO; } } + elsif ( $type eq 'k' ) { + + # Keyword 'foreach' is a special case for the -kpit logic since the + # opening paren does not always immediately follow the keyword. So + # we have to search forward for the paren in this case. I have + # limited the search to 10 tokens ahead, just in case somebody + # has a big file and no opening paren. This should be enough for + # all normal code. + if ( $token eq 'foreach' + && %keyword_paren_inner_tightness + && defined( $keyword_paren_inner_tightness{$token} ) + && $j < $jmax ) + { + my $jp = $j; + for ( my $inc = 1 ; $inc < 10 ; $inc++ ) { + $jp++; + last if ( $jp > $jmax ); + next unless ( $rLL->[$jp]->[_TOKEN_] eq '(' ); + my $seqno = $rLL->[$jp]->[_TYPE_SEQUENCE_]; + $set_container_ws_by_keyword->( $token, $seqno ); + last; + } + } + } my $ws_4; $ws_4 = $ws @@ -9379,8 +9403,16 @@ sub pad_token { # token if ( $pad_spaces < 0 ) { + # Deactivated for -kpit due to conflict. This block deletes + # a space in an attempt to improve alignment in some cases, + # but it may conflict with user spacing requests. For now + # it is just deactivated if the -kpit option is used, to + # avoid changing existing formatting, but really it adds + # little value and could eventually be completely removed. if ( $pad_spaces == -1 ) { - if ( $ipad > $ibeg && $types_to_go[ $ipad - 1 ] eq 'b' ) + if ( $ipad > $ibeg + && $types_to_go[ $ipad - 1 ] eq 'b' + && !%keyword_paren_inner_tightness ) { $self->pad_token( $ipad - 1, $pad_spaces ); } diff --git a/t/snippets/expect/kpit.def b/t/snippets/expect/kpit.def index 8354af2e..0c628fef 100644 --- a/t/snippets/expect/kpit.def +++ b/t/snippets/expect/kpit.def @@ -1 +1,16 @@ if ( seek( DATA, 0, 0 ) ) { ... } + +# The foreach keyword may be separated from the next opening paren +foreach $req (@bgQueue) { + ...; +} + +# This had trouble because a later padding operation removed the inside space +while ($CmdJob eq "" && @CmdQueue > 0 && $RunNightlyWhenIdle != 1 + || @CmdQueue > 0 + && $RunNightlyWhenIdle == 2 + && $bpc->isAdminJob( $CmdQueue[0]->{host} ) ) +{ + ...; +} + diff --git a/t/snippets/expect/kpit.kpit b/t/snippets/expect/kpit.kpit index 5c640af3..fd96ddec 100644 --- a/t/snippets/expect/kpit.kpit +++ b/t/snippets/expect/kpit.kpit @@ -1 +1,16 @@ if ( seek(DATA, 0, 0) ) { ... } + +# The foreach keyword may be separated from the next opening paren +foreach $req ( @bgQueue ) { + ...; +} + +# This had trouble because a later padding operation removed the inside space +while ( $CmdJob eq "" && @CmdQueue > 0 && $RunNightlyWhenIdle != 1 + || @CmdQueue > 0 + && $RunNightlyWhenIdle == 2 + && $bpc->isAdminJob($CmdQueue[0]->{host}) ) +{ + ...; +} + diff --git a/t/snippets/kpit.in b/t/snippets/kpit.in index 5c640af3..e22d4e70 100644 --- a/t/snippets/kpit.in +++ b/t/snippets/kpit.in @@ -1 +1,13 @@ if ( seek(DATA, 0, 0) ) { ... } + +# The foreach keyword may be separated from the next opening paren +foreach $req(@bgQueue) { + ... +} + +# This had trouble because a later padding operation removed the inside space +while ($CmdJob eq "" && @CmdQueue > 0 && $RunNightlyWhenIdle != 1 + || @CmdQueue > 0 && $RunNightlyWhenIdle == 2 && $bpc->isAdminJob($CmdQueue[0]->{host})) { + ... +} + diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 6b456710..f68982d6 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -249,6 +249,7 @@ ../snippets20.t kpit.kpit ../snippets20.t kpitl.def ../snippets20.t kpitl.kpitl +../snippets20.t hanging_side_comments3.def ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -389,4 +390,3 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets20.t hanging_side_comments3.def diff --git a/t/snippets20.t b/t/snippets20.t index fe88ed0a..7d55f2a8 100644 --- a/t/snippets20.t +++ b/t/snippets20.t @@ -134,6 +134,18 @@ my $mapping = [ 'kpit' => <<'----------', if ( seek(DATA, 0, 0) ) { ... } + +# The foreach keyword may be separated from the next opening paren +foreach $req(@bgQueue) { + ... +} + +# This had trouble because a later padding operation removed the inside space +while ($CmdJob eq "" && @CmdQueue > 0 && $RunNightlyWhenIdle != 1 + || @CmdQueue > 0 && $RunNightlyWhenIdle == 2 && $bpc->isAdminJob($CmdQueue[0]->{host})) { + ... +} + ---------- 'kpitl' => <<'----------', @@ -501,6 +513,21 @@ my $mapping = [ params => "def", expect => <<'#14...........', if ( seek( DATA, 0, 0 ) ) { ... } + +# The foreach keyword may be separated from the next opening paren +foreach $req (@bgQueue) { + ...; +} + +# This had trouble because a later padding operation removed the inside space +while ($CmdJob eq "" && @CmdQueue > 0 && $RunNightlyWhenIdle != 1 + || @CmdQueue > 0 + && $RunNightlyWhenIdle == 2 + && $bpc->isAdminJob( $CmdQueue[0]->{host} ) ) +{ + ...; +} + #14........... }, @@ -509,6 +536,21 @@ if ( seek( DATA, 0, 0 ) ) { ... } params => "kpit", expect => <<'#15...........', if ( seek(DATA, 0, 0) ) { ... } + +# The foreach keyword may be separated from the next opening paren +foreach $req ( @bgQueue ) { + ...; +} + +# This had trouble because a later padding operation removed the inside space +while ( $CmdJob eq "" && @CmdQueue > 0 && $RunNightlyWhenIdle != 1 + || @CmdQueue > 0 + && $RunNightlyWhenIdle == 2 + && $bpc->isAdminJob($CmdQueue[0]->{host}) ) +{ + ...; +} + #15........... },