How can I make perltidy work with Method::Signatures?
Asked Answered
G

4

12

I'm using Eclipse combined with EPIC to write my Perl code. I configured EPIC to use Perltidy with "-pbp" (perl best practices style) to format my code.

This doesn't work well when using Method::Signatures' named parameters. E.g., func (:$arg1, : $arg2) is formatted as func (: $arg1, : $arg2) which yields an error.

Also, func keyword is not recognized like sub so indentation is wrong.

Related to this previous unanswered question and this cross post.

Gormless answered 9/10, 2010 at 6:33 Comment(0)
J
4

You can modify the perlcritic script with a pre and post filter. The changelog provides the following example

Perl::Tidy::perltidy(
  prefilter => sub { $_ = $_[0]; s/^method (.*)/sub $1 \#__METHOD/gm; return $_ },
  postfilter => sub { $_ = $_[0]; s/^sub (.*?)\s* \#__METHOD/method $1/gm; return $_ }
);

Perlcritic will now treat method as a sub for purposes of formatting. We can do the same with func. I modified my /usr/local/bin/perlcritic to work with func as follows:

#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
package main;

use Perl::Tidy;

my $arg_string = undef;

# give Macs a chance to provide command line parameters
if ($^O =~ /Mac/) {
    $arg_string =
      MacPerl::Ask( 'Please enter @ARGV (-h for help)',
        defined $ARGV[0] ? "\"$ARGV[0]\"" : "" );
}

Perl::Tidy::perltidy(
    argv => $arg_string,
    prefilter => sub { $_ = $_[0]; s/\W\Kfunc\((.*)/sub($1 \#__FUNC/gm; return $_ },
    postfilter => sub { $_ = $_[0]; s/\W\Ksub\((.*?)\s* \#__FUNC/func($1/gm; return $_ }
);
Jactitation answered 10/12, 2011 at 7:58 Comment(1)
This fixes the method keyword. See this PerlTidy subclass to fix with the method signature issue that the OP highlighted.Roister
P
3

Perl::Tidy/perltidy does not make use of PPI, it predates PPI by about 9 years ( http://sourceforge.net/projects/perltidy/ says Registered: 2000-12-23 )

Puncheon answered 9/10, 2010 at 6:34 Comment(1)
search.cpan.org/dist/Perl-Tidy/META.yml proves that the current release also did not migrate to PPI.Sleekit
I
2

You can't, unless you make PPI, which is what Perltidy uses for most of its work, aware of the various signature modules such as MooseX::Method::Signatures, Method::Signatures::Simple, or Method::Signatures.

A reasonable workaround might be to not run Perltidy on all of your code, but only on the chunks of it you've just written and want formatted in some way. That way you can easily skip running it on any method signatures and have it process only the method bodies instead.

Illstarred answered 9/10, 2010 at 6:41 Comment(3)
I'm so used to Alt+Ctrl+F (autoformat all) and like it. I'll be really sad to give up on it.Gormless
So would I. Though, sadly, the only alternatives I can see are 1) giving up on automatically formatting /all/ of your code without consideration, 2) giving up on parts of method signatures, or 3) doing some work on PPI. Your choice.Illstarred
perltidy does NOT use PPI - it has it's own parserPelagianism
R
1

In the meantime a new module exists on CPAN which solves this problems. It is called Perl::Tidy::Sweetened and offers the script perltidier.

It also uses the prefilter and postfilter hooks of Perl::Tidy, but you do not need to take care about that on your own.

Roughen answered 4/1, 2017 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.