From: Steve Hancock Date: Sat, 6 Mar 2021 02:25:54 +0000 (-0800) Subject: added flag -fpva, --function-paren-vertical-alignment; see git #53 X-Git-Tag: 20210402~23 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=312be4c6a8183d182099717dbc302e7027920ba2;p=perltidy.git added flag -fpva, --function-paren-vertical-alignment; see git #53 --- diff --git a/bin/perltidy b/bin/perltidy index 3008a553..453a61b1 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -1332,6 +1332,21 @@ reformatted with -sfp: In this particular case the syntax error can be removed if the line order is reversed, so that Perl parses 'sub filename' first. +=item B<-fpva> or B<--function-paren-vertical-alignment> + +A side-effect of using the B<-sfp> flag is that the parens may become vertically +aligned. For example, + + # perltidy -sfp + myfun ( $aaa, $b, $cc ); + mylongfun ( $a, $b, $c ); + +This is the default behavior. To prevent this alignment use B<-nfpva>: + + # perltidy -sfp -nfpva + myfun ( $aaa, $b, $cc ); + mylongfun ( $a, $b, $c ); + =item B<-spp=n> or B<--space-prototype-paren=n> This flag can be used to control whether a function prototype is preceded by a space. For example, the following prototype does not have a space. @@ -3862,11 +3877,11 @@ aligned in the following statement: Proto => 'tcp' ); -The only explicit control on vertical alignment is to turn it off using -B<-novalign>, a flag mainly intended for debugging. However, vertical -alignment can be forced to stop and restart by selectively introducing blank -lines. For example, a blank has been inserted in the following code -to keep somewhat similar things aligned. +Vertical alignment can be completely turned off using B<-novalign>, a flag +mainly intended for debugging. However, vertical alignment can be forced to +stop and restart by selectively introducing blank lines. For example, a blank +has been inserted in the following code to keep somewhat similar things +aligned. %option_range = ( 'format' => [ 'tidy', 'html', 'user' ], @@ -3879,6 +3894,20 @@ to keep somewhat similar things aligned. 'square-bracket-tightness' => [ 0, 2 ], ); +Vertical alignment is implemented by locally increasing an existing blank space +to produce alignment with an adjacent line. It cannot occur if there is no +blank space to increase. So if a particular space is removed by one of the +existing controls then vertical alignment cannot occur. Likewise, if a space is +added with one of the controls, then vertical alignment might occur. + +For example, + + # perltidy -nwls='=>' + $data = $pkg->new( + PeerAddr=> join( ".", @port[ 0 .. 3 ] ), + PeerPort=> $port[4] * 256 + $port[5], + Proto=> 'tcp' + ); =head2 Other Controls diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index f593e8a5..14fe9035 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3,7 +3,7 @@ # # perltidy - a perl script indenter and formatter # -# Copyright (c) 2000-2020 by Steve Hancock +# Copyright (c) 2000-2021 by Steve Hancock # Distributed under the GPL license agreement; see file COPYING # # This program is free software; you can redistribute it and/or modify @@ -2269,6 +2269,7 @@ sub generate_options { $add_option->( 'brace-tightness', 'bt', '=i' ); $add_option->( 'delete-old-whitespace', 'dws', '!' ); $add_option->( 'delete-semicolons', 'dsm', '!' ); + $add_option->( 'function-paren-vertical-alignment', 'fpva', '!' ); $add_option->( 'keyword-paren-inner-tightness', 'kpit', '=i' ); $add_option->( 'keyword-paren-inner-tightness-list', 'kpitl', '=s' ); $add_option->( 'logical-padding', 'lop', '!' ); @@ -2580,6 +2581,7 @@ sub generate_options { delete-old-newlines delete-semicolons extended-syntax + function-paren-vertical-alignment fuzzy-line-length hanging-side-comments indent-block-comments @@ -4206,7 +4208,7 @@ sub show_version { print STDOUT <<"EOM"; This is perltidy, v$VERSION -Copyright 2000-2020, Steve Hancock +Copyright 2000-2021, Steve Hancock Perltidy is free software and may be copied under the terms of the GNU General Public License, which is included in the distribution files. diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 6a57f1c2..d6e47f02 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -173,6 +173,7 @@ my ( $rOpts_add_whitespace, $rOpts_delete_old_whitespace, $rOpts_freeze_whitespace, + $rOpts_function_paren_vertical_alignment, # Static hashes initialized in a BEGIN block %is_assignment, @@ -1499,6 +1500,9 @@ EOM $rOpts_delete_old_whitespace = $rOpts->{'delete-old-whitespace'}; $rOpts_freeze_whitespace = $rOpts->{'freeze-whitespace'}; + $rOpts_function_paren_vertical_alignment = + $rOpts->{'function-paren-vertical-alignment'}; + # Note that both opening and closing tokens can access the opening # and closing flags of their container types. %opening_vertical_tightness = ( @@ -14963,8 +14967,6 @@ sub set_continuation_breaks { || $ris_broken_container->{$seqno} <= 1 ) { $ok = 0; - print - "BOOGA, $ris_broken_container->{$seqno}\n"; } } @@ -18234,9 +18236,12 @@ sub send_lines_to_vertical_aligner { /^(if|unless|elsif)$/; } - # Do not align a spaced-function-paren - fixes git #53 - # Note that index $i-1 is a blank token if we get here - if ( $i > $ibeg + 1 ) { + # Do not align a spaced-function-paren if requested. + # Issue git #53. Note that $i-1 is a blank token if we + # get here. + if ( !$rOpts_function_paren_vertical_alignment + && $i > $ibeg + 1 ) + { my $type_m = $types_to_go[ $i - 2 ]; my $token_m = $tokens_to_go[ $i - 2 ]; diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 23a63fcf..dba05a0f 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,13 @@ =over 4 +=item B + +A flag -fpva, --function-paren-vertical-alignment, is added to +prevent vertical alignment of function parens when the -sfp flag is used. +This is on by default, so that existing formatting remains unchanged unless +the user requests that vertical alignment not occur with -nfpva. + =item B Introducing a space before a function call paren had a side effect of @@ -24,7 +31,7 @@ have been the default but this side-effect was missed when the -sfp parameter was added. Note that parens following keywords are likewise not vertically aligned. -5 Mar 2021. +5 Mar 2021, 00431bf. =item B diff --git a/t/snippets/expect/fpva.def b/t/snippets/expect/fpva.def new file mode 100644 index 00000000..1931ffb6 --- /dev/null +++ b/t/snippets/expect/fpva.def @@ -0,0 +1,5 @@ +log_something_with_long_function( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); diff --git a/t/snippets/expect/fpva.fpva1 b/t/snippets/expect/fpva.fpva1 new file mode 100644 index 00000000..0b3a299a --- /dev/null +++ b/t/snippets/expect/fpva.fpva1 @@ -0,0 +1,5 @@ +log_something_with_long_function ( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep ( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); diff --git a/t/snippets/expect/fpva.fpva2 b/t/snippets/expect/fpva.fpva2 new file mode 100644 index 00000000..f502043c --- /dev/null +++ b/t/snippets/expect/fpva.fpva2 @@ -0,0 +1,5 @@ +log_something_with_long_function ( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep ( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); diff --git a/t/snippets/fpva.in b/t/snippets/fpva.in new file mode 100644 index 00000000..cc9022af --- /dev/null +++ b/t/snippets/fpva.in @@ -0,0 +1,5 @@ +log_something_with_long_function( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); diff --git a/t/snippets/fpva1.par b/t/snippets/fpva1.par new file mode 100644 index 00000000..c93d6ccd --- /dev/null +++ b/t/snippets/fpva1.par @@ -0,0 +1 @@ +-sfp diff --git a/t/snippets/fpva2.par b/t/snippets/fpva2.par new file mode 100644 index 00000000..d8c8b00b --- /dev/null +++ b/t/snippets/fpva2.par @@ -0,0 +1 @@ +-sfp -nfpva diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 296c0997..a2011422 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -308,6 +308,8 @@ ../snippets23.t git47.def ../snippets23.t git47.git47 ../snippets23.t qw.def +../snippets24.t git54.def +../snippets24.t git54.git54 ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -448,5 +450,6 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets24.t git54.def -../snippets24.t git54.git54 +../snippets24.t fpva.def +../snippets24.t fpva.fpva1 +../snippets24.t fpva.fpva2 diff --git a/t/snippets24.t b/t/snippets24.t index bae30dc3..2da12fe8 100644 --- a/t/snippets24.t +++ b/t/snippets24.t @@ -3,6 +3,9 @@ # Contents: #1 git54.def #2 git54.git54 +#3 fpva.def +#4 fpva.fpva1 +#5 fpva.fpva2 # To locate test #13 you can search for its name or the string '#13' @@ -21,6 +24,8 @@ BEGIN { ########################################### $rparams = { 'def' => "", + 'fpva1' => "-sfp", + 'fpva2' => "-sfp -nfpva", 'git54' => "-bbp=3 -bbpi=2 -ci=4 -lp", }; @@ -29,6 +34,14 @@ BEGIN { ############################ $rsources = { + 'fpva' => <<'----------', +log_something_with_long_function( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); +---------- + 'git54' => <<'----------', # testing sensitivity to excess commas my $definition => @@ -204,6 +217,42 @@ my $list = ); #2........... }, + + 'fpva.def' => { + source => "fpva", + params => "def", + expect => <<'#3...........', +log_something_with_long_function( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); +#3........... + }, + + 'fpva.fpva1' => { + source => "fpva", + params => "fpva1", + expect => <<'#4...........', +log_something_with_long_function ( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep ( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); +#4........... + }, + + 'fpva.fpva2' => { + source => "fpva", + params => "fpva2", + expect => <<'#5...........', +log_something_with_long_function ( 'This is a log message.', 2 ); +Coro::AnyEvent::sleep ( 3, 4 ); +use Carp (); +use File::Spec (); +use File::Path (); +#5........... + }, }; my $ntests = 0 + keys %{$rtests};