Skip to content

Make formatting idempotent for a one-line table argument that cannot fit - #1194

Closed
claykrs wants to merge 1 commit into
JohnnyMorganz:mainfrom
claykrs:fix-hug-table-idempotence
Closed

claykrs wants to merge 1 commit into
JohnnyMorganz:mainfrom
claykrs:fix-hug-table-idempotence

Conversation

@claykrs

@claykrs claykrs commented Nov 26, 2026

Copy link
Copy Markdown

Fixes #1193

Problem

function_args_multiline_heuristic decides whether a table argument is "multiline" by checking whether the input already has a newline after {. That makes the result depend on how the input was laid out.

First pass. The last table is written on one line and is far too wide, so it's counted at its full width. The call goes over budget and every argument goes on its own line. On its own line the table still doesn't fit, so it gets expanded:

assert.are.same(
	{ true, false, false, false },
	{
		forms.keeps("heading", "identifier"),
		...
	}
)

Second pass. The table now starts with a newline after {, so it counts as an expanded argument and gets hugged:

assert.are.same({ true, false, false, false }, {
	forms.keeps("heading", "identifier"),
	...
})

So stylua file.lua && stylua --check file.lua fails.

Fix

In the table-constructor branch of the heuristic, a table written on one line is now also treated as expanded when it can't fit on a line of its own, at the indentation the expanded arguments would use (plus its trailing comma). Such a table is expanded by the formatter either way, so this decides the layout the same way the second pass does. For those tables, only the opening { is counted towards the current line, as for a table that is already expanded.

Tables that do fit on their own line are unaffected, as are tables that are already expanded.

Tests

  • New input tests/inputs/function-call-hug-table-idempotent.lua (the repro from the issue), with its snapshot.
  • New test_standard_is_idempotent, which formats that input twice and asserts the results are equal. It fails on main and passes with this change.
  • All existing snapshot tests pass unchanged (cargo test --all-features --tests).
  • The 7 lsp::tests failures I see on Windows also fail on main (path handling) and are unrelated.
  • cargo fmt --check and cargo clippy are clean. Changelog updated.

Real-world code that relied on the old first-pass output may now hug in the first pass, which matches what a second pass already produced. The external comparison run will show those diffs.

@claykrs claykrs closed this Nov 28, 2026
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.

Formatting is not idempotent: a call's last table argument hugs only on the second pass

1 participant