Emacs: reformat parameter lists

jplindstrom on 2008-01-07T16:31:37

So, this is a line-wrappingly long line.

my $link = API::Link->new({rel => 'meta:version', href => $uri->as_string, type => 'application/xml'}),
If you think that's not a long line, imagine a really long line with a lot of parameters for the sake of argument.

One param per line is much nicer in this case.
my $link = API::Link->new({
    rel => 'meta:version',
    href => $uri->as_string,
    type => 'application/xml',
}),
But it is a bit of a hassle to reformat by hand. Manual drudgery which easily makes me lose focus on why I just just wrote that method call. Now, why did I create that $link again?

Here is some nice elisp I wrote to reformat parameter lists either into a single line, into multiple lines, or toggle between the two.

So just put the cursor somewhere inside the parameter list and hit C-o m p (or whatever key binding strikes your fancy, but this one will work well with out-of-the-box PerlySense conventions) and it will reformat things into the other layout.

Once you have the nice multi-line layout, you can also align the parameters if that makes things look more sensible. Hit C-o m a et voila:
my $link = API::Link->new({
    rel  => 'meta:version',
    href => $uri->as_string,
    type => 'application/xml',
}),
Well, not that much difference with those parameter names, but I can't be arsed. You get the picture.

Enjoy!

(Now I'm just waiting for someone to tell me I reinvented the wheel. I just couldn't find any.)


Perl::Tidy

AndyArmstrong on 2008-01-07T16:39:39

Perl::Tidy is your friend! :)

I don't manually format any code any more.

Re:Perl::Tidy

Aristotle on 2008-01-07T17:54:59

And you can make it do this, how?

Re:Perl::Tidy

jplindstrom on 2008-01-07T18:20:47

Well Perl::Tidy turns this:

my $blodrfrf= $c->model('Fdsfkjsdlfkajd')->find({ id => $id, otehrs => $sdfkjsd });
into this:

my $blodrfrf= $c->model('Fdsfkjsdlfkajd')
        ->find({ id => $id, otehrs => $sdfkjsd });
instead of:

my $blodrfrf= $c->model('Fdsfkjsdlfkajd')->find({
    id => $id,
    otehrs => $sdfkjsd,
});
I'd appreciate any config hints you have, I just started and am currently trying to set it up to match our current coding standards as closely as possible.

Re:Perl::Tidy

AndyArmstrong on 2008-01-07T18:23:04

I normally hit Command-Shift-H :P

My .perltidyrc doesn't cuddle the ( and {, } and ) but I think I recall an option that covers that.

Here's what that code looks like for me:

my $link = API::Link->new(
    {
        rel  => 'meta:version',
        href => $uri->as_string,
        type => 'application/xml'
    }
);

cperl-mode has some functions for this

jrockway on 2008-01-21T06:56:49

cperl-mode handles most of this, but its implementation is sort of broken.  I guess I will be fixing that sometime soon.

One nit on your elisp code... the style like this:

<code>
(foo
  (bar
    baz
  )
)
</code>

Looks really weird.  All the close parens should be on the same line:

<code>
(foo
  (bar
    baz))
</code>

This is the "official" emacs lisp standard, and I think most other Lisps like the same sort of formatting.