]> git.donarmstrong.com Git - perltidy.git/commitdiff
minor adjustments in sub decide_if_aligned_pair()
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 29 Oct 2019 01:19:44 +0000 (18:19 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 29 Oct 2019 01:19:44 +0000 (18:19 -0700)
lib/Perl/Tidy/VerticalAligner.pm
t/snippets/expect/andor8.def
t/snippets/expect/listop1.def
t/snippets1.t
t/snippets5.t

index c262695ef107b1d0f6c07e609b9214b068dd1f97..78dbaaf4a225c415ef6bcc041908cdda26c285ea 100644 (file)
@@ -1627,9 +1627,9 @@ sub salvage_equality_matches {
     # If we had a peek at the subsequent line we could make a much better
     # decision here, but for now this is not available.
     for ( my $j = 1 ; $j < $jmax_new - 1 ; $j++ ) {
-        my $new_tok           = $rtokens->[$j];
+        my $new_tok = $rtokens->[$j];
 
-       # git#16: do not consider fat commas as good aligmnents here
+        # git#16: do not consider fat commas as good aligmnents here
         my $is_good_alignment =
           ( $new_tok =~ /^(=|\?|if|unless|\|\||\&\&)/ && $new_tok !~ /^=>/ );
         return if ($is_good_alignment);
@@ -2301,12 +2301,12 @@ EOM
         # is decorated as follows:
         #    ,2+report-6  => (tok,lev,tag) =qw( ,   2   +report-6)
 
-       # An optional token count may be appended with a leading dot.
-       # Currently this is only done for '=' tokens but this could change.
-       # For example, consider the following line:
+        # An optional token count may be appended with a leading dot.
+        # Currently this is only done for '=' tokens but this could change.
+        # For example, consider the following line:
         #   $nport   = $port = shift || $name;
-       # The first '=' may either be '=0' or '=0.1' [level 0, first equals]
-       # The second '=' will be '=0.2' [level 0, second equals]
+        # The first '=' may either be '=0' or '=0.1' [level 0, first equals]
+        # The second '=' will be '=0.2' [level 0, second equals]
 
         my ( $tok, $lev, $tag, $tok_count ) = ( $token, 0, "", 1 );
         if ( $tok =~ /^(\D+)(\d+)([^\.]*)(\.(\d+))?$/ ) {
@@ -2315,8 +2315,8 @@ EOM
             $tag       = $3;
             $tok_count = $5 if ($5);
         }
-       
-       # okay to delete second and higher copies of a token
+
+        # okay to delete second and higher copies of a token
         if ( $tok_count > 1 ) { return 1 }
 
         # only remove lower level commas
@@ -2460,8 +2460,8 @@ sub delete_unmatched_tokens {
             }
         }
 
-       # OLD: Leave two lines alone unless they are an if/else or ternary.
-       # NEW: Treat two lines the same as longer runs; results are better. 
+        # OLD: Leave two lines alone unless they are an if/else or ternary.
+        # NEW: Treat two lines the same as longer runs; results are better.
         ## next if ( $nlines <= 2 && !$is_full_block );
 
         # remove unwanted alignment tokens
@@ -2511,7 +2511,7 @@ sub delete_unmatched_tokens {
     return;
 }
 
-{    # decide_if_aligned_pair
+{        # decide_if_aligned_pair
 
     my %is_if_or;
     my %is_assignment;
@@ -2548,8 +2548,11 @@ sub delete_unmatched_tokens {
         my $leading_equals = ( $rtokens->[0] =~ /=/ );
 
         # scan the tokens on the second line
-        my $rtokens1        = $group_lines[1]->get_rtokens();
+        # $all_group_level => all non-tagged tokens are at group level
+        # $all_high_level  => all non-tagged tokens are above group level
         my $all_group_level = 1;
+        my $all_high_level  = 1;
+        my $rtokens1        = $group_lines[1]->get_rtokens();
         my $saw_if_or;
         my $raw_tokb = "";
         for ( my $j = 0 ; $j < $jmax1 - 1 ; $j++ ) {
@@ -2562,12 +2565,8 @@ sub delete_unmatched_tokens {
                 if ( $j == 0 ) { $raw_tokb = $raw_tok }
                 $saw_if_or ||= $is_if_or{$raw_tok};
 
-                if ( !$tag ) {
-
-                    # mark line as variable level if we see any untagged
-                    # higher level tokens
-                    $all_group_level &&= ( $lev == $group_level );
-                }
+                $all_high_level  &&= ( $lev > $group_level && !$tag );
+                $all_group_level &&= ( $lev == $group_level || $tag );
             }
         }
 
@@ -2576,6 +2575,12 @@ sub delete_unmatched_tokens {
         # we can allow matching in some specific cases.
         my $is_marginal = $marginal_match;
 
+        # A line leading '{' and all high level tokens is marginal. For
+        # example, do not align the {} here:
+        #   $foo->hash_int( {} );
+        #   is_deeply( $foo->hash_int, {}, "hash_int - correct contents" );
+        $is_marginal ||= ( $all_high_level && $raw_tokb eq '{' );
+
         # See if the lines end with semicolons...
         my $rpatterns0 = $group_lines[0]->get_rpatterns();
         my $rpatterns1 = $group_lines[1]->get_rpatterns();
@@ -2592,8 +2597,16 @@ sub delete_unmatched_tokens {
             $sc_term1 = $pat1 =~ /;b?$/;
         }
 
-        # lines not terminated similarly are always considered marginal
-        $is_marginal ||= ( $sc_term0 ne $sc_term1 );
+        if ( !$is_marginal && !$sc_term0 ) {
+
+            # First line of assignment should be semicolon terminated.
+            # For example, do not align here:
+            #  $$href{-NUM_TEXT_FILES} = $$href{-NUM_BINARY_FILES} =
+            #    $$href{-NUM_DIRS} = 0;
+            if ( $is_assignment{$raw_tokb} ) {
+                $is_marginal = 1;
+            }
+        }
 
         # Undo the marginal match flag in certain cases,
         # but only if all matching tokens are at group level.
@@ -2626,21 +2639,21 @@ sub delete_unmatched_tokens {
             my $pat1 = $rpatterns1->[0];
             if ( $is_assignment{$raw_tokb} ) {
 
-               # undo marginal flag if first line is semicolon terminated 
-               # and leading patters match
+                # undo marginal flag if first line is semicolon terminated
+                # and leading patters match
                 if ($sc_term0) {    # && $sc_term1) {
                     $is_marginal = $pat0 ne $pat1;
                 }
             }
             elsif ( $raw_tokb eq '=>' ) {
 
-               # undo marginal flag if patterns match
+                # undo marginal flag if patterns match
                 $is_marginal = $pat0 ne $pat1;
             }
             elsif ( $raw_tokb eq '=~' ) {
 
                 # undo marginal flag if both lines are semicolon terminated
-               # and leading patters match
+                # and leading patters match
                 if ( $sc_term1 && $sc_term0 ) {
                     $is_marginal = $pat0 ne $pat1;
                 }
index eca2d34c50c47b07e98a60412b47c7747eaf3c9f..ec80fa97a64b58301731a060f99cffd0dc800845 100644 (file)
@@ -1,4 +1,4 @@
         # original is broken:
         $a = 1
-          if $l and !$r
+          if $l  and !$r
           or !$l and $r;
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 604224116028b9ae5c7cfadd7723c8caf73d7f9c..68927e328eb75f7fa08d90fcae615bf1815eb4a9 100644 (file)
@@ -482,7 +482,7 @@ sub is_miniwhile {    # check for one-line loop (`foo() while $y--')
             expect => <<'#19...........',
         # original is broken:
         $a = 1
-          if $l and !$r
+          if $l  and !$r
           or !$l and $r;
 #19...........
         },
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...........
         },