StyLua
StyLua copied to clipboard
Align trailing comments
in this example, spacing before and inside of comments is key to legibility (in fixed-width font, anyway):
function foo(
tinky, -- this is
winky, -- my aligned
inky, -- ascii art
blinky -- awesomeness
)
end
the aligned comments become this:

This is an interesting one, because it leads us onto something else to consider: should StyLua purposefully align items, such as comments? I know its something done in gofmt, but not in prettier or others. It has its pros and cons, the issue mainly being polluting diffs and it being quite challenging to implement.
Back to your original suggestion of keeping the spacing of comments alone in the first place, one thing I try not to do is rely on the input AST too much for formatting choices. In the case you mentioned it works because you are trying to align things, but it also starts affecting places where you want stylua to remove the spacing.
local x = 1 -- i did some refactoring leaving spacing before this comment, i want it removed
in your example, it's not an inline comment on a type/argument/table element list. forcing alignment in those cases is interesting, but as you mention it then requires a larger AST block to be the basis of the analysis/formatting, potentially opening up a can of worms in terms of complexity/accuracy.
ping on this. is this doable for argument, type, and table lists?
I'm still not 100% because it opens a gap in the automated formatting. If I were to add it, it would have to be behind an option
Another use case to consider for this is when lining up equal signs:
{
foo_bar = { a = 10, b = 2 },
fo_br = { a = 100, b = 2 },
foo = { a = 1, b = 2 },
fooo_bar = { a = 1000, b = 2 },
}
vs.
{
foo_bar = { a = 10, b = 2 },
fo_br = { a = 100, b = 2 },
foo = { a = 1, b = 2 },
fooo_bar = { a = 1000, b = 2 },
}
The ability to quickly parse the data structure and see the common elements is very nice in my opinion. Rubocop (ruby formatter) respects manual formatting of columns, though doesn't automatically format them for you, effectively allowing an implicit user override, which I think is a good approach. I'm not sure if its helpful, but I believe this is the implementation: https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cop/layout/extra_spacing.rb
As as side note, luals does this by default through EmmyLua