Skip to content
Merged
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 @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added flag `--no-ignore-vcs` to continue formatting files listed in a `.gitignore` file, instead of skipping over them ([#895](https://github.com/JohnnyMorganz/StyLua/issues/895))

### Fixed

- 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.3.1] - 2025-11-01

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion src/formatters/trivia_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,11 @@ impl HasInlineComments for Expression {
fn has_inline_comments(&self) -> bool {
match self {
Expression::BinaryOperator { lhs, binop, rhs } => {
contains_comments(binop) || contains_comments(lhs) || rhs.has_inline_comments()
contains_comments(binop)
|| lhs.has_trailing_comments(CommentSearch::Single)
|| rhs.has_leading_comments(CommentSearch::Single)
|| lhs.has_inline_comments()
|| rhs.has_inline_comments()
}
Expression::UnaryOperator { unop, expression } => {
let op_contains_comments = match unop {
Expand Down
10 changes: 10 additions & 0 deletions tests/inputs/hang-binop-comments-3.lua
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap
Original file line number Diff line number Diff line change
@@ -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()
Loading