]> git.donarmstrong.com Git - perltidy.git/commitdiff
fix problem with some comments exceeding line length limit
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 3 Nov 2021 23:47:51 +0000 (16:47 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 3 Nov 2021 23:47:51 +0000 (16:47 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index be291f54b5c0da0aecf120b188c940d07323f4e1..5f30327db3f287d6a390f182c805df16f1ee0995 100644 (file)
@@ -7183,7 +7183,7 @@ sub is_in_block_by_i {
     #     or is at root level
     #     or there is some kind of error (i.e. unbalanced file)
     # returns false otherwise
-    return 1 if ( $i < 0 );  # shouldn't happen, bad call
+    return 1 if ( $i < 0 );    # shouldn't happen, bad call
     my $seqno = $parent_seqno_to_go[$i];
     return 1 if ( !$seqno || $seqno eq SEQ_ROOT );
     return 1 if ( $self->[_rblock_type_of_seqno_]->{$seqno} );
@@ -12717,25 +12717,44 @@ sub starting_one_line_block {
             # It would be possible to fix this by changing bond strengths,
             # but they are high to prevent errors in older versions of perl.
 
+            # TODO: See if the sort/map/grep check is still needed, and note
+            # that according to the above we should use $is_sort_map_grep_eval.
             if (   $Ki < $K_last
-                && $rLL->[$Ki_nonblank]->[_TYPE_] eq '#'
-                && !$is_sort_map_grep{$block_type} )
+                && $rLL->[$K_last]->[_TYPE_] eq '#'
+                && $rLL->[$K_last]->[_LEVEL_] == $rLL->[$Ki]->[_LEVEL_]
+                && !$rOpts_ignore_side_comment_lengths
+                && !$is_sort_map_grep{$block_type}
+                && $K_last - $Ki_nonblank <= 2 )
             {
+                # Only include the side comment for if/else/elsif/unless if it
+                # immediately follows (because the current '$rbrace_follower'
+                # logic for these will give an immediate brake after these
+                # closing braces).  So for example a line like this
+                #     if (...) { ... } ; # very long comment......
+                # will already break like this:
+                #     if (...) { ... }
+                #     ; # very long comment......
+                # so we do not need to include the length of the comment, which
+                # would break the block. Project 'bioperl' has coding like this.
+                if (   $block_type !~ /^(if|else|elsif|unless)$/
+                    || $K_last == $Ki_nonblank )
+                {
+                    $Ki_nonblank = $K_last;
+                    $pos += $rLL->[$Ki_nonblank]->[_TOKEN_LENGTH_];
 
-                $pos += $rLL->[$Ki_nonblank]->[_TOKEN_LENGTH_];
-
-                if ( $Ki_nonblank > $Ki + 1 ) {
+                    if ( $Ki_nonblank > $Ki + 1 ) {
 
-                    # source whitespace could be anything, assume
-                    # at least one space before the hash on output
-                    if ( $rLL->[ $Ki + 1 ]->[_TYPE_] eq 'b' ) {
-                        $pos += 1;
+                        # source whitespace could be anything, assume
+                        # at least one space before the hash on output
+                        if ( $rLL->[ $Ki + 1 ]->[_TYPE_] eq 'b' ) {
+                            $pos += 1;
+                        }
+                        else { $pos += $rLL->[ $Ki + 1 ]->[_TOKEN_LENGTH_] }
                     }
-                    else { $pos += $rLL->[ $Ki + 1 ]->[_TOKEN_LENGTH_] }
-                }
 
-                if ( $pos >= $maximum_line_length ) {
-                    return 0;
+                    if ( $pos >= $maximum_line_length ) {
+                        return 0;
+                    }
                 }
             }
 
index 3ec02ca68c4d4ee9d75b2e7203349ea7ed473146..c442a321e8a69c34d4b05d5dcbb67d7cd868a938 100644 (file)
@@ -1,3 +1,31 @@
+=head1 Issues fixed after release 20211029
+
+=over 4
+
+=item B<Some blocks with side comments exceed line length>
+
+In some rare cases, one-line blocks with side comments were exceeding the line
+length limit.  These usually had a semicolon between the closing block brace
+and the side comment.  For example:
+
+        my $size
+            = do { local $^W; -f $local && -s _ }; # no ALLO if sending data from a pipe
+
+This update breaks the one-line block in an attempt to keep the total length below
+the line length limit.  The result on the above is:
+
+        my $size = do {
+            local $^W;
+            -f $local && -s _;
+        };    # no ALLO if sending data from a pipe
+
+Note that this break can be prevented by including the flag
+--ignore-side-comment-lengths or -iscl.
+
+3 Nov 2021.
+
+=back
+
 =head1 Issues fixed after release 20210625
 
 =over 4