From 921629870354ba772aeeaba8cdfb60bccb18f669 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 6 Apr 2021 14:38:58 -0700 Subject: [PATCH] Fix problem with excess blank line generation with -blao --- bin/perltidy | 5 ++++- lib/Perl/Tidy/Formatter.pm | 44 ++++++++++++++++++++++++++++++-------- local-docs/BugLog.pod | 40 ++++++++++++++++++++++++++++++---- t/snippets/rt113689.par | 1 + t/snippets7.t | 1 + 5 files changed, 77 insertions(+), 14 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index ffa7c527..e5fe794f 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -3481,6 +3481,9 @@ with an opening block brace of a specified type. By default, this only applies to the block of a named B, but this can be changed (see B<-blaol> below). The default is not to do this (B). +If the value of B is greater than the limit specified with the parameter +B<--maximum-consecutive-blank-lines=n>, it will be reduced to that value. + Please see the note below on using the B<-blao> and B<-blbc> options. =item B<-blbc=i> or B<--blank-lines-before-closing-block=i> @@ -3518,7 +3521,7 @@ this using perltidy -blao=2 -blbc=2 -blaol='*' -blbcl='*' filename Now suppose the script continues to be developed, but at some later date we -decide we don't want these spaces after all. we might expect that running with +decide we don't want these spaces after all. We might expect that running with the flags B<-blao=0> and B<-blbc=0> will undo them. However, by default perltidy retains single blank lines, so the blank lines remain. diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 1147c8b3..b0a66da4 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -7205,9 +7205,13 @@ EOM $ris_essential_old_breakpoint->{$Kprev} = 1; # Back up and count length from a token like '=' or '=>' if -lp - # is used; this fixes b520 - if ($rOpts_line_up_parentheses) { - if ( substr( $rLL->[$Kprev]->[_TYPE_], 0, 1 ) eq '=' ) { + # is used (this fixes b520) + # ...or if a break is wanted before there (this fixes b1041). + my $type_prev = $rLL->[$Kprev]->[_TYPE_]; + if ( $rOpts_line_up_parentheses + || $want_break_before{$type_prev} ) + { + if ( substr( $type_prev, 0, 1 ) eq '=' ) { $Kref = $Kprev; } } @@ -11499,9 +11503,10 @@ sub compare_indentation_levels { sub grind_batch_of_CODE { my ($self) = @_; - my $file_writer_object = $self->[_file_writer_object_]; - my $this_batch = $self->[_this_batch_]; + my $file_writer_object = $self->[_file_writer_object_]; + my $rlines = $self->[_rlines_]; + my $this_batch = $self->[_this_batch_]; $batch_count++; my $starting_in_quote = $this_batch->[_starting_in_quote_]; @@ -11952,14 +11957,35 @@ EOM # write requested number of blank lines after an opening block brace if ( $iterm >= $imin && $types_to_go[$iterm] eq '{' ) { - if ( $rOpts->{'blank-lines-after-opening-block'} + my $nblanks = $rOpts->{'blank-lines-after-opening-block'}; + if ( $nblanks && $block_type_to_go[$iterm] && $block_type_to_go[$iterm] =~ /$blank_lines_after_opening_block_pattern/ ) { - my $nblanks = $rOpts->{'blank-lines-after-opening-block'}; - $self->flush_vertical_aligner(); - $file_writer_object->require_blank_code_lines($nblanks); + + if ( $nblanks > $rOpts_maximum_consecutive_blank_lines ) { + $nblanks = $rOpts_maximum_consecutive_blank_lines; + } + + # Reduce by the existing blank count .. fixes case b1073 + my $Kterm = $K_to_go[$iterm]; + my $iline = $rLL->[$Kterm]->[_LINE_INDEX_] + 1; + while ( $nblanks > 0 ) { + my $line_of_tokens = $rlines->[$iline]; + last unless defined($line_of_tokens); + my $line_type = $line_of_tokens->{_line_type}; + last + if ( $line_type ne 'CODE' + || $line_of_tokens->{_code_type} ne 'BL' ); + $nblanks--; + $iline++; + } + + if ($nblanks) { + $self->flush_vertical_aligner(); + $file_writer_object->require_blank_code_lines($nblanks); + } } } } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 45960e1d..790d2800 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -3,6 +3,38 @@ =over 4 +=item B + +Random testing produced some cases where excess blank lines could be generated +with the parameter -blank-lines-after-opening-block. Case b1073 has the +following script + + sub stop { + + 1; + } + +and the following parameters + + --blank-lines-after-opening-block=2 + --maximum-consecutive-blank-lines=10 + --maximum-line-length=15 + +When run, blank lines keep getting generated until the maximum is reached. +This has been fixed. + +6 Apr 2021. + + +=item B + +A change was made to a welding rule involving the -lp option or -wbb='=', and +very short maximum line lengths. This correctly fixes case b1041. It replaces +a fix for this case reported on 2 Apr 2021. + +5 Apr 2021. + + =item B Random testing produced some edge cases in which formatting with the -bbx=2 flags, @@ -14,14 +46,14 @@ changing much existing formatting. This update fixes cases b1068 b1069 b1070 b1072 b1073 b1076. -5 Apr 2021. +5 Apr 2021, 16c4591. =item B The previous update produced some problems in testing which are corrected with this update. -5 Apr 2021. +5 Apr 2021, ffef089. =item B @@ -31,7 +63,7 @@ adjusting a tolerance in the line length calculation. This fixes cases b1041 b1055. -2 Apr 2021. +2 Apr 2021, a8b6259. =item B @@ -40,7 +72,7 @@ converge. This was fixed by turning off -xci for braces under -bli control. This fixes case b1065. -2 Apr 2021. +2 Apr 2021, d20ea80. =back diff --git a/t/snippets/rt113689.par b/t/snippets/rt113689.par index e1f0f8c6..80f49e25 100644 --- a/t/snippets/rt113689.par +++ b/t/snippets/rt113689.par @@ -2,3 +2,4 @@ -blbc=1 -blaol='*' -blbcl='*' +-mbl=2 diff --git a/t/snippets7.t b/t/snippets7.t index e89b4995..aff36100 100644 --- a/t/snippets7.t +++ b/t/snippets7.t @@ -52,6 +52,7 @@ BEGIN { -blbc=1 -blaol='*' -blbcl='*' +-mbl=2 ---------- 'rt119970' => "-wn", }; -- 2.39.5