From c34ee553dc412652c0f81e1fb4aef9631845027e Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Thu, 6 Dec 2018 22:14:15 -0800 Subject: [PATCH] simplified input with two flags, kgbd and kgbi --- bin/perltidy | 88 +++++++++++++------------------------- docs/ChangeLog.html | 7 ++- docs/perltidy.html | 47 +++++--------------- lib/Perl/Tidy.pm | 14 +++--- lib/Perl/Tidy/Formatter.pm | 44 +++++++++---------- 5 files changed, 73 insertions(+), 127 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index bdd3c4a4..94257ffb 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2679,15 +2679,15 @@ B<--keyword-group-blanks-before=n>, or B<-kgbb=n>; B is 0, 1, or 2 B<--keyword-group-blanks-after=n>, or B<-kgba=n>; B is 0, 1, or 2 -B<--keyword-group-blanks-inside=n>, or B<-kgbi=n>; B is 0 or 1 +B<--keyword-group-blanks-inside>, or B<-kgbi> is an on/off switch -B<--keyword-group-blanks-delete>, or B<-kgbd>; is an on/off switch +B<--keyword-group-blanks-delete>, or B<-kgbd> is an on/off switch In addition, the following abbreviations are available to simplify usage: -B<--keyword-group-blanks>, or B<-kgb>, is short for B<-kgbb=1 -kgba=1 -kgbi=1> +B<--keyword-group-blanks>, or B<-kgb>, is short for B<-kgbb=1 -kgba=1 kgbi> -B<--nokeyword-group-blanks>, or B<-nkgb>, is short for B<-kgbb=0 -kgba=0 -kgbi=0> +B<--nokeyword-group-blanks>, or B<-nkgb>, is short for B<-kgbb=0 -kgba=0 nkgbi> Before describing the meaning of the parameters in detail let us look at an example which is formatted with default parameter settings. @@ -2709,20 +2709,20 @@ example which is formatted with default parameter settings. using B gives: print "Entering test 2\n"; - + <-this blank controlled by -kgbb use Test; use Encode qw(from_to encode decode encode_utf8 decode_utf8 find_encoding is_utf8); use charnames qw(greek); - + <-this blank controlled by -kgbi my @encodings = grep( /iso-?8859/, Encode::encodings() ); my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' ); my @source = qw(ascii iso8859-1 cp1250); my @destiny = qw(cp1047 cp37 posix-bc); my @ebcdic_sets = qw(cp1047 cp37 posix-bc); my $str = join( '', map( chr($_), 0x20 .. 0x7E ) ); - + <-this blank controlled by -kgba return unless ($str); Blank lines have been introduced around the B and B sequences. What @@ -2733,34 +2733,6 @@ blanks were placed before and after the entire group. Then, since there was also a subsequence of six B lines, a blank line was introduced to separate them. -Now suppose there had been some spaces in the starting script, such as - - print "Entering test 2\n"; - use Test; - use Encode qw(from_to encode decode - encode_utf8 decode_utf8 - find_encoding is_utf8); - - use charnames qw(greek); - my @encodings = grep( /iso-?8859/, Encode::encodings() ); - my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' ); - my @source = qw(ascii iso8859-1 cp1250); - - my @destiny = qw(cp1047 cp37 posix-bc); - my @ebcdic_sets = qw(cp1047 cp37 posix-bc); - my $str = join( '', map( chr($_), 0x20 .. 0x7E ) ); - return unless ($str); - -Using just B would have no effect on this script because the -groups each have fewer than the threshold number of statements. If more -compact code is desirable there is a parameter B<-kgbd> available which -causes perltidy to make a preliminary pass through the input script to delete -blank lines between lines with the keywords being sought. This allows -keyword groups of maximum possible length to be formed. - -So using B on the above script would produce the same -output as previously shown. - Finer control over blank placement can be achieved by using the individual parameters rather than the B<-kgb> flag. The individual controls are as follows. @@ -2788,30 +2760,28 @@ scheme: n=1 => a blank line is introduced if possible n=2 => an existing blank line will be removed -B<--keyword-group-blanks-inside=n>, or B<-kgbi=n>, specifies -whether subgroups of a single statement type with more than the minimum number (as -specified with B<-kgbc=n>) may be separated from the other sub-groups with -blank lines, where - - n=0 => no blank internal line is introduced [Default] - n=1 => a blank line is introduced before and after a subgroup - -B<--keyword-group-blanks-delete>, or B<-kgbd>, causes blank lines between two -statements with one of the sought keywords to be deleted before groups are -formed. This will produce the most compact code and maximum group sizes. To -leave these blank lines unchanged use B<--nokeyword-group-blanks-delete>, or -B<-nkgbd>. Note that the B<-kgbd> flag only removes blank lines between -statements with one of the keywords being sought. Blanks before and after -keyword sequences can be controlled with parameters B<-kgbb> and B<-kgba>. -Also note that blanks deleted by this flag are not subject to any threshold -number. All qualifying blank lines will be deleted. - -B<--keyword-group-blanks>, or B<-kgb>, which is equivalent to setting -B<-kgbb=1 -kgba=1 -kgbi=1>. This turns on keyword group formatting -with a set of default values. - -B<--nokeyword-group-blanks>, or B<-nkgb>, is equivalent to B<-kgbb=0 -kgba kgbi=0>. -This flag turns off keyword group blank lines and is the default setting. +B<--keyword-group-blanks-inside>, or B<-kgbi>, controls +the insertion of blank lines between the first and last statement of the entire +group. If there is a continuous run of a single statement type with more than +the minimum threshold number (as specified with B<-kgbc=n>) then this +switch causes a blank line be inserted between this +subgroup and the others. In the example above this happened between the +B and B statements. + +B<--keyword-group-blanks-delete>, or B<-kgbd>, controls the deletion of any +blank lines that exist in the the group when it is first scanned. When +statements are initially scanned, any existing blank lines are included in the +collection. Any such orignial blank lines will be deleted before any other +insertions are made when the parameter B<-kgbd> is set. The default is not to +do this, B<-nkgbd>. + +B<--keyword-group-blanks>, or B<-kgb>, is an abbreviation equivalent to setting +B<-kgbb=1 -kgba=1 -kgbi>. This turns on keyword group formatting with a set of +default values. + +B<--nokeyword-group-blanks>, or B<-nkgb>, is equivalent to B<-kgbb=0 -kgba +nkgbi>. This flag turns off keyword group blank lines and is the default +setting. Here are a few notes about the functioning of this technique. diff --git a/docs/ChangeLog.html b/docs/ChangeLog.html index b95de2c3..6a754cd4 100644 --- a/docs/ChangeLog.html +++ b/docs/ChangeLog.html @@ -2,9 +2,14 @@

