]> git.donarmstrong.com Git - perltidy.git/commitdiff
fixed glitch with comma alignment; added tests
authorSteve Hancock <perltidy@users.sourceforge.net>
Mon, 3 Jun 2019 03:10:20 +0000 (20:10 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Mon, 3 Jun 2019 03:10:20 +0000 (20:10 -0700)
19 files changed:
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/VerticalAligner.pm
t/snippets/align29.in [new file with mode: 0644]
t/snippets/align30.in [new file with mode: 0644]
t/snippets/expect/align29.def [new file with mode: 0644]
t/snippets/expect/align30.def [new file with mode: 0644]
t/snippets/expect/align4.def
t/snippets/expect/given1.def
t/snippets/expect/kgb3.def
t/snippets/expect/kgb3.kgb
t/snippets/expect/listop1.def
t/snippets/expect/smart.def
t/snippets/packing_list.txt
t/snippets1.t
t/snippets10.t
t/snippets14.t
t/snippets15.t
t/snippets3.t
t/snippets5.t

index c1314a335cd4a72037efea63d79957b72c81b29b..13b688304fd808beca629498665f6b7be5bd7a07 100644 (file)
@@ -11535,20 +11535,25 @@ sub get_seqno {
                     $alignment_type = $vert_last_nonblank_type;
                 }
 
-##                #--------------------------------------------------------
-##               # DEACTIVATED; DOES NOT SEEM NEEDED WITH NEW VERTICAL ALIGNER
-##                # patch for =~ operator.  We only align this if it
-##                # is the first operator in a line, and the line is a simple
-##                # statement.  Aligning them within a statement
-##                # could interfere with other good alignments.
-##                #--------------------------------------------------------
-##                if ( $alignment_type eq '=~' ) {
-##                    my $terminal_type = $types_to_go[$i_terminal];
-##                    if ( $count > 0 || $max_line > 0 || $terminal_type ne ';' )
-##                    {
-##                        $alignment_type = "";
-##                    }
-##                }
+                #--------------------------------------------------------
+                # patch for =~ operator.  We only align this if it
+                # is the first operator in a line, and the line is a simple
+                # statement.  Aligning them within a statement
+                # could interfere with other good alignments.
+                #--------------------------------------------------------
+                if ( $alignment_type eq '=~' ) {
+                    ##my $terminal_type = $types_to_go[$i_terminal];
+                    ##if ( $count > 0 || $max_line > 0 || $terminal_type ne ';' )
+                   ## TEMPORARY PATCH: allow =~ except in an 'elsif' statement
+                   ## BUBBA FIXME: This prevents loss of padding in if/elsif lines, but the
+                   ## Vertical aligner should be updated to handle this automatically.
+                   ## Then this section can be removed
+                    if (   $types_to_go[$ibeg] eq 'k'
+                        && $tokens_to_go[$ibeg] eq 'elsif' )
+                    {
+                        $alignment_type = "";
+                    }
+                }
 
                 #--------------------------------------------------------
                 # then store the value
index ee490d66dd7ac08074d7cb9fdf3e2ac72154c932..e386d5384d525d263865ac3bf66a11dd0c84790b 100644 (file)
@@ -2307,7 +2307,7 @@ EOM
 
             #print "tok=$tok, lev=$lev, gl=$group_level, i=$i, ieq=$i_eq\n";
             return if ( defined($i_eq) && $i < $i_eq );
-            return if ( $lev >= $group_level );
+            return if ( $lev <= $group_level );
         }
 
         # most operators with an equals sign should be retained if at
