]> git.donarmstrong.com Git - perltidy.git/commitdiff
split -wtc=h into -wtc=h and -wtc=i
authorSteve Hancock <perltidy@users.sourceforge.net>
Thu, 20 Oct 2022 21:42:53 +0000 (14:42 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Thu, 20 Oct 2022 21:42:53 +0000 (14:42 -0700)
bin/perltidy
lib/Perl/Tidy/Formatter.pm
t/snippets/expect/wtc.wtc7 [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets/wtc6.par
t/snippets/wtc7.par [new file with mode: 0644]
t/snippets27.t

index 5334f7e204d008248b90cdbb3af808f129f2e98b..a44ce53c255beafd677323bf5f111e0b0bb69abc 100755 (executable)
@@ -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<inode> value. This may avoid
+implementation preserves the file system B<inode> value. This may avoid
 problems with other software running simultaneously.  This change was made
 as part of issue B<git #103> 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<gt>> 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')
index ab966326aae5061c0e889496d8d2cc586b77878e..3fd0f2a4a098ac54dba18a191ee7e1437a9bc8c2 100644 (file)
@@ -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 (file)
index 0000000..bed3485
--- /dev/null
@@ -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 } } }
+  };
+
index 2ab911b925f8b8e996c1f22fa41934258bcfd4dc..2627bf184d8f976f464a49a70d83fe36a77f1027 100644 (file)
 ../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
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets27.t        dwic.def
-../snippets27.t        dwic.dwic
+../snippets27.t        wtc.wtc7
index fec2da3c1fc11e8d21bdfd69b701f15c171d7a88..898eca9e4929988a91a4d099318d1913a3e672b4 100644 (file)
@@ -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 (file)
index 0000000..fec2da3
--- /dev/null
@@ -0,0 +1 @@
+-wtc=h -atc -dtc -vtc=2
index 581f9161640d079d87a4f0ed74d60ed7bf10c555..bb43faf8163d27ff3eb49d2808aa10c6a3979bf2 100644 (file)
@@ -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};