2018 11 20.01

-
- Add repository URLs to META files 
+
- RT#12764, introduced new feature allowing placement of blanks around
+  sequences of selected keywords such as use, my, our, local. This can
+  be activated with the -kgb* series of parameters described in the manual.
+
 - Rewrote vertical algnment module.  It is much better at finding
   patterns in complex code.
+
+- Add repository URLs to META files
 

2018 11 20

diff --git a/docs/perltidy.html b/docs/perltidy.html index 9da5b442..6713a9f5 100644 --- a/docs/perltidy.html +++ b/docs/perltidy.html @@ -2121,15 +2121,15 @@

--keyword-group-blanks-after=n, or -kgba=n; n is 0, 1, or 2

-

--keyword-group-blanks-inside=n, or -kgbi=n; n is 0 or 1

+

--keyword-group-blanks-inside, or -kgbi is an on/off switch

-

--keyword-group-blanks-delete, or -kgbd; is an on/off switch

+

--keyword-group-blanks-delete, or -kgbd is an on/off switch

In addition, the following abbreviations are available to simplify usage:

-

--keyword-group-blanks, or -kgb, is short for -kgbb=1 -kgba=1 -kgbi=1

+

--keyword-group-blanks, or -kgb, is short for -kgbb=1 -kgba=1 kgbi

