From 90cceb1a3b87ca4d4794e37f2726e5dffc4fcec8 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sun, 16 May 2021 07:13:59 -0700 Subject: [PATCH] Fix inconsistency involving counting commas --- CHANGES.md | 5 +++++ lib/Perl/Tidy/Formatter.pm | 36 ++++++++++++++++++++++++++++-------- local-docs/BugLog.pod | 14 ++++++++++++-- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 3f27fb65..cabd19d0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,11 @@ - Added a new option for closing paren placement, -vtc=3, requested in rt #136417. + - Some nested structures formatted with the -lp indentation option may have + some changes in indentation. This is due to some updates which were made to + prevent formatting instability when line lengths are limited by the maximum line + length. Most scripts will not be affected. + - A more complete list of updates is at https://github.com/perltidy/perltidy/blob/master/local-docs/BugLog.pod diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 06f95b6d..55e2e511 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -5110,8 +5110,9 @@ sub respace_tokens { # Fix for case b1100: Count a line ending in ', [' as having # a line-ending comma. Otherwise, these commas can be hidden # with something like --opening-square-bracket-right - if ( $Ktoken_vars == $Klast_old_code - && $last_nonblank_type eq ',' ) + if ( $last_nonblank_type eq ',' + && $Ktoken_vars == $Klast_old_code + && $Ktoken_vars > $Kfirst_old ) { $rlec_count_by_seqno->{$type_sequence}++; } @@ -5750,7 +5751,9 @@ sub respace_tokens { if ($seqno) { $rtype_count_by_seqno->{$seqno} ->{$last_nonblank_type}--; - if ( $last_nonblank_type eq ',' + + if ( $KK == $Kfirst + && $last_nonblank_type eq ',' && $rlec_count_by_seqno->{$seqno} ) { $rlec_count_by_seqno->{$seqno}--; @@ -8729,7 +8732,12 @@ sub break_before_list_opening_containers { elsif ( $break_option == 2 ) { # break if this list contains a broken list with line-ending comma - my $ok_to_break = $has_list_with_lec; + my $ok_to_break; + my $Msg = ""; + if ($has_list_with_lec) { + $ok_to_break = 1; + $Msg = "has list with lec;"; + } if ( !$ok_to_break ) { @@ -8742,21 +8750,33 @@ sub break_before_list_opening_containers { if ($has_list) { $rno_xci_by_seqno->{$seqno} = 1 } my $parent = $rparent_of_seqno->{$seqno}; - $ok_to_break ||= $self->is_list_by_seqno($parent); + if ( $self->is_list_by_seqno($parent) ) { + $Msg = "parent is list"; + $ok_to_break = 1; + } } # Patch to fix b1099 for -lp # ok in -lp mode if this is a list which contains a list if ( !$ok_to_break && $rOpts_line_up_parentheses ) { - $ok_to_break ||= $is_list && $has_list; + if ( $is_list && $has_list ) { + $ok_to_break = 1; + $Msg = "is list or has list"; + } } - next unless ($ok_to_break); + if ( !$ok_to_break ) { + DEBUG_BBX + && print STDOUT "Not breaking at seqno=$seqno: $Msg\n"; + next; + } + + DEBUG_BBX + && print STDOUT "OK to break at seqno=$seqno: $Msg\n"; # Patch: turn off -xci if -bbx=2 and -lp # This fixes cases b1090 b1095 b1101 b1116 b1118 b1121 b1122 $rno_xci_by_seqno->{$seqno} = 1 if ($rOpts_line_up_parentheses); - } # -bbx=3 = always break diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index cc2bbe13..840a6743 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,16 +2,26 @@ =over 4 +=item B + +Random testing produced a formatting instability involving the combination of +flags -bbp=2 -xci -vt=2 -bvtc=2. The problem was traced to an error in +counting the number of line ending commas in lists. + +This fixes case b1130. + +15 May 2021. + =item B -Random testing produced a edge case of formatting instability for -lp indentation +Random testing produced an edge case of formatting instability for -lp indentation which was traced to checking for an old line break at a '=>'. This has been fixed. Some existing formatting with deeply nested structures may be slightly changed due to the fix, but most existing formatting will be unchanged. This fixes b1035. -15 May 2021. +15 May 2021, dd42648. =item B -- 2.39.5