]> git.donarmstrong.com Git - perltidy.git/commitdiff
add --closing-side-comment-exclusion-list
authorSteve Hancock <perltidy@users.sourceforge.net>
Mon, 29 Jul 2024 21:16:13 +0000 (14:16 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Mon, 29 Jul 2024 21:16:13 +0000 (14:16 -0700)
14 files changed:
CHANGES.md
bin/perltidy
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm
t/snippets/csc.in
t/snippets/csc1.par
t/snippets/csc3.par [new file with mode: 0644]
t/snippets/expect/csc.csc1
t/snippets/expect/csc.csc2
t/snippets/expect/csc.csc3 [new file with mode: 0644]
t/snippets/expect/csc.def
t/snippets/packing_list.txt
t/snippets18.t
t/snippets30.t

index 75ef969ca4da6933080fb499a75c6d36c71e3466..6909225b6c75fdbfceb405b498a383e2afa54e3c 100644 (file)
@@ -2,6 +2,12 @@
 
 ## 2024 05 11.07
 
+    - Add parameter --closing-side-comment-exclusion-list=string, or
+      -cscxl=string, where string is a list of block types to exclude
+      for closing side comment operations.  Also, closing side comments
+      now work for anonymous subs.  Use -cscxl=asub if you do not
+      want this.
+
     - Include signature variables in --dump-unusual-variables and
       --warn-variable-types; see git #158.
 
index 8333df00f774a90cb20a912c42f258fd444f129b..4147b76a005639c8836015c27032db3166add552 100755 (executable)
@@ -1969,10 +1969,23 @@ label (such as C<if>, C<sub>, and so on) will be tagged.  The B<-cscl>
 command changes the default list to be any selected block types; see
 L<"Specifying Block Types">.
 For example, the following command
-requests that only C<sub>'s, labels, C<BEGIN>, and C<END> blocks be
-affected by any B<-csc> or B<-dcsc> operation:
+requests that only C<sub>'s, labels, C<BEGIN>, and C<END> blocks be affected by
+operations which add (B<-csc>) or delete (B<-dcsc>) closing side comments:
 
-   -cscl="sub : BEGIN END"
+   --closing-side-comment-list='sub : BEGIN END'
+
+=item B<-cscxl=string>, or B<--closing-side-comment-exclusion-list>
+
+where C<string> is a list of block types which should NOT be tagged with
+closing side comments. If a block type appears in both B<-cscl> and
+B<-cscxl>, then B<-cscxl> has priority and the block will not be tagged.
+
+For example, the following command requests that anonymous subs
+should not be affected by any B<-csc> or B<-dcsc> operation:
+
+   --closing-side-comment-exclusion-list='asub'
+
+By default, no block types are excluded.
 
 =item B<-csct=n>, or B<--closing-side-comment-maximum-text=n>
 
index fbfcdffe4d1f6baa46e4ece63c0604c62ba75f85..2c474ab7ab6b9990bc0602ba5cf32d9d70c0fc39 100644 (file)
@@ -3564,35 +3564,36 @@ sub generate_options {
     ########################################
     $category = 4;    # Comment controls
     ########################################
-    $add_option->( 'closing-side-comment-else-flag',    'csce', '=i' );
-    $add_option->( 'closing-side-comment-interval',     'csci', '=i' );
-    $add_option->( 'closing-side-comment-list',         'cscl', '=s' );
-    $add_option->( 'closing-side-comment-maximum-text', 'csct', '=i' );
-    $add_option->( 'closing-side-comment-prefix',       'cscp', '=s' );
-    $add_option->( 'closing-side-comment-warnings',     'cscw', '!' );
-    $add_option->( 'closing-side-comments',             'csc',  '!' );
-    $add_option->( 'closing-side-comments-balanced',    'cscb', '!' );
-    $add_option->( 'code-skipping',                     'cs',   '!' );
-    $add_option->( 'code-skipping-begin',               'csb',  '=s' );
-    $add_option->( 'code-skipping-end',                 'cse',  '=s' );
-    $add_option->( 'format-skipping',                   'fs',   '!' );
-    $add_option->( 'format-skipping-begin',             'fsb',  '=s' );
-    $add_option->( 'format-skipping-end',               'fse',  '=s' );
-    $add_option->( 'hanging-side-comments',             'hsc',  '!' );
-    $add_option->( 'indent-block-comments',             'ibc',  '!' );
-    $add_option->( 'indent-spaced-block-comments',      'isbc', '!' );
-    $add_option->( 'fixed-position-side-comment',       'fpsc', '=i' );
-    $add_option->( 'minimum-space-to-comment',          'msc',  '=i' );
-    $add_option->( 'non-indenting-braces',              'nib',  '!' );
-    $add_option->( 'non-indenting-brace-prefix',        'nibp', '=s' );
-    $add_option->( 'outdent-long-comments',             'olc',  '!' );
-    $add_option->( 'outdent-static-block-comments',     'osbc', '!' );
-    $add_option->( 'static-block-comment-prefix',       'sbcp', '=s' );
-    $add_option->( 'static-block-comments',             'sbc',  '!' );
-    $add_option->( 'static-side-comment-prefix',        'sscp', '=s' );
-    $add_option->( 'static-side-comments',              'ssc',  '!' );
-    $add_option->( 'ignore-side-comment-lengths',       'iscl', '!' );
-    $add_option->( 'ignore-perlcritic-comments',        'ipc',  '!' );
+    $add_option->( 'closing-side-comment-else-flag',      'csce',  '=i' );
+    $add_option->( 'closing-side-comment-interval',       'csci',  '=i' );
+    $add_option->( 'closing-side-comment-list',           'cscl',  '=s' );
+    $add_option->( 'closing-side-comment-exclusion-list', 'cscxl', '=s' );
+    $add_option->( 'closing-side-comment-maximum-text',   'csct',  '=i' );
+    $add_option->( 'closing-side-comment-prefix',         'cscp',  '=s' );
+    $add_option->( 'closing-side-comment-warnings',       'cscw',  '!' );
+    $add_option->( 'closing-side-comments',               'csc',   '!' );
+    $add_option->( 'closing-side-comments-balanced',      'cscb',  '!' );
+    $add_option->( 'code-skipping',                       'cs',    '!' );
+    $add_option->( 'code-skipping-begin',                 'csb',   '=s' );
+    $add_option->( 'code-skipping-end',                   'cse',   '=s' );
+    $add_option->( 'format-skipping',                     'fs',    '!' );
+    $add_option->( 'format-skipping-begin',               'fsb',   '=s' );
+    $add_option->( 'format-skipping-end',                 'fse',   '=s' );
+    $add_option->( 'hanging-side-comments',               'hsc',   '!' );
+    $add_option->( 'indent-block-comments',               'ibc',   '!' );
+    $add_option->( 'indent-spaced-block-comments',        'isbc',  '!' );
+    $add_option->( 'fixed-position-side-comment',         'fpsc',  '=i' );
+    $add_option->( 'minimum-space-to-comment',            'msc',   '=i' );
+    $add_option->( 'non-indenting-braces',                'nib',   '!' );
+    $add_option->( 'non-indenting-brace-prefix',          'nibp',  '=s' );
+    $add_option->( 'outdent-long-comments',               'olc',   '!' );
+    $add_option->( 'outdent-static-block-comments',       'osbc',  '!' );
+    $add_option->( 'static-block-comment-prefix',         'sbcp',  '=s' );
+    $add_option->( 'static-block-comments',               'sbc',   '!' );
+    $add_option->( 'static-side-comment-prefix',          'sscp',  '=s' );
+    $add_option->( 'static-side-comments',                'ssc',   '!' );
+    $add_option->( 'ignore-side-comment-lengths',         'iscl',  '!' );
+    $add_option->( 'ignore-perlcritic-comments',          'ipc',   '!' );
 
     ########################################
     $category = 5;    # Linebreak controls
index 9eaf2ab8ce0a18222a9d9dd4bd6f8c5f7d226c71..4bfa1283d6085b2582e9330f92557e13eafc64b2 100644 (file)
@@ -454,6 +454,7 @@ my (
     # INITIALIZER: sub make_closing_side_comment_list_pattern
     $closing_side_comment_list_pattern,
     $closing_side_comment_want_asub,
+    $closing_side_comment_exclusion_pattern,
 
     # Table to efficiently find indentation and max line length
     # from level.
@@ -5690,14 +5691,28 @@ sub make_closing_side_comment_list_pattern {
 
     # turn any input list into a regex for recognizing selected block types
     $closing_side_comment_list_pattern = '^\w+';
-    $closing_side_comment_want_asub    = 0;
-    if ( defined( $rOpts->{'closing-side-comment-list'} )
-        && $rOpts->{'closing-side-comment-list'} )
-    {
+
+    # '1' is an impossible block name
+    $closing_side_comment_exclusion_pattern = '^1';
+
+    # Need a separate flag for anonymous subs because they are the only
+    # types where the side comment might follow a ';'
+    $closing_side_comment_want_asub = 1;
+
+    my $cscl = $rOpts->{'closing-side-comment-list'};
+    if ( defined($cscl) && $cscl ) {
         $closing_side_comment_list_pattern =
-          make_block_pattern( '-cscl', $rOpts->{'closing-side-comment-list'} );
-        $closing_side_comment_want_asub =
-          $rOpts->{'closing-side-comment-list'} =~ /\basub\b/;
+          make_block_pattern( '-cscl', $cscl );
+        $closing_side_comment_want_asub = $cscl =~ /\basub\b/;
+    }
+
+    my $cscxl = $rOpts->{'closing-side-comment-exclusion-list'};
+    if ( defined($cscxl) && $cscxl ) {
+        $closing_side_comment_exclusion_pattern =
+          make_block_pattern( '-cscxl', $cscxl );
+        if ( $cscxl =~ /\basub\b/ ) {
+            $closing_side_comment_want_asub = 0;
+        }
     }
     return;
 } ## end sub make_closing_side_comment_list_pattern
@@ -10564,7 +10579,9 @@ EOM
                 my $block_type_m = $rblock_type_of_seqno->{$seqno_m};
                 if (   $block_type_m
                     && $token        =~ /$closing_side_comment_prefix_pattern/
-                    && $block_type_m =~ /$closing_side_comment_list_pattern/ )
+                    && $block_type_m =~ /$closing_side_comment_list_pattern/
+                    && $block_type_m !~
+                    /$closing_side_comment_exclusion_pattern/ )
                 {
                     $delete_side_comment = 1;
                 }
@@ -38053,7 +38070,8 @@ sub set_vertical_tightness_flags {
             if (   $type eq 'k'
                 && $csc_new_statement_ok
                 && $is_if_elsif_else_unless_while_until_for_foreach{$token}
-                && $token =~ /$closing_side_comment_list_pattern/ )
+                && $token =~ /$closing_side_comment_list_pattern/
+                && $token !~ /$closing_side_comment_exclusion_pattern/ )
             {
                 $self->set_block_text_accumulator($i);
             }
@@ -38343,10 +38361,8 @@ sub add_closing_side_comment {
         # .. and if this is one of the types of interest
         && $block_type_to_go[$i_terminal] =~
         /$closing_side_comment_list_pattern/
-
-        # anonymous sub only if specifically requested (c380)
-        && (   $block_type_to_go[$i_terminal] ne 'sub'
-            || $closing_side_comment_want_asub )
+        && $block_type_to_go[$i_terminal] !~
+        /$closing_side_comment_exclusion_pattern/
 
         # ..and the corresponding opening brace must is not in this batch
         # (because we do not need to tag one-line blocks, although this
index 87ba7a895ad76617a002d559f993e873403f04c0..b04001e8e3ef9573b1825e6bb738345f25d5ab5c 100644 (file)
@@ -6,3 +6,12 @@
                 print( $_[0], "\n" );
             }
         } ## end sub message
+
+        my $message =sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
index 19840cab62c079736475e9ad0af2b3ae9ec349fe..3594c52f41e5bb59ec4a77e25802eda9f87bd578 100644 (file)
@@ -1 +1 @@
--csc -csci=2 -ncscb
+-csc -csci=2 -ncscb -cscxl=asub
diff --git a/t/snippets/csc3.par b/t/snippets/csc3.par
new file mode 100644 (file)
index 0000000..19840ca
--- /dev/null
@@ -0,0 +1 @@
+-csc -csci=2 -ncscb
index 6a0fe7b755d386294d60f0cf8303161fd54dc087..8e1444c3053003e31f8db20860e26b59a7ac19b8 100644 (file)
@@ -6,3 +6,12 @@
                 print( $_[0], "\n" );
             } ## end else [ if ( !defined( $_[0] ))
         } ## end sub message
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            } ## end if ( !defined( $_[0] ))
+            else {
+                print( $_[0], "\n" );
+            } ## end else [ if ( !defined( $_[0] ))
+        };
index 5d390ad6371007e76d0d77e5d30dd008792b9646..f42c90de4fdedfb14efae97e5976447777631f65 100644 (file)
@@ -6,3 +6,12 @@
                 print( $_[0], "\n" );
             }
         }
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
diff --git a/t/snippets/expect/csc.csc3 b/t/snippets/expect/csc.csc3
new file mode 100644 (file)
index 0000000..49dbe2e
--- /dev/null
@@ -0,0 +1,17 @@
+        sub message {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            } ## end if ( !defined( $_[0] ))
+            else {
+                print( $_[0], "\n" );
+            } ## end else [ if ( !defined( $_[0] ))
+        } ## end sub message
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            } ## end if ( !defined( $_[0] ))
+            else {
+                print( $_[0], "\n" );
+            } ## end else [ if ( !defined( $_[0] ))
+        }; ## end $message = sub
index 87ba7a895ad76617a002d559f993e873403f04c0..550382989ad9c21a74bea7195a2dffa60ac51be9 100644 (file)
@@ -6,3 +6,12 @@
                 print( $_[0], "\n" );
             }
         } ## end sub message
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
index 042f2c81d6b5a9eeedea494adc11354391e743c3..2636797b06f75b305d031c8da16e75a79d964414 100644 (file)
 ../snippets30.t        dltc.def
 ../snippets30.t        dltc.dltc1
 ../snippets30.t        dltc.dltc2
