]> git.donarmstrong.com Git - perltidy.git/commitdiff
fix c269: -ame trouble with hanging side comment
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 15 Sep 2023 22:23:11 +0000 (15:23 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 15 Sep 2023 22:23:11 +0000 (15:23 -0700)
lib/Perl/Tidy/Formatter.pm
t/snippets/c269.in [new file with mode: 0644]
t/snippets/c269.par [new file with mode: 0644]
t/snippets/expect/c269.c269 [new file with mode: 0644]
t/snippets/expect/c269.def [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets28.t

index cd00f8473e2462a1d314cca9bbc1be2b6073bcf5..fc983f54f65b4e3c4a12d4a1f853e1ee1a7db29e 100644 (file)
@@ -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 (file)
index 0000000..bb1b6c2
--- /dev/null
@@ -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 (file)
index 0000000..fc81af5
--- /dev/null
@@ -0,0 +1 @@
+-ame
diff --git a/t/snippets/expect/c269.c269 b/t/snippets/expect/c269.c269
new file mode 100644 (file)
index 0000000..f0cc754
--- /dev/null
@@ -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 (file)
index 0000000..bb1b6c2
--- /dev/null
@@ -0,0 +1,11 @@
+if ($xxxxx) {
+    $file = "$xxxxx";
+}
+elsif ($yyyyyy) {
+    $file = "$yyyyy";
+}    # side comment
+     # hanging side comment
+elsif ($zzzzz) {
+
+    # comment
+}
index fed612932f433f649254224bec039df2b31fbfa5..de698c3a0bf2d3d993f28dfe751d45ee0edc2093 100644 (file)
 ../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
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets28.t        git124.def
+../snippets28.t        c269.c269
+../snippets28.t        c269.def
index 173bee127fbe77755ae2bcdf9442c8b1c204d5d8..38e8b95da1242ce59b9d26260b7dbc0f5a1f211a 100644 (file)
@@ -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};