Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed function calls with a multiline anonymous function argument being expanded when `collapse_simple_statement = "Input"`, instead of keeping the function hugged to the call parentheses ([#1175](https://github.com/JohnnyMorganz/StyLua/issues/1175))

## [2.6.0] - 2026-10-10

### Added
Expand Down
11 changes: 8 additions & 3 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,17 @@ fn function_args_multiline_heuristic(
// Use state values to determine the type of arguments we have seen so far
let mut current_state = ArgumentState::new();

for pair in first_iter_formatted_arguments {
for (pair, original_argument) in first_iter_formatted_arguments.zip(arguments.iter()) {
let argument = pair.value();
match argument {
Expression::Function(anonymous_function) => {
// Check to see whether it has been expanded
let is_expanded = !should_collapse_function_body(ctx, anonymous_function.body());
// Check to see whether it has been expanded. Use the original function body where possible:
// the formatted one has lost the input positions that `CollapseSimpleStatement::Input` relies on
let function_body = match original_argument {
Expression::Function(original_function) => original_function.body(),
_ => anonymous_function.body(),
};
let is_expanded = !should_collapse_function_body(ctx, function_body);
if is_expanded {
// If we have a mixture of multiline args, and other arguments
// Then the function args should be expanded
Expand Down
62 changes: 62 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,68 @@ end)
);
}

const COLLAPSE_FUNCTION_ARGUMENT_INPUT: &str = r#"pcall(function()
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb
end, 5)
pcall(function() return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb end, 5)
foo(1, function()
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb
end)
pcall(function() return x end, 5)
"#;

#[test]
fn test_collapse_simple_statement_input_keeps_multiline_function_argument_hug() {
let config = Config {
collapse_simple_statement: CollapseSimpleStatement::Input,
..Config::default()
};

insta::assert_snapshot!(
format_code(COLLAPSE_FUNCTION_ARGUMENT_INPUT, config, None, OutputVerification::None).unwrap(),
@r###"
pcall(function()
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb
end, 5)
pcall(
function() return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb end,
5
)
foo(1, function()
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb
end)
pcall(function() return x end, 5)
"###
);
}

#[test]
fn test_collapse_simple_statement_always_function_argument_hug() {
let config = Config {
collapse_simple_statement: CollapseSimpleStatement::Always,
..Config::default()
};

insta::assert_snapshot!(
format_code(COLLAPSE_FUNCTION_ARGUMENT_INPUT, config, None, OutputVerification::None).unwrap(),
@r###"
pcall(
function() return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb end,
5
)
pcall(
function() return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb end,
5
)
foo(
1,
function() return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == bbb end
)
pcall(function() return x end, 5)
"###
);
}

#[test]
fn test_preserve_block_newline_gaps() {
insta::glob!("inputs-preserve-block-newline-gaps/*.lua", |path| {
Expand Down