From: Steve Hancock Date: Wed, 22 Sep 2021 22:30:34 +0000 (-0700) Subject: Partial fix for issue git #74 on -lp at anonymous subs X-Git-Tag: 20210717.04~2 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=4fd58f74cdb935b1495fa26e559a2d89599e0a4c;p=perltidy.git Partial fix for issue git #74 on -lp at anonymous subs --- diff --git a/bin/perltidy b/bin/perltidy index 8a50fb44..80784486 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -810,6 +810,9 @@ In situations where perltidy does not have complete freedom to choose line breaks it may temporarily revert to its default indentation method. This can occur for example if there are blank lines, block comments, multi-line quotes, or side comments between the opening and closing parens, braces, or brackets. +It will also occur if a multi-line anonymous sub occurs within a container +since that will impose specific line breaks (such as line breaks +after statements). In addition, any parameter which significantly restricts the ability of perltidy to choose newlines will conflict with B<-lp> and will cause diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 8e49e94f..64d5bc24 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -11302,6 +11302,9 @@ EOM $rdepth_of_opening_seqno = $self->[_rdepth_of_opening_seqno_]; $rblock_type_of_seqno = $self->[_rblock_type_of_seqno_]; + my $K_closing_container = $self->[_K_closing_container_]; + my $K_opening_container = $self->[_K_opening_container_]; + my $file_writer_object = $self->[_file_writer_object_]; my $rbreak_container = $self->[_rbreak_container_]; my $rshort_nested = $self->[_rshort_nested_]; @@ -11821,7 +11824,36 @@ EOM elsif ( $block_type =~ /$ASUB_PATTERN/ ) { if ($is_one_line_block) { + $rbrace_follower = \%is_anon_sub_1_brace_follower; + + # Exceptions to help keep -lp intact, see git #74 ... + # Exception 1: followed by '}' on this line + if ( $Ktoken_vars < $K_last + && $next_nonblank_token eq '}' ) + { + $rbrace_follower = undef; + $keep_going = 1; + } + + # Exception 2: followed by '}' on next line + elsif ( $Ktoken_vars == $K_last ) { + + my $p_seqno = $parent_seqno_to_go[$max_index_to_go]; + my $Kc = $K_closing_container->{$p_seqno}; + if ( defined($Kc) + && $rLL->[$Kc]->[_TOKEN_] eq '}' + && $Kc - $Ktoken_vars <= 2 ) + { + $rbrace_follower = undef; + $keep_going = 1; + + # Keep the break if container is fully broken + my $Ko = $K_opening_container->{$p_seqno}; + $self->set_forced_breakpoint($max_index_to_go) + if ( $Ko < $K_first ); + } + } } else { $rbrace_follower = \%is_anon_sub_brace_follower; diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 04bd6154..768c08e4 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,42 @@ =over 4 +=item B + +In the following snippet, the final one-line anonymous sub is not followed by a +comma. This caused the -lp mode to revert to standard indentation mode because +a forced line break was being made after the closing sub brace: + + # OLD, perltidy -lp + $got = timethese( + $iterations, + { + Foo => sub { ++$foo }, + Bar => '++$bar', + Baz => sub { ++$baz } + } + ); + +An update was made to check for and fix this. + + # NEW, perltidy -lp + $got = timethese( + $iterations, + { + Foo => sub { ++$foo }, + Bar => '++$bar', + Baz => sub { ++$baz } + } + ); + +But note that this only applies to one-line anonymous subs. If an anonymous +sub is broken into several lines due to its length or complexity, then +these forced line breaks cause indentation to revert to the standard +indentation scheme. + +22 Sep 2021. + + =item B Testing with random parameters produced a formatting instability related to