From 00431bf3a82f8998d18d4ceac3e4b39b1a9e0c66 Mon Sep 17 00:00:00 2001
From: Steve Hancock <perltidy@users.sourceforge.net>
Date: Fri, 5 Mar 2021 08:39:58 -0800
Subject: [PATCH] Fix for issue git #53, do not align spaced function parens

---
 lib/Perl/Tidy/Formatter.pm | 40 +++++++++++++++++++++++++++-----------
 local-docs/BugLog.pod      | 26 ++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm
index 155762ee..6a57f1c2 100644
--- a/lib/Perl/Tidy/Formatter.pm
+++ b/lib/Perl/Tidy/Formatter.pm
@@ -5781,14 +5781,19 @@ sub respace_tokens {
         my $K_opening = $K_opening_container->{$seqno};
         next unless defined($K_opening);
 
-        # only for lists, not for code blocks
-        my $block_type = $rLL_new->[$K_opening]->[_BLOCK_TYPE_];
-        next if ($block_type);
-
         # code errors may leave undefined closing tokens
         my $K_closing = $K_closing_container->{$seqno};
         next unless defined($K_closing);
 
+        my $lx_open   = $rLL_new->[$K_opening]->[_LINE_INDEX_];
+        my $lx_close  = $rLL_new->[$K_closing]->[_LINE_INDEX_];
+        my $line_diff = $lx_close - $lx_open;
+        $ris_broken_container->{$seqno} = $line_diff;
+
+        # The rest is only for lists, not for code blocks
+        my $block_type = $rLL_new->[$K_opening]->[_BLOCK_TYPE_];
+        next if ($block_type);
+
         my $rtype_count = $rtype_count_by_seqno->{$seqno};
         next unless ($rtype_count);
         my $comma_count     = $rtype_count->{','};
@@ -5800,12 +5805,7 @@ sub respace_tokens {
         my $is_list = ( $comma_count || $fat_comma_count ) && !$semicolon_count;
         if ($is_list) { $ris_list_by_seqno->{$seqno} = $seqno }
 
-        my $lx_open   = $rLL_new->[$K_opening]->[_LINE_INDEX_];
-        my $lx_close  = $rLL_new->[$K_closing]->[_LINE_INDEX_];
-        my $line_diff = $lx_close - $lx_open;
-
         if ($line_diff) {
-            $ris_broken_container->{$seqno} = $line_diff;
             my $seqno_parent = $rparent_of_seqno->{$seqno};
             if ( defined($seqno_parent) && $seqno_parent ne SEQ_ROOT ) {
                 $rhas_broken_container->{$seqno_parent} = 1;
@@ -14959,10 +14959,12 @@ sub set_continuation_breaks {
                         # line difference is > 1 (see case b977)
                         if ($ok) {
                             my $seqno = $type_sequence_to_go[$i_line_start];
-                            if (   $ris_broken_container->{$seqno}
-                                && $ris_broken_container->{$seqno} <= 1 )
+                            if (  !$ris_broken_container->{$seqno}
+                                || $ris_broken_container->{$seqno} <= 1 )
                             {
                                 $ok = 0;
+                                print
+                                  "BOOGA, $ris_broken_container->{$seqno}\n";
                             }
                         }
 
@@ -18231,6 +18233,22 @@ sub send_lines_to_vertical_aligner {
                               unless $vert_last_nonblank_token =~
                               /^(if|unless|elsif)$/;
                         }
+
+                        # Do not align a spaced-function-paren - fixes git #53
+                        # Note that index $i-1 is a blank token if we get here
+                        if ( $i > $ibeg + 1 ) {
+                            my $type_m  = $types_to_go[ $i - 2 ];
+                            my $token_m = $tokens_to_go[ $i - 2 ];
+
+                            # this is the same test as 'space-function-paren'
+                            if (   $type_m =~ /^[wUG]$/
+                                || $type_m eq '->'
+                                || $type_m  =~ /^[wi]$/
+                                && $token_m =~ /^(\&|->)/ )
+                            {
+                                $alignment_type = "";
+                            }
+                        }
                     }
 
                     # be sure the alignment tokens are unique
diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod
index b503086f..23a63fcf 100644
--- a/local-docs/BugLog.pod
+++ b/local-docs/BugLog.pod
@@ -2,6 +2,30 @@
 
 =over 4
 
+=item B<Fix for issue git #53, do not align spaced function parens>
+
+Introducing a space before a function call paren had a side effect of
+allowing the vertical aligner to align the parens, as in the example.
+
+    # OLD and NEW, default without -sfp:
+    log_something_with_long_function( 'This is a log message.', 2 );
+    Coro::AnyEvent::sleep( 3, 4 );
+
+    # OLD: perltidy -sfp 
+    log_something_with_long_function ( 'This is a log message.', 2 );
+    Coro::AnyEvent::sleep            ( 3, 4 );
+
+    # NEW: perltidy -sfp 
+    log_something_with_long_function ( 'This is a log message.', 2 );
+    Coro::AnyEvent::sleep ( 3, 4 );
+
+This update changes the default to not do this vertical alignment.  This should
+have been the default but this side-effect was missed when the -sfp parameter
+was added.  Note that parens following keywords are likewise not vertically
+aligned.
+
+5 Mar 2021.
+
 =item B<Fix issue git#54 involving -bbp=n and -bbpi=n>
 
 In this issue, different results were obtained depending upon the existance of
@@ -11,7 +35,7 @@ requiring one or more commas to requiring either a fat comma or a comma.
 At the same time, a known problem involving the combination -lp -bbp=n -bbpi=n
 was fixed.  This fixes cases b826 b909 b989.
 
-4 Mar 2021.
+4 Mar 2021, 872d4b4.
 
 =item B<Fix several minor weld issues>
 
-- 
2.39.5