From: Steve Hancock Date: Thu, 16 Sep 2021 23:17:27 +0000 (-0700) Subject: added new parameters -vc -vsc -vbc X-Git-Tag: 20210717.03~5 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=59563433583a158234a0a1811a7b6551aae2c1b7;p=perltidy.git added new parameters -vc -vsc -vbc --- diff --git a/bin/perltidy b/bin/perltidy index 49c7411a..b847c252 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -4058,6 +4058,32 @@ For example, The default is to use vertical alignment, but bertical alignment can be completely turned of with the B<-novalign> flag. +A lower level of control of vertical alignment is possible with three parameters +B<-vc>, B<-vsc>, and B<-vbc>. These independently control alignment +of code, side comments and block comments. They are described in the +next section. + +The parameter B<-valign> is in fact an alias for B<-vc -vsc -vbc>, and its +negative B<-novalign> is an alias for B<-nvc -nvsc -nvbc>. + +=item B + +The B<-vc> flag enables alignment of code symbols such as B<=>. The default is B<-vc>. + +=item B + +The B<-vsc> flag enables alignment of side comments and is enabled by default. If side +comment aligment is disabled with B<-nvsc> they will appear at a fixed space from the +preceding code token. The default is B<-vsc> + +=item B + +When B<-vbc> is enabled, block comments can become aligned for example if one +comment of a consecutive sequence of comments becomes outdented due a length in +excess of the maximum line length. If this occurs, the entire group of +comments will remain aligned and be outdented by the same amount. This coordinated +alignment will not occur if B<-nvbc> is set. The default is B<-vbc>. + =back =head2 Other Controls diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index b1fadb17..ffbfc4c4 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1,3 +1,4 @@ +#!/usr/bin/perl # ########################################################### # @@ -2125,7 +2126,6 @@ sub generate_options { # which is mainly for debugging # scl --> short-concatenation-item-length # helps break at '.' # recombine # for debugging line breaks - # valign # for debugging vertical alignment # I --> DIAGNOSTICS # for debugging [**DEACTIVATED**] ###################################################################### @@ -2181,7 +2181,6 @@ sub generate_options { no-profile npro recombine! - valign! notidy ); @@ -2323,6 +2322,9 @@ sub generate_options { $add_option->( 'want-left-space', 'wls', '=s' ); $add_option->( 'want-right-space', 'wrs', '=s' ); $add_option->( 'space-prototype-paren', 'spp', '=i' ); + $add_option->( 'valign-code', 'vc', '!' ); + $add_option->( 'valign-block-comments', 'vbc', '!' ); + $add_option->( 'valign-side-comments', 'vsc', '!' ); ######################################## $category = 4; # Comment controls @@ -2660,7 +2662,9 @@ sub generate_options { noweld-nested-containers recombine nouse-unicode-gcstring - valign + valign-code + valign-block-comments + valign-side-comments short-concatenation-item-length=8 space-for-semicolon space-backslash-quote=1 @@ -2817,6 +2821,9 @@ sub generate_options { 'conv' => [qw(it=4)], 'nconv' => [qw(it=1)], + 'valign' => [qw(vc vsc vbc)], + 'novalign' => [qw(nvc nvsc nvbc)], + # NOTE: This is a possible future shortcut. But it will remain # deactivated until the -lpxl flag is no longer experimental. # 'line-up-function-parentheses' => [ qw(lp), q#lpxl=[ { F(2# ], diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index b95b0a54..133cc507 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -183,6 +183,8 @@ my ( $rOpts_tee_side_comments, $rOpts_variable_maximum_line_length, $rOpts_valign, + $rOpts_valign_code, + $rOpts_valign_side_comments, $rOpts_whitespace_cycle, # Static hashes initialized in a BEGIN block @@ -1670,6 +1672,8 @@ EOM $rOpts_tee_pod = $rOpts->{'tee-pod'}; $rOpts_tee_side_comments = $rOpts->{'tee-side-comments'}; $rOpts_valign = $rOpts->{'valign'}; + $rOpts_valign_code = $rOpts->{'valign-code'}; + $rOpts_valign_side_comments = $rOpts->{'valign-side-comments'}; $rOpts_variable_maximum_line_length = $rOpts->{'variable-maximum-line-length'}; @@ -20127,6 +20131,7 @@ EOM my ( $self, $ri_first, $ri_last ) = @_; my $rspecial_side_comment_type = $self->[_rspecial_side_comment_type_]; + my $rLL = $self->[_rLL_]; my $ralignment_type_to_go; my $alignment_count = 0; @@ -20161,28 +20166,52 @@ EOM my $token = $tokens_to_go[$max_i]; my $KK = $K_to_go[$max_i]; - unless ( + # Do not align various special side comments + my $do_not_align = ( # it is any specially marked side comment ( defined($KK) && $rspecial_side_comment_type->{$KK} ) # or it is a static side comment - || ( $rOpts->{'static-side-comments'} + || ( $rOpts->{'static-side-comments'} && $token =~ /$static_side_comment_pattern/ ) - # or a closing side comment - || ( $types_to_go[$i_terminal] eq '}' + # or a closing side comment + || ( $types_to_go[$i_terminal] eq '}' && $tokens_to_go[$i_terminal] eq '}' && $token =~ /$closing_side_comment_prefix_pattern/ ) - ) + ); + + # - For the particular combination -vc -nvsc, we put all side comments + # at fixed locations. Note that we will lose hanging side comment + # alignments. Otherwise, hsc's can move to strange locations. + # - For -nvc -nvsc we will make all side comments vertical alignments + # because the vertical aligner will check for -nvsc and be able + # to reduce the final padding to the side comments for long lines. + # and keep hanging side comments aligned. + if ( !$do_not_align + && !$rOpts_valign_side_comments + && $rOpts_valign_code ) { + + $do_not_align = 1; + my $ipad = $max_i - 1; + if ( $types_to_go[$ipad] eq 'b' ) { + my $pad_spaces = + $rOpts->{'minimum-space-to-comment'} - + $token_lengths_to_go[$ipad]; + $self->pad_token( $ipad, $pad_spaces ); + } + } + + if ( !$do_not_align ) { $ralignment_type_to_go->[$max_i] = '#'; $alignment_count++; } } - # Nothing more to do if -novalign is set - if ( !$rOpts_valign ) { + # Nothing more to do on this line if -nvc is set + if ( !$rOpts_valign_code ) { return ( $ralignment_type_to_go, $alignment_count ); } @@ -20197,7 +20226,7 @@ EOM my $ibeg = $ri_first->[$line]; my $iend = $ri_last->[$line]; - # back up before did any side comment + # back up before any side comment if ( $iend > $i_terminal ) { $iend = $i_terminal } my $level_beg = $levels_to_go[$ibeg]; diff --git a/lib/Perl/Tidy/VerticalAligner.pm b/lib/Perl/Tidy/VerticalAligner.pm index ade5229f..24ba98ab 100644 --- a/lib/Perl/Tidy/VerticalAligner.pm +++ b/lib/Perl/Tidy/VerticalAligner.pm @@ -100,7 +100,9 @@ BEGIN { _rOpts_minimum_space_to_comment_ => $i++, _rOpts_maximum_line_length_ => $i++, _rOpts_variable_maximum_line_length_ => $i++, - _rOpts_valign_ => $i++, + _rOpts_valign_code_ => $i++, + _rOpts_valign_block_comments_ => $i++, + _rOpts_valign_side_comments_ => $i++, _last_level_written_ => $i++, _last_side_comment_column_ => $i++, @@ -181,7 +183,9 @@ sub new { $self->[_rOpts_maximum_line_length_] = $rOpts->{'maximum-line-length'}; $self->[_rOpts_variable_maximum_line_length_] = $rOpts->{'variable-maximum-line-length'}; - $self->[_rOpts_valign_] = $rOpts->{'valign'}; + $self->[_rOpts_valign_code_] = $rOpts->{'valign-code'}; + $self->[_rOpts_valign_block_comments_] = $rOpts->{'valign-block-comments'}; + $self->[_rOpts_valign_side_comments_] = $rOpts->{'valign-side-comments'}; # Batch of lines being collected $self->[_rgroup_lines_] = []; @@ -514,8 +518,16 @@ sub valign_input { if ( $level < 0 ) { $level = 0 } # do not align code across indentation level changes - # or if vertical alignment is turned off for debugging - if ( $level != $group_level || $is_outdented || !$self->[_rOpts_valign_] ) { + # or if vertical alignment is turned off + if ( + $level != $group_level + || $is_outdented + || ( $is_block_comment && !$self->[_rOpts_valign_block_comments_] ) + || ( !$is_block_comment + && !$self->[_rOpts_valign_side_comments_] + && !$self->[_rOpts_valign_code_] ) + ) + { $self->_flush_group_lines( $level - $group_level ); @@ -4280,7 +4292,8 @@ sub is_good_side_comment_column { my $short_diff = SC_LONG_LINE_DIFF / ( 1 + $alev_diff * $num5 ); goto FORGET - if ( $line_diff > $short_diff ) || !$self->[_rOpts_valign_]; + if ( $line_diff > $short_diff + || !$self->[_rOpts_valign_side_comments_] ); # RULE3: Forget a side comment if this line is at lower level and # ends a block diff --git a/t/snippets/expect/novalign.def b/t/snippets/expect/novalign.def index 61f326db..d0f43a22 100644 --- a/t/snippets/expect/novalign.def +++ b/t/snippets/expect/novalign.def @@ -1,9 +1,13 @@ +{ # simple vertical alignment of '=' and '#' -my $lines = 0; # checksum: #lines -my $bytes = 0; # checksum: #bytes -my $sum = 0; # checksum: system V sum -my $patchdata = 0; # saw patch data -my $pos = 0; # start of patch data -my $endkit = 0; # saw end of kit -my $fail = 0; # failed +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} diff --git a/t/snippets/expect/novalign.novalign b/t/snippets/expect/novalign.novalign deleted file mode 100644 index ee672bc4..00000000 --- a/t/snippets/expect/novalign.novalign +++ /dev/null @@ -1,9 +0,0 @@ -# simple vertical alignment of '=' and '#' -my $lines = 0; # checksum: #lines -my $bytes = 0; # checksum: #bytes -my $sum = 0; # checksum: system V sum -my $patchdata = 0; # saw patch data -my $pos = 0; # start of patch data -my $endkit = 0; # saw end of kit -my $fail = 0; # failed - diff --git a/t/snippets/expect/novalign.novalign1 b/t/snippets/expect/novalign.novalign1 new file mode 100644 index 00000000..087a94ea --- /dev/null +++ b/t/snippets/expect/novalign.novalign1 @@ -0,0 +1,13 @@ +{ + # simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} + diff --git a/t/snippets/expect/novalign.novalign2 b/t/snippets/expect/novalign.novalign2 new file mode 100644 index 00000000..763bfddf --- /dev/null +++ b/t/snippets/expect/novalign.novalign2 @@ -0,0 +1,13 @@ +{ + # simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} + diff --git a/t/snippets/expect/novalign.novalign3 b/t/snippets/expect/novalign.novalign3 new file mode 100644 index 00000000..fbcbe34b --- /dev/null +++ b/t/snippets/expect/novalign.novalign3 @@ -0,0 +1,13 @@ +{ +# simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} + diff --git a/t/snippets/novalign.in b/t/snippets/novalign.in index ee672bc4..1c618b99 100644 --- a/t/snippets/novalign.in +++ b/t/snippets/novalign.in @@ -1,9 +1,13 @@ +{ # simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left my $lines = 0; # checksum: #lines my $bytes = 0; # checksum: #bytes my $sum = 0; # checksum: system V sum my $patchdata = 0; # saw patch data my $pos = 0; # start of patch data + # a hanging side comment my $endkit = 0; # saw end of kit my $fail = 0; # failed +} diff --git a/t/snippets/novalign.par b/t/snippets/novalign.par deleted file mode 100644 index 230ed038..00000000 --- a/t/snippets/novalign.par +++ /dev/null @@ -1 +0,0 @@ --novalign diff --git a/t/snippets/novalign1.par b/t/snippets/novalign1.par new file mode 100644 index 00000000..230ed038 --- /dev/null +++ b/t/snippets/novalign1.par @@ -0,0 +1 @@ +-novalign diff --git a/t/snippets/novalign2.par b/t/snippets/novalign2.par new file mode 100644 index 00000000..59609dfb --- /dev/null +++ b/t/snippets/novalign2.par @@ -0,0 +1 @@ +-nvsc -nvbc -msc=2 diff --git a/t/snippets/novalign3.par b/t/snippets/novalign3.par new file mode 100644 index 00000000..b769159e --- /dev/null +++ b/t/snippets/novalign3.par @@ -0,0 +1 @@ +-nvc diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index bffe841d..5df46196 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -327,6 +327,8 @@ ../snippets24.t git51.def ../snippets24.t git51.git51 ../snippets24.t pretok.def +../snippets25.t novalign.def +../snippets25.t novalign.novalign ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -467,5 +469,6 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets25.t novalign.def -../snippets25.t novalign.novalign +../snippets25.t novalign.novalign1 +../snippets25.t novalign.novalign2 +../snippets25.t novalign.novalign3 diff --git a/t/snippets25.t b/t/snippets25.t index cd91e975..3bf1cb7d 100644 --- a/t/snippets25.t +++ b/t/snippets25.t @@ -2,7 +2,9 @@ # Contents: #1 novalign.def -#2 novalign.novalign +#2 novalign.novalign1 +#3 novalign.novalign2 +#4 novalign.novalign3 # To locate test #13 you can search for its name or the string '#13' @@ -20,8 +22,10 @@ BEGIN { # BEGIN SECTION 1: Parameter combinations # ########################################### $rparams = { - 'def' => "", - 'novalign' => "-novalign", + 'def' => "", + 'novalign1' => "-novalign", + 'novalign2' => "-nvsc -nvbc -msc=2", + 'novalign3' => "-nvc", }; ############################ @@ -30,14 +34,18 @@ BEGIN { $rsources = { 'novalign' => <<'----------', +{ # simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left my $lines = 0; # checksum: #lines my $bytes = 0; # checksum: #bytes my $sum = 0; # checksum: system V sum my $patchdata = 0; # saw patch data my $pos = 0; # start of patch data + # a hanging side comment my $endkit = 0; # saw end of kit my $fail = 0; # failed +} ---------- }; @@ -51,33 +59,81 @@ my $fail = 0; # failed source => "novalign", params => "def", expect => <<'#1...........', +{ # simple vertical alignment of '=' and '#' -my $lines = 0; # checksum: #lines -my $bytes = 0; # checksum: #bytes -my $sum = 0; # checksum: system V sum -my $patchdata = 0; # saw patch data -my $pos = 0; # start of patch data -my $endkit = 0; # saw end of kit -my $fail = 0; # failed +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} #1........... }, - 'novalign.novalign' => { + 'novalign.novalign1' => { source => "novalign", - params => "novalign", + params => "novalign1", expect => <<'#2...........', -# simple vertical alignment of '=' and '#' -my $lines = 0; # checksum: #lines -my $bytes = 0; # checksum: #bytes -my $sum = 0; # checksum: system V sum -my $patchdata = 0; # saw patch data -my $pos = 0; # start of patch data -my $endkit = 0; # saw end of kit -my $fail = 0; # failed +{ + # simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} #2........... }, + + 'novalign.novalign2' => { + source => "novalign", + params => "novalign2", + expect => <<'#3...........', +{ + # simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} + +#3........... + }, + + 'novalign.novalign3' => { + source => "novalign", + params => "novalign3", + expect => <<'#4...........', +{ +# simple vertical alignment of '=' and '#' +# A long line to test -nvbc ... normally this will cause the previous line to move left + my $lines = 0; # checksum: #lines + my $bytes = 0; # checksum: #bytes + my $sum = 0; # checksum: system V sum + my $patchdata = 0; # saw patch data + my $pos = 0; # start of patch data + # a hanging side comment + my $endkit = 0; # saw end of kit + my $fail = 0; # failed +} + +#4........... + }, }; my $ntests = 0 + keys %{$rtests};