Skip to content

Add Input mode for collapse_simple_statement - #1112

Merged
JohnnyMorganz merged 9 commits into
JohnnyMorganz:mainfrom
ddashdev:preserve-collapse-simple-statement
Oct 10, 2026
Merged

JohnnyMorganz merged 9 commits into
JohnnyMorganz:mainfrom
ddashdev:preserve-collapse-simple-statement

Conversation

@ddashdev

@ddashdev ddashdev commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Adds a new Input mode for collapse_simple_statement.

It allows Stylua to preserve whether simple functions and conditionals were originally collapsed or expanded, instead of enforcing one global collapse behavior across the whole codebase.

With this mode:

if x then return end

stays collapsed, while:

if x then
    return
end

stays expanded.

The motivation is making it possible for users to keep a mixed style where some simple statements are collapsed and others are intentionally expanded, not all simple statements carry the same importance in a codebase. In some cases a collapsed statement improves readability by keeping a guard clause or empty function compact, while in others, expanding the statement makes the control flow or intent more visible.

The existing collapse_simple_statement modes require users to choose one global behavior: collapse matching statements everywhere, or expand them everywhere. This can be limiting when different simple statements benefit from different formatting choices.

And, while stylua: ignore comments can work around individual cases, they add noise and become difficult to maintain when used repeatedly for formatting preferences. Adding an Input mode follows the existing input-preserving semantics used by other Stylua rules like call_parenthesis, and gives users the ability to preserve these choices per situation without changing the default behavior.

Note: I used AI assistance while working through parts of the implementation, but reviewed the resulting changes and verified the behavior with tests.

Closes #863

@ddashdev

Copy link
Copy Markdown
Contributor Author

Also closes #863

@d-enk

d-enk commented Oct 6, 2026 •

Copy link
Copy Markdown

Thanks for working on this — I'm the author of #863, and this addresses what I was hoping for.

One gap though: the mode currently only preserves the input shape for functions and if-guards, but regular block statements (do/for/while) still get expanded even when they were collapsed in the input. For example, this idiom (common in UTF-8 decoders and similar tables) currently expands into 12 lines:

-- input
for i = 0x00, 0x7F do sequence_len_by_byte[i] = 1 end
for i = 0xC0, 0xDF do sequence_len_by_byte[i] = 2 end
for i = 0xE0, 0xEF do sequence_len_by_byte[i] = 3 end
for i = 0xF0, 0xFF do sequence_len_by_byte[i] = 4 end

The same applies to do local x = 1 end and while cond do step() end.

The approach could extend naturally to all block-bearing statements: check whether the whole statement spans a single line in the input and the block is "simple" (a single laststmt / simple stmt, no comments), then inline the block instead of deferring to format_block().

@JohnnyMorganz JohnnyMorganz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@JohnnyMorganz
JohnnyMorganz merged commit 1c5060e into JohnnyMorganz:main Oct 10, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No uncollapse simple expressions, collapse_simple_statement only when already collapsed

3 participants