From 8a2495e919d6c25a63d5232f3e9714b8528e7802 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sun, 18 Oct 2020 16:59:49 -0700 Subject: [PATCH] change method for parsing sub signatures --- lib/Perl/Tidy/Tokenizer.pm | 18 ++++++++++++++---- local-docs/BugLog.pod | 26 ++++++++++++++++++++------ t/filter_example.t | 4 ++-- t/snippets/switch1.in | 2 +- t/snippets11.t | 2 +- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 4c4b0032..46c79f26 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -2250,8 +2250,8 @@ sub prepare_for_a_new_file { } # ATTRS: check for a ':' which introduces an attribute list - # (this might eventually get its own token type) - elsif ( $statement_type =~ /^sub\b/ ) { + # either after a 'sub' keyword or within a paren list + elsif ($statement_type =~ /^sub\b/ ) { $type = 'A'; $in_attribute_list = 1; } @@ -2281,6 +2281,16 @@ sub prepare_for_a_new_file { $type = 'J'; } + # Withing a signature. For example, + # from 't/filter_example.t': + # method foo4 ( $class: $bar ) { $class->bar($bar) } + elsif ( !is_balanced_closing_container(QUESTION_COLON) + && $paren_type[$paren_depth] =~ /^sub\b/ ) + { + $type = 'A'; + $in_attribute_list = 1; + } + # otherwise, it should be part of a ?/: operator else { ( $type_sequence, $indent_flag ) = @@ -6690,8 +6700,8 @@ sub scan_identifier_do { # particular, we stop if we see any nested parens, braces, or commas. # Also note, a valid prototype cannot contain any alphabetic character # -- see https://perldoc.perl.org/perlsub - # But it appears that an underscore may be valid now, so this is - # using [A-Za-z] instead of \w + # old PROTO: + # $input_line =~ m/\G(\s*\([^\)\(\}\{\,#]*\))? # PROTO my $saw_opening_paren = $input_line =~ /\G\s*\(/; if ( $input_line =~ m/\G(\s*\([^\)\(\}\{\,#A-Za-z]*\))? # PROTO diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 0d56a833..7ce22c09 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,19 +2,33 @@ =item b -Simple signatures were being parsed with the code for prototypes, which -prevented them from being formatted with the usual formatting rules. All -signatures are now formatted separately from prototypes with the normal -formatting rules. For example: +Simple signatures (those without commas) were being parsed with code originally +written for prototypes. This prevented them from being formatted with the +usual formatting rules. This was changed so that all signatures are now +formatted with the normal formatting rules. For example: - # Old, intput and after formatting: + # Old, input and after formatting: sub t123 ($list=wantarray) { $list ? "list" : "scalar" } # New, after formatting sub t123 ( $list = wantarray ) { $list ? "list" : "scalar" } Notice that some spaces have been introduced within the signature. Previously -the contents of the parens was left unchanged. +the contents of the parens not changed unless the parens contained a list. + +This change introduced a problem parsing extended syntax within +signatures which has been fixed. In the following snippet, the ':' caused +a parsing error which was fixed. + + # perltidy -sal='method' + method foo4 ( $class : $bar, $bubba ) { $class->bar($bar) } + +The ':' here is given a type of 'A'. This may be used to change +the spacing around it. For example: + + # perltidy -sal='method' -nwls='A' + method foo4 ( $class: $bar, $bubba ) { $class->bar($bar) } + =item b diff --git a/t/filter_example.t b/t/filter_example.t index 825c2c14..bdecf449 100755 --- a/t/filter_example.t +++ b/t/filter_example.t @@ -24,7 +24,7 @@ use Method::Signatures::Simple; # change invocant name method -foo4 ($class: $bar) { $class->bar($bar) } +foo4 ( $class : $bar ) { $class->bar($bar) } ENDS my $expect = <<'ENDE'; @@ -42,7 +42,7 @@ method foo3 : lvalue { } # change invocant name -method foo4 ($class: $bar) { $class->bar($bar) } +method foo4 ( $class : $bar ) { $class->bar($bar) } ENDE my $output; diff --git a/t/snippets/switch1.in b/t/snippets/switch1.in index 908b9761..ce31bde9 100644 --- a/t/snippets/switch1.in +++ b/t/snippets/switch1.in @@ -1,4 +1,4 @@ -sub classify_digit($digit) +sub classify_digit ($digit) { switch($digit) { case 0 { return 'zero' } case [ 2, 4, 6, 8 ]{ return 'even' } case [ 1, 3, 4, 7, 9 ]{ return 'odd' } case /[A-F]/i { return 'hex' } } diff --git a/t/snippets11.t b/t/snippets11.t index 2e869f86..b417965e 100644 --- a/t/snippets11.t +++ b/t/snippets11.t @@ -95,7 +95,7 @@ $a->(); ---------- 'switch1' => <<'----------', -sub classify_digit($digit) +sub classify_digit ($digit) { switch($digit) { case 0 { return 'zero' } case [ 2, 4, 6, 8 ]{ return 'even' } case [ 1, 3, 4, 7, 9 ]{ return 'odd' } case /[A-F]/i { return 'hex' } } -- 2.39.5