StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

Align trailing comments

Open matthargett opened this issue 4 years ago • 7 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: image

matthargett avatar Aug 31 '21 03:08 matthargett

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

JohnnyMorganz avatar Aug 31 '21 20:08 JohnnyMorganz

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.

matthargett avatar Aug 31 '21 22:08 matthargett

ping on this. is this doable for argument, type, and table lists?

matthargett avatar Oct 13 '21 16:10 matthargett

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

JohnnyMorganz avatar Oct 16 '21 19:10 JohnnyMorganz

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

CKolkey avatar Apr 04 '22 09:04 CKolkey

As as side note, luals does this by default through EmmyLua

baggiponte avatar Jul 01 '23 15:07 baggiponte