From 605445c44d17dbae76e2ae04af4d9a7b37804594 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 3 Nov 2021 16:47:51 -0700 Subject: [PATCH] fix problem with some comments exceeding line length limit --- lib/Perl/Tidy/Formatter.pm | 47 ++++++++++++++++++++++++++------------ local-docs/BugLog.pod | 28 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index be291f54..5f30327d 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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; + } } } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 3ec02ca6..c442a321 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -1,3 +1,31 @@ +=head1 Issues fixed after release 20211029 + +=over 4 + +=item B + +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 -- 2.39.5