]> git.donarmstrong.com Git - perltidy.git/commitdiff
param --valign-if-unless, -viu, added for git #116
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 25 Mar 2023 01:34:17 +0000 (18:34 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 25 Mar 2023 01:34:17 +0000 (18:34 -0700)
CHANGES.md
bin/perltidy
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm
t/snippets/expect/git116.def [new file with mode: 0644]
t/snippets/expect/git116.git116 [new file with mode: 0644]
t/snippets/git116.in [new file with mode: 0644]
t/snippets/git116.par [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets28.t

index 6bffaf6c0bd9914b168ddfbcea07c7a6156cbcdd..790fada7c68f704605b181c6054cb813f59c9357 100644 (file)
@@ -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
index 236493bb3bdb38ef6e7469118808dca49f66da79..e00be9b44ecffdacbe89180b8884fa5156867cbd 100755 (executable)
@@ -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<Aligning postfix unless and if with --valign-if-unless or -viu>
+
+By default, postfix B<if> terms align and postfix B<unless> 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<unless> to be treated as if it were a
+postfix B<if> 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
index c729f99cb4f8e8186ccb8e2a2ec81654fb97269f..cf2493b5efcd423841cd4cc6c9a3a6efb9886c19 100644 (file)
@@ -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
index cccb27f45eeb70e642bf6e9919218a0453a88c98..2da5ab982162ee455d9d77fd9d5a7dcb36fbe2bb 100644 (file)
@@ -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 (file)
index 0000000..74e5097
--- /dev/null
@@ -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 (file)
index 0000000..7394fda
--- /dev/null
@@ -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 (file)
index 0000000..99eeeb0
--- /dev/null
@@ -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 (file)
index 0000000..1468dca
--- /dev/null
@@ -0,0 +1 @@
+-viu
index b4a28e8424ce2049ebaaf0f8f347c683d5300fff..028adda1a7a6c3e4f876c6b1cd0f47ab70c60473 100644 (file)
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
+../snippets28.t        git116.def
+../snippets28.t        git116.git116
index ae680597b7055400a454bfce607b27ec82f20038..e0a3712d81559fd3d2fdf19133bf26362d9694d9 100644 (file)
@@ -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};