diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac6dc18..85791b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index ab3244e1..1efb6669 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -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 @@ -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; } diff --git a/tests/inputs/function-call-hug-table-idempotent.lua b/tests/inputs/function-call-hug-table-idempotent.lua new file mode 100644 index 00000000..09136b7b --- /dev/null +++ b/tests/inputs/function-call-hug-table-idempotent.lua @@ -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") }) diff --git a/tests/snapshots/tests__standard@function-call-hug-table-idempotent.lua.snap b/tests/snapshots/tests__standard@function-call-hug-table-idempotent.lua.snap new file mode 100644 index 00000000..ce25efad --- /dev/null +++ b/tests/snapshots/tests__standard@function-call-hug-table-idempotent.lua.snap @@ -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"), +}) + diff --git a/tests/tests.rs b/tests/tests.rs index 908a390f..57a4cc36 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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| {