From 1faeda9de4852c000ab7c73e51d7075bc5e25cb0 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 24 Mar 2023 18:34:17 -0700 Subject: [PATCH] param --valign-if-unless, -viu, added for git #116 --- CHANGES.md | 4 ++++ bin/perltidy | 20 ++++++++++++++++++++ lib/Perl/Tidy.pm | 1 + lib/Perl/Tidy/Formatter.pm | 26 +++++++++++++++++++++----- t/snippets/expect/git116.def | 4 ++++ t/snippets/expect/git116.git116 | 4 ++++ t/snippets/git116.in | 4 ++++ t/snippets/git116.par | 1 + t/snippets/packing_list.txt | 2 ++ t/snippets28.t | 32 ++++++++++++++++++++++++++++++++ 10 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 t/snippets/expect/git116.def create mode 100644 t/snippets/expect/git116.git116 create mode 100644 t/snippets/git116.in create mode 100644 t/snippets/git116.par diff --git a/CHANGES.md b/CHANGES.md index 6bffaf6c..790fada7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## 2023 03 09.01 + - Issue git #116. A new flag --valign-if-unless, -viu, was added to + allow postfix 'unless' terms to align with postfix 'if' terms. The + default remains not to do this. + - Fixed git #115. In the two most recent CPAN releases, when the Perl::Tidy module was called with the source pointing to a file, but no destination specified, the output went to the standard diff --git a/bin/perltidy b/bin/perltidy index 236493bb..e00be9b4 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -4734,6 +4734,26 @@ alignment cannot be made for some reason. But also notice that side comments remain aligned because their alignment is controlled separately with the parameter B<--valign-side_comments> described above. +=item B + +By default, postfix B terms align and postfix B terms align, +but separately. For example, + + # perltidy [DEFAULT] + print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); + print "Would need: @DepList\n" if ( @DepList and !$Quiet ); + print "RPM Output:\n" unless $Quiet; + print join( "\n", @RPMOutput ) . "\n" unless $Quiet; + +The B<-viu> flag causes a postfix B to be treated as if it were a +postfix B for purposes of alignment. Thus + + # perltidy -viu + print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); + print "Would need: @DepList\n" if ( @DepList and !$Quiet ); + print "RPM Output:\n" unless $Quiet; + print join( "\n", @RPMOutput ) . "\n" unless $Quiet; + =back =head2 Extended Syntax diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index c729f99c..cf2493b5 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3234,6 +3234,7 @@ sub generate_options { $add_option->( 'valign-side-comments', 'vsc', '!' ); $add_option->( 'valign-exclusion-list', 'vxl', '=s' ); $add_option->( 'valign-inclusion-list', 'vil', '=s' ); + $add_option->( 'valign-if-unless', 'viu', '!' ); ######################################## $category = 4; # Comment controls diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index cccb27f4..2da5ab98 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -233,6 +233,7 @@ my ( $rOpts_variable_maximum_line_length, $rOpts_valign_code, $rOpts_valign_side_comments, + $rOpts_valign_if_unless, $rOpts_whitespace_cycle, $rOpts_extended_line_up_parentheses, @@ -2395,6 +2396,7 @@ sub initialize_global_option_vars { $rOpts_tee_side_comments = $rOpts->{'tee-side-comments'}; $rOpts_valign_code = $rOpts->{'valign-code'}; $rOpts_valign_side_comments = $rOpts->{'valign-side-comments'}; + $rOpts_valign_if_unless = $rOpts->{'valign-if-unless'}; $rOpts_variable_maximum_line_length = $rOpts->{'variable-maximum-line-length'}; @@ -26446,6 +26448,13 @@ EOM # /^(if|unless|and|or|eq|ne)$/ if ( $is_vertical_alignment_keyword{$token} ) { $alignment_type = $token; + + # Align postfix 'unless' and 'if' if requested (git #116) + # These are the only equivalent keywords. For equivalent + # token types see '%operator_map'. + if ( $token eq 'unless' && $rOpts_valign_if_unless ) { + $alignment_type = 'if'; + } } } @@ -27566,8 +27575,12 @@ sub xlp_tweak { # Note: %block_type_map is now global to enable the -gal=s option - # map certain keywords to the same 'if' class to align - # long if/elsif sequences. [elsif.pl] + # Map certain keywords to the same 'if' class to align + # long if/elsif sequences. [elsif.pl]. But note that this is + # only for purposes of making the patterns, not alignment tokens. + # The only possible equivalent alignment tokens are 'if' and 'unless', + # and this is handled earlier under control of $rOpts_valign_if_unless + # to avoid making this a global hash. %keyword_map = ( 'unless' => 'if', 'else' => 'if', @@ -27580,7 +27593,10 @@ sub xlp_tweak { 'undef' => 'Q', ); - # map certain operators to the same class for pattern matching + # Map certain operators to the same class for alignment. + # Note that this map is for the alignment tokens, not the patterns. + # We could have placed 'unless' => 'if' here, but since that is + # under control of $rOpts_valign_if_unless, it is handled elsewhere. %operator_map = ( '!~' => '=~', '+=' => '+=', @@ -27611,8 +27627,8 @@ sub xlp_tweak { # token types which prevent using leading word as a container name @q = qw( - x / : % . | ^ < = > || >= != *= => !~ == && |= .= -= =~ += <= %= ^= x= ~~ ** << /= - &= // >> ~. &. |. ^. + x / : % . | ^ < = > || >= != *= => !~ == && |= .= -= =~ += <= + %= ^= x= ~~ ** << /= &= // >> ~. &. |. ^. **= <<= >>= &&= ||= //= <=> !~~ &.= |.= ^.= <<~ ); push @q, ','; diff --git a/t/snippets/expect/git116.def b/t/snippets/expect/git116.def new file mode 100644 index 00000000..74e5097a --- /dev/null +++ b/t/snippets/expect/git116.def @@ -0,0 +1,4 @@ +print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); +print "Would need: @DepList\n" if ( @DepList and !$Quiet ); +print "RPM Output:\n" unless $Quiet; +print join( "\n", @RPMOutput ) . "\n" unless $Quiet; diff --git a/t/snippets/expect/git116.git116 b/t/snippets/expect/git116.git116 new file mode 100644 index 00000000..7394fdac --- /dev/null +++ b/t/snippets/expect/git116.git116 @@ -0,0 +1,4 @@ +print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); +print "Would need: @DepList\n" if ( @DepList and !$Quiet ); +print "RPM Output:\n" unless $Quiet; +print join( "\n", @RPMOutput ) . "\n" unless $Quiet; diff --git a/t/snippets/git116.in b/t/snippets/git116.in new file mode 100644 index 00000000..99eeeb00 --- /dev/null +++ b/t/snippets/git116.in @@ -0,0 +1,4 @@ +print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); +print "Would need: @DepList\n" if ( @DepList and !$Quiet ); +print "RPM Output:\n" unless $Quiet; +print join( "\n", @RPMOutput ) . "\n" unless $Quiet; diff --git a/t/snippets/git116.par b/t/snippets/git116.par new file mode 100644 index 00000000..1468dca8 --- /dev/null +++ b/t/snippets/git116.par @@ -0,0 +1 @@ +-viu diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index b4a28e84..028adda1 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -529,3 +529,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def +../snippets28.t git116.def +../snippets28.t git116.git116 diff --git a/t/snippets28.t b/t/snippets28.t index ae680597..e0a3712d 100644 --- a/t/snippets28.t +++ b/t/snippets28.t @@ -6,6 +6,8 @@ #3 recombine6.def #4 recombine7.def #5 recombine8.def +#6 git116.def +#7 git116.git116 # To locate test #13 you can search for its name or the string '#13' @@ -24,6 +26,7 @@ BEGIN { ########################################### $rparams = { 'def' => "", + 'git116' => "-viu", 'olbxl2' => <<'----------', -olbxl='*' ---------- @@ -34,6 +37,13 @@ BEGIN { ############################ $rsources = { + 'git116' => <<'----------', +print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); +print "Would need: @DepList\n" if ( @DepList and !$Quiet ); +print "RPM Output:\n" unless $Quiet; +print join( "\n", @RPMOutput ) . "\n" unless $Quiet; +---------- + 'olbxl' => <<'----------', eval { require Ace }; @@ -183,6 +193,28 @@ $v_gb = -1 * ( eval($pmt_gb) ) * ( ); #5........... }, + + 'git116.def' => { + source => "git116", + params => "def", + expect => <<'#6...........', +print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); +print "Would need: @DepList\n" if ( @DepList and !$Quiet ); +print "RPM Output:\n" unless $Quiet; +print join( "\n", @RPMOutput ) . "\n" unless $Quiet; +#6........... + }, + + 'git116.git116' => { + source => "git116", + params => "git116", + expect => <<'#7...........', +print "Tried to add: @ResolveRPM\n" if ( @ResolveRPM and !$Quiet ); +print "Would need: @DepList\n" if ( @DepList and !$Quiet ); +print "RPM Output:\n" unless $Quiet; +print join( "\n", @RPMOutput ) . "\n" unless $Quiet; +#7........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5