# 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,
=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
=head1 VERSION
-This man page documents perltidy version 20230701
+This man page documents perltidy version 20230701.01
=head1 BUG REPORTS
'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 ],
recombine
code-skipping-begin
code-skipping-end
+ line-range-tidy
);
my %skip;
# 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 {
_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++,
# 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';
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/;
# 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'}
}
}
- 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 ) {
$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):
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_];
# 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
=head1 VERSION
-This man page documents Perl::Tidy version 20230701
+This man page documents Perl::Tidy version 20230701.01
=head1 LICENSE
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{ };
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 }
use strict;
use warnings;
use English qw( -no_match_vars );
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
use constant EMPTY_STRING => q{};
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{};
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()
package Perl::Tidy::HtmlWriter;
use strict;
use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
use English qw( -no_match_vars );
use File::Basename;
use strict;
use warnings;
use Carp;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
use constant EMPTY_STRING => q{};
use strict;
use warnings;
use Carp;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
sub AUTOLOAD {
package Perl::Tidy::IndentationItem;
use strict;
use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
BEGIN {
package Perl::Tidy::LineBuffer;
use strict;
use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
sub AUTOLOAD {
package Perl::Tidy::LineSink;
use strict;
use warnings;
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
sub AUTOLOAD {
use strict;
use warnings;
use English qw( -no_match_vars );
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
use constant DEVEL_MODE => 0;
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;
use warnings;
use English qw( -no_match_vars );
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
use Carp;
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;
{ #<<< A non-indenting brace
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
sub new {
my ( $class, $rarg ) = @_;
use strict;
use warnings;
use English qw( -no_match_vars );
-our $VERSION = '20230701';
+our $VERSION = '20230701.01';
sub AUTOLOAD {
--- /dev/null
+
+=pod
+sub hello{ print
+"Hello World!"}
+=cut
--- /dev/null
+=pod
+sub hello {
+ print "Hello World!";
+}
+=cut
--- /dev/null
+=pod
+sub hello{ print
+"Hello World!"}
+=cut
--- /dev/null
+--line-range-tidy=2:3
../snippets9.t rt98902.def
../snippets9.t rt98902.rt98902
../snippets9.t rt99961.def
+../snippets28.t lrt.def
+../snippets28.t lrt.lrt
#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'
$rparams = {
'def' => "",
'git116' => "-viu",
+ 'lrt' => "--line-range-tidy=2:3",
'olbxl2' => <<'----------',
-olbxl='*'
----------
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' => <<'----------',
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};