From: Steve Hancock Date: Fri, 18 Sep 2020 01:07:10 +0000 (-0700) Subject: added flags -ihb -isb -ip X-Git-Tag: 20201001~31 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=b13ff01f133cdcdf158520679231b5f6b8f7842c;p=perltidy.git added flags -ihb -isb -ip --- diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index f9160720..24896094 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -2286,6 +2286,9 @@ sub generate_options { $add_option->( 'break-before-hash-brace', 'bbhb', '=i' ); $add_option->( 'break-before-square-bracket', 'bbsb', '=i' ); $add_option->( 'break-before-paren', 'bbp', '=i' ); + $add_option->( 'indent-hash-brace', 'ihb', '=i' ); + $add_option->( 'indent-square-bracket', 'isb', '=i' ); + $add_option->( 'indent-paren', 'ip', '=i' ); ######################################## $category = 6; # Controlling list formatting @@ -2475,6 +2478,9 @@ sub generate_options { break-before-hash-brace=0 break-before-square-bracket=0 break-before-paren=0 + indent-hash-brace=0 + indent-square-bracket=0 + indent-paren=0 comma-arrow-breakpoints=5 nocheck-syntax character-encoding=guess diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index fbf7eda6..12f3ad14 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -106,6 +106,7 @@ my ( %want_break_before, %break_before_container_types, + %container_indentation_options, %space_after_keyword, @@ -4577,24 +4578,47 @@ sub adjust_indentation_levels { my ($self) = @_; - # Two options, -nib and -wc, are implemented by defining adjusted levels in - # $self->[_radjusted_levels_]. They will create this array if - # they are active, and otherwise it will be an empty array. + # These routines adjust levels either by changing _CI_LEVEL_ directly or + # by setting modified levels in the array $self->[_radjusted_levels_]. + # They will create this array if they are active, and otherwise it will be + # an empty array for later efficiency. # Set adjusted levels for any non-indenting braces. # If this option is used it will create the _radjusted_levels_ array. + # Important: This must be the first routine called which touches + # _radjusted_levels_ $self->non_indenting_braces(); - # Set adjusted levels for the whitespace cycle option. If this option is - # used it will create or modify the _radjusted_levels_ array. + # Adjust indentation for list containers + $self->adjust_container_indentation(); + + # Set adjusted levels for the whitespace cycle option. $self->whitespace_cycle_adjustment(); + # Adjust continuation indentation if -bli is set + $self->bli_adjustment(); + # Now clip any adjusted levels to be non-negative $self->clip_adjusted_levels(); return; } +sub initialize_adjusted_levels { + my ($self) = @_; + + # Initialize _radjusted_levels if it has not yet been initialized. + my $radjusted_levels = $self->[_radjusted_levels_]; + my $rLL = $self->[_rLL_]; + my $Kmax = @{$rLL} - 1; + if ( !defined($radjusted_levels) || ( @{$radjusted_levels} != @{$rLL} ) ) { + foreach my $KK ( 0 .. $Kmax ) { + $radjusted_levels->[$KK] = $rLL->[$KK]->[_LEVEL_]; + } + } + return; +} + sub clip_adjusted_levels { # Replace any negative adjusted levels with zero. @@ -4608,7 +4632,8 @@ sub clip_adjusted_levels { sub non_indenting_braces { - # remove indentation within marked braces if requested + # Remove indentation within marked braces if requested + # NOTE: This must be the first routine to reference $radjusted_levels; my ($self) = @_; return unless ( $rOpts->{'non-indenting-braces'} ); @@ -4685,14 +4710,7 @@ sub whitespace_cycle_adjustment { my $Kmax = @{$rLL} - 1; - # We have to start with any existing adjustments - my $adjusted_levels_defined = - defined($radjusted_levels) && @{$radjusted_levels} == @{$rLL}; - if ( !$adjusted_levels_defined ) { - foreach my $KK ( 0 .. $Kmax ) { - $radjusted_levels->[$KK] = $rLL->[$KK]->[_LEVEL_]; - } - } + $self->initialize_adjusted_levels(); my $whitespace_last_level = -1; my @whitespace_level_stack = (); @@ -4747,6 +4765,80 @@ sub whitespace_cycle_adjustment { return; } +sub adjust_container_indentation { + + # adjust continuation indentation for certain tokens if requested + + # -ihb=n , --indent-for-hash-brace=n + # -isb=n , --indent-for-square-bracket=n + # -ip=n , --indent-for-paren=n + + # n=0 default indentation (usually one ci) + # n=1 outdent one ci + # n=2 indent one level (minus one ci) + # n=3 indent one extra ci [This may be dropped] + + my ($self) = @_; + + return unless %container_indentation_options; + + my $rLL = $self->[_rLL_]; + return unless ( defined($rLL) && @{$rLL} ); + + # Option 2 needs the following array: + my $radjusted_levels = $self->[_radjusted_levels_]; + + # We will only initialize it if option 2 has been selected + foreach my $key (%container_indentation_options) { + my $val = $container_indentation_options{$key}; + if ( defined($val) && $val == 2 ) { + $self->initialize_adjusted_levels(); + last; + } + } + + # Loop over all opening container tokens + my $K_opening_container = $self->[_K_opening_container_]; + foreach my $seqno ( keys %{$K_opening_container} ) { + my $KK = $K_opening_container->{$seqno}; + my $rtoken_vars = $rLL->[$KK]; + my $block_type = $rtoken_vars->[_BLOCK_TYPE_]; + + # this routine is not for code block braces + next if ($block_type); + + my $token = $rtoken_vars->[_TOKEN_]; + my $flag = $container_indentation_options{$token}; + next unless ($flag); + + # This is only for list containers + next unless $self->is_list($seqno); + + # NOTE: We are adjusting indentation of the opening container. The + # closing container will normally follow the indentation of the opening + # container automatically, so this is not currently done. + my $ci = $rLL->[$KK]->[_CI_LEVEL_]; + + # option 1: outdent + if ( $flag == 1 ) { + $ci -= 1; + } + + # option 2: indent one level + elsif ( $flag == 2 ) { + $ci -= 1; + $radjusted_levels->[$KK] += 1; + } + + # option 3: for testing only, probably will be deleted + elsif ( $flag == 3 ) { + $ci += 1; + } + $rLL->[$KK]->[_CI_LEVEL_] = $ci if ( $ci >= 0 ); + } + return; +} + sub bli_adjustment { # if -bli is set, adds one continuation indentation for certain braces @@ -5000,9 +5092,6 @@ sub finish_formatting { $self->adjust_indentation_levels(); - # Adjust continuation indentation if -bli is set - $self->bli_adjustment(); - # Finishes formatting and write the result to the line sink. # Eventually this call should just change the 'rlines' data according to the # new line breaks and then return so that we can do an internal iteration @@ -5874,7 +5963,6 @@ sub wrapup { "First indentation disagreement seen at input line $first_tabbing_disagreement\n" ); } - if ($in_tabbing_disagreement) { write_logfile_entry( "Ending with indentation disagreement which started at input line $in_tabbing_disagreement\n" @@ -6172,6 +6260,27 @@ EOM $break_before_container_types{'('} = $_ if $_ && $_ > 0; } + %container_indentation_options = (); + for ( $rOpts->{'indent-hash-brace'} ) { + my $tok = '{'; + if ( defined($_) && $_>0 && $break_before_container_types{$tok} ) { + $container_indentation_options{$tok} = $_; + } + } + for ( $rOpts->{'indent-square-bracket'} ) { + my $tok = '['; + if ( defined($_) && $_>0 && $break_before_container_types{$tok} ) { + $container_indentation_options{$tok} = $_; + } + } + for ( $rOpts->{'indent-paren'} ) { + my $tok = '('; + if ( defined($_) && $_>0 && $break_before_container_types{$tok} ) { + $container_indentation_options{$tok} = $_; + } + } + + # Define here tokens which may follow the closing brace of a do statement # on the same line, as in: # } while ( $something); @@ -17820,6 +17929,22 @@ sub break_equals { return; } +sub is_list { + + # Try to decide if the immediate contents of a container is a list. + + my ( $self, $seqno ) = @_; + my $rLL = $self->[_rLL_]; + my $rtype_count_by_seqno = $self->[_rtype_count_by_seqno_]; + + # We will require at least 2 commas or 1 fat comma in the + # immediate lower level. + my $fat_comma_count = $rtype_count_by_seqno->{$seqno}->{'=>'}; + my $comma_count = $rtype_count_by_seqno->{$seqno}->{','}; + my $is_list = ( $fat_comma_count || $comma_count && $comma_count > 1 ); + return $is_list; +} + sub insert_breaks_before_list_opening_containers { my ( $self, $ri_left, $ri_right ) = @_; @@ -17830,7 +17955,6 @@ sub insert_breaks_before_list_opening_containers { 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_]; my $rhas_broken_container = $self->[_rhas_broken_container_]; @@ -17874,12 +17998,7 @@ sub insert_breaks_before_list_opening_containers { # Require a space before the line ending token next unless ( $rLL->[ $Kend - 1 ]->[_TYPE_] eq 'b' ); - # 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 $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 ); + next unless $self->is_list($seqno); # Do not break a weld next if ( $self->weld_len_left( $seqno, $token_end ) ); @@ -17929,6 +18048,9 @@ sub insert_breaks_before_list_opening_containers { next if ( $ibreak < $il ); next if ( $nobreak_to_go[$ibreak] ); push @insert_list, $ibreak; + + # FIXME: at this point we could inform any child containers that they + # are part of a complex container. } # insert any new break points diff --git a/t/snippets/bbhb4.par b/t/snippets/bbhb4.par new file mode 100644 index 00000000..6fd2493d --- /dev/null +++ b/t/snippets/bbhb4.par @@ -0,0 +1 @@ +-bbhb=3 -bbp=3 -ihb=2 -ip=2 diff --git a/t/snippets/bbhb5.par b/t/snippets/bbhb5.par new file mode 100644 index 00000000..88198261 --- /dev/null +++ b/t/snippets/bbhb5.par @@ -0,0 +1 @@ +-bbhb=3 -bbp=3 -ihb=1 -ip=1 diff --git a/t/snippets/expect/bbhb.bbhb4 b/t/snippets/expect/bbhb.bbhb4 new file mode 100644 index 00000000..99e82ab7 --- /dev/null +++ b/t/snippets/expect/bbhb.bbhb4 @@ -0,0 +1,9 @@ +my %temp = + ( + supsup => 123, + nested => + { + asdf => 456, + yarg => 'yarp', + }, + ); diff --git a/t/snippets/expect/bbhb.bbhb5 b/t/snippets/expect/bbhb.bbhb5 new file mode 100644 index 00000000..3d6d21e1 --- /dev/null +++ b/t/snippets/expect/bbhb.bbhb5 @@ -0,0 +1,9 @@ +my %temp = +( + supsup => 123, + nested => + { + asdf => 456, + yarg => 'yarp', + }, +); diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index ee997287..93e1f572 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -271,6 +271,9 @@ ../snippets21.t scbb-csc.scbb-csc ../snippets21.t here_long.def ../snippets22.t here_long.here_long +../snippets22.t bbhb.bbhb2 +../snippets22.t bbhb.bbhb3 +../snippets22.t bbhb.def ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -411,6 +414,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets22.t bbhb.bbhb2 -../snippets22.t bbhb.bbhb3 -../snippets22.t bbhb.def +../snippets22.t bbhb.bbhb4 +../snippets22.t bbhb.bbhb5 diff --git a/t/snippets22.t b/t/snippets22.t index a640288a..4ae6b702 100644 --- a/t/snippets22.t +++ b/t/snippets22.t @@ -5,6 +5,8 @@ #2 bbhb.bbhb2 #3 bbhb.bbhb3 #4 bbhb.def +#5 bbhb.bbhb4 +#6 bbhb.bbhb5 # To locate test #13 you can search for its name or the string '#13' @@ -24,6 +26,8 @@ BEGIN { $rparams = { 'bbhb2' => "-bbhb=2 -bbp=2", 'bbhb3' => "-bbhb=3 -bbp=3", + 'bbhb4' => "-bbhb=3 -bbp=3 -ihb=2 -ip=2", + 'bbhb5' => "-bbhb=3 -bbp=3 -ihb=1 -ip=1", 'def' => "", 'here_long' => "-l=33", }; @@ -116,6 +120,38 @@ my %temp = ( ); #4........... }, + + 'bbhb.bbhb4' => { + source => "bbhb", + params => "bbhb4", + expect => <<'#5...........', +my %temp = + ( + supsup => 123, + nested => + { + asdf => 456, + yarg => 'yarp', + }, + ); +#5........... + }, + + 'bbhb.bbhb5' => { + source => "bbhb", + params => "bbhb5", + expect => <<'#6...........', +my %temp = +( + supsup => 123, + nested => + { + asdf => 456, + yarg => 'yarp', + }, +); +#6........... + }, }; my $ntests = 0 + keys %{$rtests};