From: Steve Hancock Date: Wed, 7 Apr 2021 01:28:30 +0000 (-0700) Subject: Moved logic of previous update to the FileWriter module X-Git-Tag: 20210402.01~80 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=756e930cc388f46b973332adfffcb3702764a2e5;p=perltidy.git Moved logic of previous update to the FileWriter module --- diff --git a/bin/perltidy b/bin/perltidy index e5fe794f..142c0960 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -3481,9 +3481,6 @@ 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> diff --git a/lib/Perl/Tidy/FileWriter.pm b/lib/Perl/Tidy/FileWriter.pm index fb5af8e1..10e72ac3 100644 --- a/lib/Perl/Tidy/FileWriter.pm +++ b/lib/Perl/Tidy/FileWriter.pm @@ -47,25 +47,26 @@ BEGIN { # Array index names for variables my $i = 0; use constant { - _line_sink_object_ => $i++, - _logger_object_ => $i++, - _rOpts_ => $i++, - _output_line_number_ => $i++, - _consecutive_blank_lines_ => $i++, - _consecutive_nonblank_lines_ => $i++, - _first_line_length_error_ => $i++, - _max_line_length_error_ => $i++, - _last_line_length_error_ => $i++, - _first_line_length_error_at_ => $i++, - _max_line_length_error_at_ => $i++, - _last_line_length_error_at_ => $i++, - _line_length_error_count_ => $i++, - _max_output_line_length_ => $i++, - _max_output_line_length_at_ => $i++, - _rK_checklist_ => $i++, - _K_arrival_order_matches_ => $i++, - _K_sequence_error_msg_ => $i++, - _K_last_arrival_ => $i++, + _line_sink_object_ => $i++, + _logger_object_ => $i++, + _rOpts_ => $i++, + _output_line_number_ => $i++, + _consecutive_blank_lines_ => $i++, + _consecutive_nonblank_lines_ => $i++, + _consecutive_new_blank_lines_ => $i++, + _first_line_length_error_ => $i++, + _max_line_length_error_ => $i++, + _last_line_length_error_ => $i++, + _first_line_length_error_at_ => $i++, + _max_line_length_error_at_ => $i++, + _last_line_length_error_at_ => $i++, + _line_length_error_count_ => $i++, + _max_output_line_length_ => $i++, + _max_output_line_length_at_ => $i++, + _rK_checklist_ => $i++, + _K_arrival_order_matches_ => $i++, + _K_sequence_error_msg_ => $i++, + _K_last_arrival_ => $i++, }; } @@ -89,25 +90,26 @@ sub new { my ( $class, $line_sink_object, $rOpts, $logger_object ) = @_; my $self = []; - $self->[_line_sink_object_] = $line_sink_object; - $self->[_logger_object_] = $logger_object; - $self->[_rOpts_] = $rOpts; - $self->[_output_line_number_] = 1; - $self->[_consecutive_blank_lines_] = 0; - $self->[_consecutive_nonblank_lines_] = 0; - $self->[_first_line_length_error_] = 0; - $self->[_max_line_length_error_] = 0; - $self->[_last_line_length_error_] = 0; - $self->[_first_line_length_error_at_] = 0; - $self->[_max_line_length_error_at_] = 0; - $self->[_last_line_length_error_at_] = 0; - $self->[_line_length_error_count_] = 0; - $self->[_max_output_line_length_] = 0; - $self->[_max_output_line_length_at_] = 0; - $self->[_rK_checklist_] = []; - $self->[_K_arrival_order_matches_] = 0; - $self->[_K_sequence_error_msg_] = ""; - $self->[_K_last_arrival_] = -1; + $self->[_line_sink_object_] = $line_sink_object; + $self->[_logger_object_] = $logger_object; + $self->[_rOpts_] = $rOpts; + $self->[_output_line_number_] = 1; + $self->[_consecutive_blank_lines_] = 0; + $self->[_consecutive_nonblank_lines_] = 0; + $self->[_consecutive_new_blank_lines_] = 0; + $self->[_first_line_length_error_] = 0; + $self->[_max_line_length_error_] = 0; + $self->[_last_line_length_error_] = 0; + $self->[_first_line_length_error_at_] = 0; + $self->[_max_line_length_error_at_] = 0; + $self->[_last_line_length_error_at_] = 0; + $self->[_line_length_error_count_] = 0; + $self->[_max_output_line_length_] = 0; + $self->[_max_output_line_length_at_] = 0; + $self->[_rK_checklist_] = []; + $self->[_K_arrival_order_matches_] = 0; + $self->[_K_sequence_error_msg_] = ""; + $self->[_K_last_arrival_] = -1; # save input stream name for local error messages $input_stream_name = ""; @@ -163,6 +165,10 @@ sub get_consecutive_nonblank_lines { return $_[0]->[_consecutive_nonblank_lines_]; } +sub get_consecutive_blank_lines { + return $_[0]->[_consecutive_blank_lines_]; +} + sub reset_consecutive_blank_lines { $_[0]->[_consecutive_blank_lines_] = 0; return; @@ -199,16 +205,26 @@ sub write_blank_code_line { if (!$forced && $self->[_consecutive_blank_lines_] >= $rOpts->{'maximum-consecutive-blank-lines'} ); - $self->[_consecutive_blank_lines_]++; + $self->[_consecutive_nonblank_lines_] = 0; + + if ( !$forced && $self->[_consecutive_new_blank_lines_] > 0 ) { + $self->[_consecutive_new_blank_lines_]--; + return; + } + $self->write_line("\n"); + $self->[_consecutive_blank_lines_]++; + $self->[_consecutive_new_blank_lines_]++ if ($forced); + return; } sub write_code_line { my ( $self, $str, $K ) = @_; - $self->[_consecutive_blank_lines_] = 0; + $self->[_consecutive_blank_lines_] = 0; + $self->[_consecutive_new_blank_lines_] = 0; $self->[_consecutive_nonblank_lines_]++; $self->write_line($str); diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index b0a66da4..9a87efe7 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -8962,7 +8962,7 @@ sub process_all_lines { my $rwant_blank_line_after = $self->keyword_group_scan(); my $line_type = ""; - my $i_last_POD_END = -1; + my $i_last_POD_END = -10; my $i = -1; foreach my $line_of_tokens ( @{$rlines} ) { $i++; @@ -11503,10 +11503,9 @@ sub compare_indentation_levels { sub grind_batch_of_CODE { my ($self) = @_; - my $file_writer_object = $self->[_file_writer_object_]; - my $rlines = $self->[_rlines_]; - my $this_batch = $self->[_this_batch_]; + + my $this_batch = $self->[_this_batch_]; $batch_count++; my $starting_in_quote = $this_batch->[_starting_in_quote_]; @@ -11957,35 +11956,14 @@ EOM # write requested number of blank lines after an opening block brace if ( $iterm >= $imin && $types_to_go[$iterm] eq '{' ) { - my $nblanks = $rOpts->{'blank-lines-after-opening-block'}; - if ( $nblanks + if ( $rOpts->{'blank-lines-after-opening-block'} && $block_type_to_go[$iterm] && $block_type_to_go[$iterm] =~ /$blank_lines_after_opening_block_pattern/ ) { - - 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); - } + my $nblanks = $rOpts->{'blank-lines-after-opening-block'}; + $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 790d2800..dc79b19f 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -3,6 +3,14 @@ =over 4 +=item B + +The previous update regarding blank line generation was not sufficiently +general to handle all possible parameter combinations. The problem was solved +and simplified by moving the logic to a lower level, in the FileWriter module. + +6 Apr 2021. + =item B Random testing produced some cases where excess blank lines could be generated