]> git.donarmstrong.com Git - perltidy.git/commitdiff
Fix unusual hanging side comment issue, c070
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 10 Sep 2021 22:12:57 +0000 (15:12 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 10 Sep 2021 22:12:57 +0000 (15:12 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index c0201f8e36f5bc8017fa50cce670baaa6e3a26d3..a3ca1d4865aa80fd9ed232f9ecfbbd78736cedd5 100644 (file)
@@ -5092,6 +5092,8 @@ sub set_CODE_type {
     my $In_format_skipping_section = 0;
     my $Saw_VERSION_in_this_file   = 0;
     my $has_side_comment           = 0;
+    my ( $Kfirst, $Klast );
+    my $CODE_type;
 
     ###############################
     # TASK 1: Loop to set CODE_type
@@ -5123,10 +5125,14 @@ sub set_CODE_type {
         $has_side_comment = 0;
 
         next unless ( $line_type eq 'CODE' );
+
+        my $Klast_prev = $Klast;
+
         my $rK_range = $line_of_tokens->{_rK_range};
-        my ( $Kfirst, $Klast ) = @{$rK_range};
+        ( $Kfirst, $Klast ) = @{$rK_range};
 
-        my $CODE_type = "";
+        my $last_CODE_type = $CODE_type;
+        $CODE_type = "";
 
         my $input_line = $line_of_tokens->{_line_text};
         my $jmax       = defined($Kfirst) ? $Klast - $Kfirst : -1;
@@ -5242,20 +5248,56 @@ sub set_CODE_type {
                 $is_static_block_comment = 1;
             }
 
-            # look for hanging side comment
+            # look for hanging side comment ...
             if (
                 $Last_line_had_side_comment    # last line had side comment
                 && !$no_leading_space          # there is some leading space
                 && !
                 $is_static_block_comment    # do not make static comment hanging
-                && $rOpts->{'hanging-side-comments'}    # user is allowing
-                                                        # hanging side comments
-                                                        # like this
               )
             {
-                $has_side_comment = 1;
-                $CODE_type        = 'HSC';
-                goto NEXT;
+
+                #  continuing an existing HSC chain?
+                if ( $last_CODE_type eq 'HSC' ) {
+                    $has_side_comment = 1;
+                    $CODE_type        = 'HSC';
+                    goto NEXT;
+                }
+
+                #  starting a new HSC chain?
+                elsif (
+
+                    $rOpts->{'hanging-side-comments'}    # user is allowing
+                                                         # hanging side comments
+                                                         # like this
+
+                    && ( defined($Klast_prev) && $Klast_prev > 1 )
+
+                    # and the previous side comment was not static (issue c070)
+                    && !(
+                           $rOpts->{'static-side-comments'}
+                        && $rLL->[$Klast_prev]->[_TOKEN_] =~
+                        /$static_side_comment_pattern/
+                    )
+
+                  )
+                {
+
+                    # and it is not a closing side comment (issue c070).
+                    my $K_penult = $Klast_prev - 1;
+                    $K_penult -= 1 if ( $rLL->[$K_penult]->[_TYPE_] eq 'b' );
+                    my $follows_csc =
+                      (      $rLL->[$K_penult]->[_TOKEN_] eq '}'
+                          && $rLL->[$K_penult]->[_TYPE_] eq '}'
+                          && $rLL->[$Klast_prev]->[_TOKEN_] =~
+                          /$closing_side_comment_prefix_pattern/ );
+
+                    if ( !$follows_csc ) {
+                        $has_side_comment = 1;
+                        $CODE_type        = 'HSC';
+                        goto NEXT;
+                    }
+                }
             }
 
             if ($is_static_block_comment) {
index 8f67aaeef127b6aa4cae0f492e0c0a18989f4ef0..470a1616e2c23e67ae136943c755be8a6f4ccc6e 100644 (file)
@@ -2,6 +2,44 @@
 
 =over 4
 
+=item B<Fix unusual hanging side comment issue, c070>
+
+This issues can be illustrated with the following input script:
+
+    {
+        if ($xxx) {
+          ...
+        } ## end if ($xxx ...
+        # b <filename>:<line> [<condition>]
+    }
+
+
+    # OLD: perltidy -fpsc=21
+    {
+        if ($xxx) {
+            ...;
+        } ## end if ($xxx ...
+                        # b <filename>:<line> [<condition>]
+    }
+
+The comment '# b ..' moved over to the column 21 to the right as if it were
+a side comment.  The reason is that it accidentally got marked as a hanging
+side comment.  It should not have been because the previous side comment was
+a closing side comment.  This update fixes this:
+
+    # NEW: perltidy -fpsc=21
+    {
+        if ($xxx) {
+            ...;
+        } ## end if ($xxx ...
+
+        # b <filename>:<line> [<condition>]
+    }
+
+This fixes issue c070.
+
+10 Sep 2021.
+
 =item B<Fix parsing issue c068>
 
 This issue is illustrated with the following line: