From 66be4555f796dff5b6b3d9841a1f455d92037431 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 8 Feb 2021 10:07:49 -0800 Subject: [PATCH] Restrict references to old line breaks --- lib/Perl/Tidy/Formatter.pm | 39 +++++++++----------- local-docs/BugLog.pod | 51 +++++++++++++++++++++++++-- t/snippets/expect/andor6.def | 3 +- t/snippets/expect/multiple_equals.def | 4 +-- t/snippets1.t | 3 +- t/snippets12.t | 2 +- t/snippets16.t | 4 +-- 7 files changed, 75 insertions(+), 31 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 95d11282..eb0eb50a 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -3210,8 +3210,8 @@ EOM my $list_str = $left_bond_strength{'?'}; my ( $block_type, $i_next, $i_next_nonblank, $next_nonblank_token, - $next_nonblank_type, $next_token, $next_type, $total_nesting_depth, - ); + $next_nonblank_type, $next_token, $next_type, + $total_nesting_depth, ); # main loop to compute bond strengths between each pair of tokens foreach my $i ( 0 .. $max_index_to_go ) { @@ -3574,8 +3574,14 @@ EOM : $next_nonblank_token : $next_nonblank_type; - # add any bias set by sub scan_list at old comma break points. - if ( $type eq ',' ) { $bond_str += $bond_strength_to_go[$i] } + if ( $type eq ',' ) { + + # add any bias set by sub scan_list at old comma break points + $bond_str += $bond_strength_to_go[$i]; + + # Avoid breaking at a useless terminal comma + $bond_str += 0.001 if ( $next_nonblank_type eq '}' ); + } # bias left token elsif ( defined( $bias{$left_key} ) ) { @@ -13609,25 +13615,14 @@ sub set_continuation_breaks { $strength = $bond_strength_to_go[$i_test]; if ( $type eq 'b' ) { $strength = $last_strength } - # use old breaks as a tie-breaker. For example to - # prevent blinkers with -pbp in this code: - -##@keywords{ -## qw/ARG OUTPUT PROTO CONSTRUCTOR RETURNS DESC PARAMS SEEALSO EXAMPLE/} -## = (); - - # At the same time try to prevent a leading * in this code - # with the default formatting: - # -## return -## factorial( $a + $b - 1 ) / factorial( $a - 1 ) / factorial( $b - 1 ) -## * ( $x**( $a - 1 ) ) -## * ( ( 1 - $x )**( $b - 1 ) ); - - # reduce strength a bit to break ties at an old breakpoint ... + # reduce strength a bit to break ties at an old comma breakpoint ... if ( + $old_breakpoint_to_go[$i_test] + # Patch: limited to just commas to avoid blinking states + && $type eq ',' + # which is a 'good' breakpoint, meaning ... # we don't want to break before it && !$want_break_before{$type} @@ -20232,8 +20227,8 @@ sub set_vertical_tightness_flags { my $valid_flag = 1; my $spaces = ( $types_to_go[ $ibeg_next - 1 ] eq 'b' ) ? 1 : 0; @{$rvertical_tightness_flags} = - ( 2, $spaces, $type_sequence_to_go[$ibeg_next], $valid_flag, - ); + ( 2, $spaces, $type_sequence_to_go[$ibeg_next], + $valid_flag, ); } } } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 23153c52..fe6c3620 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,53 @@ =over 4 +=item B + +A number of cases of blinking states were traced to code which biased +the breaking of long lines to existing breaks. This was fixed by restricting +this coding to just use old comma line break points. + +The following cases were fixed with this update: + +b193 b194 b195 b197 b198 b199 b216 b217 b218 b219 b220 b221 b244 b245 b246 b247 +b249 b251 b252 b253 b254 b256 b257 b258 b259 b260 b261 b262 b263 b264 b265 b266 +b268 b269 b270 b271 b272 b274 b275 b278 b280 b281 b283 b285 b288 b291 b295 b296 +b297 b299 b302 b304 b305 b307 b310 b311 b312 b313 b314 b315 b316 b317 b318 b319 +b320 b321 b322 b323 b324 b325 b326 b327 b329 b330 b331 b332 b333 b334 b335 b336 +b337 b338 b339 b340 b341 b342 b343 b344 b345 b346 b347 b348 b349 + +8 Feb 2021. + +=item B + +Given the following input line with a length of 81, the default formatting +will break at the last comma: + + emit_subroutine_test( $test_file, $name, $capitalization_scheme, $failures ); + + # perltidy -sil=1 -iob + emit_subroutine_test( $test_file, $name, $capitalization_scheme, + $failures ); + +But if the input line has a comma at the end of the list + + emit_subroutine_test( $test_file, $name, $capitalization_scheme, $failures, ); + +# Then the break is at the last comma + + # OLD: perltidy -sil=1 -iob + emit_subroutine_test( $test_file, $name, $capitalization_scheme, $failures, + ); + +This update causes the break to be at the previous comma, so that the output +is similar to the output without the needless ending comma: + + # NEW: perltidy -sil=1 -iob + emit_subroutine_test( $test_file, $name, $capitalization_scheme, + $failures, ); + +8 Feb 2021. + =item B Random testing revealed a rare alternating state which could occur when both @@ -12,7 +59,7 @@ realistic parameter values. The following case was fixed with this update: b690. -6 Feb 2021. +6 Feb 2021, 3e96930. =item B @@ -22,7 +69,7 @@ unknown, a weld will be avoided. The following cases were fixed with this update: b611 b626. -6 Feb 2021, 5083ab9. +6 Feb 2021, 6b1f44a =item B diff --git a/t/snippets/expect/andor6.def b/t/snippets/expect/andor6.def index f5513afa..7f41552d 100644 --- a/t/snippets/expect/andor6.def +++ b/t/snippets/expect/andor6.def @@ -2,7 +2,8 @@ sub is_miniwhile { # check for one-line loop (`foo() while $y--') my $op = shift; return ( - !null($op) and null( $op->sibling ) + !null($op) + and null( $op->sibling ) and $op->ppaddr eq "pp_null" and class($op) eq "UNOP" and ( diff --git a/t/snippets/expect/multiple_equals.def b/t/snippets/expect/multiple_equals.def index f10eefe3..6e65950a 100644 --- a/t/snippets/expect/multiple_equals.def +++ b/t/snippets/expect/multiple_equals.def @@ -4,5 +4,5 @@ $full_index = 1 if $opt_i; $query_all = $opt_A if $opt_A; # not aligning multiple '='s here -$start = $end = $len = $ismut = $number = $allele_ori = $allele_mut = - $proof = $xxxxreg = $reg = $dist = ''; +$start = $end = $len = $ismut = $number = $allele_ori = $allele_mut = $proof = + $xxxxreg = $reg = $dist = ''; diff --git a/t/snippets1.t b/t/snippets1.t index 210e5ca0..546cee53 100644 --- a/t/snippets1.t +++ b/t/snippets1.t @@ -450,7 +450,8 @@ ok( ( $obj->name() eq $obj2->name() ) sub is_miniwhile { # check for one-line loop (`foo() while $y--') my $op = shift; return ( - !null($op) and null( $op->sibling ) + !null($op) + and null( $op->sibling ) and $op->ppaddr eq "pp_null" and class($op) eq "UNOP" and ( diff --git a/t/snippets12.t b/t/snippets12.t index 89172f42..d2b7edee 100644 --- a/t/snippets12.t +++ b/t/snippets12.t @@ -167,7 +167,7 @@ use_all_ok( @{ $coords[0] }, @{ $coords[1] } ) ) ); # OLD: do not weld to a one-line block because the function could - # get separated from its opening paren. + # get separated from its opening paren. # NEW: (30-jan-2021): keep one-line block together for stability $_[0]->code_handler ( sub { $morexxxxxxxxxxxxxxxxxx .= $_[1] . ":" . $_[0] . "\n" } ); diff --git a/t/snippets16.t b/t/snippets16.t index 5951bf60..7322a8b5 100644 --- a/t/snippets16.t +++ b/t/snippets16.t @@ -254,8 +254,8 @@ $full_index = 1 if $opt_i; $query_all = $opt_A if $opt_A; # not aligning multiple '='s here -$start = $end = $len = $ismut = $number = $allele_ori = $allele_mut = - $proof = $xxxxreg = $reg = $dist = ''; +$start = $end = $len = $ismut = $number = $allele_ori = $allele_mut = $proof = + $xxxxreg = $reg = $dist = ''; #6........... }, -- 2.39.5