From 74e3fa07502d6fa37b978cdf234aead1c9af1e3a Mon Sep 17 00:00:00 2001
From: Steve Hancock <perltidy@users.sourceforge.net>
Date: Fri, 15 Sep 2023 15:23:11 -0700
Subject: [PATCH] fix c269: -ame trouble with hanging side comment

---
 lib/Perl/Tidy/Formatter.pm  | 26 ++++++++++++++---
 t/snippets/c269.in          | 11 ++++++++
 t/snippets/c269.par         |  1 +
 t/snippets/expect/c269.c269 | 14 ++++++++++
 t/snippets/expect/c269.def  | 11 ++++++++
 t/snippets/packing_list.txt |  4 ++-
 t/snippets28.t              | 56 +++++++++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 5 deletions(-)
 create mode 100644 t/snippets/c269.in
 create mode 100644 t/snippets/c269.par
 create mode 100644 t/snippets/expect/c269.c269
 create mode 100644 t/snippets/expect/c269.def

diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm
index cd00f847..fc983f54 100644
--- a/lib/Perl/Tidy/Formatter.pm
+++ b/lib/Perl/Tidy/Formatter.pm
@@ -10481,8 +10481,15 @@ sub K_next_code {
             Fault("Undefined entry for k=$Knnb") if (DEVEL_MODE);
             return;
         }
-        if (   $rLL->[$Knnb]->[_TYPE_] ne 'b'
-            && $rLL->[$Knnb]->[_TYPE_] ne '#' )
+        my $type = $rLL->[$Knnb]->[_TYPE_];
+        if (
+               $type ne 'b'
+            && $type ne '#'
+
+            # c269: a zero-length q is a blank before hanging side comment
+            && ( $type ne 'q'
+                || length( $rLL->[$Knnb]->[_TOKEN_] ) )
+          )
         {
             return $Knnb;
         }
@@ -10496,6 +10503,8 @@ sub K_next_nonblank {
 
     # return the index K of the next nonblank token, or
     # return undef if none
+    # NOTE: does not skip over the leading type 'q' of a hanging side comment
+    # (use K_next_code)
     return if ( !defined($KK) );
     return if ( $KK < 0 );
 
@@ -10554,8 +10563,15 @@ sub K_previous_code {
     }
     my $Kpnb = $KK - 1;
     while ( $Kpnb >= 0 ) {
-        if (   $rLL->[$Kpnb]->[_TYPE_] ne 'b'
-            && $rLL->[$Kpnb]->[_TYPE_] ne '#' )
+        my $type = $rLL->[$Kpnb]->[_TYPE_];
+        if (
+               $type ne 'b'
+            && $type ne '#'
+
+            # c269: a zero-length q is a blank before hanging side comment
+            && ( $type ne 'q'
+                || length( $rLL->[$Kpnb]->[_TOKEN_] ) )
+          )
         {
             return $Kpnb;
         }
@@ -10568,6 +10584,8 @@ sub K_previous_nonblank {
 
     # return index of previous nonblank token before item K;
     # Call with $KK=undef to start search at the top of the array
+    # NOTE: does not skip over the leading type 'q' of a hanging side comment
+    # (use K_previous_code)
     my ( $self, $KK, $rLL ) = @_;
 
     # use the standard array unless given otherwise
diff --git a/t/snippets/c269.in b/t/snippets/c269.in
new file mode 100644
index 00000000..bb1b6c24
--- /dev/null
+++ b/t/snippets/c269.in
@@ -0,0 +1,11 @@
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
diff --git a/t/snippets/c269.par b/t/snippets/c269.par
new file mode 100644
index 00000000..fc81af5e
--- /dev/null
+++ b/t/snippets/c269.par
@@ -0,0 +1 @@
+-ame
diff --git a/t/snippets/expect/c269.c269 b/t/snippets/expect/c269.c269
new file mode 100644
index 00000000..f0cc7546
--- /dev/null
+++ b/t/snippets/expect/c269.c269
@@ -0,0 +1,14 @@
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
+else {
+    ##FIXME - added with perltidy -ame
+}
diff --git a/t/snippets/expect/c269.def b/t/snippets/expect/c269.def
new file mode 100644
index 00000000..bb1b6c24
--- /dev/null
+++ b/t/snippets/expect/c269.def
@@ -0,0 +1,11 @@
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt
index fed61293..de698c3a 100644
--- a/t/snippets/packing_list.txt
+++ b/t/snippets/packing_list.txt
@@ -399,6 +399,7 @@
 ../snippets28.t	lrt.lrt
 ../snippets28.t	ame.ame
 ../snippets28.t	ame.def
+../snippets28.t	git124.def
 ../snippets3.t	ce_wn1.ce_wn
 ../snippets3.t	ce_wn1.def
 ../snippets3.t	colin.colin
@@ -539,4 +540,5 @@
 ../snippets9.t	rt98902.def
 ../snippets9.t	rt98902.rt98902
 ../snippets9.t	rt99961.def
-../snippets28.t	git124.def
+../snippets28.t	c269.c269
+../snippets28.t	c269.def
diff --git a/t/snippets28.t b/t/snippets28.t
index 173bee12..38e8b95d 100644
--- a/t/snippets28.t
+++ b/t/snippets28.t
@@ -17,6 +17,8 @@
 #14 ame.ame
 #15 ame.def
 #16 git124.def
+#17 c269.c269
+#18 c269.def
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -35,6 +37,7 @@ BEGIN {
     ###########################################
     $rparams = {
         'ame'    => "--add-missing-else",
+        'c269'   => "-ame",
         'def'    => "",
         'git116' => "-viu",
         'lrt'    => "--line-range-tidy=2:3",
@@ -58,6 +61,20 @@ BEGIN {
     elsif ( $level == 2 ) { $val = $global{'chapter'} }
 ----------
 
+        'c269' => <<'----------',
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
+----------
+
         'git116' => <<'----------',
 print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet );
 print "Would need: @DepList\n" if ( @DepList and !$Quiet );
@@ -392,6 +409,45 @@ sub git124 {
 }
 #16...........
         },
+
+        'c269.c269' => {
+            source => "c269",
+            params => "c269",
+            expect => <<'#17...........',
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
+else {
+    ##FIXME - added with perltidy -ame
+}
+#17...........
+        },
+
+        'c269.def' => {
+            source => "c269",
+            params => "def",
+            expect => <<'#18...........',
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
+#18...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};
-- 
2.39.5