"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/Perl/Tidy/VerticalAligner.pm" between
Perl-Tidy-20210402.tar.gz and Perl-Tidy-20210717.tar.gz

About: Perltidy is a Perl script indenter and reformatter (beautifier).

VerticalAligner.pm  (Perl-Tidy-20210402):VerticalAligner.pm  (Perl-Tidy-20210717)
package Perl::Tidy::VerticalAligner; package Perl::Tidy::VerticalAligner;
use strict; use strict;
use warnings; use warnings;
our $VERSION = '20210402'; our $VERSION = '20210717';
use Perl::Tidy::VerticalAligner::Alignment; use Perl::Tidy::VerticalAligner::Alignment;
use Perl::Tidy::VerticalAligner::Line; use Perl::Tidy::VerticalAligner::Line;
# The Perl::Tidy::VerticalAligner package collects output lines and # The Perl::Tidy::VerticalAligner package collects output lines and
# attempts to line up certain common tokens, such as => and #, which are # attempts to line up certain common tokens, such as => and #, which are
# identified by the calling routine. # identified by the calling routine.
# #
# Usage: # Usage:
# - Initiate an object with a call to new(). # - Initiate an object with a call to new().
skipping to change at line 121 skipping to change at line 121
_first_outdented_line_at_ => $i++, _first_outdented_line_at_ => $i++,
_last_outdented_line_at_ => $i++, _last_outdented_line_at_ => $i++,
_consecutive_block_comments_ => $i++, _consecutive_block_comments_ => $i++,
_rgroup_lines_ => $i++, _rgroup_lines_ => $i++,
_group_level_ => $i++, _group_level_ => $i++,
_group_type_ => $i++, _group_type_ => $i++,
_zero_count_ => $i++, _zero_count_ => $i++,
_last_leading_space_count_ => $i++, _last_leading_space_count_ => $i++,
_comment_leading_space_count_ => $i++, _comment_leading_space_count_ => $i++,
_extra_indent_ok_ => $i++,
}; };
# Debug flag. This is a relic from the original program development # Debug flag. This is a relic from the original program development
# looking for problems with tab characters. Caution: this debug flag can # looking for problems with tab characters. Caution: this debug flag can
# produce a lot of output It should be 0 except when debugging small # produce a lot of output It should be 0 except when debugging small
# scripts. # scripts.
use constant DEBUG_TABS => 0; use constant DEBUG_TABS => 0;
my $debug_warning = sub { my $debug_warning = sub {
skipping to change at line 194 skipping to change at line 193
$rOpts->{'variable-maximum-line-length'}; $rOpts->{'variable-maximum-line-length'};
$self->[_rOpts_valign_] = $rOpts->{'valign'}; $self->[_rOpts_valign_] = $rOpts->{'valign'};
# Batch of lines being collected # Batch of lines being collected
$self->[_rgroup_lines_] = []; $self->[_rgroup_lines_] = [];
$self->[_group_level_] = 0; $self->[_group_level_] = 0;
$self->[_group_type_] = ""; $self->[_group_type_] = "";
$self->[_zero_count_] = 0; $self->[_zero_count_] = 0;
$self->[_comment_leading_space_count_] = 0; $self->[_comment_leading_space_count_] = 0;
$self->[_last_leading_space_count_] = 0; $self->[_last_leading_space_count_] = 0;
$self->[_extra_indent_ok_] = 0;
# Memory of what has been processed # Memory of what has been processed
$self->[_last_level_written_] = -1; $self->[_last_level_written_] = -1;
$self->[_last_side_comment_column_] = 0; $self->[_last_side_comment_column_] = 0;
$self->[_last_side_comment_line_number_] = 0; $self->[_last_side_comment_line_number_] = 0;
$self->[_last_side_comment_length_] = 0; $self->[_last_side_comment_length_] = 0;
$self->[_last_side_comment_level_] = -1; $self->[_last_side_comment_level_] = -1;
$self->[_outdented_line_count_] = 0; $self->[_outdented_line_count_] = 0;
$self->[_first_outdented_line_at_] = 0; $self->[_first_outdented_line_at_] = 0;
$self->[_last_outdented_line_at_] = 0; $self->[_last_outdented_line_at_] = 0;
skipping to change at line 403 skipping to change at line 401
my ( $self, $rline_hash ) = @_; my ( $self, $rline_hash ) = @_;
my $level = $rline_hash->{level}; my $level = $rline_hash->{level};
my $level_end = $rline_hash->{level_end}; my $level_end = $rline_hash->{level_end};
my $level_adj = $rline_hash->{level_adj}; my $level_adj = $rline_hash->{level_adj};
my $indentation = $rline_hash->{indentation}; my $indentation = $rline_hash->{indentation};
my $list_seqno = $rline_hash->{list_seqno}; my $list_seqno = $rline_hash->{list_seqno};
my $outdent_long_lines = $rline_hash->{outdent_long_lines}; my $outdent_long_lines = $rline_hash->{outdent_long_lines};
my $is_terminal_ternary = $rline_hash->{is_terminal_ternary}; my $is_terminal_ternary = $rline_hash->{is_terminal_ternary};
my $is_terminal_statement = $rline_hash->{is_terminal_statement};
my $rvertical_tightness_flags = $rline_hash->{rvertical_tightness_flags}; my $rvertical_tightness_flags = $rline_hash->{rvertical_tightness_flags};
my $level_jump = $rline_hash->{level_jump}; my $level_jump = $rline_hash->{level_jump};
my $rfields = $rline_hash->{rfields}; my $rfields = $rline_hash->{rfields};
my $rtokens = $rline_hash->{rtokens}; my $rtokens = $rline_hash->{rtokens};
my $rpatterns = $rline_hash->{rpatterns}; my $rpatterns = $rline_hash->{rpatterns};
my $rfield_lengths = $rline_hash->{rfield_lengths}; my $rfield_lengths = $rline_hash->{rfield_lengths};
my $terminal_block_type = $rline_hash->{terminal_block_type}; my $terminal_block_type = $rline_hash->{terminal_block_type};
my $batch_count = $rline_hash->{batch_count}; my $batch_count = $rline_hash->{batch_count};
my $break_alignment_before = $rline_hash->{break_alignment_before}; my $break_alignment_before = $rline_hash->{break_alignment_before};
my $break_alignment_after = $rline_hash->{break_alignment_after}; my $break_alignment_after = $rline_hash->{break_alignment_after};
skipping to change at line 510 skipping to change at line 507
} }
} }
# shouldn't happen: # shouldn't happen:
if ( $level < 0 ) { $level = 0 } if ( $level < 0 ) { $level = 0 }
# do not align code across indentation level changes # do not align code across indentation level changes
# or if vertical alignment is turned off for debugging # or if vertical alignment is turned off for debugging
if ( $level != $group_level || $is_outdented || !$self->[_rOpts_valign_] ) { if ( $level != $group_level || $is_outdented || !$self->[_rOpts_valign_] ) {
# we are allowed to shift a group of lines to the right if its $self->_flush_group_lines( $level - $group_level );
# level is greater than the previous and next group
$self->[_extra_indent_ok_] =
( $level < $group_level
&& $self->[_last_level_written_] < $group_level );
$self->_flush_group_lines();
# If we know that this line will get flushed out by itself because
# of level changes, we can leave the extra_indent_ok flag set.
# That way, if we get an external flush call, we will still be
# able to do some -lp alignment if necessary.
$self->[_extra_indent_ok_] =
( $is_terminal_statement && $level > $group_level );
$group_level = $level; $group_level = $level;
$self->[_group_level_] = $group_level; $self->[_group_level_] = $group_level;
# wait until after the above flush to get the leading space # wait until after the above flush to get the leading space
# count because it may have been changed if the -icp flag is in # count because it may have been changed if the -icp flag is in
# effect # effect
$leading_space_count = get_spaces($indentation); $leading_space_count = get_spaces($indentation);
} }
skipping to change at line 656 skipping to change at line 640
} }
); );
return; return;
} }
} }
else { else {
$self->[_zero_count_] = 0; $self->[_zero_count_] = 0;
} }
# programming check: (shouldn't happen)
# The number of tokens which separate the fields must always be
# one less than the number of fields. If this is not true then
# an error has been made by the Formatter in defining these
# quantities. See Formatter.pm/sub make_alignment_patterns.
if ( @{$rfields} && ( @{$rtokens} != ( @{$rfields} - 1 ) ) ) {
my $nt = @{$rtokens};
my $nf = @{$rfields};
my $msg = <<EOM;
"Program bug in Perl::Tidy::VerticalAligner - number of tokens = $nt should be o
ne less than number of fields: $nf)\n"
EOM
$self->warning($msg);
$self->report_definite_bug();
# TODO: this has never happened, but we should probably call Die here.
# Needs some testing
# Perl::Tidy::Die($msg);
}
my $maximum_line_length_for_level = my $maximum_line_length_for_level =
$self->maximum_line_length_for_level($level); $self->maximum_line_length_for_level($level);
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# It simplifies things to create a zero length side comment # It simplifies things to create a zero length side comment
# if none exists. # if none exists.
# -------------------------------------------------------------------- # --------------------------------------------------------------------
$self->make_side_comment( $rtokens, $rfields, $rpatterns, $rfield_lengths ); $self->make_side_comment( $rtokens, $rfields, $rpatterns, $rfield_lengths );
$jmax = @{$rfields} - 1; $jmax = @{$rfields} - 1;
skipping to change at line 732 skipping to change at line 698
$self->push_group_line($new_line); $self->push_group_line($new_line);
# output this group if it ends in a terminal else or ternary line # output this group if it ends in a terminal else or ternary line
if ( defined($j_terminal_match) ) { if ( defined($j_terminal_match) ) {
$self->_flush_group_lines(); $self->_flush_group_lines();
} }
# Force break after jump to lower level # Force break after jump to lower level
if ( $level_jump < 0 ) { if ( $level_jump < 0 ) {
$self->_flush_group_lines(); $self->_flush_group_lines($level_jump);
} }
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Some old debugging stuff # Some old debugging stuff
# -------------------------------------------------------------------- # --------------------------------------------------------------------
DEBUG_VALIGN && do { DEBUG_VALIGN && do {
print STDOUT "exiting valign_input fields:"; print STDOUT "exiting valign_input fields:";
dump_array( @{$rfields} ); dump_array( @{$rfields} );
print STDOUT "exiting valign_input tokens:"; print STDOUT "exiting valign_input tokens:";
dump_array( @{$rtokens} ); dump_array( @{$rtokens} );
skipping to change at line 1399 skipping to change at line 1365
} }
###################################################### ######################################################
# CODE SECTION 5: Code to process groups of code lines # CODE SECTION 5: Code to process groups of code lines
###################################################### ######################################################
sub _flush_group_lines { sub _flush_group_lines {
# This is the vertical aligner internal flush, which leaves the cache # This is the vertical aligner internal flush, which leaves the cache
# intact # intact
my ($self) = @_; my ( $self, $level_jump ) = @_;
# $level_jump = $next_level-$group_level, if known
# = undef if not known
my $rgroup_lines = $self->[_rgroup_lines_]; my $rgroup_lines = $self->[_rgroup_lines_];
return unless ( @{$rgroup_lines} ); return unless ( @{$rgroup_lines} );
my $group_type = $self->[_group_type_]; my $group_type = $self->[_group_type_];
my $group_level = $self->[_group_level_]; my $group_level = $self->[_group_level_];
# Debug # Debug
0 && do { 0 && do {
my ( $a, $b, $c ) = caller(); my ( $a, $b, $c ) = caller();
my $nlines = @{$rgroup_lines}; my $nlines = @{$rgroup_lines};
skipping to change at line 1448 skipping to change at line 1417
sweep_left_to_right( $rgroup_lines, $rgroups, $group_level ) sweep_left_to_right( $rgroup_lines, $rgroups, $group_level )
if ( @{$rgroups} > 1 ); if ( @{$rgroups} > 1 );
# STEP 4: Move side comments to a common column if possible. # STEP 4: Move side comments to a common column if possible.
if ($saw_side_comment) { if ($saw_side_comment) {
$self->align_side_comments( $rgroup_lines, $rgroups ); $self->align_side_comments( $rgroup_lines, $rgroups );
} }
# STEP 5: For the -lp option, increase the indentation of lists # STEP 5: For the -lp option, increase the indentation of lists
# to the desired amount, but do not exceed the line length limit. # to the desired amount, but do not exceed the line length limit.
# We are allowed to shift a group of lines to the right if:
# (1) its level is greater than the level of the previous group, and
# (2) its level is greater than the level of the next line to be written.
my $extra_indent_ok;
if ( $group_level > $self->[_last_level_written_] ) {
# Use the level jump to next line to come, if given
if ( defined($level_jump) ) {
$extra_indent_ok = $level_jump < 0;
}
# Otherwise, assume the next line has the level of the end of last line.
# This fixes case c008.
else {
my $level_end = $rgroup_lines->[-1]->get_level_end();
$extra_indent_ok = $group_level > $level_end;
}
}
my $extra_leading_spaces = my $extra_leading_spaces =
$self->[_extra_indent_ok_] $extra_indent_ok
? get_extra_leading_spaces( $rgroup_lines, $rgroups ) ? get_extra_leading_spaces( $rgroup_lines, $rgroups )
: 0; : 0;
# STEP 6: Output the lines. # STEP 6: Output the lines.
# All lines in this batch have the same basic leading spacing: # All lines in this batch have the same basic leading spacing:
my $group_leader_length = $rgroup_lines->[0]->get_leading_space_count(); my $group_leader_length = $rgroup_lines->[0]->get_leading_space_count();
foreach my $line ( @{$rgroup_lines} ) { foreach my $line ( @{$rgroup_lines} ) {
$self->valign_output_step_A( $self->valign_output_step_A(
{ {
skipping to change at line 5101 skipping to change at line 5091
$cached_line_flag = $tightness_flag; $cached_line_flag = $tightness_flag;
$cached_seqno = $seqno; $cached_seqno = $seqno;
$cached_line_valid = $valid; $cached_line_valid = $valid;
$cached_line_leading_space_count = $leading_space_count; $cached_line_leading_space_count = $leading_space_count;
$cached_seqno_string = $seqno_string; $cached_seqno_string = $seqno_string;
$cached_line_Kend = $Kend; $cached_line_Kend = $Kend;
} }
$self->[_last_level_written_] = $level; $self->[_last_level_written_] = $level;
$self->[_last_side_comment_length_] = $side_comment_length; $self->[_last_side_comment_length_] = $side_comment_length;
$self->[_extra_indent_ok_] = 0;
return; return;
} }
} }
############################### ###############################
# CODE SECTION 8: Output Step C # CODE SECTION 8: Output Step C
############################### ###############################
{ ## closure for sub valign_output_step_C { ## closure for sub valign_output_step_C
 End of changes. 11 change blocks. 
41 lines changed or deleted 29 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)