From 93063a7bfc12a0167df30cd57c7bfbd3b8b4770c Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 3 May 2021 18:22:13 -0700 Subject: [PATCH] Add option -pvtc=3, requested in rt136416 --- CHANGES.md | 8 +++++ bin/perltidy | 24 +++++++++++++-- lib/Perl/Tidy/Formatter.pm | 20 +++++++++++++ local-docs/BugLog.pod | 8 +++++ t/snippets/expect/rt136417.def | 9 ++++++ t/snippets/expect/rt136417.rt136417 | 8 +++++ t/snippets/packing_list.txt | 4 ++- t/snippets/rt136417.in | 8 +++++ t/snippets/rt136417.par | 1 + t/snippets24.t | 45 +++++++++++++++++++++++++++++ 10 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 t/snippets/expect/rt136417.def create mode 100644 t/snippets/expect/rt136417.rt136417 create mode 100644 t/snippets/rt136417.in create mode 100644 t/snippets/rt136417.par diff --git a/CHANGES.md b/CHANGES.md index 2b9503b2..3f27fb65 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,13 @@ # Perltidy Change Log +## 2021 xx xx + + - Added a new option for closing paren placement, -vtc=3, requested in rt #136417. + + - A more complete list of updates is at + + https://github.com/perltidy/perltidy/blob/master/local-docs/BugLog.pod + ## 2021 04 02 - This release fixes several non-critical bugs which have been found since the last diff --git a/bin/perltidy b/bin/perltidy index 6b0eee24..04ed1edc 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2696,9 +2696,11 @@ B<--vertical-tightness-closing=n>, where by a semicolon or another closing token, and is not in a list environment. -vtc=2 never break before a closing token. + -vtc=3 Like -vtc=1 except always break before a closing token + if the corresponding opening token follows an = or =>. -The rules for B<-vtc=1> are designed to maintain a reasonable balance -between tightness and readability in complex lists. +The rules for B<-vtc=1> and B<-vtc=3> are designed to maintain a reasonable +balance between tightness and readability in complex lists. =item * @@ -2739,6 +2741,24 @@ Here are some examples: three => 'III', four => 'IV', ); + # perltidy -vtc=3 + my_function( + one => 'I', + two => 'II', + three => 'III', + four => 'IV', ); + + # perltidy -vtc=3 + %romanNumerals = ( + one => 'I', + two => 'II', + three => 'III', + four => 'IV', + ); + +In the last example for B<-vtc=3>, the opening paren is preceded by an equals +so the closing paren is placed on a new line. + The difference between B<-vt=1> and B<-vt=2> is shown here: # perltidy -lp -vt=1 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index c1092bee..b888c0c5 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -439,6 +439,7 @@ BEGIN { _rbreak_before_container_by_seqno_ => $i++, _ris_essential_old_breakpoint_ => $i++, _roverride_cab3_ => $i++, + _ris_assigned_structure_ => $i++, }; # Array index names for _this_batch_ (in above list) @@ -798,6 +799,7 @@ sub new { $self->[_rbreak_before_container_by_seqno_] = {}; $self->[_ris_essential_old_breakpoint_] = {}; $self->[_roverride_cab3_] = {}; + $self->[_ris_assigned_structure_] = {}; # This flag will be updated later by a call to get_save_logfile() $self->[_save_logfile_] = defined($logger_object); @@ -5038,6 +5040,7 @@ sub respace_tokens { my $rparent_of_seqno = {}; my $rchildren_of_seqno = {}; my $roverride_cab3 = {}; + my $ris_assigned_structure = {}; my $last_nonblank_type = ';'; my $last_nonblank_token = ';'; @@ -5663,6 +5666,14 @@ sub respace_tokens { if ($type_sequence) { if ( $is_opening_token{$token} ) { + + if ( $last_nonblank_type eq '=' + || $last_nonblank_type eq '=>' ) + { + $ris_assigned_structure->{$type_sequence} = + $last_nonblank_type; + } + my $seqno_parent = $seqno_stack{ $depth_next - 1 }; $seqno_parent = SEQ_ROOT unless defined($seqno_parent); push @{ $rchildren_of_seqno->{$seqno_parent} }, @@ -6196,6 +6207,7 @@ sub respace_tokens { $self->[_rchildren_of_seqno_] = $rchildren_of_seqno; $self->[_ris_list_by_seqno_] = $ris_list_by_seqno; $self->[_roverride_cab3_] = $roverride_cab3; + $self->[_ris_assigned_structure_] = $ris_assigned_structure; # DEBUG OPTION: make sure the new array looks okay. # This is no longer needed but should be retained for future development. @@ -21299,6 +21311,14 @@ sub set_vertical_tightness_flags { { my $ovt = $opening_vertical_tightness{$token_next}; my $cvt = $closing_vertical_tightness{$token_next}; + + # Implement cvt=3: like cvt=0 for assigned structures, like cvt=1 + # otherwise. Added for rt136417. + if ( $cvt == 3 ) { + my $seqno = $type_sequence_to_go[$ibeg_next]; + $cvt = $self->[_ris_assigned_structure_]->{$seqno} ? 0 : 1; + } + if ( # Never append a trailing line like ')->pack(' because it diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index e5344c0c..65345e92 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,14 @@ =over 4 +=item B + +A new integer option, n=3, has been added to the vertical tightness closing flags. +For a container with n=3, the closing token will behave as for n=0 if the opening +token is preceded by an '=' or '=>', and like n=1 otherwise. + +3 May 2021. + =item B This update fixes a problem with unwanted vertical alignment rasied in diff --git a/t/snippets/expect/rt136417.def b/t/snippets/expect/rt136417.def new file mode 100644 index 00000000..5417670f --- /dev/null +++ b/t/snippets/expect/rt136417.def @@ -0,0 +1,9 @@ +function( + # + a, b, c +); + +%hash = ( + a => b, + c => d, +); diff --git a/t/snippets/expect/rt136417.rt136417 b/t/snippets/expect/rt136417.rt136417 new file mode 100644 index 00000000..1977d744 --- /dev/null +++ b/t/snippets/expect/rt136417.rt136417 @@ -0,0 +1,8 @@ +function( + # + a, b, c ); + +%hash = ( + a => b, + c => d, +); diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index fdb8571f..2ff9249d 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -319,6 +319,7 @@ ../snippets24.t lpxl.lpxl4 ../snippets24.t lpxl.lpxl5 ../snippets24.t git63.def +../snippets24.t align35.def ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -459,4 +460,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets24.t align35.def +../snippets24.t rt136417.def +../snippets24.t rt136417.rt136417 diff --git a/t/snippets/rt136417.in b/t/snippets/rt136417.in new file mode 100644 index 00000000..3f21a974 --- /dev/null +++ b/t/snippets/rt136417.in @@ -0,0 +1,8 @@ +function( + # + a, b, c); + +%hash = ( + a => b, + c => d, +); diff --git a/t/snippets/rt136417.par b/t/snippets/rt136417.par new file mode 100644 index 00000000..d4d577a7 --- /dev/null +++ b/t/snippets/rt136417.par @@ -0,0 +1 @@ +-vtc=3 diff --git a/t/snippets24.t b/t/snippets24.t index 7b944a79..f171b4bd 100644 --- a/t/snippets24.t +++ b/t/snippets24.t @@ -13,6 +13,8 @@ #10 lpxl.lpxl5 #11 git63.def #12 align35.def +#13 rt136417.def +#14 rt136417.rt136417 # To locate test #13 you can search for its name or the string '#13' @@ -44,6 +46,7 @@ BEGIN { 'lpxl5' => <<'----------', -lp -lpxl='{ [ F(2' ---------- + 'rt136417' => "-vtc=3", }; ############################ @@ -202,6 +205,17 @@ $behaviour = { dog => {prowl => "growl", pool => "drool"}, mouse => {nibble => "kibble"}, }; +---------- + + 'rt136417' => <<'----------', +function( + # + a, b, c); + +%hash = ( + a => b, + c => d, +); ---------- }; @@ -731,6 +745,37 @@ use constant COUNTUP => reverse 1, 2, 3, 4, 5; use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5; #12........... }, + + 'rt136417.def' => { + source => "rt136417", + params => "def", + expect => <<'#13...........', +function( + # + a, b, c +); + +%hash = ( + a => b, + c => d, +); +#13........... + }, + + 'rt136417.rt136417' => { + source => "rt136417", + params => "rt136417", + expect => <<'#14...........', +function( + # + a, b, c ); + +%hash = ( + a => b, + c => d, +); +#14........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5