From: Steve Hancock Date: Wed, 18 Dec 2019 00:37:13 +0000 (-0800) Subject: reworked coding which removes extra semicolons to use new data structures X-Git-Tag: 20200110~17 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=9cda893c99d9a07fed68eb80a19e2bbe367c4ff6;p=perltidy.git reworked coding which removes extra semicolons to use new data structures --- diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 3024baf8..d1b1e320 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -2389,8 +2389,10 @@ sub respace_tokens { # A sub to store one token in the new array # All new tokens must be stored by this sub so that it can update # all data structures on the fly. - my $last_nonblank_type = ';'; - my $store_token = sub { + my $last_nonblank_type = ';'; + my $last_nonblank_token = ';'; + my $last_nonblank_block_type = ''; + my $store_token = sub { my ($item) = @_; # This will be the index of this item in the new array @@ -2437,7 +2439,11 @@ sub respace_tokens { $item->[_CUMULATIVE_LENGTH_] = $cumulative_length; my $type = $item->[_TYPE_]; - if ( $type ne 'b' ) { $last_nonblank_type = $type } + if ( $type && $type ne 'b' && $type ne '#' ) { + $last_nonblank_type = $type; + $last_nonblank_token = $item->[_TOKEN_]; + $last_nonblank_block_type = $item->[_BLOCK_TYPE_]; + } # and finally, add this item to the new array push @{$rLL_new}, $item; @@ -2997,6 +3003,52 @@ sub respace_tokens { # $check_Q->($KK, $Kfirst); } + # handle semicolons + elsif ( $type eq ';' ) { + + # Remove unnecessary semicolons, but not after bare + # blocks, where it could be unsafe if the brace is + # mistokenized. + if ( + $rOpts->{'delete-semicolons'} + && ( + ( + $last_nonblank_type eq '}' + && ( + $is_block_without_semicolon{ + $last_nonblank_block_type} + || $last_nonblank_block_type =~ /$SUB_PATTERN/ + || $last_nonblank_block_type =~ /^\w+:$/ ) + ) + || $last_nonblank_type eq ';' + ) + ) + { + + my $has_side_comment; + if ( $KK < $Klast ) { + my $Kn = $self->K_next_nonblank($KK); + my $next_nonblank_token_type = ""; + if ( defined($Kn) && $Kn <= $Klast ) { + $next_nonblank_token_type = $rLL->[$Kn]->[_TYPE_]; + $has_side_comment = + $next_nonblank_token_type eq '#'; + } + } + + # don't delete ; before a # because it would promote it + # to a block comment + if ( !$has_side_comment ) { + note_deleted_semicolon(); + next; + } + else { + write_logfile_entry( + "Extra ';' at line $input_line_number\n"); + } + } + } + elsif ($type_sequence) { # if ( $is_opening_token{$token} ) { @@ -4624,6 +4676,15 @@ sub resync_lines_and_tokens { $line_of_tokens->{_line_text} =~ s/\s+$//; } $line_of_tokens->{_rK_range} = [ $Kfirst, $Klast ]; + + # Deleting semicolons can create new empty code lines + # which should be marked as blank + if ( !defined($Kfirst) ) { + my $code_type = $line_of_tokens->{_code_type}; + if ( !$code_type ) { + $line_of_tokens->{_code_type} = 'BL'; + } + } } } @@ -5526,7 +5587,7 @@ sub wrapup { write_logfile_entry( " Last at input line $last_deleted_semicolon_at\n"); } - write_logfile_entry(" (Use -ndsc to prevent semicolon deletion)\n"); + write_logfile_entry(" (Use -ndsm to prevent semicolon deletion)\n"); write_logfile_entry("\n"); } @@ -7723,40 +7784,6 @@ EOM destroy_one_line_block(); } - # Remove unnecessary semicolons, but not after bare - # blocks, where it could be unsafe if the brace is - # mistokenized. - if ( - ( - $last_nonblank_token eq '}' - && ( - $is_block_without_semicolon{ - $last_nonblank_block_type} - || $last_nonblank_block_type =~ /$SUB_PATTERN/ - || $last_nonblank_block_type =~ /^\w+:$/ ) - ) - || $last_nonblank_type eq ';' - ) - { - - if ( - $rOpts->{'delete-semicolons'} - - # don't delete ; before a # because it would promote it - # to a block comment - && ( $next_nonblank_token_type ne '#' ) - ) - { - note_deleted_semicolon(); - $self->output_line_to_go() - unless ( $no_internal_newlines - || $index_start_one_line_block != UNDEFINED_INDEX ); - next; - } - else { - write_logfile_entry("Extra ';'\n"); - } - } $self->store_token_to_go(); $self->output_line_to_go() @@ -8164,7 +8191,7 @@ sub note_deleted_semicolon { $first_deleted_semicolon_at = $last_deleted_semicolon_at; } $deleted_semicolon_count++; - write_logfile_entry("Deleted unnecessary ';'\n"); # i hope ;) + write_logfile_entry("Deleted unnecessary ';' at line $input_line_number\n"); return; } diff --git a/t/snippets/expect/ndsm1.def b/t/snippets/expect/ndsm1.def new file mode 100644 index 00000000..c36b944f --- /dev/null +++ b/t/snippets/expect/ndsm1.def @@ -0,0 +1,8 @@ +; # 1 trapped semicolon +sub numerically { $a <=> $b } + +sub Numerically { $a <=> $b }; # trapped semicolon +@: = qw;2c72656b636168 + 2020202020 + ;; +__; diff --git a/t/snippets/expect/ndsm1.ndsm b/t/snippets/expect/ndsm1.ndsm new file mode 100644 index 00000000..d5e53dd9 --- /dev/null +++ b/t/snippets/expect/ndsm1.ndsm @@ -0,0 +1,16 @@ +; +; +; +; +; # 1 trapped semicolon +sub numerically { $a <=> $b }; +; +; +; +; +; +sub Numerically { $a <=> $b }; # trapped semicolon +@: = qw;2c72656b636168 + 2020202020 + ;; +__; diff --git a/t/snippets/ndsm.par b/t/snippets/ndsm.par new file mode 100644 index 00000000..736d0a65 --- /dev/null +++ b/t/snippets/ndsm.par @@ -0,0 +1 @@ +-ndsm diff --git a/t/snippets/ndsm1.in b/t/snippets/ndsm1.in new file mode 100644 index 00000000..35b21756 --- /dev/null +++ b/t/snippets/ndsm1.in @@ -0,0 +1,7 @@ +;;;;; # 1 trapped semicolon +sub numerically {$a <=> $b}; +;;;;; +sub Numerically {$a <=> $b}; # trapped semicolon +@: = qw;2c72656b636168 + 2020202020 + ;; __; diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index c2148c71..5ab06fd8 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -147,6 +147,8 @@ ../snippets16.t almost2.def ../snippets16.t almost3.def ../snippets16.t rt130394.def +../snippets16.t rt131115.def +../snippets16.t rt131115.rt131115 ../snippets2.t angle.def ../snippets2.t arrows1.def ../snippets2.t arrows2.def @@ -307,5 +309,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets16.t rt131115.def -../snippets16.t rt131115.rt131115 +../snippets16.t ndsm1.def +../snippets16.t ndsm1.ndsm diff --git a/t/snippets16.t b/t/snippets16.t index 5770bb58..ed36de83 100644 --- a/t/snippets16.t +++ b/t/snippets16.t @@ -14,6 +14,8 @@ #11 rt130394.def #12 rt131115.def #13 rt131115.rt131115 +#14 ndsm1.def +#15 ndsm1.ndsm # To locate test #13 you can search for its name or the string '#13' @@ -33,6 +35,7 @@ BEGIN { $rparams = { 'def' => "", 'git10' => "-wn -ce -cbl=sort,map,grep", + 'ndsm' => "-ndsm", 'rt131115' => "-bli", 'spp1' => "-spp=1", 'spp2' => "-spp=2", @@ -100,6 +103,16 @@ $start = $end = $len = $ismut = $number = $allele_ori = $allele_mut = $proof = $xxxxreg = $reg = $dist = ''; ---------- + 'ndsm1' => <<'----------', +;;;;; # 1 trapped semicolon +sub numerically {$a <=> $b}; +;;;;; +sub Numerically {$a <=> $b}; # trapped semicolon +@: = qw;2c72656b636168 + 2020202020 + ;; __; +---------- + 'rt130394' => <<'----------', # rt130394: keep on one line $factorial = sub { reduce { $a * $b } 1 .. 11 }; @@ -288,6 +301,44 @@ sub a } #13........... }, + + 'ndsm1.def' => { + source => "ndsm1", + params => "def", + expect => <<'#14...........', +; # 1 trapped semicolon +sub numerically { $a <=> $b } + +sub Numerically { $a <=> $b }; # trapped semicolon +@: = qw;2c72656b636168 + 2020202020 + ;; +__; +#14........... + }, + + 'ndsm1.ndsm' => { + source => "ndsm1", + params => "ndsm", + expect => <<'#15...........', +; +; +; +; +; # 1 trapped semicolon +sub numerically { $a <=> $b }; +; +; +; +; +; +sub Numerically { $a <=> $b }; # trapped semicolon +@: = qw;2c72656b636168 + 2020202020 + ;; +__; +#15........... + }, }; my $ntests = 0 + keys %{$rtests};