alejandra icon indicating copy to clipboard operation
alejandra copied to clipboard

Split long `or` statements into multiple lines

Open DavHau opened this issue 2 years ago • 5 comments

input:

{
  getSourceSpec = pname: version:
    sources."${pname}"."${version}"
    or (throw "The source spec for ${pname}#${version} is not defined in lockfile.");
}

alejandra result:

{
  getSourceSpec = pname: version:
    sources."${pname}"."${version}" or (throw "The source spec for ${pname}#${version} is not defined in lockfile.");
}

This is going way beyond column 80.

I think it would be good to always split the line at an or when the line is too long, like done in the first snippet.

DavHau avatar Mar 03 '22 04:03 DavHau

You can currently do that:

https://kamadorueda.github.io/alejandra/?before=%7B%0A++getSourceSpec+%3D+pname%3A+version%3A%0A++++sources.%22%24%7Bpname%7D%22.%22%24%7Bversion%7D%22%0A++++or+%28throw+%22The+source+spec+for+%24%7Bpname%7D%23%24%7Bversion%7D+is+not+defined+in+lockfile.%22%29%3B%0A%7D

Maybe am I not understanding?

On Wed, Mar 2, 2022 at 11:24 PM DavHau @.***> wrote:

input:

{ getSourceSpec = pname: version: sources."${pname}"."${version}" or (throw "The source spec for ${pname}#${version} is not defined in lockfile."); }

alejandra result:

{ getSourceSpec = pname: version: sources."${pname}"."${version}" or (throw "The source spec for ${pname}#${version} is not defined in lockfile."); }

This is going way beyond column 80.

I think it would be good to always split the line at an or when the line is too long, like done in the first snippet.

— Reply to this email directly, view it on GitHub https://github.com/kamadorueda/alejandra/issues/240, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKH4QEQCB56T6BXTQ7QUKLU6A5G7ANCNFSM5PZK4P4A . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Kevin Amado

kamadorueda avatar Mar 03 '22 04:03 kamadorueda

Sorry my bad. My actual input was different than what I posted.

input:

{
  getSourceSpec = pname: version:
    sources."${pname}"."${version}" or (
      throw "The source spec for ${pname}#${version} is not defined in lockfile."
    );
}

alejandra:

{
  getSourceSpec = pname: version:
    sources."${pname}"."${version}" or (throw "The source spec for ${pname}#${version} is not defined in lockfile.");
}

So, in this case it looks like it would have been the better decision for alejandra to move the or down instead of pulling the content up. Not sure how much complexity this would introduce into alejandra. Maybe it's not worth changing.

DavHau avatar Mar 03 '22 05:03 DavHau

You can add a newline before/after the or in order to get the result you want:

https://kamadorueda.github.io/alejandra/?before=%7B%0A++getSourceSpec+%3D+pname%3A+version%3A%0A++++sources.%22%24%7Bpname%7D%22.%22%24%7Bversion%7D%22+%0A++or+%28%0A++++++throw+%22The+source+spec+for+%24%7Bpname%7D%23%24%7Bversion%7D+is+not+defined+in+lockfile.%22%0A++++%29%3B%0A++getSourceSpec+%3D+pname%3A+version%3A%0A++++sources.%22%24%7Bpname%7D%22.%22%24%7Bversion%7D%22++or%0A++++%28%0A++++++throw+%22The+source+spec+for+%24%7Bpname%7D%23%24%7Bversion%7D+is+not+defined+in+lockfile.%22%0A++++%29%3B%0A%7D

On Thu, Mar 3, 2022 at 12:04 AM DavHau @.***> wrote:

Sorry my bad. My actual input was different than what I posted.

input:

{ getSourceSpec = pname: version: sources."${pname}"."${version}" or ( throw "The source spec for ${pname}#${version} is not defined in lockfile." ); }

alejandra:

{ getSourceSpec = pname: version: sources."${pname}"."${version}" or (throw "The source spec for ${pname}#${version} is not defined in lockfile."); }

So, in this case it looks like it would have been the better decision for alejandra to move the or down instead of pulling the content up. Not sure how much complexity this would introduce into alejandra. Maybe it's not worth changing.

— Reply to this email directly, view it on GitHub https://github.com/kamadorueda/alejandra/issues/240#issuecomment-1057669763, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKH4QHMXO5IDX3L5PX2BX3U6BB6ZANCNFSM5PZK4P4A . You are receiving this because you commented.Message ID: @.***>

-- Kevin Amado

kamadorueda avatar Mar 03 '22 05:03 kamadorueda

Sure, I can do that, and it isn't really a problem for me. I just think for large code bases it might be better if alejandra did this automatically and people won't have to fixup after running the formatter. Of course, only if it doesn't introduce too much complexity to alejandra.

DavHau avatar Mar 03 '22 05:03 DavHau

Changed the status from "needs discussing" to "it's a good thing"

Currently:

  • Users have the ability to insert a newline to make it multiline
  • If there is a chain of 2 or or more, it's multiline

Any light-weight additions to make the code more readable are appreciated

kamadorueda avatar Mar 03 '22 22:03 kamadorueda