diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac6dc18..6e4fccd1 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 a single-line comment before `do` in a numeric `for` loop, or right after the opening parenthesis of a hanging condition, commenting out the code that followed it ([#1176](https://github.com/JohnnyMorganz/StyLua/issues/1176)) + ## [2.6.0] - 2026-10-10 ### Added diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index 6291eedd..0c78cdb1 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -1422,7 +1422,12 @@ fn format_hanging_expression_( let formatted_expression = format_expression(ctx, expression, lhs_shape + 1); // 1 = opening parentheses let expression_str = formatted_expression.to_string(); + // A single-line comment after the opening parenthesis needs the expression on the next line if !contains_comments(expression) + && !contained + .tokens() + .0 + .has_trailing_comments(CommentSearch::Single) && !lhs_shape.add_width(2 + expression_str.len()).over_budget() { // The expression inside the parentheses is small, we do not need to break it down further diff --git a/src/formatters/stmt.rs b/src/formatters/stmt.rs index 1f2b2a6d..5ae84298 100644 --- a/src/formatters/stmt.rs +++ b/src/formatters/stmt.rs @@ -785,8 +785,41 @@ pub fn format_numeric_for(ctx: &Context, numeric_for: &NumericFor, shape: Shape) _ => unreachable!("Got numeric for end step comma with no step or vice versa"), }; - let do_token = fmt_symbol!(ctx, numeric_for.do_token(), " do", shape) - .update_trailing_trivia(FormatTriviaType::Append(trailing_trivia.to_owned())); + // A single-line comment before `do` would comment it out, so put `do` on its own line, + // as for `while` and generic `for` + let do_on_own_line = numeric_for + .step() + .unwrap_or(numeric_for.end()) + .has_trailing_comments(CommentSearch::Single) + || numeric_for + .do_token() + .has_leading_comments(CommentSearch::Single); + + let (end, step) = match (do_on_own_line, step) { + (true, Some(step)) => ( + end, + Some(step.update_trailing_trivia(FormatTriviaType::Append(vec![ + create_newline_trivia(ctx), + ]))), + ), + (true, None) => ( + end.update_trailing_trivia(FormatTriviaType::Append(vec![create_newline_trivia(ctx)])), + None, + ), + (false, step) => (end, step), + }; + + let do_token = match do_on_own_line { + true => format_end_token( + ctx, + numeric_for.do_token(), + EndTokenType::IndentComments, + shape, + ) + .update_leading_trivia(FormatTriviaType::Append(leading_trivia.to_owned())), + false => fmt_symbol!(ctx, numeric_for.do_token(), " do", shape), + } + .update_trailing_trivia(FormatTriviaType::Append(trailing_trivia.to_owned())); let block_shape = shape.reset().increment_block_indent(); let block = format_block(ctx, numeric_for.block(), block_shape); let end_token = format_end_token( diff --git a/tests/inputs/numeric-for-comment-before-do.lua b/tests/inputs/numeric-for-comment-before-do.lua new file mode 100644 index 00000000..47695554 --- /dev/null +++ b/tests/inputs/numeric-for-comment-before-do.lua @@ -0,0 +1,19 @@ +for i = 1, 2 -- comment +do + print(i) +end + +for i = 1, 10, 2 -- comment +do + print(i) +end + +for i = 1, 2 +-- comment +do + print(i) +end + +for i = 1, 2 --[[ comment ]] do + print(i) +end diff --git a/tests/inputs/parentheses-comment-after-open.lua b/tests/inputs/parentheses-comment-after-open.lua new file mode 100644 index 00000000..c0efe6d8 --- /dev/null +++ b/tests/inputs/parentheses-comment-after-open.lua @@ -0,0 +1,12 @@ +local a, b, c = true, false, true +if a + and ( -- comment + b or c + ) +then + print("yes") +end + +local x = ( -- comment + 1 + 2 +) diff --git a/tests/snapshots/tests__standard@numeric-for-comment-before-do.lua.snap b/tests/snapshots/tests__standard@numeric-for-comment-before-do.lua.snap new file mode 100644 index 00000000..18f14ecc --- /dev/null +++ b/tests/snapshots/tests__standard@numeric-for-comment-before-do.lua.snap @@ -0,0 +1,25 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Lua51)" +input_file: tests/inputs/numeric-for-comment-before-do.lua +--- +for i = 1, 2 -- comment +do + print(i) +end + +for i = 1, 10, 2 -- comment +do + print(i) +end + +for i = 1, 2 + -- comment +do + print(i) +end + +for i = 1, 2 --[[ comment ]] do + print(i) +end + diff --git a/tests/snapshots/tests__standard@parentheses-comment-after-open.lua.snap b/tests/snapshots/tests__standard@parentheses-comment-after-open.lua.snap new file mode 100644 index 00000000..921fec06 --- /dev/null +++ b/tests/snapshots/tests__standard@parentheses-comment-after-open.lua.snap @@ -0,0 +1,18 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Lua51)" +input_file: tests/inputs/parentheses-comment-after-open.lua +--- +local a, b, c = true, false, true +if + a and ( -- comment + b or c + ) +then + print("yes") +end + +local x = ( -- comment + 1 + 2 +) +