diff --git a/t/snippets/align29.in b/t/snippets/align29.in
new file mode 100644 (file)
index 0000000..78cd0e6
--- /dev/null
@@ -0,0 +1,6 @@
+# alignment with lots of commas
+is( floor(1.23441242), 1, "Basic floor(1.23441242) test" );
+is( fmod( 3.5, 2.0 ), 1.5, "Basic fmod(3.5, 2.0) test" );
+is( join( " ", frexp(1) ), "0.5 1", "Basic frexp(1) test" );
+is( ldexp( 0, 1 ), 0, "Basic ldexp(0,1) test" );
+is( log10(1),  0, "Basic log10(1) test" );
diff --git a/t/snippets/align30.in b/t/snippets/align30.in
new file mode 100644 (file)
index 0000000..74b64e3
--- /dev/null
@@ -0,0 +1,3 @@
+# commas on lhs align, commas on rhs do not (different subs)
+($x,$y,$z)=spherical_to_cartesian($rho,$theta,$phi);
+($rho_c,$theta,$z)=spherical_to_cylindrical($rho_s,$theta,$phi);
diff --git a/t/snippets/expect/align29.def b/t/snippets/expect/align29.def
new file mode 100644 (file)
index 0000000..b30f966
--- /dev/null
@@ -0,0 +1,6 @@
+# alignment with lots of commas
+is( floor(1.23441242),     1,       "Basic floor(1.23441242) test" );
+is( fmod( 3.5, 2.0 ),      1.5,     "Basic fmod(3.5, 2.0) test" );
+is( join( " ", frexp(1) ), "0.5 1", "Basic frexp(1) test" );
+is( ldexp( 0, 1 ),         0,       "Basic ldexp(0,1) test" );
+is( log10(1),              0,       "Basic log10(1) test" );
diff --git a/t/snippets/expect/align30.def b/t/snippets/expect/align30.def
new file mode 100644 (file)
index 0000000..39cd8af
--- /dev/null
@@ -0,0 +1,3 @@
+# commas on lhs align, commas on rhs do not (different subs)
+( $x,     $y,     $z ) = spherical_to_cartesian( $rho, $theta, $phi );
+( $rho_c, $theta, $z ) = spherical_to_cylindrical( $rho_s, $theta, $phi );
index ebdfa58b90fba2c994bcbeecd4156c7234fb9dd8..255219d6dc5f9b14cade3cdd2c76d9be11f7f24a 100644 (file)
@@ -1,6 +1,6 @@
 # removed 'eq' and '=~' from alignment tokens to get alignment of '?'s
 my $salute =
-    $name eq $EMPTY_STR                      ? 'Customer'
+    $name eq $EMPTY_STR ? 'Customer'
   : $name =~ m/\A((?:Sir|Dame) \s+ \S+) /xms ? $1
   : $name =~ m/(.*), \s+ Ph[.]?D \z     /xms ? "Dr $1"
   :                                            $name;
index 37ae011d8cb7124aebd7fbba542c3139f5af992f..f3e71bde9120b3be6a08633c82da863b8f73d287 100644 (file)
@@ -1,10 +1,10 @@
         given ( [ 9, "a", 11 ] ) {
             when (qr/\d/) {
                 given ($count) {
-                    when (1) { ok( $count == 1 ) }
-                    else     { ok( $count != 1 ) }
+                    when (1)          { ok( $count == 1 ) }
+                    else              { ok( $count != 1 ) }
                     when ( [ 5, 6 ] ) { ok(0) }
-                    else { ok(1) }
+                    else              { ok(1) }
                 }
             }
             ok(1) when 11;
index 0f805444ee63b95cba475500582d76f81c1fb333..5cdbe88c55c5ce22a5143d609e7e7bee9d326b43 100644 (file)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-use strict;    # with -kgb, no break after hash bang
+use strict;                # with -kgb, no break after hash bang
 our ( @Changed, $TAP );    # break after isolated 'our'
 use File::Compare;
 use Symbol;
index 61e1c6d567efc8dc6570dbd77e63c367c52c0257..159b0c265e635dc99b3cb753c743847a578ce16f 100644 (file)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-use strict;    # with -kgb, no break after hash bang
+use strict;                # with -kgb, no break after hash bang
 our ( @Changed, $TAP );    # break after isolated 'our'
 
 use File::Compare;
index d116a8f3ae432e82f00ee63e764f2255de827787..267fcb9db115b6825c00a203afed29f38fae90a1 100644 (file)
@@ -1,3 +1,3 @@
 my @sorted = map { $_->[0] }
   sort { $a->[1] <=> $b->[1] }
-  map { [ $_, rand ] } @list;
+  map  { [ $_, rand ] } @list;
index cf1600c5509b84a84ceb57fa31efebf2222a9cc3..d02785625f604d457eefdec6869863d1eef70e1a 100644 (file)
@@ -38,28 +38,28 @@ b_const      ~~ a_const;
     map { $_ => 'x' } keys %main::
 }
 ~~ \%main::;
