From 0ade3cfac7ca2bf57d3712c8ae3dab6532d60a8a Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 18 Jul 2023 08:24:58 -0700 Subject: [PATCH] add parameter --line-range-tidy=n1:n2 (see git #122) --- CHANGES.md | 6 ++ bin/perltidy | 28 +++++++- dev-bin/perltidy_random_setup.pl | 3 + lib/Perl/Tidy.pm | 80 ++++++++++++++++++++-- lib/Perl/Tidy.pod | 2 +- lib/Perl/Tidy/Debugger.pm | 2 +- lib/Perl/Tidy/DevNull.pm | 2 +- lib/Perl/Tidy/Diagnostics.pm | 2 +- lib/Perl/Tidy/FileWriter.pm | 2 +- lib/Perl/Tidy/Formatter.pm | 2 +- lib/Perl/Tidy/HtmlWriter.pm | 2 +- lib/Perl/Tidy/IOScalar.pm | 2 +- lib/Perl/Tidy/IOScalarArray.pm | 2 +- lib/Perl/Tidy/IndentationItem.pm | 2 +- lib/Perl/Tidy/LineBuffer.pm | 2 +- lib/Perl/Tidy/LineSink.pm | 2 +- lib/Perl/Tidy/LineSource.pm | 2 +- lib/Perl/Tidy/Logger.pm | 2 +- lib/Perl/Tidy/Tokenizer.pm | 2 +- lib/Perl/Tidy/VerticalAligner.pm | 2 +- lib/Perl/Tidy/VerticalAligner/Alignment.pm | 2 +- lib/Perl/Tidy/VerticalAligner/Line.pm | 2 +- t/snippets/expect/lrt.def | 5 ++ t/snippets/expect/lrt.lrt | 5 ++ t/snippets/lrt.in | 4 ++ t/snippets/lrt.par | 1 + t/snippets/packing_list.txt | 2 + t/snippets28.t | 34 +++++++++ 28 files changed, 178 insertions(+), 26 deletions(-) create mode 100644 t/snippets/expect/lrt.def create mode 100644 t/snippets/expect/lrt.lrt create mode 100644 t/snippets/lrt.in create mode 100644 t/snippets/lrt.par diff --git a/CHANGES.md b/CHANGES.md index d6b7c503..33a62e17 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Perltidy Change Log +## 2023 07 01.01 + + - Issue git #122. Added parameter -lrt=n1:n2, or --line-range-tidy=n1:n2 + to limit tidy operations to a limited line range. Line numbers start + with 1. The man pages have details. + ## 2023 07 01 - Issue git #121. Added parameters -xbt, or --extended-block-tightness, diff --git a/bin/perltidy b/bin/perltidy index 8ef10b2a..1a1038b4 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2294,6 +2294,32 @@ The default is equivalent to -cse='#>>V'. =back +=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 +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 +and the code will not be tidied. There are a couple of limitations +on the use of this command: (1) it may not be applied to multiple files, and +(2) it only applies to code tidying and not, for example, html formatting. + +=over 4 + +=item B<-lrt=n1:n2>, B<--line-range-tidy=n1:n2> + +The range of lines is specified by integers B and B, where B is the first line number to be formatted (start counting with 1) and B is the last line number to be formatted. If B is not given, or exceeds the actual number of lines, then formatting continues to the end of the file. + +Examples: + + -line-range-tidy=43:109 # tidy lines 43 through 109 + -line-range-tidy=' 43 : 109' # tidy lines 43 through 109 (spaces ok in quotes) + -line-range-tidy=1: # tidy all lines + -line-range-tidy=0:90 # ERROR (n1 must be >= 1) + +=back + =head2 Line Break Control The parameters in this and the next sections control breaks after @@ -5748,7 +5774,7 @@ The perltidy binary uses the Perl::Tidy module and is installed when that module =head1 VERSION -This man page documents perltidy version 20230701 +This man page documents perltidy version 20230701.01 =head1 BUG REPORTS diff --git a/dev-bin/perltidy_random_setup.pl b/dev-bin/perltidy_random_setup.pl index 7befb70f..aa44a5b3 100755 --- a/dev-bin/perltidy_random_setup.pl +++ b/dev-bin/perltidy_random_setup.pl @@ -1173,6 +1173,8 @@ EOM 'use-feature' => [ 'class', ' ', 'xyzzy' ], + 'line-range-tidy' => [ '1:', '1:' ], + # Arbitrary limits to keep things readable 'blank-lines-after-opening-block' => [ 0, 4 ], 'blank-lines-before-closing-block' => [ 0, 3 ], @@ -1303,6 +1305,7 @@ EOM recombine code-skipping-begin code-skipping-end + line-range-tidy ); my %skip; diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 615ff84f..727de1ef 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -113,7 +113,7 @@ BEGIN { # then the Release version must be bumped, and it is probably past time for # a release anyway. - $VERSION = '20230701'; + $VERSION = '20230701.01'; } ## end BEGIN sub DESTROY { @@ -451,6 +451,8 @@ BEGIN { _length_function_ => $i++, _line_separator_default_ => $i++, _line_separator_ => $i++, + _line_tidy_begin_ => $i++, + _line_tidy_end_ => $i++, _logger_object_ => $i++, _output_file_ => $i++, _postfilter_ => $i++, @@ -845,17 +847,18 @@ EOM # This is a safety precaution in case a user accidentally adds -dbs to the # command line parameters and is expecting formatted output to stdout. # Another precaution, added elsewhere, is to ignore -dbs in a .perltidyrc - my $numf = @Arg_files; - if ( $rOpts->{'dump-block-summary'} && $numf != 1 ) { + my $num_files = @Arg_files; + if ( $rOpts->{'dump-block-summary'} && $num_files != 1 ) { Die(<check_options( $is_Windows, $Windows_type, $rpending_complaint ); + $self->check_options( $is_Windows, $Windows_type, $rpending_complaint, + $num_files ); if ($user_formatter) { $rOpts->{'format'} = 'user'; @@ -2168,6 +2171,27 @@ sub process_filter_layer { my $change_line_separator = $self->[_line_separator_] ne "\n"; + # reduce '$buf' to a limited formatting line range if requested + my @buf_lines_pre; + my @buf_lines_post; + my $line_tidy_begin = $self->[_line_tidy_begin_]; + if ( $line_tidy_begin && $rOpts->{'format'} eq 'tidy' ) { + + 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; + } + + $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 ]; + } + my $remove_terminal_newline = !$rOpts->{'add-terminal-newline'} && substr( $buf, -1, 1 ) !~ /\n/; @@ -2213,6 +2237,8 @@ sub process_filter_layer { # These are used below, just after iterations are made. $use_postfilter_buffer = $postfilter + || @buf_lines_pre + || @buf_lines_post || $change_line_separator || $remove_terminal_newline || $rOpts->{'assert-tidy'} @@ -2338,7 +2364,8 @@ EOM } } - my @buf_post_lines = split /^/, $buf_post; + my @buf_post_lines = + ( @buf_lines_pre, ( split /^/, $buf_post ), @buf_lines_post ); # Copy the filtered buffer to the final destination if ( !$remove_terminal_newline ) { @@ -3135,6 +3162,7 @@ sub generate_options { $add_option->( 'use-unicode-gcstring', 'gcs', '!' ); $add_option->( 'warning-output', 'w', '!' ); $add_option->( 'add-terminal-newline', 'atnl', '!' ); + $add_option->( 'line-range-tidy', 'lrt', '=s' ); # options which are both toggle switches and values moved here # to hide from tidyview (which does not show category 0 flags): @@ -4262,7 +4290,10 @@ sub cleanup_word_list { sub check_options { - my ( $self, $is_Windows, $Windows_type, $rpending_complaint ) = @_; + my ( $self, $is_Windows, $Windows_type, $rpending_complaint, $num_files ) = + @_; + + # $num_files = number of files to be processed, for error checks my $rOpts = $self->[_rOpts_]; @@ -4480,6 +4511,41 @@ EOM # Define the default line ending, before any -ple option is applied $self->[_line_separator_default_] = get_line_separator_default($rOpts); + $self->[_line_tidy_begin_] = undef; + $self->[_line_tidy_end_] = undef; + my $line_range_tidy = $rOpts->{'line-range-tidy'}; + if ($line_range_tidy) { + + if ( $num_files > 1 ) { + Die(<=1 but n1=$n1 +EOM + } + if ( defined($n2) && $n2 < $n1 ) { + Die(<=n1 but n1=$n1 and n2=$n2 +EOM + } + $self->[_line_tidy_begin_] = $n1; + $self->[_line_tidy_end_] = $n2; + } + else { + Die( +"unrecognized 'line-range-tidy'; expecting format '-lrt=n1:n2'\n" + ); + } + } + return; } ## end sub check_options diff --git a/lib/Perl/Tidy.pod b/lib/Perl/Tidy.pod index 7502a303..636fbb76 100644 --- a/lib/Perl/Tidy.pod +++ b/lib/Perl/Tidy.pod @@ -469,7 +469,7 @@ The module 'Perl::Tidy' comes with a binary 'perltidy' which is installed when t =head1 VERSION -This man page documents Perl::Tidy version 20230701 +This man page documents Perl::Tidy version 20230701.01 =head1 LICENSE diff --git a/lib/Perl/Tidy/Debugger.pm b/lib/Perl/Tidy/Debugger.pm index 5016e9bd..c5efb74b 100644 --- a/lib/Perl/Tidy/Debugger.pm +++ b/lib/Perl/Tidy/Debugger.pm @@ -8,7 +8,7 @@ package Perl::Tidy::Debugger; use strict; use warnings; use English qw( -no_match_vars ); -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use constant EMPTY_STRING => q{}; use constant SPACE => q{ }; diff --git a/lib/Perl/Tidy/DevNull.pm b/lib/Perl/Tidy/DevNull.pm index aa45d5f1..b9f5d217 100644 --- a/lib/Perl/Tidy/DevNull.pm +++ b/lib/Perl/Tidy/DevNull.pm @@ -7,7 +7,7 @@ package Perl::Tidy::DevNull; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; sub new { my $self = shift; return bless {}, $self } sub print { return } sub close { return } diff --git a/lib/Perl/Tidy/Diagnostics.pm b/lib/Perl/Tidy/Diagnostics.pm index 72fbf19f..798b72ac 100644 --- a/lib/Perl/Tidy/Diagnostics.pm +++ b/lib/Perl/Tidy/Diagnostics.pm @@ -18,7 +18,7 @@ package Perl::Tidy::Diagnostics; use strict; use warnings; use English qw( -no_match_vars ); -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use constant EMPTY_STRING => q{}; diff --git a/lib/Perl/Tidy/FileWriter.pm b/lib/Perl/Tidy/FileWriter.pm index 1a8c2f24..f1879d07 100644 --- a/lib/Perl/Tidy/FileWriter.pm +++ b/lib/Perl/Tidy/FileWriter.pm @@ -7,7 +7,7 @@ package Perl::Tidy::FileWriter; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use constant DEVEL_MODE => 0; use constant EMPTY_STRING => q{}; diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 0591ce66..02a6966f 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -53,7 +53,7 @@ use constant SPACE => q{ }; use Carp; use English qw( -no_match_vars ); use List::Util qw( min max ); # min, max are in Perl 5.8 -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; # The Tokenizer will be loaded with the Formatter ##use Perl::Tidy::Tokenizer; # for is_keyword() diff --git a/lib/Perl/Tidy/HtmlWriter.pm b/lib/Perl/Tidy/HtmlWriter.pm index b3da89e6..073182dd 100644 --- a/lib/Perl/Tidy/HtmlWriter.pm +++ b/lib/Perl/Tidy/HtmlWriter.pm @@ -7,7 +7,7 @@ package Perl::Tidy::HtmlWriter; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use English qw( -no_match_vars ); use File::Basename; diff --git a/lib/Perl/Tidy/IOScalar.pm b/lib/Perl/Tidy/IOScalar.pm index bbc71658..d76457ef 100644 --- a/lib/Perl/Tidy/IOScalar.pm +++ b/lib/Perl/Tidy/IOScalar.pm @@ -10,7 +10,7 @@ package Perl::Tidy::IOScalar; use strict; use warnings; use Carp; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use constant EMPTY_STRING => q{}; diff --git a/lib/Perl/Tidy/IOScalarArray.pm b/lib/Perl/Tidy/IOScalarArray.pm index e5e57005..3d0823f6 100644 --- a/lib/Perl/Tidy/IOScalarArray.pm +++ b/lib/Perl/Tidy/IOScalarArray.pm @@ -14,7 +14,7 @@ package Perl::Tidy::IOScalarArray; use strict; use warnings; use Carp; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; sub AUTOLOAD { diff --git a/lib/Perl/Tidy/IndentationItem.pm b/lib/Perl/Tidy/IndentationItem.pm index 360bee5d..9a48f47f 100644 --- a/lib/Perl/Tidy/IndentationItem.pm +++ b/lib/Perl/Tidy/IndentationItem.pm @@ -8,7 +8,7 @@ package Perl::Tidy::IndentationItem; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; BEGIN { diff --git a/lib/Perl/Tidy/LineBuffer.pm b/lib/Perl/Tidy/LineBuffer.pm index f1abd12d..5ffe8546 100644 --- a/lib/Perl/Tidy/LineBuffer.pm +++ b/lib/Perl/Tidy/LineBuffer.pm @@ -12,7 +12,7 @@ package Perl::Tidy::LineBuffer; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; sub AUTOLOAD { diff --git a/lib/Perl/Tidy/LineSink.pm b/lib/Perl/Tidy/LineSink.pm index 1b2c2f2e..4ca43965 100644 --- a/lib/Perl/Tidy/LineSink.pm +++ b/lib/Perl/Tidy/LineSink.pm @@ -8,7 +8,7 @@ package Perl::Tidy::LineSink; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; sub AUTOLOAD { diff --git a/lib/Perl/Tidy/LineSource.pm b/lib/Perl/Tidy/LineSource.pm index 0c462480..1688c4d8 100644 --- a/lib/Perl/Tidy/LineSource.pm +++ b/lib/Perl/Tidy/LineSource.pm @@ -9,7 +9,7 @@ package Perl::Tidy::LineSource; use strict; use warnings; use English qw( -no_match_vars ); -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use constant DEVEL_MODE => 0; diff --git a/lib/Perl/Tidy/Logger.pm b/lib/Perl/Tidy/Logger.pm index 5b25252c..c2d5d4ed 100644 --- a/lib/Perl/Tidy/Logger.pm +++ b/lib/Perl/Tidy/Logger.pm @@ -8,7 +8,7 @@ package Perl::Tidy::Logger; use strict; use warnings; -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use English qw( -no_match_vars ); use constant DEVEL_MODE => 0; diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index a6e5c349..a692273e 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -22,7 +22,7 @@ use strict; use warnings; use English qw( -no_match_vars ); -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use Carp; diff --git a/lib/Perl/Tidy/VerticalAligner.pm b/lib/Perl/Tidy/VerticalAligner.pm index 5b4ed5b6..80abc751 100644 --- a/lib/Perl/Tidy/VerticalAligner.pm +++ b/lib/Perl/Tidy/VerticalAligner.pm @@ -3,7 +3,7 @@ use strict; use warnings; use Carp; use English qw( -no_match_vars ); -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; use Perl::Tidy::VerticalAligner::Alignment; use Perl::Tidy::VerticalAligner::Line; diff --git a/lib/Perl/Tidy/VerticalAligner/Alignment.pm b/lib/Perl/Tidy/VerticalAligner/Alignment.pm index 2e7af8ec..542fe0d2 100644 --- a/lib/Perl/Tidy/VerticalAligner/Alignment.pm +++ b/lib/Perl/Tidy/VerticalAligner/Alignment.pm @@ -10,7 +10,7 @@ use warnings; { #<<< A non-indenting brace -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; sub new { my ( $class, $rarg ) = @_; diff --git a/lib/Perl/Tidy/VerticalAligner/Line.pm b/lib/Perl/Tidy/VerticalAligner/Line.pm index f595fc82..549f5d71 100644 --- a/lib/Perl/Tidy/VerticalAligner/Line.pm +++ b/lib/Perl/Tidy/VerticalAligner/Line.pm @@ -10,7 +10,7 @@ package Perl::Tidy::VerticalAligner::Line; use strict; use warnings; use English qw( -no_match_vars ); -our $VERSION = '20230701'; +our $VERSION = '20230701.01'; sub AUTOLOAD { diff --git a/t/snippets/expect/lrt.def b/t/snippets/expect/lrt.def new file mode 100644 index 00000000..9e54ee87 --- /dev/null +++ b/t/snippets/expect/lrt.def @@ -0,0 +1,5 @@ + +=pod +sub hello{ print +"Hello World!"} +=cut diff --git a/t/snippets/expect/lrt.lrt b/t/snippets/expect/lrt.lrt new file mode 100644 index 00000000..0054d183 --- /dev/null +++ b/t/snippets/expect/lrt.lrt @@ -0,0 +1,5 @@ +=pod +sub hello { + print "Hello World!"; +} +=cut diff --git a/t/snippets/lrt.in b/t/snippets/lrt.in new file mode 100644 index 00000000..acfda6a2 --- /dev/null +++ b/t/snippets/lrt.in @@ -0,0 +1,4 @@ +=pod +sub hello{ print +"Hello World!"} +=cut diff --git a/t/snippets/lrt.par b/t/snippets/lrt.par new file mode 100644 index 00000000..54a3351c --- /dev/null +++ b/t/snippets/lrt.par @@ -0,0 +1 @@ +--line-range-tidy=2:3 diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 2b096e36..98bc25a8 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -535,3 +535,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def +../snippets28.t lrt.def +../snippets28.t lrt.lrt diff --git a/t/snippets28.t b/t/snippets28.t index 7bd31a19..bb9687a0 100644 --- a/t/snippets28.t +++ b/t/snippets28.t @@ -12,6 +12,8 @@ #9 xbt.xbt1 #10 xbt.xbt2 #11 xbt.xbt3 +#12 lrt.def +#13 lrt.lrt # To locate test #13 you can search for its name or the string '#13' @@ -31,6 +33,7 @@ BEGIN { $rparams = { 'def' => "", 'git116' => "-viu", + 'lrt' => "--line-range-tidy=2:3", 'olbxl2' => <<'----------', -olbxl='*' ---------- @@ -51,6 +54,13 @@ print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); print "Would need: @DepList\n" if ( @DepList and !$Quiet ); print "RPM Output:\n" unless $Quiet; print join( "\n", @RPMOutput ) . "\n" unless $Quiet; +---------- + + 'lrt' => <<'----------', +=pod +sub hello{ print +"Hello World!"} +=cut ---------- 'olbxl' => <<'----------', @@ -302,6 +312,30 @@ my @matches = @{$nodes_ref} > 1 ? @{$nodes_ref}[ 1 .. $#{$nodes_ref} ] : (); grep {defined &{${"${class}::"}{$_}}} &{"${class}::Clear"}(); #11........... }, + + 'lrt.def' => { + source => "lrt", + params => "def", + expect => <<'#12...........', + +=pod +sub hello{ print +"Hello World!"} +=cut +#12........... + }, + + 'lrt.lrt' => { + source => "lrt", + params => "lrt", + expect => <<'#13...........', +=pod +sub hello { + print "Hello World!"; +} +=cut +#13........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5