nixfmt icon indicating copy to clipboard operation
nixfmt copied to clipboard

Inconsistent splitting with long lines

Open jtojnar opened this issue 11 months ago • 2 comments

Description

The second optionals will get split onto two extra lines, probably since the line spans 110 columns but splitting it vertically only reduces it down three columns – not a very good trade in my opinion.

Small example input

{
  patches =
    attrs.patches or [ ]
    ++ lib.optionals (lib.versionOlder prev.php.version "7.4") [
      ./foo.patch
    ]
    ++ lib.optionals (lib.versionAtLeast prev.php.version "8.0" && lib.versionOlder prev.php.version "8.1") [
      ./bar.patch
    ];
}

Expected output

{
  patches =
    attrs.patches or [ ]
    ++ lib.optionals (lib.versionOlder prev.php.version "7.4") [
      ./foo.patch
    ]
    ++ lib.optionals (lib.versionAtLeast prev.php.version "8.0" && lib.versionOlder prev.php.version "8.1") [
      ./bar.patch
    ];
}

Actual output

{
  patches =
    attrs.patches or [ ]
    ++ lib.optionals (lib.versionOlder prev.php.version "7.4") [
      ./foo.patch
    ]
    ++
      lib.optionals (lib.versionAtLeast prev.php.version "8.0" && lib.versionOlder prev.php.version "8.1")
        [
          ./bar.patch
        ];
}

jtojnar avatar Jan 24 '25 01:01 jtojnar

This is an artifact of the line length limit, your expected output exceeds it. If you increase the limit you will get the desired formatting again

piegamesde avatar Jan 24 '25 08:01 piegamesde

Right. I am arguing for the line length limit to be a bit more fuzzy.

For example, nixfmt could prefer more compact vertical layout when splitting a line would also overflow the line limit and the reduction of width would be insignificant (it is less than 3% here).

jtojnar avatar Jan 24 '25 13:01 jtojnar