nixfmt
nixfmt copied to clipboard
Preserve whitespace when used to align comments, assignments
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";
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.
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.
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.