StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

Feature Request: Option to expand/collapse tables based on entry count

Open deletewithfire opened this issue 6 months ago • 2 comments

There's no way to enforce a consistent style based on the number of entries in a table. For readability, it would be beneficial to always expand tables that contain several items while keeping very small tables compact and inline.

I would like a new configuration option in stylua.toml that allows setting a threshold for table expansion. For example, if the threshold is set to 3, any table with 3 or more entries would always be expanded (multi-line), while any table with fewer than 3 entries would be collapsed (single-line). An "entry" is any list-style element or key-value pair.

For example:

# stylua.toml
table_expansion_threshold = 3
-- Input
local my_table = { "one", "two", "three" }
local another_table = {
	key1 = "value1",
	key2 = "value2",
}

-- Expected output with table_expansion_threshold = 3
local my_table = {
	"one",
	"two",
	"three",
}
local another_table = { key1 = "value1", key2 = "value2" }

For nested tables, the expansion rule is applied independently to each table.

The existing column_width rule should take precedence. A table with fewer entries than the threshold should still be expanded if its single-line representation exceeds column_width.

deletewithfire avatar Jul 08 '25 13:07 deletewithfire

We currently follow a rule (similar to prettier) where a newline between the opening brace { and the first entry will force the table to always be expanded, regardless of the number of entries etc.

Is that suitable for your current use case? It has a disadvantage where it is difficult to enforce it automatically and consistently, and relies on the initial input.

If table_expansion_threshold was hypothetically defined, would we then disable this rule? i.e., in your current example, we end up collapsing another_table, but if we followed the rule then another_table would still remain expanded

Related: #264

JohnnyMorganz avatar Aug 10 '25 12:08 JohnnyMorganz

Yeah, I'm aware about the newline table rule, but as you said I've found it difficult to enforce it automatically and consistently. If table_expansion_threshold was defined, I think it would make most sense to disable this rule.

deletewithfire avatar Oct 19 '25 11:10 deletewithfire