]> git.donarmstrong.com Git - perltidy.git/commitdiff
add another error check for -line-range-tidy
authorSteve Hancock <perltidy@users.sourceforge.net>
Thu, 20 Jul 2023 22:24:39 +0000 (15:24 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Thu, 20 Jul 2023 22:24:39 +0000 (15:24 -0700)
bin/perltidy
lib/Perl/Tidy.pm

index 1a1038b4a2ed657ab7ad3f1c17e7876ffb1bb349..8c58b20639ebde7872aee38061324a0dafbbf3f9 100755 (executable)
@@ -2297,7 +2297,7 @@ The default is equivalent to -cse='#>>V'.
 =head2 Formatting a Limited Range of Lines
 
 A command B<--line-range-tidy=n1:n2> is available to pass just a selected range
-of lines to perltidy.  This is command is mainly of interest to interactive
+of lines to perltidy.  This command is mainly of interest to interactive
 code editors.  When it is used, just the selected range of lines are passed
 through the perltidy tokenizer and formatter, so they need to contain a
 complete statement or balanced container.  Otherwise, a syntax error will occur
index 727de1ef9d6f84562072a6d8d8254e8215b34346..de21c88d2eda6194c4cc0ae85bbcccff568ff149 100644 (file)
@@ -2179,17 +2179,28 @@ sub process_filter_layer {
 
         my @buf_lines = split /^/, $buf;
 
-        my $num           = @buf_lines;
-        my $line_tidy_end = $self->[_line_tidy_end_];
-        if ( !defined($line_tidy_end) || $line_tidy_end > $num ) {
-            $line_tidy_end = $num;
+        my $num = @buf_lines;
+        if ( $line_tidy_begin > $num ) {
+            Warn(<<EOM);
+#--line-range-tidy=n1:n2 has n1=$line_tidy_begin which exceeds max line number of $num
+EOM
+            # Try to continue with an empty string to format, so that the
+            # caller gets everything back. If this causes trouble, we could
+            # call Die instead of Warn.
+            $buf           = EMPTY_STRING;
+            @buf_lines_pre = @buf_lines;
         }
+        else {
+            my $line_tidy_end = $self->[_line_tidy_end_];
+            if ( !defined($line_tidy_end) || $line_tidy_end > $num ) {
+                $line_tidy_end = $num;
+            }
+            $buf = join EMPTY_STRING,
+              @buf_lines[ $line_tidy_begin - 1 .. $line_tidy_end - 1 ];
 
-        $buf = join EMPTY_STRING,
-          @buf_lines[ $line_tidy_begin - 1 .. $line_tidy_end - 1 ];
-
-        @buf_lines_pre  = @buf_lines[ 0 .. $line_tidy_begin - 2 ];
-        @buf_lines_post = @buf_lines[ $line_tidy_end .. $num - 1 ];
+            @buf_lines_pre  = @buf_lines[ 0 .. $line_tidy_begin - 2 ];
+            @buf_lines_post = @buf_lines[ $line_tidy_end .. $num - 1 ];
+        }
     }
 
     my $remove_terminal_newline =