Subject: | Patch to insert blank lines after opening and before closing braces |
The attached patch allows perltidy to insert specified number of blank lines after opening and before closing braces of blocks and subs.
For how to use this patch, see the patched POD docs.
Note that my patch works accordingly my testing, but I am not sure it is errorless, as my understanding of perltidy internals is partial.
Subject: | perltidy-blank.diff |
diff '--exclude=*~' '--exclude=*.b[ac]k' --exclude .svn --exclude .libs --exclude .deps --exclude autom4te.cache '--exclude=*.old' -Naur Perl-Tidy-20160302/bin/perltidy perltidy/bin/perltidy
--- Perl-Tidy-20160302/bin/perltidy 2016-03-01 18:00:03.000000000 +0200
+++ perltidy/bin/perltidy 2016-04-11 20:58:10.547693528 +0300
@@ -1642,6 +1642,71 @@
This flag is negated with B<-nasbl>, and the default is B<-nasbl>.
+=item B<-bso=n>, B<--blank-lines-after-opening-brace>
+
+=item B<-bsc=n>, B<--blank-lines-before-closing-brace>
+
+Add n (default zero) blank lines after opening brace or before closing
+brace respectively:
+
+ if ( $input_file eq '-' ) { # -bso=1 -bsc=1
+
+ important_function();
+
+ }
+
+These flags apply to all structural blocks, including named sub's (unless
+the B<-sbso> or B<-sbsc> flags are set -- see next item).
+
+=item B<-sbso=n>, B<--blank-lines-after-opening-sub-brace>
+
+=item B<-sbsc=n>, B<--blank-lines-before-closing-sub-brace>
+
+The flags B<-sbso> and B<-sbsc> can be used to override the value of B<-bso>
+and B<-bsc> for the braces of named sub's. For example,
+
+ perltidy -sbso=1 -sbsc=1
+
+produces this result:
+
+ sub message
+ {
+
+ if (!defined($_[0])) {
+ print("Hello, World\n");
+ }
+ else {
+ print($_[0], "\n");
+ }
+
+ }
+
+If B<-sbso> or B<-sbsc> is not specified, the value of B<-bso> or B<-bsc>
+is used.
+
+=item B<-asbso=n>, B<--blank-lines-after-opening-anonymous-sub-brace>
+
+=item B<-asbsc=n>, B<--blank-lines-before-closing-anonymous-sub-brace>
+
+The flag B<-asbso> and B<-asbsc> are like the B<-sbso> and B<-sbsc> flags except
+that it applies to anonymous sub's instead of named subs. For example
+
+ perltidy -asbso=1 -asbsc=1
+
+produces this result:
+
+ $a = sub
+ {
+
+ if ( !defined( $_[0] ) ) {
+ print("Hello, World\n");
+ }
+ else {
+ print( $_[0], "\n" );
+ }
+
+ };
+
=item B<-bli>, B<--brace-left-and-indent>
The flag B<-bli> is the same as B<-bl> but in addition it causes one
diff '--exclude=*~' '--exclude=*.b[ac]k' --exclude .svn --exclude .libs --exclude .deps --exclude autom4te.cache '--exclude=*.old' -Naur Perl-Tidy-20160302/lib/Perl/Tidy.pm perltidy/lib/Perl/Tidy.pm
--- Perl-Tidy-20160302/lib/Perl/Tidy.pm 2016-03-01 16:46:22.000000000 +0200
+++ perltidy/lib/Perl/Tidy.pm 2016-04-11 22:17:21.015503834 +0300
@@ -1671,11 +1671,17 @@
$add_option->( 'delete-old-newlines', 'dnl', '!' );
$add_option->( 'opening-brace-always-on-right', 'bar', '!' );
$add_option->( 'opening-brace-on-new-line', 'bl', '!' );
+ $add_option->( 'blank-lines-after-opening-brace', 'bso', '=i' );
+ $add_option->( 'blank-lines-before-closing-brace', 'bsc', '=i' );
$add_option->( 'opening-hash-brace-right', 'ohbr', '!' );
$add_option->( 'opening-paren-right', 'opr', '!' );
$add_option->( 'opening-square-bracket-right', 'osbr', '!' );
$add_option->( 'opening-anonymous-sub-brace-on-new-line', 'asbl', '!' );
+ $add_option->( 'blank-lines-after-opening-anonymous-sub-brace', 'asbso', '=i' );
+ $add_option->( 'blank-lines-before-closing-anonymous-sub-brace', 'asbsc', '=i' );
$add_option->( 'opening-sub-brace-on-new-line', 'sbl', '!' );
+ $add_option->( 'blank-lines-after-opening-sub-brace', 'sbso', '=i' );
+ $add_option->( 'blank-lines-before-closing-sub-brace', 'sbsc', '=i' );
$add_option->( 'paren-vertical-tightness', 'pvt', '=i' );
$add_option->( 'paren-vertical-tightness-closing', 'pvtc', '=i' );
$add_option->( 'stack-closing-block-brace', 'scbb', '!' );
@@ -1906,6 +1912,8 @@
backup-file-extension=bak
format-skipping
default-tabsize=8
+ blank-lines-after-opening-brace=0
+ blank-lines-before-closing-brace=0
pod2html
html-table-of-contents
@@ -2570,6 +2578,30 @@
$rOpts->{'opening-brace-on-new-line'};
}
+ # the same for -sbso -bso
+ if ( !defined( $rOpts->{'blank-lines-after-opening-sub-brace'} ) ) {
+ $rOpts->{'blank-lines-after-opening-sub-brace'} =
+ $rOpts->{'blank-lines-after-opening-brace'};
+ }
+
+ # the same for -sbsc -bsc
+ if ( !defined( $rOpts->{'blank-lines-before-closing-sub-brace'} ) ) {
+ $rOpts->{'blank-lines-before-closing-sub-brace'} =
+ $rOpts->{'blank-lines-before-closing-brace'};
+ }
+
+ # the same for -asbso -abso
+ if ( !defined( $rOpts->{'blank-lines-after-opening-anonymous-sub-brace'} ) ) {
+ $rOpts->{'blank-lines-after-opening-anonymous-sub-brace'} =
+ $rOpts->{'blank-lines-after-opening-sub-brace'};
+ }
+
+ # the same for -asbsc -absc
+ if ( !defined( $rOpts->{'blank-lines-before-closing-anonymous-sub-brace'} ) ) {
+ $rOpts->{'blank-lines-before-closing-anonymous-sub-brace'} =
+ $rOpts->{'blank-lines-before-closing-sub-brace'};
+ }
+
if ( $rOpts->{'entab-leading-whitespace'} ) {
if ( $rOpts->{'entab-leading-whitespace'} < 0 ) {
Warn "-et=n must use a positive integer; ignoring -et\n";
@@ -9853,6 +9885,19 @@
# use -asbl flag for an anonymous sub block
: $rOpts->{'opening-anonymous-sub-brace-on-new-line'};
+ my $lines_after_opening_brace =
+
+ # use -bco option if not a sub block of any type
+ $block_type !~ /^sub/
+ ? $rOpts->{'blank-lines-after-opening-brace'}
+
+ # use -sbco option for a named sub block
+ : $block_type !~ /^sub\W*$/
+ ? $rOpts->{'blank-lines-after-opening-sub-brace'}
+
+ # use -asbco option for an anonymous sub block
+ : $rOpts->{'blank-lines-after-opening-anonymous-sub-brace'};
+
# Break before an opening '{' ...
if (
@@ -9892,10 +9937,27 @@
unless ($no_internal_newlines) {
output_line_to_go();
}
+ if ($index_start_one_line_block == UNDEFINED_INDEX) {
+ flush();
+ $file_writer_object->require_blank_code_lines($lines_after_opening_brace);
+ }
}
elsif ($is_closing_BLOCK) {
+ my $lines_before_closing_brace =
+
+ # use -bco option if not a sub block of any type
+ $block_type !~ /^sub/
+ ? $rOpts->{'blank-lines-before-closing-brace'}
+
+ # use -sbco option for a named sub block
+ : $block_type !~ /^sub\W*$/
+ ? $rOpts->{'blank-lines-before-closing-sub-brace'}
+
+ # use -asbco option for an anonymous sub block
+ : $rOpts->{'blank-lines-before-closing-anonymous-sub-brace'};
+
# If there is a pending one-line block ..
if ( $index_start_one_line_block != UNDEFINED_INDEX ) {
@@ -9917,6 +9979,7 @@
}
# put a break before this closing curly brace if appropriate
+ $file_writer_object->require_blank_code_lines($lines_before_closing_brace);
unless ( $no_internal_newlines
|| $index_start_one_line_block != UNDEFINED_INDEX )
{