Skip to content

Commit 3e86a9d

Browse files
Preserve comments after excess parentheses are removed when hanging expression (#1034)
* Add test case * Preserve comments when removing parentheses in hanging expression * Update snapshot * Update changelog
1 parent 8f6aa2b commit 3e86a9d

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed comments lost from expression after parentheses are removed when we are attempting to "hang" the expression. ([#1033](https://github.com/JohnnyMorganz/StyLua/issues/1033))
13+
1014
## [2.2.0] - 2025-09-14
1115

1216
### Added

src/formatters/expression.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,29 +253,8 @@ fn format_expression_internal(
253253

254254
// If the context is for a prefix, we should always keep the parentheses, as they are always required
255255
if use_internal_expression && !keep_parentheses {
256-
// Get the leading and trailing comments from contained span and append them onto the expression
257-
let (start_parens, end_parens) = contained.tokens();
258-
let leading_comments = start_parens
259-
.leading_trivia()
260-
.filter(|token| trivia_util::trivia_is_comment(token))
261-
.flat_map(|x| {
262-
vec![
263-
create_indent_trivia(ctx, shape),
264-
x.to_owned(),
265-
create_newline_trivia(ctx),
266-
]
267-
})
268-
// .chain(std::iter::once(create_indent_trivia(ctx, shape)))
269-
.collect();
270-
271-
let trailing_comments = end_parens
272-
.trailing_trivia()
273-
.filter(|token| trivia_util::trivia_is_comment(token))
274-
.flat_map(|x| {
275-
// Prepend a single space beforehand
276-
vec![Token::new(TokenType::spaces(1)), x.to_owned()]
277-
})
278-
.collect();
256+
let (leading_comments, trailing_comments) =
257+
contained_span_comments(ctx, contained, shape);
279258

280259
format_expression(ctx, expression, shape)
281260
.update_leading_trivia(FormatTriviaType::Append(leading_comments))
@@ -358,6 +337,37 @@ fn format_expression_internal(
358337
}
359338
}
360339

340+
fn contained_span_comments(
341+
ctx: &Context,
342+
contained_span: &ContainedSpan,
343+
shape: Shape,
344+
) -> (Vec<Token>, Vec<Token>) {
345+
// Get the leading and trailing comments from contained span and append them onto the expression
346+
let (start_parens, end_parens) = contained_span.tokens();
347+
let leading_comments = start_parens
348+
.leading_trivia()
349+
.filter(|token| trivia_util::trivia_is_comment(token))
350+
.flat_map(|x| {
351+
vec![
352+
create_indent_trivia(ctx, shape),
353+
x.to_owned(),
354+
create_newline_trivia(ctx),
355+
]
356+
})
357+
// .chain(std::iter::once(create_indent_trivia(ctx, shape)))
358+
.collect();
359+
360+
let trailing_comments = end_parens
361+
.trailing_trivia()
362+
.filter(|token| trivia_util::trivia_is_comment(token))
363+
.flat_map(|x| {
364+
// Prepend a single space beforehand
365+
vec![Token::new(TokenType::spaces(1)), x.to_owned()]
366+
})
367+
.collect();
368+
(leading_comments, trailing_comments)
369+
}
370+
361371
/// Determines whether the provided [`Expression`] is a brackets string, i.e. `[[string]]`
362372
/// We care about this because `[ [[string] ]` is invalid syntax if we remove the whitespace
363373
pub fn is_brackets_string(expression: &Expression) -> bool {
@@ -1352,13 +1362,17 @@ fn format_hanging_expression_(
13521362

13531363
// If the context is for a prefix, we should always keep the parentheses, as they are always required
13541364
if use_internal_expression && !keep_parentheses {
1365+
let (leading_comments, trailing_comments) =
1366+
contained_span_comments(ctx, contained, shape);
13551367
format_hanging_expression_(
13561368
ctx,
13571369
expression,
13581370
lhs_shape,
13591371
expression_context,
13601372
lhs_range,
13611373
)
1374+
.update_leading_trivia(FormatTriviaType::Append(leading_comments))
1375+
.update_trailing_trivia(FormatTriviaType::Append(trailing_comments))
13621376
} else {
13631377
let contained = format_contained_span(ctx, contained, lhs_shape);
13641378

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/1033
2+
3+
_ = {
4+
("foo"),
5+
("foo"),
6+
-- ("foo")
7+
-- ("foo")
8+
-- ("foo")
9+
("foo"),
10+
("foo")
11+
}

tests/snapshots/[email protected]

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Lua51)"
4+
input_file: tests/inputs/excess-parentheses-comments-2.lua
5+
---
6+
-- https://github.com/JohnnyMorganz/StyLua/issues/1033
7+
8+
_ = {
9+
"foo",
10+
"foo",
11+
-- ("foo")
12+
-- ("foo")
13+
-- ("foo")
14+
"foo",
15+
"foo",
16+
}

0 commit comments

Comments
 (0)