Skip to content

Keep multiline function arguments hugged with Input collapse mode - #1179

Open
Lay-quq wants to merge 1 commit into
JohnnyMorganz:mainfrom
Lay-quq:contrib/JohnnyMorganz__StyLua__1175
Open

Lay-quq wants to merge 1 commit into
JohnnyMorganz:mainfrom
Lay-quq:contrib/JohnnyMorganz__StyLua__1175

Conversation

@Lay-quq

@Lay-quq Lay-quq commented Oct 25, 2026

Copy link
Copy Markdown

Fixes #1175

With collapse_simple_statement = "Input", a call whose anonymous function argument is written across multiple lines, e.g.

pcall(function()
	return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb
end, 5)

was expanded to pcall(\n\tfunction()\n\t\t...\n\tend,\n\t5\n) once the single-line width of the whole call (the call prefix, the collapsed function and the remaining arguments) exceeded column_width. The root cause comes from #1112: function_args_multiline_heuristic decides whether a function argument is expanded by calling should_collapse_function_body on the argument after it has been reformatted, and the reformatted node no longer has the input positions that Input mode uses to tell whether the function was single-line, so a multiline function was treated as collapsed.

This change passes the original argument's function body to that check instead (the reformatted one is still used for the width calculation). I went with this rather than checking the reformatted function for newline trivia, as done for tables in the branch below, because it keeps the exact predicate that format_function_body uses for the same node, so Always, Never and FunctionOnly are unaffected by construction and no function signatures need to change.

Tests added in tests/tests.rs, sharing one input:

  • Input mode: the input from the issue stays hugged; the same function written on a single line stays single-line and the call is still expanded when too wide; a multiline function as the last argument stays hugged; a short single-line function argument stays single-line.
  • Always mode: the same input, which formats identically before and after this change.

The Input test fails without the fix and passes with it. I also ran cargo test for the 9 feature combinations from ci.yml, cargo clippy --all-features -- -D warnings and cargo fmt -- --check before and after the change (rustc 1.99.1, Linux x86_64); the only difference in test results is the two new tests. Formatting the issue input and a few call variants (method calls, local ok = pcall(...), nested calls, functions in tables) gives identical output before and after in Always, Never and FunctionOnly modes, and Input output passes --verify and is stable when formatted again. I did not run the wasm, Docker or large-scale comparison workflows.

With `collapse_simple_statement = "Input"`, the function call argument
heuristic checked whether an anonymous function argument was expanded
using the reformatted argument. The reformatted node no longer has the
input positions that `Input` mode relies on, so a multiline function was
treated as collapsed, its single-line width counted against the column
width, and the whole call was expanded.

Use the original argument's function body for that check instead.

Fixes JohnnyMorganz#1175

This branch has not been deployed

No deployments
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.

collapse_simple_statement = "Input" unnecessarily expands hugged calls with multiline function arguments (2.6.0 regression)

1 participant