From 27295978d179ba80ee35f66fea17e81d6c823952 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Thu, 20 Oct 2022 14:42:53 -0700 Subject: [PATCH] split -wtc=h into -wtc=h and -wtc=i --- bin/perltidy | 30 ++++++++++++--------- lib/Perl/Tidy/Formatter.pm | 45 ++++++++++++++++++------------- t/snippets/expect/wtc.wtc7 | 42 +++++++++++++++++++++++++++++ t/snippets/packing_list.txt | 5 ++-- t/snippets/wtc6.par | 2 +- t/snippets/wtc7.par | 1 + t/snippets27.t | 53 ++++++++++++++++++++++++++++++++++++- 7 files changed, 143 insertions(+), 35 deletions(-) create mode 100644 t/snippets/expect/wtc.wtc7 create mode 100644 t/snippets/wtc7.par diff --git a/bin/perltidy b/bin/perltidy index 5334f7e2..a44ce53c 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -292,7 +292,7 @@ default and is specified with B<-bm='copy'>. The older implementation is specified with B<-bm='move'>. The difference is that the older implementation made the backup by moving the input file to the backup file, and the newer implementation makes the backup by copying the input file. The newer -implemenation preserves the file system B value. This may avoid +implementation preserves the file system B value. This may avoid problems with other software running simultaneously. This change was made as part of issue B at github. @@ -3512,12 +3512,9 @@ Since the default is not to add or delete commas, this feature is off by default =item B<--want-trailing-commas=s> or B<-wtc=s>, B<--add-trailing-commas> or B<-atc>, and B<--delete-trailing-commas> or B<-dtc> A trailing comma is a comma following the last item of a list. Perl allows -trailing commas but they are not required. So using them is optional, but they -can be useful. In particular, having a comma at the end of each line of a list -can simplify the use of an editor to reorder or add new lines. - -By default, perltidy does not add or delete commas, but it is possible to -manipulate trailing commas with the following set of three related parameters: +trailing commas but they are not required. By default, perltidy does not add +or delete trailing commas, but it is possible to manipulate them with the +following set of three related parameters: --want-trailing-commas=s, -wtc=s - defines where trailing commas are wanted --add-trailing-commas, -atc - gives permission to add trailing commas to match the style wanted @@ -3529,8 +3526,10 @@ The parameter B<--want-trailing-commas=s>, or B<-wtc=s>, defines a preferred sty s=1 or * : every list should have a trailing comma s=m a multi-line list should have a trailing commas s=b trailing commas should be 'bare' (comma followed by newline) - s=h trailing commas should be 'bare' for lists of key=>value pairs, or other lists with about one comma per line. - s=' ', or -wtc not defined : leave trailing commas unchanged [DEFAULT]. + s=h lists of key=>value pairs, with about one one '=>' and one ',' per line, + with a bare trailing comma + s=i lists with about one comma per line, with a bare trailing comma + s=' ' or -wtc not defined : leave trailing commas unchanged [DEFAULT]. This parameter by itself only indicates the where trailing commas are wanted. Perltidy only adds these trailing commas if the flag B<--add-trailing-commas>, or B<-atc> is set. And perltidy only removes unwanted trailing commas @@ -3539,10 +3538,10 @@ if the flag B<--delete-trailing-commas>, or B<-dtc> is set. Here are some example parameter combinations and their meanings -wtc=0 -dtc : delete all trailing commas - -wtc=1 -atc : put trailing commas on all lists - -wtc=m -atc : all multi-line lists get trailing commas, and + -wtc=1 -atc : all lists get trailing commas + -wtc=m -atc : all multi-line lists get trailing commas, but single line lists remain unchanged. - -wtc=m -dtc : multi-line lists remain unchanged, and + -wtc=m -dtc : multi-line lists remain unchanged, but any trailing commas on single line lists are removed. -wtc=m -atc -dtc : all multi-line lists get trailing commas, and any trailing commas on single line lists are removed. @@ -3650,6 +3649,11 @@ or two to reach a final state. When using these parameters for the first time it is a good idea to practice on some test scripts and verify that the results are as expected. +=item * + +Since the default behavior is not to add or delete commas, these parameters +can be useful on a temporary basis for reformatting a script. + =back =item B<-dwic>, B<--delete-weld-interfering-commas> @@ -3747,7 +3751,7 @@ For example, given this snippet: By default, a method call arrow C<-E> is considered a candidate for a breakpoint, but method chains will fill to the line width before a break is considered. With B<-bom>, breaks before the arrow are preserved, so if you -have preformatted a method chain: +have pre-formatted a method chain: my $q = $rs ->related_resultset('CDs') diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index ab966326..3fd0f2a4 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -2490,7 +2490,7 @@ sub initialize_trailing_comma_rules { # This routine must be called after the alpha and beta stress levels # have been defined. - my $rvalid_flags = [qw(0 1 * m b h)]; + my $rvalid_flags = [qw(0 1 * m b h i)]; my $option = $rOpts->{'want-trailing-commas'}; @@ -8000,13 +8000,18 @@ sub match_trailing_comma_rule { # List of $trailing_comma_style values: # undef stable: do not change - # '0' never want trailing commas - # '* or 1' always want trailing commas - # 'm' want multiline trailing commas - # (i.e., opening and closing tokens are on different lines) - # 'b' want bare trailing commas ( followed by newline ) - # 'h' add a bare trailing comma to a stable list with about - # one comma per line (such as Hash list of key=>value pairs). + # '0' : no list should have a trailing comma + # '1' or '*' : every list should have a trailing comma + # 'm' a multi-line list should have a trailing commas + # 'b' trailing commas should be 'bare' (comma followed by newline) + # 'h' lists of key=>value pairs should have a bare trailing comma + # 'i' same as s=h but also include any list with about one comma per line + # ' ' or -wtc not defined : leave trailing commas unchanged [DEFAULT]. + + # Note: an interesting generalization would be to let an upper case + # letter denote the negation of styles 'm', 'b', 'h', 'i'. This might + # be useful for undoing operations. It would be implemented as a wrapper + # around this routine. #----------------------------------------- # No style defined : do not add or delete @@ -8069,9 +8074,10 @@ sub match_trailing_comma_rule { #------------------------------------------------------------------ # 'h' matches a bare stable list of key=>values ('h' is for 'Hash') - # or stable single field lists with about 1 comma per line. + # 'i' same as 'h' but also matches stable single field lists with about 1 + # comma per line. #------------------------------------------------------------------ - elsif ( $trailing_comma_style eq 'h' ) { + elsif ( $trailing_comma_style eq 'h' || $trailing_comma_style eq 'i' ) { # This is a minimal style which can put trailing commas where # they are most useful - at the end of simple lists which might, @@ -8108,7 +8114,7 @@ sub match_trailing_comma_rule { } #--------------------------------------------------------- - # Style 'h', Section 1: check for a stable key=>value list + # Styles 'h' and 'i': check for a stable key=>value list #--------------------------------------------------------- my $fat_comma_count = $rtype_count->{'=>'}; @@ -8139,17 +8145,20 @@ sub match_trailing_comma_rule { } #-------------------------------------------------------------- - # Style 'h', Section 2: check for a stable single-field list of + # Style 'i': check for a stable single-field list of # items stabilized by blank lines, comments, or the -boc flag #-------------------------------------------------------------- - elsif ( + if ( !$match && $trailing_comma_style eq 'i' ) { # We are looking for lists with <= 1 comma per line - $line_diff > $comma_count && ( $is_permanently_broken - || $rOpts_break_at_old_comma_breakpoints ) - ) - { - $match = 1; + if ( + $line_diff > $comma_count + && ( $is_permanently_broken + || $rOpts_break_at_old_comma_breakpoints ) + ) + { + $match = 1; + } } } diff --git a/t/snippets/expect/wtc.wtc7 b/t/snippets/expect/wtc.wtc7 new file mode 100644 index 00000000..bed34858 --- /dev/null +++ b/t/snippets/expect/wtc.wtc7 @@ -0,0 +1,42 @@ +# both single and multiple line lists: +@LoL = ( + [ "fred", "barney" ], + [ "george", "jane", "elroy" ], + [ "homer", "marge", "bart" ] ); + +# single line +( $name, $body ) = ( $2, $3 ); + +# multiline, but not bare +$text = $main->Scrolled( TextUndo, $yyy, $zzz, $wwwww, + selectbackgroundxxxxx => 'yellow' ); + +# this will pass for 'h' +my $new = { + %$item, + text => $leaf, + color => 'green', +}; + +# and this +my @list = ( + + $xx, + $yy ); + +# does not match 'h' +$c1->create( + 'rectangle', 40, 60, 80, 80, + -fill => 'red', + -tags => 'rectangle' ); + +$dasm_frame->Button( + -text => 'Locate', + -command => sub { + $target_binary = $fs->Show( -popover => 'cursor', -create => 1 ); + } )->pack( -side => 'left' ); + +my $no_index_1_1 = + { 'map' => { ':key' => { name => \&string, list => { value => \&string } } } + }; + diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 2ab911b9..2627bf18 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -371,6 +371,8 @@ ../snippets27.t wtc.wtc4 ../snippets27.t wtc.wtc5 ../snippets27.t wtc.wtc6 +../snippets27.t dwic.def +../snippets27.t dwic.dwic ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -511,5 +513,4 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets27.t dwic.def -../snippets27.t dwic.dwic +../snippets27.t wtc.wtc7 diff --git a/t/snippets/wtc6.par b/t/snippets/wtc6.par index fec2da3c..898eca9e 100644 --- a/t/snippets/wtc6.par +++ b/t/snippets/wtc6.par @@ -1 +1 @@ --wtc=h -atc -dtc -vtc=2 +-wtc=i -atc -dtc -vtc=2 diff --git a/t/snippets/wtc7.par b/t/snippets/wtc7.par new file mode 100644 index 00000000..fec2da3c --- /dev/null +++ b/t/snippets/wtc7.par @@ -0,0 +1 @@ +-wtc=h -atc -dtc -vtc=2 diff --git a/t/snippets27.t b/t/snippets27.t index 581f9161..bb43faf8 100644 --- a/t/snippets27.t +++ b/t/snippets27.t @@ -9,6 +9,7 @@ #6 wtc.wtc6 #7 dwic.def #8 dwic.dwic +#9 wtc.wtc7 # To locate test #13 you can search for its name or the string '#13' @@ -33,7 +34,8 @@ BEGIN { 'wtc3' => "-wtc=m -atc", 'wtc4' => "-wtc=m -atc -dtc", 'wtc5' => "-wtc=b -atc -dtc -vtc=2", - 'wtc6' => "-wtc=h -atc -dtc -vtc=2", + 'wtc6' => "-wtc=i -atc -dtc -vtc=2", + 'wtc7' => "-wtc=h -atc -dtc -vtc=2", }; ############################ @@ -452,6 +454,55 @@ my $no_index_1_1 = ) ] ); #8........... }, + + 'wtc.wtc7' => { + source => "wtc", + params => "wtc7", + expect => <<'#9...........', +# both single and multiple line lists: +@LoL = ( + [ "fred", "barney" ], + [ "george", "jane", "elroy" ], + [ "homer", "marge", "bart" ] ); + +# single line +( $name, $body ) = ( $2, $3 ); + +# multiline, but not bare +$text = $main->Scrolled( TextUndo, $yyy, $zzz, $wwwww, + selectbackgroundxxxxx => 'yellow' ); + +# this will pass for 'h' +my $new = { + %$item, + text => $leaf, + color => 'green', +}; + +# and this +my @list = ( + + $xx, + $yy ); + +# does not match 'h' +$c1->create( + 'rectangle', 40, 60, 80, 80, + -fill => 'red', + -tags => 'rectangle' ); + +$dasm_frame->Button( + -text => 'Locate', + -command => sub { + $target_binary = $fs->Show( -popover => 'cursor', -create => 1 ); + } )->pack( -side => 'left' ); + +my $no_index_1_1 = + { 'map' => { ':key' => { name => \&string, list => { value => \&string } } } + }; + +#9........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5