]> git.donarmstrong.com Git - perltidy.git/commitdiff
reworked coding which removes extra semicolons to use new data structures
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 18 Dec 2019 00:37:13 +0000 (16:37 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 18 Dec 2019 00:37:13 +0000 (16:37 -0800)
lib/Perl/Tidy/Formatter.pm
t/snippets/expect/ndsm1.def [new file with mode: 0644]
t/snippets/expect/ndsm1.ndsm [new file with mode: 0644]
t/snippets/ndsm.par [new file with mode: 0644]
t/snippets/ndsm1.in [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets16.t

index 3024baf8c4c0e56b197ee72a1bfe1eb178807634..d1b1e3203ee4267904517afdb99fe28224b71923 100644 (file)
@@ -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 (file)
index 0000000..c36b944
--- /dev/null
@@ -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 (file)
index 0000000..d5e53dd
--- /dev/null
@@ -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 (file)
index 0000000..736d0a6
--- /dev/null
@@ -0,0 +1 @@
+-ndsm
diff --git a/t/snippets/ndsm1.in b/t/snippets/ndsm1.in
new file mode 100644 (file)
index 0000000..35b2175
--- /dev/null
@@ -0,0 +1,7 @@
+;;;;; # 1 trapped semicolon 
+sub numerically {$a <=> $b};
+;;;;; 
+sub Numerically {$a <=> $b};  # trapped semicolon
+@: = qw;2c72656b636168 
+  2020202020 
+  ;; __;
index c2148c71bce85fb6816ae6c4433d6f3ab6733e18..5ab06fd8d8d5b6d36da0a9982fb4ee9009c0cd8b 100644 (file)
 ../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
 ../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
index 5770bb58892d7476953e0ee86978b266c0158549..ed36de83e409bbb04c35cecfb0bc302fd90cbcf9 100644 (file)
@@ -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};