From: Steve Hancock Date: Fri, 28 Dec 2018 14:58:19 +0000 (-0800) Subject: -kgbl can specify comment lines with 'BC' and 'SBC' X-Git-Tag: 20190601~34 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=3aed9a600f37ff3e2c9f104eef6f8fdb16557c61;p=perltidy.git -kgbl can specify comment lines with 'BC' and 'SBC' --- diff --git a/bin/perltidy b/bin/perltidy index e47c40e5..b0f87acb 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2771,7 +2771,7 @@ parameters rather than the B<-kgb> flag. The individual controls are as follows B<--keyword-group-blanks-list=s>, or B<-kgbl=s>, where B 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 B, but any list of keywords may be used. +our my sub">, but any list of keywords may be used. Comment lines may also be included in a keyword group, even though they are not keywords. To include ordinary block comments, include the symbol 'BC'. To include special block comments (which normally begin with '##'), include the symbol 'SBC'. B<--keyword-group-blanks-size=s>, or B<-kgbs=s>, where B is a string describing the number of consecutive keyword statements forming a group. If diff --git a/docs/perltidy.html b/docs/perltidy.html index b4321697..424638a1 100644 --- a/docs/perltidy.html +++ b/docs/perltidy.html @@ -2205,7 +2205,7 @@

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.

+

--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. Comment lines may also be included in a keyword group, even though they are not keywords. To include ordinary block comments, include the symbol 'BC'. To include special block comments (which normally begin with '##'), include the symbol 'SBC'.

--keyword-group-blanks-size=s, or -kgbs=s, where s is a string describing the number of consecutive keyword statements forming a group. If s is an integer then it is the minimum number required for a group. A maximum value may also be given with the format s=min.max, where min is the minimum number and max is the maximum number, and the min and max values are separated by one or more dots. No groups will be found if the maximum is less than the minimum. The maximum is unlimited if not given. The default is s=5. Some examples:

diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 2b7b5b91..4eb8ec82 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -243,6 +243,7 @@ use vars qw{ $block_brace_vertical_tightness_pattern $keyword_group_list_pattern + $keyword_group_list_comment_pattern $rOpts_add_newlines $rOpts_add_whitespace @@ -1021,7 +1022,8 @@ EOM || $Opt_blanks_inside || $Opt_blanks_delete ); - my $Opt_pattern = $keyword_group_list_pattern; + my $Opt_pattern = $keyword_group_list_pattern; + my $Opt_comment_pattern = $keyword_group_list_comment_pattern; my $Opt_repeat_count = $rOpts->{'keyword-group-blanks-repeat-count'}; # '-kgbr' @@ -1167,10 +1169,8 @@ EOM if ( $line_text && $line_text =~ /^#/ ); } - # Do not inseert a blank after a comment + # Do not insert a blank after a comment # (this could be subject to a flag in the future) - #if ( $Opt_blanks_after_comments != 0 - # || $code_type !~ /(BC|SBC|SBCX)/ ) if ( $code_type !~ /(BC|SBC|SBCX)/ ) { if ( $Opt_blanks_before == INSERT ) { $insert_blank_after->( $ibeg - 1 ); @@ -1229,6 +1229,7 @@ EOM # If the keyword lines ends with an open token, find the closing token # '$K_closing' so that we can easily skip past the contents of the # container. + return if ( $K_last <= $K_first ); my $KK = $K_last; my $type_last = $rLL->[$KK]->[_TYPE_]; my $tok_last = $rLL->[$KK]->[_TOKEN_]; @@ -1267,9 +1268,6 @@ EOM # New sub-group? if ( !@group || $token ne $group[-1]->[1] ) { push @subgroup, scalar(@group); - my $num = @group; - my $oldtok = $num ? $group[-1]->[1] : ""; - } push @group, [ $i, $token, $count ]; @@ -1344,6 +1342,30 @@ EOM my $token = $rLL->[$K_first]->[_TOKEN_]; my $ci_level = $rLL->[$K_first]->[_CI_LEVEL_]; + # see if this is a code type we seek (i.e. comment) + if ( $CODE_type + && $Opt_comment_pattern + && $CODE_type =~ /$Opt_comment_pattern/o ) + { + + my $tok = $CODE_type; + + # Continuing a group + if ( $ibeg >= 0 && $level == $level_beg ) { + $add_to_group->( $i, $tok, $level ); + } + + # Start new group + else { + + # first end old group if any; we might be starting new + # keywords at different level + if ( $ibeg > 0 ) { $end_group->(); } + $add_to_group->( $i, $tok, $level ); + } + next; + } + # See if it is a keyword we seek, but never start a group in a # continuation line; the code may be badly formatted. if ( $ci_level == 0 @@ -5962,13 +5984,27 @@ sub make_keyword_group_list_pattern { # turn any input list into a regex for recognizing selected block types. # Here are the defaults: - ##$keyword_group_list_pattern = '^((our|local|my|use|require|)$|sub)'; - $keyword_group_list_pattern = '^(our|local|my|use|require|)$'; + $keyword_group_list_pattern = '^(our|local|my|use|require|)$'; + $keyword_group_list_comment_pattern = ''; if ( defined( $rOpts->{'keyword-group-blanks-list'} ) && $rOpts->{'keyword-group-blanks-list'} ) { + my @words = split /\s+/, $rOpts->{'keyword-group-blanks-list'}; + my @keyword_list; + my @comment_list; + foreach my $word (@words) { + if ( $word =~ /^(BC|SBC)$/ ) { + push @comment_list, $word; + if ( $word eq 'SBC' ) { push @comment_list, 'SBCX' } + } + else { + push @keyword_list, $word; + } + } $keyword_group_list_pattern = make_block_pattern( '-kgbl', $rOpts->{'keyword-group-blanks-list'} ); + $keyword_group_list_comment_pattern = + make_block_pattern( '-kgbl', join( ' ', @comment_list ) ); } return; }