]> git.donarmstrong.com Git - perltidy.git/commitdiff
Partial fix for issue git #74 on -lp at anonymous subs
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 22 Sep 2021 22:30:34 +0000 (15:30 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 22 Sep 2021 22:30:34 +0000 (15:30 -0700)
bin/perltidy
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index 8a50fb446d57585200b73e454aa0eab98d3835dd..80784486b89f459a739859faa1fa00b4e3b5098b 100755 (executable)
@@ -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
index 8e49e94fd8e861b3ab4d010ffb44d478321ebb26..64d5bc242f3ec904dc35b71a4867c78dabeadd55 100644 (file)
@@ -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;
index 04bd61549e50b9daf3d774cdd9bcdd5fe978ee35..768c08e409213d55619a8d92b72368c3f77008bf 100644 (file)
@@ -2,6 +2,42 @@
 
 =over 4
 
+=item B<Partial fix for issue git #74 on -lp at anonymous subs>
+
+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<Fix issues b1209 related to -vmll>
 
 Testing with random parameters produced a formatting instability related to