From 765c32364f03eafc1d5e468ace11f2223f5a8ced Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 1 Jul 2020 19:28:23 -0700 Subject: [PATCH] minor vertical alignment improvement + test case --- lib/Perl/Tidy/VerticalAligner.pm | 32 ++++++++++++++++++++------------ t/snippets/align33.in | 9 +++++++++ t/snippets/expect/align33.def | 9 +++++++++ t/snippets/packing_list.txt | 1 + t/snippets21.t | 29 +++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 t/snippets/align33.in create mode 100644 t/snippets/expect/align33.def diff --git a/lib/Perl/Tidy/VerticalAligner.pm b/lib/Perl/Tidy/VerticalAligner.pm index 388a2b34..5b5df468 100644 --- a/lib/Perl/Tidy/VerticalAligner.pm +++ b/lib/Perl/Tidy/VerticalAligner.pm @@ -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 index 00000000..6a824c4c --- /dev/null +++ b/t/snippets/align33.in @@ -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 index 00000000..388a266e --- /dev/null +++ b/t/snippets/expect/align33.def @@ -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/packing_list.txt b/t/snippets/packing_list.txt index 21da717f..de4d2221 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -397,3 +397,4 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def +../snippets21.t align33.def diff --git a/t/snippets21.t b/t/snippets21.t index 77c4485b..501e46bf 100644 --- a/t/snippets21.t +++ b/t/snippets21.t @@ -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}; -- 2.39.5