nixfmt icon indicating copy to clipboard operation
nixfmt copied to clipboard

Preserve whitespace when used to align comments, assignments

Open emptyflask opened this issue 5 years ago • 2 comments

Description

Horizontally aligned comments and values make blocks of code containing such things much more readable. nixfmt shouldn't force this alignment, but it should preserve it if it's there, IMO.

Small example input / expected output

      enable       = true;            # enable compton
      fade         = true;            # enable fading
      fadeDelta    = 5;               # a comment
      fadeSteps    = ["0.04" "0.04"]; # ...
      shadow       = true;            # etc.
      backend      = "xrender";

Actual output

      enable = true; # enable compton
      fade = true; # enable fading
      fadeDelta = 5; # a comment
      fadeSteps = [ "0.04" "0.04" ]; # ...
      shadow = true; # etc.
      backend = "xrender";

emptyflask avatar Dec 20 '19 03:12 emptyflask

Nixfmt is built not to preserve any whitespace in order to make results independent of how messy the input formatting is. That said, I'm willing to consider a feature where any sequence of more than two spaces is considered somewhat like a smart tab and aligns with the smart tabs in its neighboring lines. This is by no means easy to build though, it would interact heavily with the current layout function which is already not so simple.

I might build this at some point, but don't count on it. If you'd like to work on this yourself, I can give you a more detailed description of the problem. With the current nixfmt, I recommend avoiding trailing line comments and putting those comments on the line before it instead.

I'd also like to apologize for the late response, I messed up my notification settings.

Lucus16 avatar Jan 30 '20 19:01 Lucus16

I'm inclined to prefer the nixfmt output in this case since the aligned format can cause patches that touch unrelated lines when a longer variable/attribute name is introduced/removed.

Though, for inline comments I can agree that alignment is nice but I do tend to avoid them in favor of putting the comment above the relevant line.

rycee avatar Feb 01 '20 23:02 rycee

The RFC 166 that no guarantees towards preserving vertical alignment will be made. However, since it also states to preserve line comments in their entirety, you could in theory do the following:

enable = true; #               enable compton
fade = true; #                 enable fading
fadeDelta = 5; #               a comment
fadeSteps = ["0.04" "0.04"]; # ...
shadow = true; #               etc.
backend = "xrender";

However, this still makes no guarantees that the comments will actually stay at their place in a way that preserves their vertical alignment.

piegamesde avatar Mar 14 '24 20:03 piegamesde