-\%hash           ~~ \%tied_hash;
-\%tied_hash      ~~ \%hash;
-\%tied_hash      ~~ \%tied_hash;
-\%tied_hash      ~~ \%tied_hash;
-\%::             ~~ [ keys %main:: ];
-[ keys %main:: ] ~~ \%::;
-\%::             ~~ [];
-[]               ~~ \%::;
-{ "" => 1 }      ~~ [undef];
-[undef]          ~~ { "" => 1 };
-{ foo => 1 }     ~~ qr/^(fo[ox])$/;
-qr/^(fo[ox])$/   ~~ { foo => 1 };
-+{ 0 .. 100 }    ~~ qr/[13579]$/;
-qr/[13579]$/     ~~ +{ 0 .. 100 };
+\%hash                  ~~ \%tied_hash;
+\%tied_hash             ~~ \%hash;
+\%tied_hash             ~~ \%tied_hash;
+\%tied_hash             ~~ \%tied_hash;
+\%::                    ~~ [ keys %main:: ];
+[ keys %main:: ]        ~~ \%::;
+\%::                    ~~ [];
+[]                      ~~ \%::;
+{ "" => 1 }             ~~ [undef];
+[undef]                 ~~ { "" => 1 };
+{ foo => 1 }            ~~ qr/^(fo[ox])$/;
+qr/^(fo[ox])$/          ~~ { foo => 1 };
++{ 0 .. 100 }           ~~ qr/[13579]$/;
+qr/[13579]$/            ~~ +{ 0 .. 100 };
 +{ foo => 1, bar => 2 } ~~ "foo";
-"foo" ~~ +{ foo => 1, bar => 2 };
+"foo"                   ~~ +{ foo => 1, bar => 2 };
 +{ foo => 1, bar => 2 } ~~ "baz";
-"baz" ~~ +{ foo => 1, bar => 2 };
-[]  ~~ [];
-[]  ~~ [];
-[]  ~~ [1];
-[1] ~~ [];
+"baz"                   ~~ +{ foo => 1, bar => 2 };
+[]                      ~~ [];
+[]                      ~~ [];
+[]                      ~~ [1];
+[1]                     ~~ [];
 [ ["foo"], ["bar"] ] ~~ [ qr/o/, qr/a/ ];
 [ qr/o/, qr/a/ ] ~~ [ ["foo"], ["bar"] ];
 [ "foo", "bar" ] ~~ [ qr/o/, qr/a/ ];
index c30742a06c4b2341ccb2230e004ca8f4e8367c87..a4ded4f3d32714e207fb565d101411d464e6b684 100644 (file)
 ../snippets15.t        break_old_methods.def
 ../snippets15.t        bom1.bom
 ../snippets15.t        bom1.def
