From bdc861938d5b45ca7f4a76fe5ed4172b4b42c969 Mon Sep 17 00:00:00 2001
From: Steve Hancock <perltidy@users.sourceforge.net>
Date: Wed, 20 Apr 2022 20:19:07 -0700
Subject: [PATCH] trim spaces in token split by line break (c139)

---
 lib/Perl/Tidy/Tokenizer.pm  | 27 +++++++++++++++++--
 t/snippets/c139.in          | 23 ++++++++++++++++
 t/snippets/expect/c139.def  | 18 +++++++++++++
 t/snippets/packing_list.txt |  1 +
 t/snippets26.t              | 52 +++++++++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 2 deletions(-)
 create mode 100644 t/snippets/c139.in
 create mode 100644 t/snippets/expect/c139.def

diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm
index 6a060b62..1925338f 100644
--- a/lib/Perl/Tidy/Tokenizer.pm
+++ b/lib/Perl/Tidy/Tokenizer.pm
@@ -7310,6 +7310,7 @@ sub scan_identifier_do {
 
                 $tok_is_blank = 1;
 
+                # note: an id with a leading '&' does not actually come this way
                 if ( $identifier =~ /^[\$\%\*\&\@]/ ) {
 
                     if ( length($identifier) > 1 ) {
@@ -7320,6 +7321,13 @@ sub scan_identifier_do {
                     }
                     else {
 
+                        # fix c139: trim line-ending type 't'
+                        if ( $i == $max_token_index ) {
+                            $i    = $i_save;
+                            $type = 't';
+                            last;
+                        }
+
                         # spaces after $'s are common, and space after @
                         # is harmless, so only complain about space
                         # after other type characters. Space after $ and
@@ -7335,8 +7343,16 @@ sub scan_identifier_do {
                     }
                 }
 
-                # else:
-                # space after '->' is ok
+                elsif ( $identifier eq '->' ) {
+
+                    # space after '->' is ok except at line end ..
+                    # so trim line-ending in type '->' (fixes c139)
+                    if ( $i == $max_token_index ) {
+                        $i    = $i_save;
+                        $type = '->';
+                        last;
+                    }
+                }
             }
             elsif ( $tok eq '^' ) {
 
@@ -7595,6 +7611,13 @@ sub scan_identifier_do {
             }
             elsif ( $tok =~ /^\s*$/ ) {               # allow space
                 $tok_is_blank = 1;
+
+                # fix c139: trim line-ending type 't'
+                if ( length($identifier) == 1 && $i == $max_token_index ) {
+                    $i    = $i_save;
+                    $type = 't';
+                    last;
+                }
             }
             elsif ( $tok eq '::' ) {                  # leading ::
                 $id_scan_state = 'A';                 # accept alpha next
diff --git a/t/snippets/c139.in b/t/snippets/c139.in
new file mode 100644
index 00000000..bba5d5f2
--- /dev/null
+++ b/t/snippets/c139.in
@@ -0,0 +1,23 @@
+# The '&' has trailing spaces
+@l = &    
+_  
+( -49, -71 );
+
+# This '$' has trailing spaces
+my $    
+b = 40;
+
+# this arrow has trailing spaces
+$r = $c->         
+sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
+
+# spaces and blank line
+@l = &    
+
+_  
+( -49, -71 );
+
+# spaces and blank line
+$r = $c->         
+
+sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
diff --git a/t/snippets/expect/c139.def b/t/snippets/expect/c139.def
new file mode 100644
index 00000000..df2c2056
--- /dev/null
+++ b/t/snippets/expect/c139.def
@@ -0,0 +1,18 @@
+# The '&' has trailing spaces
+@l = &_( -49, -71 );
+
+# This '$' has trailing spaces
+my $b = 40;
+
+# this arrow has trailing spaces
+$r = $c->sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
+
+# spaces and blank line
+@l = &
+
+  _( -49, -71 );
+
+# spaces and blank line
+$r = $c->
+
+  sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt
index 8fd1b7f5..cdd9257a 100644
--- a/t/snippets/packing_list.txt
+++ b/t/snippets/packing_list.txt
@@ -493,3 +493,4 @@
 ../snippets9.t	rt98902.def
 ../snippets9.t	rt98902.rt98902
 ../snippets9.t	rt99961.def
+../snippets26.t	c139.def
diff --git a/t/snippets26.t b/t/snippets26.t
index 88aa7523..5cf8a6cb 100644
--- a/t/snippets26.t
+++ b/t/snippets26.t
@@ -8,6 +8,7 @@
 #5 c133.def
 #6 git93.def
 #7 git93.git93
+#8 c139.def
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -70,6 +71,32 @@ return
   $x * sin($a) + $y * cos($a);
 ----------
 
+        'c139' => <<'----------',
+# The '&' has trailing spaces
+@l = &    
+_  
+( -49, -71 );
+
+# This '$' has trailing spaces
+my $    
+b = 40;
+
+# this arrow has trailing spaces
+$r = $c->         
+sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
+
+# spaces and blank line
+@l = &    
+
+_  
+( -49, -71 );
+
+# spaces and blank line
+$r = $c->         
+
+sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
+----------
+
         'git93' => <<'----------',
 use Cwd qw[cwd];
 use Carp qw(carp);
@@ -353,6 +380,31 @@ use vars qw($curdir);
 no strict qw(vars);
 #7...........
         },
+
+        'c139.def' => {
+            source => "c139",
+            params => "def",
+            expect => <<'#8...........',
+# The '&' has trailing spaces
+@l = &_( -49, -71 );
+
+# This '$' has trailing spaces
+my $b = 40;
+
+# this arrow has trailing spaces
+$r = $c->sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
+
+# spaces and blank line
+@l = &
+
+  _( -49, -71 );
+
+# spaces and blank line
+$r = $c->
+
+  sql_set_env_attr( $evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0 );
+#8...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};
-- 
2.39.5