]> git.donarmstrong.com Git - perltidy.git/commitdiff
minor vertical alignment improvement + test case
authorSteve Hancock <perltidy@users.sourceforge.net>
Thu, 2 Jul 2020 02:28:23 +0000 (19:28 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Thu, 2 Jul 2020 02:28:23 +0000 (19:28 -0700)
lib/Perl/Tidy/VerticalAligner.pm
t/snippets/align33.in [new file with mode: 0644]
t/snippets/expect/align33.def [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets21.t

index 388a2b3482999c9709eea2a8a86f8f4225e94e1c..5b5df468013071be31917fc75cfabe3103d9558b 100644 (file)
@@ -2892,7 +2892,9 @@ sub delete_unmatched_tokens {
 
     # See if we can get better overall alignment by removing some high level
     # (deep) matches.  We can skip this call if there are none.
-    prune_alignment_tree($rlines) if ( $max_lev_diff >= 1 );
+    # Modified to send all lines through so that good breakpoints
+    # can be located.
+    prune_alignment_tree($rlines); ## if ( $max_lev_diff >= 1 );
 
     # See if we can get better overall alignment by removing some
     # ending alignment tokens of ragged lists.
@@ -3320,15 +3322,16 @@ sub prune_alignment_tree {
     my @delete_list;
     my %end_group;
 
-    my $starting_depth = 0;    # normally 0 except for debugging
+    # Define a threshold line count for forcing a break
+    my $nlines_break = 3;
 
     # We work with a list of nodes to visit at the next deeper depth.
     my @todo_list;
-    if ( defined( $match_tree[$starting_depth] ) ) {
-        @todo_list = ( 0 .. @{ $match_tree[$starting_depth] } - 1 );
+    if ( defined( $match_tree[0] ) ) {
+        @todo_list = ( 0 .. @{ $match_tree[0] } - 1 );
     }
 
-    for ( my $depth = $starting_depth ; $depth < $MAX_DEPTH ; $depth++ ) {
+    for ( my $depth = 0 ; $depth <= $MAX_DEPTH ; $depth++ ) {
         last unless (@todo_list);
         my @todo_next;
         foreach my $np (@todo_list) {
@@ -3336,6 +3339,18 @@ sub prune_alignment_tree {
                 $rindexes_p )
               = @{ $match_tree[$depth]->[$np] };
 
+            # Set a break before this block if it has significant size.
+            # Eventually this could become unnecessary if the final alignment 
+            # phase logic improves, but for now this insures that significant
+            # alignment changes are not missed.  See test 'align33.in'.
+            my $nlines_p = $jend_p - $jbeg_p + 1;
+            if (   $jbeg_p > 1
+                && $nlines_p > $nlines_break
+                && !$rlines->[$jbeg_p]->{_is_hanging_side_comment} )
+            {
+                $rlines->[ $jbeg_p - 1 ]->{_end_group} = 1;
+            }
+
             # nothing to do if no children
             next unless defined($nc_beg_p);
 
@@ -3356,7 +3371,6 @@ sub prune_alignment_tree {
             # So we will use two thresholds.  
             my $nmin_mono     = $depth + 3;
             my $nmin_non_mono = $depth + 6;
-            my $nlines_p          = $jend_p - $jbeg_p + 1;
             if ( $nmin_mono > $nlines_p - 1 ) {
                 $nmin_mono = $nlines_p - 1;
             }
@@ -3365,7 +3379,6 @@ sub prune_alignment_tree {
             }
 
             # loop to keep or delete each child node
-            my $jend_c_keep;
             foreach my $nc ( $nc_beg_p .. $nc_end_p ) {
                 my ( $jbeg_c, $jend_c, $np_c, $lev_c, $pat_c, $nc_beg_c,
                     $nc_end_c )
@@ -3377,11 +3390,6 @@ sub prune_alignment_tree {
                     push @delete_list, [ $jbeg_c, $jend_c, $lev_p ];
                 }
                 else {
-                    if ( defined($jend_c_keep) && $jbeg_c == $jend_c_keep + 1 )
-                    {
-                        $rlines->[$jend_c_keep]->{_end_group} = 1;
-                    }
-                    $jend_c_keep = $jend_c;
                     push @todo_next, $nc;
                 }
             }
diff --git a/t/snippets/align33.in b/t/snippets/align33.in
new file mode 100644 (file)
index 0000000..6a824c4
--- /dev/null
@@ -0,0 +1,9 @@
+$wl  = int( $wl * $f + .5 );
+$wr  = int( $wr * $f + .5 );
+$pag = int( $pageh * $f + .5 );
+$fe  = $opt_F ? "t" : "f";
+$cf  = $opt_U ? "t" : "f";
+$tp  = $opt_t ? "t" : "f";
+$rm  = $numbstyle ? "t" : "f";
+$pa = $showurl   ? "t" : "f";
+$nh = $seq_number ? "t" : "f";
diff --git a/t/snippets/expect/align33.def b/t/snippets/expect/align33.def
new file mode 100644 (file)
index 0000000..388a266
--- /dev/null
@@ -0,0 +1,9 @@
+$wl  = int( $wl * $f + .5 );
+$wr  = int( $wr * $f + .5 );
+$pag = int( $pageh * $f + .5 );
+$fe = $opt_F      ? "t" : "f";
+$cf = $opt_U      ? "t" : "f";
+$tp = $opt_t      ? "t" : "f";
+$rm = $numbstyle  ? "t" : "f";
+$pa = $showurl    ? "t" : "f";
+$nh = $seq_number ? "t" : "f";
index 21da717fa98df4a8f66dc6742764c40f0bd133d7..de4d222127eb13fcbaadb36e64fc6f6314505821 100644 (file)
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
+../snippets21.t        align33.def
index 77c4485b8e2703c4f5894caf6764a0f1a42c983e..501e46bfa3a311757371ad2f14a3a2dacde21efc 100644 (file)
@@ -7,6 +7,7 @@
 #4 sot.def
 #5 sot.sot
 #6 prune.def
+#7 align33.def
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -35,6 +36,18 @@ BEGIN {
     ############################
     $rsources = {
 
+        'align33' => <<'----------',
+$wl  = int( $wl * $f + .5 );
+$wr  = int( $wr * $f + .5 );
+$pag = int( $pageh * $f + .5 );
+$fe  = $opt_F ? "t" : "f";
+$cf  = $opt_U ? "t" : "f";
+$tp  = $opt_t ? "t" : "f";
+$rm  = $numbstyle ? "t" : "f";
+$pa = $showurl   ? "t" : "f";
+$nh = $seq_number ? "t" : "f";
+----------
+
         'lop' => <<'----------',
 # logical padding examples
 $same =
@@ -359,6 +372,22 @@ is_deeply \@t, [
 ];
 #6...........
         },
+
+        'align33.def' => {
+            source => "align33",
+            params => "def",
+            expect => <<'#7...........',
+$wl  = int( $wl * $f + .5 );
+$wr  = int( $wr * $f + .5 );
+$pag = int( $pageh * $f + .5 );
+$fe = $opt_F      ? "t" : "f";
+$cf = $opt_U      ? "t" : "f";
+$tp = $opt_t      ? "t" : "f";
+$rm = $numbstyle  ? "t" : "f";
+$pa = $showurl    ? "t" : "f";
+$nh = $seq_number ? "t" : "f";
+#7...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};