I think I can fix this. There are two issues. The first issue has to do
with manipulating the "continuation indentation" of lines, and the second
has to do with compacting the code.
FIRST ISSUE: In perltidy there are two types of indentation, structural
indentation specified by "i=n1" and continuation indentation specified by
"ci=n2", where n1 and n2 are the number of spaces. Structural indentation
is controlled by the nesting depth of braces and parens, and continuation
indentation is used to add spaces to the latter parts of a line when a line
broken because it exceeds the specified maximum line length. N1 and n2 may
be any values, but it is recommended that n2 be about half of n1 to avoid
problems in complex code.
The default parameters are n1=4 and and n2=2 and give:
get('
http://mojolicious.org')->then(
sub {
my $mojo = shift;
say $mojo->res->code;
return get('
http://metacpan.org');
}
)->then(
sub {
my $cpan = shift;
say $cpan->res->code;
}
)->catch(
sub {
my $err = shift;
warn "Something went wrong: $err";
}
)->wait;
[I don't know if my leading spaces will be correct in the email, but you
can reproduce this by turning off your default parameters with perltidy
-npro].
It looks like the snippet which was sent used n1=n2=2, and the equality of
these values causes the closing braces and parens to line up. A
programming fix for this is to remove continuation indentation for lines of
the form ")->then(". I looked at the perltidy source code and saw that it
is actually supposed to be doing this but it is not because of a minor bug.
This bug will be corrected in the next release, currently scheduled for
early January. With the corrected code the snippet will look okay for any
choice of n2. For n1=2 it will be:
get('
http://mojolicious.org')->then(
sub {
my $mojo = shift;
say $mojo->res->code;
return get('
http://metacpan.org');
}
)->then(
sub {
my $cpan = shift;
say $cpan->res->code;
}
)->catch(
sub {
my $err = shift;
warn "Something went wrong: $err";
}
)->wait;
SECOND ISSUE: The second point is that some programmers prefer vertically
compact code and some don't. There are some vertical tightness flags in
perltidy which give some flexibility, but they do not work for this type of
code. Programming this type of formatting is surprisingly tricky, but I
believe I can make a patch to handle this particular type of code. If so,
it will be controlled by the existing vertical tightness flags. I will try
to get this in the next release and will post a note here if I do.
It would help if someone could give me a link to a representative source of
this type of coding to use for testing.
Steve
On Sun, Nov 26, 2017 at 5:25 AM, Sebastian Riedel via RT <
bug-Perl-Tidy@rt.cpan.org> wrote:
Show quoted text> Sun Nov 26 08:25:09 2017: Request 123749 was acted upon.
> Transaction: Ticket created by SRI
> Queue: Perl-Tidy
> Subject: Problem with promises
> Broken in: (no value)
> Severity: (no value)
> Owner: Nobody
> Requestors: sri@cpan.org
> Status: new
> Ticket <URL:
https://rt.cpan.org/Ticket/Display.html?id=123749 >
>
>
> The Mojolicious project has recently adopted promises and many of our
> users will soon be using them. Sadly it appears that perltidy can't handle
> the most commonly used ->then constructs without making a bit of a mess.
>
> get('
http://mojolicious.org')->then(sub {
> my $mojo = shift;
> say $mojo->res->code;
> return get('
http://metacpan.org');
> })->then(sub {
> my $cpan = shift;
> say $cpan->res->code;
> })->catch(sub {
> my $err = shift;
> warn "Something went wrong: $err";
> })->wait;
>
> becomes
>
> get('
http://mojolicious.org')->then(
> sub {
> my $mojo = shift;
> say $mojo->res->code;
> return get('
http://metacpan.org');
> }
> )->then(
> sub {
> my $cpan = shift;
> say $cpan->res->code;
> }
> )->catch(
> sub {
> my $err = shift;
> warn "Something went wrong: $err";
> }
> )->wait;
>
> The project .perltidyrc is fairly unspectacular. We've tried finding
> options that make the result a little better, but failed so far.
>
>
https://github.com/kraih/mojo/blob/master/.perltidyrc
>
> This is the result i would have expected.
>
> get('
http://mojolicious.org')->then(
> sub {
> my $mojo = shift;
> say $mojo->res->code;
> return get('
http://metacpan.org');
> }
> )->then(
> sub {
> my $cpan = shift;
> say $cpan->res->code;
> }
> )->catch(
> sub {
> my $err = shift;
> warn "Something went wrong: $err";
> }
> )->wait;
>
> Many in the community would prefer a result like the first example, which
> i realize might be close to impossible. Any improvements to formatting
> constructs like this would be greatly appreciated.
>