]> git.donarmstrong.com Git - perltidy.git/commitdiff
-kgbl can specify comment lines with 'BC' and 'SBC'
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 28 Dec 2018 14:58:19 +0000 (06:58 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 28 Dec 2018 14:58:19 +0000 (06:58 -0800)
bin/perltidy
docs/perltidy.html
lib/Perl/Tidy/Formatter.pm

index e47c40e5855ec8ce8e8044bc676aeab1ca691009..b0f87acb7699d4848566053798ebb9cf82eb12fe 100755 (executable)
@@ -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<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 B<s="use require local
-our my sub">, 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<s> is a string
 describing the number of consecutive keyword statements forming a group.  If
index b4321697d2f32ff80cd3d0def7dc6666bfadf09f..424638a161cab22de925f62ecead33a9f734dad2 100644 (file)
 
 <p>Finer control over blank placement can be achieved by using the individual parameters rather than the <b>-kgb</b> flag. The individual controls are as follows.</p>
 
-<p><b>--keyword-group-blanks-list=s</b>, or <b>-kgbl=s</b>, where <b>s</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>s=&quot;use require local our my sub&quot;</b>, but any list of keywords may be used.</p>
+<p><b>--keyword-group-blanks-list=s</b>, or <b>-kgbl=s</b>, where <b>s</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>s=&quot;use require local our my sub&quot;</b>, 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 &#39;BC&#39;. To include special block comments (which normally begin with &#39;##&#39;), include the symbol &#39;SBC&#39;.</p>
 
 <p><b>--keyword-group-blanks-size=s</b>, or <b>-kgbs=s</b>, where <b>s</b> is a string describing the number of consecutive keyword statements forming a group. If <b>s</b> is an integer then it is the minimum number required for a group. A maximum value may also be given with the format <b>s=min.max</b>, where <b>min</b> is the minimum number and <b>max</b> 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 <b>s=5</b>. Some examples:</p>
 
index 2b7b5b916c97cc49477b2295fa904f708d45f6d4..4eb8ec82aec318731a81a0add0282ce3b1ccefc1 100644 (file)
@@ -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;
 }