-

--nokeyword-group-blanks, or -nkgb, is short for -kgbb=0 -kgba=0 -kgbi=0

+

--nokeyword-group-blanks, or -nkgb, is short for -kgbb=0 -kgba=0 nkgbi

Before describing the meaning of the parameters in detail let us look at an example which is formatted with default parameter settings.

@@ -2150,46 +2150,24 @@

using perltidy -kgb gives:

        print "Entering test 2\n";
-
+                                            <-this blank controlled by -kgbb
         use Test;
         use Encode qw(from_to encode decode
           encode_utf8 decode_utf8
           find_encoding is_utf8);
         use charnames qw(greek);
-
+                                            <-this blank controlled by -kgbi
         my @encodings     = grep( /iso-?8859/, Encode::encodings() );
         my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' );
         my @source        = qw(ascii iso8859-1 cp1250);
         my @destiny       = qw(cp1047 cp37 posix-bc);
         my @ebcdic_sets   = qw(cp1047 cp37 posix-bc);
         my $str           = join( '', map( chr($_), 0x20 .. 0x7E ) );
-
+                                             <-this blank controlled by -kgba
         return unless ($str);

Blank lines have been introduced around the my and use sequences. What happened is that the default keyword list includes my and use but not print and return. So a continuous sequence of nine my and use statements was located. This number exceeds the default threshold of five, so blanks were placed before and after the entire group. Then, since there was also a subsequence of six my lines, a blank line was introduced to separate them.

-

Now suppose there had been some spaces in the starting script, such as

- -
        print "Entering test 2\n";
-        use Test;
-        use Encode qw(from_to encode decode
-          encode_utf8 decode_utf8
-          find_encoding is_utf8);
-
-        use charnames qw(greek);
-        my @encodings     = grep( /iso-?8859/, Encode::encodings() );
-        my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' );
-        my @source        = qw(ascii iso8859-1 cp1250);
-
-        my @destiny     = qw(cp1047 cp37 posix-bc);
-        my @ebcdic_sets = qw(cp1047 cp37 posix-bc);
-        my $str         = join( '', map( chr($_), 0x20 .. 0x7E ) );
-        return unless ($str);
- -

Using just perltidy -kgb would have no effect on this script because the groups each have fewer than the threshold number of statements. If more compact code is desirable there is a parameter -kgbd available which causes perltidy to make a preliminary pass through the input script to delete blank lines between lines with the keywords being sought. This allows keyword groups of maximum possible length to be formed.

- -

So using perltidy -kgbd -kgb on the above script would produce the same output as previously shown.

-

Finer control over blank placement can be achieved by using the individual parameters rather than the -kgb flag. The individual controls are as follows.

--keyword-group-blanks-list=s, or -kgbl=s, where s is a quoted string, defines the set of keywords which will be formed into groups. The string is a space separated list of keywords. The default set is s="use require local our my sub", but any list of keywords may be used.

@@ -2208,16 +2186,13 @@ n=1 => a blank line is introduced if possible n=2 => an existing blank line will be removed
-

--keyword-group-blanks-inside=n, or -kgbi=n, specifies whether subgroups of a single statement type with more than the minimum number (as specified with -kgbc=n) may be separated from the other sub-groups with blank lines, where

- -
   n=0 => no blank internal line is introduced [Default]
-   n=1 => a blank line is introduced before and after a subgroup
+

--keyword-group-blanks-inside, or -kgbi, controls the insertion of blank lines between the first and last statement of the entire group. If there is a continuous run of a single statement type with more than the minimum threshold number (as specified with -kgbc=n) then this switch causes a blank line be inserted between this subgroup and the others. In the example above this happened between the use and my statements.

-

--keyword-group-blanks-delete, or -kgbd, causes blank lines between two statements with one of the sought keywords to be deleted before groups are formed. This will produce the most compact code and maximum group sizes. To leave these blank lines unchanged use --nokeyword-group-blanks-delete, or -nkgbd. Note that the -kgbd flag only removes blank lines between statements with one of the keywords being sought. Blanks before and after keyword sequences can be controlled with parameters -kgbb and -kgba. Also note that blanks deleted by this flag are not subject to any threshold number. All qualifying blank lines will be deleted.

