JohnnyMorganz / StyLua

An opinionated Lua code formatter
Mozilla Public License 2.0
1.59k stars 71 forks source link

Don't leave a small table expanded if it is already expanded #264

Open ok-nick opened 3 years ago

ok-nick commented 3 years ago

StyLua currently adapts to how you define tables and it preserves that style when it formats. I'd rather StyLua automatically determine the format of a table depending on the size of its content.

In this example, both tables should be formatted to the same result. Since the content of the table is small, it would make sense to format it as the upper example.

local foo = { bar = true }
local foo = {
    bar = true,
}
JohnnyMorganz commented 3 years ago

The style that StyLua preserves here is whether to leave a table expanded, this is checked by looking for a newline character between the first brace and first element in the table. The main reason for this was because tables commonly describe objects, and it can help to keep these multiline for readability - the same rationale is applied in prettier.

As you rightly point out though, in some cases this isn't really wanted. Prettier also mentions this as an issue, especially for formatting reversibility.

There hasn't really been an improved heuristic decided here yet to help mitigate this. The choices right now are to remove this formatting rationale completely (but that can hurt readability in some cases), or make this an option people can apply per project (its kind of a weird option, because you can "apply" it yourself on a case-by-case basis by just removing the newline after the first brace, but again, we do really want it all automated)