Skip to content

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

Open

Description

@d-enk

Since 2.6.0 (the Input mode for collapse_simple_statement, #1112), function calls whose arguments contain a multiline anonymous function followed by other arguments get expanded onto multiple lines — even though the input already had them hugged, and the default Never mode keeps them untouched.

Repro

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

stylua.toml:

collapse_simple_statement = "Input"

(defaults otherwise; column_width = 120, the return line is 90 chars)

Expected (and what 2.5.2 / Never mode produces):

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

Actual:

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

The expansion triggers once len(return-line) + ~25 > column_width, so any sufficiently long body inside the function argument sets it off — even though the body is formatted independently and its length is irrelevant to how the call itself should be laid out. With Never the call stays hugged no matter how long the body lines are.

Analysis

In function_args_multiline_heuristic (src/formatters/functions.rs), each function argument is classified via:

let is_expanded = !should_collapse_function_body(ctx, anonymous_function.body());

The problem: anonymous_function here is the argument already re-formatted by the heuristic's first pass (infinite width). should_collapse_function_body in Input mode decides based on node_spans_single_line, which reads token positions — but the re-formatted AST's tokens carry synthetic positions (everything on "line 0"), so a function that was multiline in the input is always classified as collapsed (is_expanded = false).

The heuristic then takes the collapsed branch, which adds the entire single-line rendering of the argument (argument.to_string().len(), 121 chars here) to the running width — going over budget and expanding the call.

Suggested fix

Determine is_expanded from the actual shape of the re-formatted argument — e.g. whether there is a newline in the trivia after the parameters parentheses or before the end token — the same approach the adjacent TableConstructor branch already uses.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions