From a4ad14641aac287077a1ce10400220c0b9cea418 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 13 Nov 2018 09:38:45 -0800 Subject: [PATCH] fixed rt127633 --- lib/Perl/Tidy/Formatter.pm | 29 +++++++++++++++++++++--- t/snippets/README.md | 17 +++++++++----- t/snippets/expect/rt127633.def | 4 ++++ t/snippets/expect/rt127633.rt127633 | 3 +++ t/snippets/packing_list.txt | 4 +++- t/snippets/rt127633.in | 2 ++ t/snippets/rt127633.par | 1 + t/snippets13.t | 35 ++++++++++++++++++++++++++++- 8 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 t/snippets/expect/rt127633.def create mode 100644 t/snippets/expect/rt127633.rt127633 create mode 100644 t/snippets/rt127633.in create mode 100644 t/snippets/rt127633.par diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 8630170c..325fbd79 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -15506,13 +15506,36 @@ sub insert_final_breaks { my $token = $tokens_to_go[$ii]; my $type = $types_to_go[$ii]; - # For now, a good break is either a comma or a 'return'. - if ( ( $type eq ',' || $type eq 'k' && $token eq 'return' ) - && in_same_container( $ii, $i_question ) ) + # For now, a good break is either a comma or, + # in a long chain, a 'return'. + # Patch for RT #126633: added the $nmax>1 check to avoid + # breaking after a return for a simple ternary. For longer + # chains the break after return allows vertical alignment, so + # it is still done. So perltidy -wba='?' will not break + # immediately after the return in the following statement: + # sub x { + # return 0 ? 'aaaaaaaaaaaaaaaaaaaaa' : + # 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'; + # } + if ( + ( + $type eq ',' + || $type eq 'k' && ( $nmax > 1 && $token eq 'return' ) + ) + && in_same_container( $ii, $i_question ) + ) { push @insert_list, $ii; last; } + +## # For now, a good break is either a comma or a 'return'. +## if ( ( $type eq ',' || $type eq 'k' && $token eq 'return' ) +## && in_same_container( $ii, $i_question ) ) +## { +## push @insert_list, $ii; +## last; +## } } # insert any new break points diff --git a/t/snippets/README.md b/t/snippets/README.md index d9c07970..65878325 100644 --- a/t/snippets/README.md +++ b/t/snippets/README.md @@ -1,7 +1,8 @@ # CODE SNIPPETS FOR TESTING PERLTIDY This directory contains some snippets of code to do simple checks of perltidy. -These are used to create the test file "snippets.t" in the source distribution. +These are used to create the test files 'snippets\*.t' in the source distribution. +These test files each contain up to about twenty test cases each. The tests are intended to give a good overall check that perltidy is working correctly at installation but they are by no means exhaustive. Thorough testing @@ -27,7 +28,6 @@ on the file names. Two rules are: - Rule 1: all scripts are run with the default parameters ("def.par") - Rule 2: if there is a parameter file with the same base name as the base name of the script file, then the script is also run with that parameter file. -then that script is For example, consider the source file "rt20421.in". The base name is 'rt20421'. It will be run with the default parameters. If a parameter file named "rt20421.par" @@ -60,13 +60,16 @@ creating new names. - Parameter files 'style1.par' and 'style35.par' will be run against 'style.in' if it exists. -It is best to avoid file names which are pure digits because they can be difficult to search for. But leading digits followed by some non-digits, would be okay. +It is best to avoid file names which are pure digits because they can be +difficult to search for. But leading digits followed by some non-digits, would +be okay. These rules may seem a little complex at first but they work quite +well. ## How to name a new snippet and parameter file, if any: - Give it a new base name with extension ".in". End the name with some digits to avoid accidentally invoking unexpected parameter combinations. If you just -want to format with default parameters, skip to the the run 'make' step. +want to format with default parameters, you can skip to the next step where you run 'make'. - For example, you might add a file named "rt126965.in" and then type 'make' and follow the directions. @@ -100,7 +103,11 @@ if everything looks good. - ./RUNME.sh -- This re-creates the 'snippet#.t' files in the upper directory. +- This re-creates the 'snippet#.t' files in the upper directory. A file +named 'packing\_list.txt' is written which shows the order of the snippets +in the snippet files. If you added test cases they should appear at the +end of the list and will be in the highest numbered snippet file. + - Verify that everything is ok by running perl on the new '.t' files or by going to the top directory and doing diff --git a/t/snippets/expect/rt127633.def b/t/snippets/expect/rt127633.def new file mode 100644 index 00000000..c7ee4ef4 --- /dev/null +++ b/t/snippets/expect/rt127633.def @@ -0,0 +1,4 @@ +# do not break after return with -wba=':' +return $ref eq 'SCALAR' + ? $self->encode_scalar( $object, $name, $type, $attr ) + : $ref eq 'ARRAY'; diff --git a/t/snippets/expect/rt127633.rt127633 b/t/snippets/expect/rt127633.rt127633 new file mode 100644 index 00000000..eed8d0e6 --- /dev/null +++ b/t/snippets/expect/rt127633.rt127633 @@ -0,0 +1,3 @@ +# do not break after return with -wba=':' +return $ref eq 'SCALAR' ? $self->encode_scalar( $object, $name, $type, $attr ) : + $ref eq 'ARRAY'; diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index c7734126..5476d1ab 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -82,6 +82,7 @@ ../snippets13.t align10.def ../snippets13.t align11.def ../snippets13.t align12.def +../snippets13.t align13.def ../snippets2.t angle.def ../snippets2.t arrows1.def ../snippets2.t arrows2.def @@ -242,4 +243,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets13.t align13.def +../snippets13.t rt127633.def +../snippets13.t rt127633.rt127633 diff --git a/t/snippets/rt127633.in b/t/snippets/rt127633.in new file mode 100644 index 00000000..0f9e1433 --- /dev/null +++ b/t/snippets/rt127633.in @@ -0,0 +1,2 @@ +# do not break after return with -wba=':' +return $ref eq 'SCALAR' ? $self->encode_scalar( $object, $name, $type, $attr ) : $ref eq 'ARRAY'; diff --git a/t/snippets/rt127633.par b/t/snippets/rt127633.par new file mode 100644 index 00000000..8125ee4c --- /dev/null +++ b/t/snippets/rt127633.par @@ -0,0 +1 @@ +-wba=':' diff --git a/t/snippets13.t b/t/snippets13.t index 51e8c9cf..8078edc6 100644 --- a/t/snippets13.t +++ b/t/snippets13.t @@ -5,6 +5,8 @@ #2 align11.def #3 align12.def #4 align13.def +#5 rt127633.def +#6 rt127633.rt127633 # To locate test #13 you can search for its name or the string '#13' @@ -21,7 +23,12 @@ BEGIN { ########################################### # BEGIN SECTION 1: Parameter combinations # ########################################### - $rparams = { 'def' => "", }; + $rparams = { + 'def' => "", + 'rt127633' => <<'----------', +-wba=':' +---------- + }; ############################ # BEGIN SECTION 2: Sources # @@ -52,6 +59,11 @@ ok( $out !~ /EXACT /, "No 'baz'" ); ok( $out =~ //, "Got 'liz'" ); # liz ok( $out =~ //, "Got 'zoo'" ); # zoo ok( $out !~ //, "Got 'zap'" ); # zap +---------- + + 'rt127633' => <<'----------', +# do not break after return with -wba=':' +return $ref eq 'SCALAR' ? $self->encode_scalar( $object, $name, $type, $attr ) : $ref eq 'ARRAY'; ---------- }; @@ -101,6 +113,27 @@ ok( $out =~ //, "Got 'zoo'" ); # zoo ok( $out !~ //, "Got 'zap'" ); # zap #4........... }, + + 'rt127633.def' => { + source => "rt127633", + params => "def", + expect => <<'#5...........', +# do not break after return with -wba=':' +return $ref eq 'SCALAR' + ? $self->encode_scalar( $object, $name, $type, $attr ) + : $ref eq 'ARRAY'; +#5........... + }, + + 'rt127633.rt127633' => { + source => "rt127633", + params => "rt127633", + expect => <<'#6...........', +# do not break after return with -wba=':' +return $ref eq 'SCALAR' ? $self->encode_scalar( $object, $name, $type, $attr ) : + $ref eq 'ARRAY'; +#6........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5