-
#####################################################################
#
# The Perl::Tidy::Formatter package adds indentation, whitespace, and
%is_assignment,
%is_keyword_returning_list,
%is_if_unless_and_or_last_next_redo_return,
+ %is_if_elsif_else_unless_while_until_for_foreach,
%is_last_next_redo_return,
%is_sort_map_grep,
%is_sort_map_grep_eval,
@q = qw(is if unless and or err last next redo return);
@is_if_unless_and_or_last_next_redo_return{@q} = (1) x scalar(@q);
+ # These block types may have text between the keyword and opening
+ # curly. Note: 'else' does not, but must be included to allow trailing
+ # if/elsif text to be appended.
+ # patch for SWITCH/CASE: added 'case' and 'when'
+ @q =
+ qw(if elsif else unless while until for foreach case when catch);
+ @is_if_elsif_else_unless_while_until_for_foreach{@q} =
+ (1) x scalar(@q);
+
@q = qw(last next redo return);
@is_last_next_redo_return{@q} = (1) x scalar(@q);
# Only make a hash entry for the next parameters if values are defined.
# That allows a quick check to be made later.
+ %break_before_container_types = ();
for ( $rOpts->{'break-before-hash-brace'} ) {
$break_before_container_types{'{'} = $_ if $_ && $_ > 0;
}
# Variables related to forming closing side comments.
- my %is_if_elsif_else_unless_while_until_for_foreach;
-
my %block_leading_text;
my %block_opening_line_number;
my $csc_new_statement_ok;
my $leading_block_text_line_length;
my $leading_block_text_line_number;
- BEGIN {
-
- # These block types may have text between the keyword and opening
- # curly. Note: 'else' does not, but must be included to allow trailing
- # if/elsif text to be appended.
- # patch for SWITCH/CASE: added 'case' and 'when'
- my @q =
- qw(if elsif else unless while until for foreach case when catch);
- @is_if_elsif_else_unless_while_until_for_foreach{@q} =
- (1) x scalar(@q);
- }
-
sub initialize_csc_vars {
%block_leading_text = ();
%block_opening_line_number = ();
}
{ ## begin closure set_adjusted_indentation
- my %is_if_elsif_else_unless_while_until_for_foreach;
my ( $last_indentation_written, $last_unadjusted_indentation,
$last_leading_token );
- BEGIN {
-
- # These block types may have text between the keyword and opening
- # curly. Note: 'else' does not, but must be included to allow trailing
- # if/elsif text to be appended.
- # patch for SWITCH/CASE: added 'case' and 'when'
- my @q = qw(if elsif else unless while until for foreach case when);
- @is_if_elsif_else_unless_while_until_for_foreach{@q} =
- (1) x scalar(@q);
- }
-
sub initialize_adjusted_indentation {
$last_indentation_written = 0;
$last_unadjusted_indentation = 0;
return unless %break_before_container_types;
- my $nmax = @{$ri_right} - 1;
+ my $nmax = @{$ri_right} - 1;
+ return unless ( $nmax >= 0 );
+
my $rLL = $self->[_rLL_];
my $rtype_count_by_seqno = $self->[_rtype_count_by_seqno_];
my $ris_broken_container = $self->[_ris_broken_container_];
$iend = $ir + ( $Kend - $Kr );
}
- # This is only for line-ending tokens
+ # This is only for line-ending tokens with a preceding blank
+ # on the same line.
next unless ( $Kl < $Kend - 1 );
- # Only for selected types of tokens
+ # And only for some kind of container token
+ my $seqno = $rLL->[$Kend]->[_TYPE_SEQUENCE_];
+ next unless ( defined($seqno) );
+
+ # And only for selected types of container tokens
my $token_end = $rLL->[$Kend]->[_TOKEN_];
my $break_option = $break_before_container_types{$token_end};
next unless ($break_option);
- # This is not for block braces
+ # This is not for code block braces
my $block_type = $rLL->[$Kend]->[_BLOCK_TYPE_];
next if ($block_type);
# This is only for list containers. This is a little fuzzy,
# but we will require at least 2 commas or 1 fat comma in the
# immediate lower level
- my $seqno = $rLL->[$Kend]->[_TYPE_SEQUENCE_];
my $fat_comma_count = $rtype_count_by_seqno->{$seqno}->{'=>'};
my $comma_count = $rtype_count_by_seqno->{$seqno}->{','};
next unless ( $fat_comma_count || $comma_count && $comma_count > 1 );
if ( $token_end eq '(' ) {
my $iend_m2 = $iend - 2;
if ( $iend_m2 >= $il ) {
- if ( $types_to_go[$iend_m2] eq 'k'
- && $is_if_unless{ $tokens_to_go[$iend_m2] } )
+ if ( $types_to_go[$iend_m2] eq 'k'
+ && $is_if_elsif_else_unless_while_until_for_foreach{
+ $tokens_to_go[$iend_m2] } )
{
next;
}