From aa5bea62b55ea8def875867fd895325efc7be6ed Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:43:53 +0200 Subject: [PATCH 1/4] Add test case --- tests/inputs/hang-binop-comments-3.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/inputs/hang-binop-comments-3.lua diff --git a/tests/inputs/hang-binop-comments-3.lua b/tests/inputs/hang-binop-comments-3.lua new file mode 100644 index 00000000..b91dce49 --- /dev/null +++ b/tests/inputs/hang-binop-comments-3.lua @@ -0,0 +1,10 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/996 + +local function f() + local a = "ab" + a = a:gsub("a".. -- " + 'b', function() return "c" end) + print(a) +end + +f() From 5ceb4141c6d6c1f8ce6cfafa5f844593d3459c63 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:44:04 +0200 Subject: [PATCH 2/4] Check for inline comments that will force hanging in function arguments --- src/formatters/functions.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index cb5393a2..3459b319 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -147,11 +147,13 @@ fn function_args_contains_comments( true } else { arguments.pairs().any(|argument| { - // Leading / Trailing trivia of expression (ignore inline comments) + // Leading / Trailing trivia of expression argument.value().leading_trivia() .iter() .chain(argument.value().trailing_trivia().iter()) .any(function_trivia_contains_comments) + // Inline comments that will force hanging + || argument.value().has_inline_comments() // Punctuation contains comments || argument .punctuation() From 5a23512bba190c9569f2946293d781febed25d77 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:44:08 +0200 Subject: [PATCH 3/4] Update snapshots --- ...s__standard@hang-binop-comments-3.lua.snap | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap diff --git a/tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap b/tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap new file mode 100644 index 00000000..274ebb5e --- /dev/null +++ b/tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap @@ -0,0 +1,20 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Lua51)" +input_file: tests/inputs/hang-binop-comments-3.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/996 + +local function f() + local a = "ab" + a = a:gsub( + "a" -- " + .. "b", + function() + return "c" + end + ) + print(a) +end + +f() From 499dff80c6ad8209aa3825b3903bb31cdee348a3 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:44:56 +0200 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 126be3d6..fe1d053b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed goto not being recognised for LuaJIT ([#986](https://github.com/JohnnyMorganz/StyLua/issues/986)) - Fixed semicolon removed after a statement ending with an if-expression leading to ambiguous syntax when the next line begins with parentheses ([#1010](https://github.com/JohnnyMorganz/StyLua/issues/1010)) - Luau: Fixed malformed formatting when there is a comment after a type specifier in a local assignment ([#995](https://github.com/JohnnyMorganz/StyLua/issues/995)) +- Fixed malformed formatting when a binary expression inside of a function call with comments around the operators is incorrectly collapsed onto one line ([#996](https://github.com/JohnnyMorganz/StyLua/issues/996)) ## [2.1.0] - 2025-04-21