From de3a9f1faa2451bc161495f97e9cd9fdfe3dcb15 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 29 Jul 2024 14:16:13 -0700 Subject: [PATCH] add --closing-side-comment-exclusion-list --- CHANGES.md | 6 ++++ bin/perltidy | 19 ++++++++++-- lib/Perl/Tidy.pm | 59 +++++++++++++++++++------------------ lib/Perl/Tidy/Formatter.pm | 42 ++++++++++++++++++-------- t/snippets/csc.in | 9 ++++++ t/snippets/csc1.par | 2 +- t/snippets/csc3.par | 1 + t/snippets/expect/csc.csc1 | 9 ++++++ t/snippets/expect/csc.csc2 | 9 ++++++ t/snippets/expect/csc.csc3 | 17 +++++++++++ t/snippets/expect/csc.def | 9 ++++++ t/snippets/packing_list.txt | 3 +- t/snippets18.t | 38 +++++++++++++++++++++++- t/snippets30.t | 46 +++++++++++++++++++++++++++++ 14 files changed, 221 insertions(+), 48 deletions(-) create mode 100644 t/snippets/csc3.par create mode 100644 t/snippets/expect/csc.csc3 diff --git a/CHANGES.md b/CHANGES.md index 75ef969c..6909225b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/bin/perltidy b/bin/perltidy index 8333df00..4147b76a 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -1969,10 +1969,23 @@ label (such as C, C, 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's, labels, C, and C blocks be -affected by any B<-csc> or B<-dcsc> operation: +requests that only C's, labels, C, and C 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 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> diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index fbfcdffe..2c474ab7 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -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 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 9eaf2ab8..4bfa1283 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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 diff --git a/t/snippets/csc.in b/t/snippets/csc.in index 87ba7a89..b04001e8 100644 --- a/t/snippets/csc.in +++ b/t/snippets/csc.in @@ -6,3 +6,12 @@ print( $_[0], "\n" ); } } ## end sub message + + my $message =sub { + if ( !defined( $_[0] ) ) { + print("Hello, World\n"); + } + else { + print( $_[0], "\n" ); + } + }; diff --git a/t/snippets/csc1.par b/t/snippets/csc1.par index 19840cab..3594c52f 100644 --- a/t/snippets/csc1.par +++ b/t/snippets/csc1.par @@ -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 index 00000000..19840cab --- /dev/null +++ b/t/snippets/csc3.par @@ -0,0 +1 @@ +-csc -csci=2 -ncscb diff --git a/t/snippets/expect/csc.csc1 b/t/snippets/expect/csc.csc1 index 6a0fe7b7..8e1444c3 100644 --- a/t/snippets/expect/csc.csc1 +++ b/t/snippets/expect/csc.csc1 @@ -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] )) + }; diff --git a/t/snippets/expect/csc.csc2 b/t/snippets/expect/csc.csc2 index 5d390ad6..f42c90de 100644 --- a/t/snippets/expect/csc.csc2 +++ b/t/snippets/expect/csc.csc2 @@ -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 index 00000000..49dbe2e6 --- /dev/null +++ b/t/snippets/expect/csc.csc3 @@ -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 diff --git a/t/snippets/expect/csc.def b/t/snippets/expect/csc.def index 87ba7a89..55038298 100644 --- a/t/snippets/expect/csc.def +++ b/t/snippets/expect/csc.def @@ -6,3 +6,12 @@ print( $_[0], "\n" ); } } ## end sub message + + my $message = sub { + if ( !defined( $_[0] ) ) { + print("Hello, World\n"); + } + else { + print( $_[0], "\n" ); + } + }; diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 042f2c81..2636797b 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -452,6 +452,7 @@ ../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 @@ -572,4 +573,4 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets30.t logical_xor.def +../snippets30.t csc.csc3 diff --git a/t/snippets18.t b/t/snippets18.t index 6f532b05..7aea3795 100644 --- a/t/snippets18.t +++ b/t/snippets18.t @@ -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........... }, diff --git a/t/snippets30.t b/t/snippets30.t index 1008631c..ea2307ce 100644 --- a/t/snippets30.t +++ b/t/snippets30.t @@ -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}; -- 2.39.5