nixfmt icon indicating copy to clipboard operation
nixfmt copied to clipboard

Weird formatting of optionals

Open jtojnar opened this issue 11 months ago • 2 comments

Description

When formatting a long line with condition and a list, splitting the condition is preferred over the list. Would be nice to have some heuristic that would prefer the opposite.

Small example input

{
  NIX_CFLAGS_COMPILE = lib.optionals (lib.versionOlder prev.php.version "7.2" && pkgs.stdenv.cc.isClang) [" -Wno-implicit-int"];
}

Expected output

{
  NIX_CFLAGS_COMPILE =
    lib.optionals (lib.versionOlder prev.php.version "7.2" && pkgs.stdenv.cc.isClang)
      [
        " -Wno-implicit-int"
      ];
}

Actual output

{
  NIX_CFLAGS_COMPILE = lib.optionals (
    lib.versionOlder prev.php.version "7.2" && pkgs.stdenv.cc.isClang
  ) [ " -Wno-implicit-int" ];
}

I can get the preferred formatting if I split the array onto multiple lines. For example

{
  NIX_CFLAGS_COMPILE = lib.optionals (lib.versionOlder prev.php.version "7.2" && pkgs.stdenv.cc.isClang) [" -Wno-implicit-int"
  ];
}```

jtojnar avatar Jan 24 '25 01:01 jtojnar

Another example, without optionals:

Input:

{
  cycle = lib.drop (lib.lists.findFirstIndex (lib.flip lib.elem sorted.loops) 0 sorted.cycle) sorted.cycle;
}

Expected output:

{
  cycle = lib.drop (
    lib.lists.findFirstIndex (lib.flip lib.elem sorted.loops) 0 sorted.cycle
  ) sorted.cycle;
}

Or perhaps:

{
  cycle = lib.drop
    (lib.lists.findFirstIndex (lib.flip lib.elem sorted.loops) 0 sorted.cycle)
    sorted.cycle;
}

Actual output:

{
  cycle = lib.drop (lib.lists.findFirstIndex (lib.flip lib.elem sorted.loops) 0
    sorted.cycle
  ) sorted.cycle;
};

Majiir avatar Jul 04 '25 21:07 Majiir

okay this example is a lot weirder, and might be a different issue. I'll investigate

piegamesde avatar Jul 04 '25 21:07 piegamesde