+../snippets15.t        align28.def
 ../snippets2.t angle.def
 ../snippets2.t arrows1.def
 ../snippets2.t arrows2.def
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets15.t        align28.def
+../snippets15.t        align29.def
+../snippets15.t        align30.def
index 68927e328eb75f7fa08d90fcae615bf1815eb4a9..94e11c2305e85e9237399227b8d5fd235808e5fd 100644 (file)
@@ -287,7 +287,7 @@ if (
             expect => <<'#5...........',
 # removed 'eq' and '=~' from alignment tokens to get alignment of '?'s
 my $salute =
-    $name eq $EMPTY_STR                      ? 'Customer'
+    $name eq $EMPTY_STR ? 'Customer'
   : $name =~ m/\A((?:Sir|Dame) \s+ \S+) /xms ? $1
   : $name =~ m/(.*), \s+ Ph[.]?D \z     /xms ? "Dr $1"
   :                                            $name;
index f2b4b21468d92dd8b1991cfaae7a012bb892af46..2bda303f530882788e0794616bc63c19cab87e6a 100644 (file)
@@ -533,28 +533,28 @@ b_const      ~~ a_const;
     map { $_ => 'x' } keys %main::
 }
 ~~ \%main::;
-\%hash           ~~ \%tied_hash;
-\%tied_hash      ~~ \%hash;
-\%tied_hash      ~~ \%tied_hash;
-\%tied_hash      ~~ \%tied_hash;
-\%::             ~~ [ keys %main:: ];
-[ keys %main:: ] ~~ \%::;
-\%::             ~~ [];
-[]               ~~ \%::;
-{ "" => 1 }      ~~ [undef];
-[undef]          ~~ { "" => 1 };
-{ foo => 1 }     ~~ qr/^(fo[ox])$/;
-qr/^(fo[ox])$/   ~~ { foo => 1 };
-+{ 0 .. 100 }    ~~ qr/[13579]$/;
-qr/[13579]$/     ~~ +{ 0 .. 100 };
+\%hash                  ~~ \%tied_hash;
+\%tied_hash             ~~ \%hash;
+\%tied_hash             ~~ \%tied_hash;
+\%tied_hash             ~~ \%tied_hash;
+\%::                    ~~ [ keys %main:: ];
+[ keys %main:: ]        ~~ \%::;
+\%::                    ~~ [];
+[]                      ~~ \%::;
+{ "" => 1 }             ~~ [undef];
+[undef]                 ~~ { "" => 1 };
+{ foo => 1 }            ~~ qr/^(fo[ox])$/;
+qr/^(fo[ox])$/          ~~ { foo => 1 };
++{ 0 .. 100 }           ~~ qr/[13579]$/;
+qr/[13579]$/            ~~ +{ 0 .. 100 };
 +{ foo => 1, bar => 2 } ~~ "foo";
-"foo" ~~ +{ foo => 1, bar => 2 };
+"foo"                   ~~ +{ foo => 1, bar => 2 };
 +{ foo => 1, bar => 2 } ~~ "baz";
-"baz" ~~ +{ foo => 1, bar => 2 };
-[]  ~~ [];
-[]  ~~ [];
-[]  ~~ [1];
-[1] ~~ [];
+"baz"                   ~~ +{ foo => 1, bar => 2 };
+[]                      ~~ [];
+[]                      ~~ [];
+[]                      ~~ [1];
+[1]                     ~~ [];
 [ ["foo"], ["bar"] ] ~~ [ qr/o/, qr/a/ ];
 [ qr/o/, qr/a/ ] ~~ [ ["foo"], ["bar"] ];
 [ "foo", "bar" ] ~~ [ qr/o/, qr/a/ ];