+

--keyword-group-blanks-delete, or -kgbd, controls the deletion of any blank lines that exist in the the group when it is first scanned. When statements are initially scanned, any existing blank lines are included in the collection. Any such orignial blank lines will be deleted before any other insertions are made when the parameter -kgbd is set. The default is not to do this, -nkgbd.

-

--keyword-group-blanks, or -kgb, which is equivalent to setting -kgbb=1 -kgba=1 -kgbi=1. This turns on keyword group formatting with a set of default values.

+

--keyword-group-blanks, or -kgb, is an abbreviation equivalent to setting -kgbb=1 -kgba=1 -kgbi. This turns on keyword group formatting with a set of default values.

-

--nokeyword-group-blanks, or -nkgb, is equivalent to -kgbb=0 -kgba kgbi=0. This flag turns off keyword group blank lines and is the default setting.

+

--nokeyword-group-blanks, or -nkgb, is equivalent to -kgbb=0 -kgba nkgbi. This flag turns off keyword group blank lines and is the default setting.

Here are a few notes about the functioning of this technique.

diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 69a2bba0..a96846c3 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1800,7 +1800,7 @@ sub generate_options { $add_option->( 'keyword-group-blanks-count', 'kgbc', '=i' ); $add_option->( 'keyword-group-blanks-before', 'kgbb', '=i' ); $add_option->( 'keyword-group-blanks-after', 'kgba', '=i' ); - $add_option->( 'keyword-group-blanks-inside', 'kgbi', '=i' ); + $add_option->( 'keyword-group-blanks-inside', 'kgbi', '!' ); $add_option->( 'keyword-group-blanks-delete', 'kgbd', '!' ); $add_option->( 'blank-lines-after-opening-block', 'blao', '=i' ); @@ -1916,7 +1916,6 @@ sub generate_options { 'keyword-group-blanks-before' => [ 0, 2 ], 'keyword-group-blanks-after' => [ 0, 2 ], - 'keyword-group-blanks-inside' => [ 0, 1 ], ); # Note: we could actually allow negative ci if someone really wants it: @@ -1940,7 +1939,8 @@ sub generate_options { keyword-group-blanks-count=5 keyword-group-blanks-before=0 keyword-group-blanks-after=0 - keyword-group-blanks-inside=0 + nokeyword-group-blanks-inside + nokeyword-group-blanks-delete block-brace-tightness=0 block-brace-vertical-tightness=0 @@ -2080,10 +2080,10 @@ sub generate_options { 'noblanks-before-subs' => [qw(blbs=0 blbp=0)], 'nbbs' => [qw(blbs=0 blbp=0)], - 'keyword-group-blanks' => [qw(kgbb=1 kgbi=1 kgba=1)], - 'kgb' => [qw(kgbb=1 kgbi=1 kgba=1)], - 'nokeyword-group-blanks' => [qw(kgbb=0 kgbi=0 kgba=0)], - 'nkgb' => [qw(kgbb=0 kgbi=0 kgba=0)], + 'keyword-group-blanks' => [qw(kgbb=1 kgbi kgba=1)], + 'kgb' => [qw(kgbb=1 kgbi kgba=1)], + 'nokeyword-group-blanks' => [qw(kgbb=0 nkgbi kgba=0)], + 'nkgb' => [qw(kgbb=0 nkgbi kgba=0)], 'break-at-old-trinary-breakpoints' => [qw(bot)], diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index ecda7046..58f74df7 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -978,17 +978,21 @@ sub keyword_group_scan { my $Opt_blanks_after = $rOpts->{'keyword-group-blanks-after'}; # '-kgba' my $Opt_blanks_inside = $rOpts->{'keyword-group-blanks-inside'}; # '-kgbi' my $Opt_blanks_delete = $rOpts->{'keyword-group-blanks-delete'}; # '-kgbd' - my $Opt_long_count = $rOpts->{'keyword-group-blanks-count'}; # '-kgbc' - my $Opt_blanks_after_comments = $rOpts->{'blanks-after-comments'}; # '-bac' - my $Opt_pattern = - $keyword_group_list_pattern; # like '^(my|local|our|use)$'; return $rhash_of_desires unless ( $Opt_blanks_before || $Opt_blanks_after - || $Opt_blanks_inside + || $Opt_blanks_inside || $Opt_blanks_delete ); + $Opt_blanks_before = 0 unless defined($Opt_blanks_before); + $Opt_blanks_after = 0 unless defined($Opt_blanks_after); + + + my $Opt_long_count = $rOpts->{'keyword-group-blanks-count'}; # '-kgbc' + my $Opt_blanks_after_comments = $rOpts->{'blanks-after-comments'}; # '-bac' + my $Opt_pattern = $keyword_group_list_pattern; + my $rlines = $self->{rlines}; my $rLL = $self->{rLL}; my $K_closing_container = $self->{K_closing_container}; @@ -1003,7 +1007,7 @@ sub keyword_group_scan { # Here we place blanks around long sub-groups of keywords # if requested. - return unless ($Opt_blanks_inside); + return unless ( $Opt_blanks_inside ); my $ib = $sublist[0]->[0]; push @sublist, [ $iend + 1, "", 0 ]; @@ -1011,10 +1015,8 @@ sub keyword_group_scan { my $ie = $sublist[$j]->[0] - 1; my $num = $sublist[ $j - 1 ]->[2]; if ( $num >= $Opt_long_count ) { - if ($Opt_blanks_inside) { - $rhash_of_desires->{ $ib - 1 } = 1 unless ( $ib == $ibeg ); - $rhash_of_desires->{$ie} = 1 unless ( $ie == $iend ); - } + $rhash_of_desires->{ $ib - 1 } = 1 unless ( $ib == $ibeg ); + $rhash_of_desires->{$ie} = 1 unless ( $ie == $iend ); } $ib = $ie + 1; } @@ -1022,6 +1024,8 @@ sub keyword_group_scan { my $delete_if_blank = sub { my ($i) = @_; + + # delete line $i if it is blank return unless ( $i >= 0 && $i < @{$rlines} ); my $line_type = $rlines->[$i]->{_line_type}; return if ( $line_type ne 'CODE' ); @@ -1033,7 +1037,7 @@ sub keyword_group_scan { my $delete_inner_blank_lines = sub { # mark blank lines for deletion if requested - return unless $Opt_blanks_delete; + return unless ($Opt_blanks_delete ); # remove trailing blank lines from the list my $i_last_nonblank = $iend; @@ -1059,12 +1063,12 @@ sub keyword_group_scan { my ($bad_ending) = @_; if ( defined($ibeg) && $ibeg >= 0 ) { - # first do any blank deletions regardless of the count - $delete_inner_blank_lines->(); - # then handle sufficiently large groups if ( $count >= $Opt_long_count ) { + # do any blank deletions regardless of the count + $delete_inner_blank_lines->(); + if ( $ibeg > 0 ) { my $code_type = $rlines->[ $ibeg - 1 ]->{_code_type}; @@ -1179,20 +1183,12 @@ sub keyword_group_scan { } # continue in a verbatim (VB) type; it may be quoted text - if ( $CODE_type eq 'VB' ) { + # and continue in blank (BL) types + if ( $CODE_type eq 'VB' || $CODE_type eq 'BL') { if ( $ibeg >= 0 ) { $iend = $i; } next; } - # continue in blank (BL) types only if we are deleting blanks - if ( $CODE_type eq 'BL' ) { - if ( $ibeg >= 0 ) { - if ($Opt_blanks_delete) { $iend = $i } - else { $end_group->() } - } - next; - } - # examine the first token of this line my $rK_range = $line_of_tokens->{_rK_range}; ( $K_first, $K_last ) = @{$rK_range}; -- 2.39.5