Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long function applications should be wrapped per-arg #267

Open
MattSturgeon opened this issue Nov 29, 2024 · 2 comments
Open

Long function applications should be wrapped per-arg #267

MattSturgeon opened this issue Nov 29, 2024 · 2 comments
Labels
style Style discussion

Comments

@MattSturgeon
Copy link

MattSturgeon commented Nov 29, 2024

Description

nixfmt currently uses heuristics to find sensible places to insert line breaks, once a line becomes too long.

In the case of multi-arg function calls this can be counter productive, and result in less-readable code than if a more simplistic "one arg per line" approach is taken.

Small example input

{
  config_file_path = defaultNullOpts.mkNullable (with types; either str (listOf str)) [ ] "Custom config file path or list of custom config file paths.";
}

Expected output

{
  config_file_path =
    defaultNullOpts.mkNullable
      (with types; either str (listOf str))
      [ ]
      "Custom config file path or list of custom config file paths.";
}

Actual output

{
  config_file_path = defaultNullOpts.mkNullable (with types; either str (listOf str)) [
  ] "Custom config file path or list of custom config file paths.";
}

Note: there is also some strange line-breaking done within the empty list here. That is #268.

This was originally discussed here nix-community/nixvim#2209 (comment)

@piegamesde
Copy link
Member

Actual actual output when ignoring #268:

  config_file_path = defaultNullOpts.mkNullable (
    with types; either str (listOf str)
  ) [ ] "Some example long text that causes the line to be too long.";

The desired style already exists, the current behavior was introduced because it was deemed an improvement in many cases. In fact, if you make the string longer or add more arguments until the trailing code hits the line limit, it will switch style again and put the string onto a new line.

@MattSturgeon
Copy link
Author

The desired style already exists, the current behavior was introduced because it was deemed an improvement in many cases.

In that case, is this issue a "won't fix"? i.e. the formatting is already as intended

In fact, if you make the string longer or add more arguments until the trailing code hits the line limit, it will switch style again and put the string onto a new line.

Interesting. I'll play around with that if I get chance.


I noticed that with #268 fixed and the changes from NixOS/nixfmt@64d3540
isSimple: Mark numbers and other tokens as simple (#269)
(nixpkgs diff), many cases similar to this end up formatted as:

winblend =
  defaultNullOpts.mkNullableWithRaw (types.ints.between 0 100) 0
    "`0` for fully opaque and `100` for fully transparent.";

Or even end up on a single line.


Although the specific example in the issue description is still formatted as:

config_file_path = defaultNullOpts.mkNullable (
  with types; either str (listOf str)
) [ ] "Some example long text that causes the line to be too long.";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
style Style discussion
Projects
Status: Todo
Development

No branches or pull requests

2 participants