index 72126a68099228063c5e1096b1eb8c4e971a5238..207683f9760946361d93ca431c81a31467fc197f 100644 (file)
@@ -750,7 +750,7 @@ sub next_sibling {
             params => "def",
             expect => <<'#10...........',
 #!/usr/bin/perl -w
-use strict;    # with -kgb, no break after hash bang
+use strict;                # with -kgb, no break after hash bang
 our ( @Changed, $TAP );    # break after isolated 'our'
 use File::Compare;
 use Symbol;
@@ -773,7 +773,7 @@ print "break before this line\n";
             params => "kgb",
             expect => <<'#11...........',
 #!/usr/bin/perl -w
-use strict;    # with -kgb, no break after hash bang
+use strict;                # with -kgb, no break after hash bang
 our ( @Changed, $TAP );    # break after isolated 'our'
 
 use File::Compare;
index 32dd4622cc4e9c7a2206a5475aa3f5dd45349e41..1d474ed756d621859a757ff11d225c042c34b59c 100644 (file)
@@ -11,6 +11,8 @@
 #8 bom1.bom
 #9 bom1.def
 #10 align28.def
+#11 align29.def
+#12 align30.def
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -53,6 +55,21 @@ eval {
     if   ( $a->{'abc'} eq 'ABC' ) { no_op(23) }
     else                          { no_op(42) }
 };
+----------
+
+        'align29' => <<'----------',
+# alignment with lots of commas
+is( floor(1.23441242), 1, "Basic floor(1.23441242) test" );
+is( fmod( 3.5, 2.0 ), 1.5, "Basic fmod(3.5, 2.0) test" );
+is( join( " ", frexp(1) ), "0.5 1", "Basic frexp(1) test" );
+is( ldexp( 0, 1 ), 0, "Basic ldexp(0,1) test" );
+is( log10(1),  0, "Basic log10(1) test" );
+----------
+
+        'align30' => <<'----------',
+# commas on lhs align, commas on rhs do not (different subs)
+($x,$y,$z)=spherical_to_cartesian($rho,$theta,$phi);
+($rho_c,$theta,$z)=spherical_to_cylindrical($rho_s,$theta,$phi);
 ----------
 
         'bom1' => <<'----------',
@@ -282,6 +299,29 @@ eval {
 };
 #10...........
         },
+
+        'align29.def' => {
+            source => "align29",
+            params => "def",
+            expect => <<'#11...........',
+# alignment with lots of commas
+is( floor(1.23441242),     1,       "Basic floor(1.23441242) test" );
+is( fmod( 3.5, 2.0 ),      1.5,     "Basic fmod(3.5, 2.0) test" );
+is( join( " ", frexp(1) ), "0.5 1", "Basic frexp(1) test" );
+is( ldexp( 0, 1 ),         0,       "Basic ldexp(0,1) test" );
+is( log10(1),              0,       "Basic log10(1) test" );
+#11...........
+        },
+
+        'align30.def' => {
+            source => "align30",
+            params => "def",
+            expect => <<'#12...........',
+# commas on lhs align, commas on rhs do not (different subs)
+( $x,     $y,     $z ) = spherical_to_cartesian( $rho, $theta, $phi );
+( $rho_c, $theta, $z ) = spherical_to_cylindrical( $rho_s, $theta, $phi );
+#12...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};
index aa37aef90991a12ec374f68fbbd6f3bf42169ee0..9d8bf290a1ef98e54bfd08807c3d46d42c451bed 100644 (file)
@@ -794,10 +794,10 @@ $_, $val
         given ( [ 9, "a", 11 ] ) {
             when (qr/\d/) {
                 given ($count) {
-                    when (1) { ok( $count == 1 ) }
-                    else     { ok( $count != 1 ) }
+                    when (1)          { ok( $count == 1 ) }
+                    else              { ok( $count != 1 ) }
                     when ( [ 5, 6 ] ) { ok(0) }
-                    else { ok(1) }
+                    else              { ok(1) }
                 }
             }
             ok(1) when 11;
index f65be6ecc051b28d70b3d762c915b7dd3d3be732..1184fd9c1a4fd2d2e1659ff519854bb382f11c35 100644 (file)
@@ -356,7 +356,7 @@ return $pdl->slice(
             expect => <<'#2...........',
 my @sorted = map { $_->[0] }
   sort { $a->[1] <=> $b->[1] }
-  map { [ $_, rand ] } @list;
+  map  { [ $_, rand ] } @list;
 #2...........
         },