%is_equal_or_fat_comma,
%is_block_with_ci,
%is_comma_or_fat_comma,
+ %is_opening_sequence_token,
+ %is_closing_sequence_token,
+ %is_container_label_type,
# Initialized in check_options. These are constants and could
# just as well be initialized in a BEGIN block.
@q = qw< } ) ] >;
@is_closing_token{@q} = (1) x scalar(@q);
+ @q = qw< { ( [ ? >;
+ @is_opening_sequence_token{@q} = (1) x scalar(@q);
+
+ @q = qw< } ) ] : >;
+ @is_closing_sequence_token{@q} = (1) x scalar(@q);
+
+ # a hash needed by sub scan_list for labeling containers
+ @q = qw( k => && || ? : . );
+ @is_container_label_type{@q} = (1) x scalar(@q);
+
# Braces -bbht etc must follow these. Note: experimentation with
# including a simple comma shows that it adds little and can lead
# to poor formatting in complex lists.
my @forced_breakpoint_undo_stack;
my $index_max_forced_break;
+ # Break before or after certain tokens based on user settings
+ my %break_before_or_after_token;
+
+ BEGIN {
+ my @q = qw( = . : ? and or xor && || );
+ push @q, ',';
+ @break_before_or_after_token{@q} = (1) x scalar(@q);
+ }
+
sub initialize_forced_breakpoint_vars {
$forced_breakpoint_count = 0;
$index_max_forced_break = UNDEFINED_INDEX;
# no breaks between welded tokens
return if ( $self->weld_len_right_to_go($i) );
- # when called with certain tokens, use bond strengths to decide
- # if we break before or after it
my $token = $tokens_to_go[$i];
- if ( $token =~ /^([\=\.\,\:\?]|and|or|xor|&&|\|\|)$/ ) {
+ # For certain tokens, use user settings to decide if we break before or
+ # after it
+ # qw( = . : ? and or xor && || )
+ if ( $break_before_or_after_token{$token} ) {
if ( $want_break_before{$token} && $i >= 0 ) { $i-- }
}
};
######################################################################
- # NOTE: if we call set_closing_breakpoint below it will then call this
- # routing back. So there is the possibility of an infinite loop if a
- # programming error is made. As a precaution, I have added a check on
- # the forced_breakpoint flag, so that we won't keep trying to set it.
- # That will give additional protection against a loop.
+ # NOTE: if we call set_closing_breakpoint below it will then call
+ # this routing back. So there is the possibility of an infinite
+ # loop if a programming error is made. As a precaution, I have
+ # added a check on the forced_breakpoint flag, so that we won't
+ # keep trying to set it. That will give additional protection
+ # against a loop.
######################################################################
+
if ( $i_nonblank >= 0
&& $nobreak_to_go[$i_nonblank] == 0
&& !$forced_breakpoint_to_go[$i_nonblank] )
= $i_nonblank;
# if we break at an opening container..break at the closing
- if ( $tokens_to_go[$i_nonblank] =~ /^[\{\[\(\?]$/ ) {
+ if ( $is_opening_sequence_token{ $tokens_to_go[$i_nonblank] } )
+ {
$self->set_closing_breakpoint($i_nonblank);
}
}
sub set_closing_breakpoint {
# set a breakpoint at a matching closing token
- # at present, this is only used to break at a ':' which matches a '?'
my ( $self, $i_break ) = @_;
if ( $mate_index_to_go[$i_break] >= 0 ) {
my @unmatched_opening_indexes_in_this_batch;
my @unmatched_closing_indexes_in_this_batch;
my %comma_arrow_count;
- my %is_opening_tok = (
- '(' => 1,
- '[' => 1,
- '{' => 1,
- '?' => 1
- );
- my %is_closing_tok = (
- ')' => 1,
- ']' => 1,
- '}' => 1,
- ':' => 1
- );
sub initialize_saved_opening_indentation {
%saved_opening_indentation = ();
foreach my $i ( 0 .. $max_index_to_go ) {
if ( $type_sequence_to_go[$i] ) {
my $token = $tokens_to_go[$i];
- if ( $is_opening_tok{$token} ) {
+ if ( $is_opening_sequence_token{$token} ) {
push @unmatched_opening_indexes_in_this_batch, $i;
}
- elsif ( $is_closing_tok{$token} ) {
+ elsif ( $is_closing_sequence_token{$token} ) {
my $i_mate = pop @unmatched_opening_indexes_in_this_batch;
if ( defined($i_mate) && $i_mate >= 0 ) {
if ($type_sequence) {
# handle any postponed closing breakpoints
- if ( $token =~ /^[\)\]\}\:]$/ ) {
+ if ( $is_closing_sequence_token{$token} ) {
if ( $type eq ':' ) {
$last_colon_sequence_number = $type_sequence;
$i_equals[$depth] = -1;
$want_comma_break[$depth] = 0;
$container_type[$depth] =
- ( $last_nonblank_type =~ /^(k|=>|&&|\|\||\?|\:|\.)$/ )
+
+ # k => && || ? : .
+ $is_container_label_type{$last_nonblank_type}
? $last_nonblank_token
: "";
$has_old_logical_breakpoints[$depth] = 0;
# 5 - stable: even for one line blocks if vt=0
if ( !$is_long_term
&& $saw_opening_structure
- && $tokens_to_go[$i_opening] =~ /^[\(\{\[]$/
+ && $is_opening_token{ $tokens_to_go[$i_opening] }
&& $index_before_arrow[ $depth + 1 ] > 0
&& !$opening_vertical_tightness{ $tokens_to_go[$i_opening] }
)