]> git.donarmstrong.com Git - perltidy.git/commitdiff
add parameter --line-range-tidy=n1:n2 (see git #122) 20230701.01
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 18 Jul 2023 15:24:58 +0000 (08:24 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 18 Jul 2023 15:24:58 +0000 (08:24 -0700)
28 files changed:
CHANGES.md
bin/perltidy
dev-bin/perltidy_random_setup.pl
lib/Perl/Tidy.pm
lib/Perl/Tidy.pod
lib/Perl/Tidy/Debugger.pm
lib/Perl/Tidy/DevNull.pm
lib/Perl/Tidy/Diagnostics.pm
lib/Perl/Tidy/FileWriter.pm
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/HtmlWriter.pm
lib/Perl/Tidy/IOScalar.pm
lib/Perl/Tidy/IOScalarArray.pm
lib/Perl/Tidy/IndentationItem.pm
lib/Perl/Tidy/LineBuffer.pm
lib/Perl/Tidy/LineSink.pm
lib/Perl/Tidy/LineSource.pm
lib/Perl/Tidy/Logger.pm
lib/Perl/Tidy/Tokenizer.pm
lib/Perl/Tidy/VerticalAligner.pm
lib/Perl/Tidy/VerticalAligner/Alignment.pm
lib/Perl/Tidy/VerticalAligner/Line.pm
t/snippets/expect/lrt.def [new file with mode: 0644]
t/snippets/expect/lrt.lrt [new file with mode: 0644]
t/snippets/lrt.in [new file with mode: 0644]
t/snippets/lrt.par [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets28.t

index d6b7c5038ed46a505a390f575cb14ad71f09c532..33a62e170cbebe48a55f778bb11eb36f0d4ece6d 100644 (file)
@@ -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,
index 8ef10b2aaadae8c9f5f81c23d1d70dcc39505066..1a1038b4a2ed657ab7ad3f1c17e7876ffb1bb349 100755 (executable)
@@ -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<n1> and B<n2>, where B<n1> is the first line number to be formatted (start counting with 1) and B<n2> is the last line number to be formatted.  If B<n2> 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
 
index 7befb70f73c14eab6f9789f51301958f79a95c63..aa44a5b39635e6fcc9ba41415acb2e55148ae923 100755 (executable)
@@ -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;
index 615ff84f3ef941a919163c7e1d3599571ceed870..727de1ef9d6f84562072a6d8d8254e8215b34346 100644 (file)
@@ -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(<<EOM);
---dump-block-summary expects 1 filename in the arg list but saw $numf filenames
+--dump-block-summary expects 1 filename in the arg list but saw $num_files filenames
 EOM
     }
 
     #----------------------------------------
     # check parameters and their interactions
     #----------------------------------------
-    $self->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(<<EOM);
+--line-range-tidy expects no more than 1 filename in the arg list but saw $num_files filenames
+EOM
+        }
+
+        $line_range_tidy =~ s/\s+//g;
+        if ( $line_range_tidy =~ /^(\d+):(\d+)?$/ ) {
+            my $n1 = $1;
+            my $n2 = $2;
+            if ( $n1 < 1 ) {
+                Die(<<EOM);
+--line-range-tidy=n1:n2 expects starting line number n1>=1 but n1=$n1
+EOM
+            }
+            if ( defined($n2) && $n2 < $n1 ) {
+                Die(<<EOM);
+--line-range-tidy=n1:n2 expects ending line number n2>=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
 
index 7502a30302332e0cc248aa330b1931a824809471..636fbb76351f91dc326722036fc934de12ea7cca 100644 (file)
@@ -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
 
index 5016e9bdc6c4315595e49742aea2c0ade27403ce..c5efb74bb15f845ea8f422074d7c57deddc3dac0 100644 (file)
@@ -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{ };
index aa45d5f14eb2d21fcd7ca007e98cdcef7287ca4f..b9f5d21726bb69add597a14aae7dd01f5694246b 100644 (file)
@@ -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 }
index 72fbf19fd44f1cbd14691e6d3397e9d38047bea5..798b72ac8c80c99e043d07e9ea99a8f87a24b74e 100644 (file)
@@ -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{};
 
index 1a8c2f24c386dae4ffbfbf5daee319a0370c1de8..f1879d077c5f084dc399cc2f81bd6a84c7943bad 100644 (file)
@@ -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{};
index 0591ce6695af1a50078e237ecb6c434abda39caf..02a6966fdd462290eb13da8712b098974f54e790 100644 (file)
@@ -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()
index b3da89e662f77c853b4e672ac53a62636a1f6d8d..073182dd8e79529e057bdd2491b23d6bfe491651 100644 (file)
@@ -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;
index bbc71658178f292c5b787501858e4a28db1d2537..d76457efa40f46fa8eec33238f70daa776c9ced2 100644 (file)
@@ -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{};
 
index e5e57005d94e17536a17012ccbacc2fcffb6d17a..3d0823f61a51fef548cf5b6921d1107eaf00f322 100644 (file)
@@ -14,7 +14,7 @@ package Perl::Tidy::IOScalarArray;
 use strict;
 use warnings;
 use Carp;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
 
 sub AUTOLOAD {
 
index 360bee5d4c27294ff10900d11d0524e05ea637b2..9a48f47f60838fa8ff623707b877fedc05b659fb 100644 (file)
@@ -8,7 +8,7 @@
 package Perl::Tidy::IndentationItem;
 use strict;
 use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
 
 BEGIN {
 
index f1abd12d56dae8c21ecd7b12ddbcfb04f9859331..5ffe8546f51e94286f57ee788381a201b1195457 100644 (file)
@@ -12,7 +12,7 @@
 package Perl::Tidy::LineBuffer;
 use strict;
 use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
 
 sub AUTOLOAD {
 
index 1b2c2f2e09091a5b021987338138c618190f52d8..4ca439657ce679ac487c6ccd35755e1339f90ce4 100644 (file)
@@ -8,7 +8,7 @@
 package Perl::Tidy::LineSink;
 use strict;
 use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
 
 sub AUTOLOAD {
 
index 0c462480711402150c02d499462c8411be4d2d18..1688c4d87e1aa9113f061a3841073247da3e200a 100644 (file)
@@ -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;
 
index 5b25252c2fed6b8ee88bf79eafed704632692012..c2d5d4ed4ede40384e2515ae3467a06d9b7eb6d1 100644 (file)
@@ -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;
index a6e5c349daefe028b145554c5176cf663a51e40d..a692273e813c8b5960531109c01ded29850e4ed1 100644 (file)
@@ -22,7 +22,7 @@ use strict;
 use warnings;
 use English qw( -no_match_vars );
 
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
 
 use Carp;
 
index 5b4ed5b6c8c1462278bf1ff7c606fc964885aa03..80abc751c2e46d22ca0fba03b1188130c985512f 100644 (file)
@@ -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;
 
index 2e7af8ec145a87cb95fb76ff17492bf1b2ecf98b..542fe0d2a6a931d7bceb27f6d8d23e14808409e8 100644 (file)
@@ -10,7 +10,7 @@ use warnings;
 
 { #<<< A non-indenting brace
 
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
 
 sub new {
     my ( $class, $rarg ) = @_;
index f595fc82b06ef8102353900280dd5215b6bf2ae8..549f5d717e64714fb9438e9902d0325ed31b89a0 100644 (file)
@@ -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 (file)
index 0000000..9e54ee8
--- /dev/null
@@ -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 (file)
index 0000000..0054d18
--- /dev/null
@@ -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 (file)
index 0000000..acfda6a
--- /dev/null
@@ -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 (file)
index 0000000..54a3351
--- /dev/null
@@ -0,0 +1 @@
+--line-range-tidy=2:3
index 2b096e36826713c548b826a3659eb076f6553ddf..98bc25a82269752f851f86f94d200a56ee9fd789 100644 (file)
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
+../snippets28.t        lrt.def
+../snippets28.t        lrt.lrt
index 7bd31a190f7982bceb3b7dda7b53ae379bef60ca..bb9687a0528843bf14282ede1b2db8e5b9c75856 100644 (file)
@@ -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};