From ec6ccf9853a5bddde4467525c3699b0a5552a10e Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 10 Sep 2021 15:12:57 -0700 Subject: [PATCH] Fix unusual hanging side comment issue, c070 --- lib/Perl/Tidy/Formatter.pm | 60 ++++++++++++++++++++++++++++++++------ local-docs/BugLog.pod | 38 ++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index c0201f8e..a3ca1d48 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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) { diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 8f67aaee..470a1616 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,44 @@ =over 4 +=item B + +This issues can be illustrated with the following input script: + + { + if ($xxx) { + ... + } ## end if ($xxx ... + # b : [] + } + + + # OLD: perltidy -fpsc=21 + { + if ($xxx) { + ...; + } ## end if ($xxx ... + # b : [] + } + +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 : [] + } + +This fixes issue c070. + +10 Sep 2021. + =item B This issue is illustrated with the following line: -- 2.39.5