Skip to content
Closed
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 formatting not being idempotent when the last argument of a function call is a table written on one line that is too long to fit: the first pass put every argument on its own line, and a second pass hugged the table with the parentheses ([#1193](https://github.com/JohnnyMorganz/StyLua/issues/1193))

## [2.6.0] - 2026-10-10

### Added
Expand Down
18 changes: 16 additions & 2 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,19 @@ fn function_args_multiline_heuristic(
}
Expression::TableConstructor(table) => {
// Check to see whether it has been expanded (there is a newline after the start brace)
let is_expanded = trivia_util::trivia_contains_newline(
let input_expanded = trivia_util::trivia_contains_newline(
table.braces().tokens().0.trailing_trivia(),
);
// A table written on one line is still expanded if it cannot fit on a line of its own
// when the arguments are expanded. Treat it as expanded already, otherwise formatting
// the expanded output again would hug it and give a different result.
let is_expanded = input_expanded || {
let trailing_comma = if pair.punctuation().is_some() { 1 } else { 0 };
(shape.reset().increment_additional_indent()
+ argument.to_string().len()
+ trailing_comma)
.over_budget()
};

if is_expanded {
// If we have a mixture of multiline args, and other arguments
Expand All @@ -303,7 +313,11 @@ fn function_args_multiline_heuristic(
}

// Include the first brace to check if we are over the column width already
singleline_shape = singleline_shape.take_first_line(table);
singleline_shape = if input_expanded {
singleline_shape.take_first_line(table)
} else {
singleline_shape + BRACKET_LEN
};
if singleline_shape.over_budget() {
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/inputs/function-call-hug-table-idempotent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/1193
-- A last table argument written on one line, too long to fit, must be formatted the same way
-- as when it is already expanded, so that formatting is idempotent.
assert.are.same({ true, false, false, false },
{ forms.keeps("heading", "identifier"), forms.keeps("heading", "style"),
forms.keeps("div", "identifier"), forms.keeps("span", "identifier") })
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/tests.rs
expression: "format(&contents, LuaVersion::Lua51)"
input_file: tests/inputs/function-call-hug-table-idempotent.lua
---
-- https://github.com/JohnnyMorganz/StyLua/issues/1193
-- A last table argument written on one line, too long to fit, must be formatted the same way
-- as when it is already expanded, so that formatting is idempotent.
assert.are.same({ true, false, false, false }, {
forms.keeps("heading", "identifier"),
forms.keeps("heading", "style"),
forms.keeps("div", "identifier"),
forms.keeps("span", "identifier"),
})

10 changes: 10 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ fn test_standard() {
})
}

#[test]
fn test_standard_is_idempotent() {
insta::glob!("inputs/function-call-hug-table-idempotent.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
let first = format(&contents, LuaVersion::Lua51);
let second = format(&first, LuaVersion::Lua51);
assert_eq!(first, second);
})
}

#[test]
fn test_full_moon_test_suite() {
insta::glob!("inputs-full_moon/*.lua", |path| {
Expand Down