From: Steve Hancock Date: Mon, 23 Nov 2020 20:35:19 +0000 (-0800) Subject: fix to prevent block comments from becoming hanging side comments X-Git-Tag: 20201202~21 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=2eb3de1627a1269dbd222eb47051aa28f3d14a83;p=perltidy.git fix to prevent block comments from becoming hanging side comments --- diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 5364ae16..afa2b0f9 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -148,6 +148,7 @@ my ( $rOpts_block_brace_tightness, $rOpts_block_brace_vertical_tightness, $rOpts_stack_closing_block_brace, + $rOpts_maximum_consecutive_blank_lines, $rOpts_recombine, $rOpts_add_newlines, @@ -1434,8 +1435,10 @@ EOM $rOpts_block_brace_vertical_tightness = $rOpts->{'block-brace-vertical-tightness'}; $rOpts_stack_closing_block_brace = $rOpts->{'stack-closing-block-brace'}; - $rOpts_recombine = $rOpts->{'recombine'}; - $rOpts_add_newlines = $rOpts->{'add-newlines'}; + $rOpts_maximum_consecutive_blank_lines = + $rOpts->{'maximum-consecutive-blank-lines'}; + $rOpts_recombine = $rOpts->{'recombine'}; + $rOpts_add_newlines = $rOpts->{'add-newlines'}; $rOpts_break_at_old_comma_breakpoints = $rOpts->{'break-at-old-comma-breakpoints'}; $rOpts_ignore_old_breakpoints = $rOpts->{'ignore-old-breakpoints'}; @@ -4426,15 +4429,20 @@ sub dump_verbatim { # extract what we need for this line.. my $input_line_number = $line_of_tokens->{_line_number}; - - my $rK_range = $line_of_tokens->{_rK_range}; + my $input_line = $line_of_tokens->{_line_text}; + my $rK_range = $line_of_tokens->{_rK_range}; my ( $Kfirst, $Klast ) = @{$rK_range}; - my $jmax = -1; - if ( defined($Kfirst) ) { $jmax = $Klast - $Kfirst } - my $input_line = $line_of_tokens->{_line_text}; + my $jmax = defined($Kfirst) ? $Klast - $Kfirst : -1; + my $is_block_comment = 0; + my $has_side_comment = 0; my $is_static_block_comment = 0; + if ( $jmax >= 0 && $rLL->[$Klast]->[_TYPE_] eq '#' ) { + if ( $jmax == 0 ) { $is_block_comment = 1; } + else { $has_side_comment = 1 } + } + # Handle a continued quote.. if ( $line_of_tokens->{_starting_in_quote} ) { @@ -4444,17 +4452,13 @@ sub dump_verbatim { if ( ( $input_line =~ "\t" ) ) { $self->note_embedded_tab($input_line_number); } - $Last_line_had_side_comment = 0; - return 'VB'; + $CODE_type = 'VB'; + goto RETURN; } } - my $is_block_comment = - ( $jmax == 0 && $rLL->[$Kfirst]->[_TYPE_] eq '#' ); - # Write line verbatim if we are in a formatting skip section if ($In_format_skipping_section) { - $Last_line_had_side_comment = 0; # Note: extra space appended to comment simplifies pattern matching if ( $is_block_comment @@ -4464,7 +4468,8 @@ sub dump_verbatim { $In_format_skipping_section = 0; write_logfile_entry("Exiting formatting skip section\n"); } - return 'FS'; + $CODE_type = 'FS'; + goto RETURN; } # See if we are entering a formatting skip section @@ -4475,8 +4480,8 @@ sub dump_verbatim { { $In_format_skipping_section = 1; write_logfile_entry("Entering formatting skip section\n"); - $Last_line_had_side_comment = 0; - return 'FS'; + $CODE_type = 'FS'; + goto RETURN; } # ignore trailing blank tokens (they will get deleted later) @@ -4486,8 +4491,8 @@ sub dump_verbatim { # Handle a blank line.. if ( $jmax < 0 ) { - $Last_line_had_side_comment = 0; - return 'BL'; + $CODE_type = 'BL'; + goto RETURN; } # see if this is a static block comment (starts with ## by default) @@ -4528,25 +4533,36 @@ sub dump_verbatim { # like this ) { - $Last_line_had_side_comment = 1; - return 'HSC'; + $has_side_comment = 1; + $CODE_type = 'HSC'; + goto RETURN; } - # remember if this line has a side comment - $Last_line_had_side_comment = - ( $jmax > 0 && $rLL->[$Klast]->[_TYPE_] eq '#' ); - # Handle a block (full-line) comment.. if ($is_block_comment) { if ($is_static_block_comment_without_leading_space) { - return 'SBCX'; + $CODE_type = 'SBCX'; + goto RETURN; } elsif ($is_static_block_comment) { - return 'SBC'; + $CODE_type = 'SBC'; + goto RETURN; + } + elsif ($Last_line_had_side_comment + && !$rOpts_maximum_consecutive_blank_lines + && $rLL->[$Kfirst]->[_LEVEL_] > 0 ) + { + # Emergency fix to keep a block comment from becoming a hanging + # side comment. This fix is for the case that blank lines + # cannot be inserted. There is related code in sub + # 'process_line_of_CODE' + $CODE_type = 'SBCX'; + goto RETURN; } else { - return 'BC'; + $CODE_type = 'BC'; + goto RETURN; } } @@ -4582,7 +4598,11 @@ sub dump_verbatim { # This code type has lower priority than others $CODE_type = 'VER' unless ($CODE_type); + goto RETURN; } + + RETURN: + $Last_line_had_side_comment = $has_side_comment; return $CODE_type; } } ## end closure scan_comments @@ -8645,13 +8665,13 @@ EOM # only if allowed && $rOpts->{'blanks-before-comments'} - # if this is NOT an empty comment - && ( $rtok_first->[_TOKEN_] ne '#' - - # FIXME: FUTURE UPDATE; still needs to be coordinated with user parameters - # unless following a side comment (otherwise need to insert - # blank to prevent creating a hanging side comment) - ) #|| $last_line_had_side_comment ) + # if this is NOT an empty comment, unless it follows a side + # comment and could become a hanging side comment. + && ( + $rtok_first->[_TOKEN_] ne '#' + || ( $last_line_had_side_comment + && $rLL->[$K_first]->[_LEVEL_] > 0 ) + ) # not after a short line ending in an opening token # because we already have space above this comment. @@ -8683,11 +8703,11 @@ EOM else { # switching to new output stream - $self->flush(); - + $self->flush(); + # Note that last arg in call here is 'undef' for comments $file_writer_object->write_code_line( - $rtok_first->[_TOKEN_] . "\n", undef ); + $rtok_first->[_TOKEN_] . "\n", undef ); $self->[_last_line_leading_type_] = '#'; } return; diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 17eb906d..0cd7d207 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,34 @@ =over 4 +=item B + +A rare situation was identified during testing in which a block comment could +be converted to be a hanging side comment. For example: + + sub macro_get_names { # + # + # %name = macro_get_names(); (key=macrohandle, value=macroname) + # + local (%name) = (); + ... + } + +For the following specific contitions the block comment in line 2 could be +converted into a hanging side comment, which is undesirable: + + 1. It contains nothing except for a '#' + 2. It follows a line with side comment + 3. It has indentation level > 0 + +An update was made to prevent this from happening. There are two cases, +depending on the value of --maximum-consecutive-blank-lines, or -mbl. If this +value is positive (the default) then a blank line is inserted above the block +comment to prevent it from becoming a hanging side comment. If this -mbl is +zero, then the comment is converted to be a static block comment which again +prevents it from becoming a hanging side comment. This fix was made 23 Nov +2020. + =item B A better test for convergence has been added. When iterations are requested, @@ -9,7 +37,7 @@ the new test will stop after the first pass if no changes in line break locations are made. Previously, at least two passes were required to verify convergnece unless the output stream had the same checksum as the input stream. Extensive testing has been made to verify the correctness of the new test. -This update was made 23 Nov 2020. +This update was made 23 Nov 2020, 29efb63. =item B diff --git a/t/snippets/comments.in b/t/snippets/comments.in index 7c0deb71..a8e39452 100644 --- a/t/snippets/comments.in +++ b/t/snippets/comments.in @@ -4,7 +4,7 @@ sub length { return length($_[0]) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # # # %name = macro_get_names(); (key=macrohandle, value=macroname) diff --git a/t/snippets/expect/comments.comments1 b/t/snippets/expect/comments.comments1 index a53e5e62..fbe3ff02 100644 --- a/t/snippets/expect/comments.comments1 +++ b/t/snippets/expect/comments.comments1 @@ -5,8 +5,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # diff --git a/t/snippets/expect/comments.comments3 b/t/snippets/expect/comments.comments3 index ec1cc230..077d0818 100644 --- a/t/snippets/expect/comments.comments3 +++ b/t/snippets/expect/comments.comments3 @@ -4,8 +4,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # diff --git a/t/snippets/expect/comments.comments4 b/t/snippets/expect/comments.comments4 index e862a51f..58638d2b 100644 --- a/t/snippets/expect/comments.comments4 +++ b/t/snippets/expect/comments.comments4 @@ -4,8 +4,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # diff --git a/t/snippets/expect/comments.comments5 b/t/snippets/expect/comments.comments5 index 158df431..9d6a92ef 100644 --- a/t/snippets/expect/comments.comments5 +++ b/t/snippets/expect/comments.comments5 @@ -2,7 +2,7 @@ # an initial hash bang line cannot be deleted with -dp sub length { return length( $_[0] ) } -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # # %name = macro_get_names(); (key=macrohandle, value=macroname) diff --git a/t/snippets/expect/comments.def b/t/snippets/expect/comments.def index 09d57974..58ffcdf7 100644 --- a/t/snippets/expect/comments.def +++ b/t/snippets/expect/comments.def @@ -4,8 +4,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 12dc2604..385985a8 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -304,6 +304,7 @@ ../snippets23.t wnxl.wnxl2 ../snippets23.t wnxl.wnxl3 ../snippets23.t wnxl.wnxl4 +../snippets23.t align34.def ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -444,4 +445,3 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets23.t align34.def diff --git a/t/snippets17.t b/t/snippets17.t index 49c0874b..318c1904 100644 --- a/t/snippets17.t +++ b/t/snippets17.t @@ -94,7 +94,7 @@ sub length { return length($_[0]) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # # # %name = macro_get_names(); (key=macrohandle, value=macroname) @@ -346,8 +346,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # @@ -500,8 +501,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # @@ -592,8 +594,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # @@ -693,8 +696,9 @@ sub length { return length( $_[0] ) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # + # # %name = macro_get_names(); (key=macrohandle, value=macroname) # diff --git a/t/snippets18.t b/t/snippets18.t index cd986388..0ff63111 100644 --- a/t/snippets18.t +++ b/t/snippets18.t @@ -123,7 +123,7 @@ sub length { return length($_[0]) } # side comment # hanging side comment # very longgggggggggggggggggggggggggggggggggggggggggggggggggggg hanging side comment -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # # # %name = macro_get_names(); (key=macrohandle, value=macroname) @@ -359,7 +359,7 @@ my ( $a, $b, $c ) = @_; # test -nsak="my for" # an initial hash bang line cannot be deleted with -dp sub length { return length( $_[0] ) } -# side comments following open brace are not currently treated as hanging side comments +# a blank will be inserted to prevent forming a hanging side comment sub macro_get_names { # # %name = macro_get_names(); (key=macrohandle, value=macroname)