sub prepare_for_next_batch {
- $max_index_to_go = UNDEFINED_INDEX;
- $summed_lengths_to_go[0] = 0;
-
initialize_forced_breakpoint_vars();
initialize_gnu_batch_vars();
initialize_batch_variables();
return;
}
-sub leading_spaces_to_go {
-
- # return the number of indentation spaces for a token in the output stream;
- # these were previously stored by 'set_leading_whitespace'.
-
- my $ii = shift;
- if ( $ii < 0 ) { $ii = 0 }
- return get_spaces( $leading_spaces_to_go[$ii] );
-
-}
-
sub get_spaces {
# return the number of leading spaces associated with an indentation
sub get_available_spaces_to_go {
- my $ii = shift;
+ my ( $self, $ii ) = @_;
my $item = $leading_spaces_to_go[$ii];
# return the number of available leading spaces associated with an
)
)
{
- check_for_long_gnu_style_lines();
+ check_for_long_gnu_style_lines($max_index_to_go);
$line_start_index_to_go = $max_index_to_go;
# back up 1 token if we want to break before that type
# look at the current estimated maximum line length, and
# remove some whitespace if it exceeds the desired maximum
+ my ($mx_index_to_go) = @_;
# this is only for the '-lp' style
return unless ($rOpts_line_up_parentheses);
# keep 2 extra free because they are needed in some cases
# (result of trial-and-error testing)
my $spaces_needed =
- $gnu_position_predictor - maximum_line_length($max_index_to_go) + 2;
+ $gnu_position_predictor - maximum_line_length($mx_index_to_go) + 2;
return if ( $spaces_needed <= 0 );
# NOTE: to be called from scan_list only for a sequence of tokens
# contained between opening and closing parens/braces/brackets
- my ( $i, $spaces_wanted ) = @_;
+ my ( $self, $i, $spaces_wanted ) = @_;
my $deleted_spaces = 0;
my $item = $leading_spaces_to_go[$i];
return $deleted_spaces;
}
-sub token_sequence_length {
-
- # return length of tokens ($ibeg .. $iend) including $ibeg & $iend
- # returns 0 if $ibeg > $iend (shouldn't happen)
- my ( $ibeg, $iend ) = @_;
- return 0 if ( $iend < 0 || $ibeg > $iend );
- return $summed_lengths_to_go[ $iend + 1 ] if ( $ibeg < 0 );
- return $summed_lengths_to_go[ $iend + 1 ] - $summed_lengths_to_go[$ibeg];
-}
-
sub total_line_length {
# return length of a line of tokens ($ibeg .. $iend)
return $maximum_line_length;
}
-sub maximum_line_length {
-
- # return maximum line length for line starting with the token at given index
- my $ii = shift;
- return maximum_line_length_for_level( $levels_to_go[$ii] );
-}
-
sub excess_line_length {
# return number of characters by which a line of tokens ($ibeg..$iend)
$is_static_block_comment,
);
+ # Called once at the start of a new file
sub initialize_process_line_of_CODE {
$last_nonblank_token = ';';
$last_nonblank_type = ';';
# Called before the start of each new batch
sub initialize_batch_variables {
- $rbrace_follower = undef;
- $comma_count_in_batch = 0;
- $ending_in_quote = 0;
+
+ # These two global variables are future closure variables
+ $max_index_to_go = UNDEFINED_INDEX;
+ $summed_lengths_to_go[0] = 0;
+
+ $rbrace_follower = undef;
+ $comma_count_in_batch = 0;
+ $ending_in_quote = 0;
destroy_one_line_block();
return;
}
+ sub maximum_line_length {
+
+ # return maximum line length for line starting with the token at given
+ # batch index
+ my ($ii) = @_;
+ if ( $ii < 0 ) { $ii = 0 }
+ return maximum_line_length_for_level( $levels_to_go[$ii] );
+ }
+
+ sub leading_spaces_to_go {
+
+ # return the number of indentation spaces for a token in the output
+ # stream; these were previously stored by 'set_leading_whitespace'.
+
+ my ($ii) = @_;
+ if ( $ii < 0 ) { $ii = 0 }
+ return get_spaces( $leading_spaces_to_go[$ii] );
+ }
+
+ sub token_sequence_length {
+
+ # return length of tokens ($ibeg .. $iend) including $ibeg & $iend
+ # returns 0 if $ibeg > $iend (shouldn't happen)
+ my ( $ibeg, $iend ) = @_;
+ return 0 if ( $iend < 0 || $ibeg > $iend );
+ return $summed_lengths_to_go[ $iend + 1 ] if ( $ibeg < 0 );
+ return $summed_lengths_to_go[ $iend + 1 ] -
+ $summed_lengths_to_go[$ibeg];
+ }
+
sub create_one_line_block {
( $index_start_one_line_block, $semicolons_before_block_self_destruct )
= @_;
# we have to actually make it by removing tentative
# breaks that were set within it
$self->undo_forced_breakpoint_stack(0);
- set_nobreaks( $index_start_one_line_block,
+ $self->set_nobreaks( $index_start_one_line_block,
$max_index_to_go - 1 );
# then re-initialize for the next one-line block
$self->write_diagnostics("$output_str\n");
};
- my $comma_arrow_count_contained = match_opening_and_closing_tokens();
+ my $comma_arrow_count_contained =
+ $self->match_opening_and_closing_tokens();
# tell the -lp option we are outputting a batch so it can close
# any unfinished items in its stack
};
# add a couple of extra terminal blank tokens
- pad_array_to_go();
+ $self->pad_array_to_go();
# set all forced breakpoints for good list formatting
my $is_long_line =
$self->break_all_chain_tokens( $ri_first, $ri_last );
- break_equals( $ri_first, $ri_last );
+ $self->break_equals( $ri_first, $ri_last );
# now we do a correction step to clean this up a bit
# (The only time we would not do this is for debugging)
# do corrector step if -lp option is used
my $do_not_pad = 0;
if ($rOpts_line_up_parentheses) {
- $do_not_pad = correct_lp_indentation( $ri_first, $ri_last );
+ $do_not_pad =
+ $self->correct_lp_indentation( $ri_first, $ri_last );
}
$self->unmask_phantom_semicolons( $ri_first, $ri_last );
if ( $rOpts_one_line_block_semicolons == 0 ) {
# . " You said $1, but did you know that it's square was "
# . $1 * $1 . " ?");
- my ( $line_open, $i_start, $closing_index, $ri_first, $ri_last ) = @_;
+ my ( $self, $line_open, $i_start, $closing_index, $ri_first, $ri_last ) =
+ @_;
my $max_line = @{$ri_first} - 1;
# must be multiple lines
# predictor is usually good, but sometimes stumbles. The corrector
# tries to patch things up once the actual opening paren locations
# are known.
- my ( $ri_first, $ri_last ) = @_;
+ my ( $self, $ri_first, $ri_last ) = @_;
my $do_not_pad = 0;
# Note on flag '$do_not_pad':
if ( $closing_index > $iend ) {
my $ibeg_next = $ri_first->[ $line + 1 ];
if ( $ci_levels_to_go[$ibeg_next] > 0 ) {
- undo_lp_ci( $line, $i, $closing_index, $ri_first,
- $ri_last );
+ $self->undo_lp_ci( $line, $i, $closing_index,
+ $ri_first, $ri_last );
}
}
}
}
sub accumulate_block_text {
- my $i = shift;
+ my ( $self, $i ) = @_;
# accumulate leading text for -csc, ignoring any side comments
if ( $accumulating_text_for_block
reset_block_text_accumulator();
}
else {
- accumulate_block_text($i);
+ $self->accumulate_block_text($i);
}
}
}
# especially for cuddled-else formatting.
if ( $terminal_block_type =~ /^els/ && $rblock_leading_if_elsif_text ) {
$block_leading_text =
- make_else_csc_text( $i_terminal, $terminal_block_type,
+ $self->make_else_csc_text( $i_terminal, $terminal_block_type,
$block_leading_text, $rblock_leading_if_elsif_text );
}
# $rif_elsif_text = a reference to a list of all previous closing
# side comments created for this if block
#
- my ( $i_terminal, $block_type, $block_leading_text, $rif_elsif_text ) =
- @_;
+ my ( $self, $i_terminal, $block_type, $block_leading_text,
+ $rif_elsif_text )
+ = @_;
my $csc_text = $block_leading_text;
my $rOpts_closing_side_comment_else_flag =
# This has to be done after all tokens are stored because unstoring
# of tokens would otherwise cause trouble.
+ my ($self) = @_;
+
@unmatched_opening_indexes_in_this_batch = ();
@unmatched_closing_indexes_in_this_batch = ();
%comma_arrow_count = ();
# to simplify coding in scan_list and set_bond_strengths, it helps
# to create some extra blank tokens at the end of the arrays
+ my ($self) = @_;
$tokens_to_go[ $max_index_to_go + 1 ] = '';
$tokens_to_go[ $max_index_to_go + 2 ] = '';
$types_to_go[ $max_index_to_go + 1 ] = 'b';
# shouldn't happen:
unless ( get_saw_brace_error() ) {
warning(
-"Program bug in scan_list: hit nesting error which should have been caught\n"
+"Program bug in pad_array_to_go: hit nesting error which should have been caught\n"
);
report_definite_bug();
}
)
)
{
- my $available_spaces = get_available_spaces_to_go($i_first_comma);
+ my $available_spaces =
+ $self->get_available_spaces_to_go($i_first_comma);
if ( $available_spaces > 0 ) {
my $spaces_wanted = $max_width - $columns; # for 1 field
if ( $spaces_wanted > 0 ) {
my $deleted_spaces =
- reduce_lp_indentation( $i_first_comma, $spaces_wanted );
+ $self->reduce_lp_indentation( $i_first_comma,
+ $spaces_wanted );
# redo the math
if ( $deleted_spaces > 0 ) {
}
sub set_nobreaks {
- my ( $i, $j ) = @_;
+ my ( $self, $i, $j ) = @_;
if ( $i >= 0 && $i <= $j && $j <= $max_index_to_go ) {
FORMATTER_DEBUG_FLAG_NOBREAK && do {
# We are given indexes to the current lines:
# $ri_beg = ref to array of BEGinning indexes of each line
# $ri_end = ref to array of ENDing indexes of each line
- my ( $ri_beg, $ri_end, $msg ) = @_;
+ my ( $self, $ri_beg, $ri_end, $msg ) = @_;
print STDERR "----Dumping breakpoints from: $msg----\n";
for my $n ( 0 .. @{$ri_end} - 1 ) {
my $ibeg = $ri_beg->[$n];
# insert any new break points
if (@insert_list) {
- insert_additional_breaks( \@insert_list, $ri_left, $ri_right );
+ $self->insert_additional_breaks( \@insert_list, $ri_left, $ri_right );
}
return;
}
# The logic here follows the logic in set_logical_padding, which
# will add the padding in the second line to improve alignment.
#
- my ( $ri_left, $ri_right ) = @_;
+ my ( $self, $ri_left, $ri_right ) = @_;
my $nmax = @{$ri_right} - 1;
return unless ( $nmax >= 2 );
# ok, insert any new break point
if (@insert_list) {
- insert_additional_breaks( \@insert_list, $ri_left, $ri_right );
+ $self->insert_additional_breaks( \@insert_list, $ri_left, $ri_right );
}
return;
}
# insert any new break points
if (@insert_list) {
- insert_additional_breaks( \@insert_list, $ri_left, $ri_right );
+ $self->insert_additional_breaks( \@insert_list, $ri_left,
+ $ri_right );
}
}
}
# the same breakpoints will occur. scbreak.t
last
if (
- $i_test == $imax # we are at the end
+ $i_test == $imax # we are at the end
&& !get_forced_breakpoint_count()
- && $saw_good_break # old line had good break
- && $type =~ /^[#;\{]$/ # and this line ends in
- # ';' or side comment
- && $i_last_break < 0 # and we haven't made a break
- && $i_lowest >= 0 # and we saw a possible break
- && $i_lowest < $imax - 1 # (but not just before this ;)
+ && $saw_good_break # old line had good break
+ && $type =~ /^[#;\{]$/ # and this line ends in
+ # ';' or side comment
+ && $i_last_break < 0 # and we haven't made a break
+ && $i_lowest >= 0 # and we saw a possible break
+ && $i_lowest < $imax - 1 # (but not just before this ;)
&& $strength - $lowest_strength < 0.5 * WEAK # and it's good
);
push @insert_list, $i_question;
}
}
- insert_additional_breaks( \@insert_list, \@i_first, \@i_last );
+ $self->insert_additional_breaks( \@insert_list, \@i_first,
+ \@i_last );
}
}
}
# this routine will add line breaks at requested locations after
# sub set_continuation_breaks has made preliminary breaks.
- my ( $ri_break_list, $ri_first, $ri_last ) = @_;
+ my ( $self, $ri_break_list, $ri_first, $ri_last ) = @_;
my $i_f;
my $i_l;
my $line_number = 0;