+../snippets30.t        logical_xor.def
 ../snippets4.t gnu1.gnu
 ../snippets4.t gnu2.def
 ../snippets4.t gnu2.gnu
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets30.t        logical_xor.def
+../snippets30.t        csc.csc3
index 6f532b0553df859d5c4b1f32a3851fcf23f639ff..7aea37952f533ec7bc655159bd221f0d0f689fc2 100644 (file)
@@ -44,7 +44,7 @@ BEGIN {
 # testing --delete-side-comments and --nostatic-block-comments
 -dsc -nsbc
 ----------
-        'csc1'   => "-csc -csci=2 -ncscb",
+        'csc1'   => "-csc -csci=2 -ncscb -cscxl=asub",
         'csc2'   => "-dcsc",
         'def'    => "",
         'iob'    => "-iob",
@@ -221,6 +221,15 @@ Some pod after __END__ to delete with -dp and trim with -trp
                 print( $_[0], "\n" );
             }
         } ## end sub message
+
+        my $message =sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
 ----------
 
         'iob' => <<'----------',
@@ -702,6 +711,15 @@ catch {
                 print( $_[0], "\n" );
             } ## end else [ if ( !defined( $_[0] ))
         } ## end sub message
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            } ## end if ( !defined( $_[0] ))
+            else {
+                print( $_[0], "\n" );
+            } ## end else [ if ( !defined( $_[0] ))
+        };
 #9...........
         },
 
