From 17a7a6fb8c2394592e2c9b67667c3b30d3d69bb1 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Thu, 20 Jul 2023 15:24:39 -0700 Subject: [PATCH] add another error check for -line-range-tidy --- bin/perltidy | 2 +- lib/Perl/Tidy.pm | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index 1a1038b4..8c58b206 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -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 diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 727de1ef..de21c88d 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -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(<[_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 = -- 2.39.5