From f180769e92e1c692668c54d05c404bc2de6549bd Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 7 Dec 2018 09:27:48 -0800 Subject: [PATCH] added kgb repeat count & adjusted controls --- bin/perltidy | 17 ++++++++++++----- docs/perltidy.html | 8 +++++--- lib/Perl/Tidy.pm | 6 ++++-- lib/Perl/Tidy/Formatter.pm | 18 ++++++++++++++---- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index c7e3ea79..fe53df24 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2673,7 +2673,7 @@ employ these controls but they can be enabled with the following parameters: B<--keyword-group-blanks-list=s>, or B<-kgbl=s>; B is a quoted string of keywords -B<--keyword-group-blanks-count=n>, or B<-kgbc=n>; B is a threshold group size +B<--keyword-group-blanks-threshold-count=n>, or B<-kgbt=n>; B is a threshold (minimum) group size B<--keyword-group-blanks-before=n>, or B<-kgbb=n>; B = (0, 1, or 2) controls a leading blank @@ -2741,10 +2741,10 @@ defines the set of keywords which will be formed into groups. The string is a space separated list of keywords. The default set is B, but any list of keywords may be used. -B<--keyword-group-blanks-count=n>, or B<-kgbc=n>, where B is the minimum +B<--keyword-group-blanks-threshold-count=n>, or B<-kgbt=n>, where B is the minimum number of consecutive keyword statements to which these controls apply. The -default is B. The special value B can be used to turn off all -searching for keyword groups. +default is B. The special value B is the same as n=infinity and +can be used to turn off all searching for keyword groups. B<--keyword-group-blanks-before=n>, or B<-kgbb=n>, specifies whether a blank should appear before the first line of the group, as follows: @@ -2760,7 +2760,7 @@ scheme (0=delete, 1=stable, 2=insert). 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 +the minimum threshold number (as specified with B<-kgbt=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. @@ -2772,6 +2772,13 @@ 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-repeat-count=n>, or B<-kgbr=n>, where B is the +maximum number of times this logic will be applied to any file. The special +value B is the same as n=infinity which means it will be applied to an +entire script [Default]. A value B could be used to make it apply just +one time for example. This might be useful for adjusting just the B +statements in the top part of a module for example. + 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. diff --git a/docs/perltidy.html b/docs/perltidy.html index 500198c8..308273b7 100644 --- a/docs/perltidy.html +++ b/docs/perltidy.html @@ -2115,7 +2115,7 @@

--keyword-group-blanks-list=s, or -kgbl=s; s is a quoted string of keywords

-

--keyword-group-blanks-count=n, or -kgbc=n; n is a threshold group size

+

--keyword-group-blanks-threshold-count=n, or -kgbt=n; n is a threshold (minimum) group size

--keyword-group-blanks-before=n, or -kgbb=n; n = (0, 1, or 2) controls a leading blank

@@ -2172,7 +2172,7 @@

--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.

-

--keyword-group-blanks-count=n, or -kgbc=n, where n is the minimum number of consecutive keyword statements to which these controls apply. The default is n=5. The special value n=0 can be used to turn off all searching for keyword groups.

+

--keyword-group-blanks-threshold-count=n, or -kgbt=n, where n is the minimum number of consecutive keyword statements to which these controls apply. The default is n=5. The special value n=0 is the same as n=infinity and can be used to turn off all searching for keyword groups.

--keyword-group-blanks-before=n, or -kgbb=n, specifies whether a blank should appear before the first line of the group, as follows:

@@ -2182,10 +2182,12 @@

--keyword-group-blanks-after=n, or -kgba=n, likewise specifies whether a blank should appear after the last line of the group, using the same scheme (0=delete, 1=stable, 2=insert).

-

--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-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 -kgbt=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, 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-repeat-count=n, or -kgbr=n, where n is the maximum number of times this logic will be applied to any file. The special value n=0 is the same as n=infinity which means it will be applied to an entire script [Default]. A value n=1 could be used to make it apply just one time for example. This might be useful for adjusting just the use statements in the top part of a module for example.

+

--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 nkgbi. This flag turns off keyword group blank lines and is the default setting.

diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index ca7c485b..0755f732 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1797,7 +1797,8 @@ sub generate_options { $add_option->( 'blanks-after-comments', 'bac', '!' ); $add_option->( 'keyword-group-blanks-list', 'kgbl', '=s' ); - $add_option->( 'keyword-group-blanks-count', 'kgbc', '=i' ); + $add_option->( 'keyword-group-blanks-threshold-count', 'kgbt', '=i' ); + $add_option->( 'keyword-group-blanks-repeat-count', 'kgbr', '=i' ); $add_option->( 'keyword-group-blanks-before', 'kgbb', '=i' ); $add_option->( 'keyword-group-blanks-after', 'kgba', '=i' ); $add_option->( 'keyword-group-blanks-inside', 'kgbi', '!' ); @@ -1936,7 +1937,8 @@ sub generate_options { blank-lines-before-packages=1 noblanks-after-comments - keyword-group-blanks-count=5 + keyword-group-blanks-threshold-count=5 + keyword-group-blanks-repeat-count=0 keyword-group-blanks-before=1 keyword-group-blanks-after=1 nokeyword-group-blanks-inside diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 749fd313..82251750 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -978,7 +978,7 @@ 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_threshold_count = $rOpts->{'keyword-group-blanks-threshold-count'}; # '-kgbt' # codes for $Opt_blanks_before and $Opt_blanks_after: # 0 = never (delete if exist) @@ -986,7 +986,7 @@ sub keyword_group_scan { # 2 = always (insert if missing) return $rhash_of_desires - unless $Opt_long_count > 0 + unless $Opt_threshold_count > 0 && ( $Opt_blanks_before != 1 || $Opt_blanks_after != 1 || $Opt_blanks_inside @@ -994,6 +994,8 @@ sub keyword_group_scan { my $Opt_blanks_after_comments = $rOpts->{'blanks-after-comments'}; # '-bac' my $Opt_pattern = $keyword_group_list_pattern; + my $Opt_repeat_count = + $rOpts->{'keyword-group-blanks-repeat-count'}; # '-kgbr' my $rlines = $self->{rlines}; my $rLL = $self->{rLL}; @@ -1005,6 +1007,8 @@ sub keyword_group_scan { # These vars will contain values for the most recently seen line: my ( $line_type, $CODE_type, $K_first, $K_last ); + my $number_of_groups_seen = 0; + my $split_into_sub_groups = sub { # Here we place blanks around long sub-groups of keywords @@ -1016,7 +1020,7 @@ sub keyword_group_scan { for ( my $j = 1 ; $j < @sublist ; $j++ ) { my $ie = $sublist[$j]->[0] - 1; my $num = $sublist[ $j - 1 ]->[2]; - if ( $num >= $Opt_long_count ) { + if ( $num >= $Opt_threshold_count ) { $rhash_of_desires->{ $ib - 1 } = 1 unless ( $ib == $ibeg ); $rhash_of_desires->{$ie} = 1 unless ( $ie == $iend ); } @@ -1066,7 +1070,9 @@ sub keyword_group_scan { if ( defined($ibeg) && $ibeg >= 0 ) { # then handle sufficiently large groups - if ( $count >= $Opt_long_count ) { + if ( $count >= $Opt_threshold_count ) { + + $number_of_groups_seen++; # do any blank deletions regardless of the count $delete_inner_blank_lines->(); @@ -1166,7 +1172,11 @@ sub keyword_group_scan { # loop over all lines of the source my $i = -1; foreach my $line_of_tokens ( @{$rlines} ) { + $i++; + last + if ( $Opt_repeat_count > 0 + && $number_of_groups_seen >= $Opt_repeat_count ); $CODE_type = ""; $K_first = undef; -- 2.39.5