@@ -717,6 +735,15 @@ catch {
                 print( $_[0], "\n" );
             }
         }
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
 #10...........
         },
 
@@ -732,6 +759,15 @@ catch {
                 print( $_[0], "\n" );
             }
         } ## end sub message
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
 #11...........
         },
 
index 1008631c034769e3b4e7a859234c5d8404251ba0..ea2307ce1725c25fba9a3f018c405facde528de6 100644 (file)
@@ -12,6 +12,7 @@
 #9 dltc.dltc1
 #10 dltc.dltc2
 #11 logical_xor.def
+#12 csc.csc3
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -31,6 +32,7 @@ BEGIN {
     $rparams = {
         'altc1'  => "-atc -wtc=m",
         'altc2'  => "-altc -atc -wtc=m",
+        'csc3'   => "-csc -csci=2 -ncscb",
         'def'    => "",
         'dltc1'  => "-dtc -wtc=0",
         'dltc2'  => "-dtc -wtc=0 -ndltc",
@@ -58,6 +60,26 @@ $self->make_grammar(
 );
 ----------
 
+        'csc' => <<'----------',
+        sub message {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        } ## end sub message
+
+        my $message =sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            }
+            else {
+                print( $_[0], "\n" );
+            }
+        };
+----------
+
         'dltc' => <<'----------',
 $self->make_grammar(
     {
@@ -242,6 +264,30 @@ $self->make_grammar(
 $x ^^ $y and say "One of x or y is true, but not both";
 #11...........
         },
+
+        'csc.csc3' => {
+            source => "csc",
+            params => "csc3",
+            expect => <<'#12...........',
+        sub message {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            } ## end if ( !defined( $_[0] ))
+            else {
+                print( $_[0], "\n" );
+            } ## end else [ if ( !defined( $_[0] ))
+        } ## end sub message
+
+        my $message = sub {
+            if ( !defined( $_[0] ) ) {
+                print("Hello, World\n");
+            } ## end if ( !defined( $_[0] ))
+            else {
+                print( $_[0], "\n" );
+            } ## end else [ if ( !defined( $_[0] ))
+        }; ## end $message = sub
+#12...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};