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

## [Unreleased]

### Fixed

- 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))

## [2.2.0] - 2025-09-14

### Added
Expand Down
60 changes: 37 additions & 23 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,29 +253,8 @@ fn format_expression_internal(

// If the context is for a prefix, we should always keep the parentheses, as they are always required
if use_internal_expression && !keep_parentheses {
// Get the leading and trailing comments from contained span and append them onto the expression
let (start_parens, end_parens) = contained.tokens();
let leading_comments = start_parens
.leading_trivia()
.filter(|token| trivia_util::trivia_is_comment(token))
.flat_map(|x| {
vec![
create_indent_trivia(ctx, shape),
x.to_owned(),
create_newline_trivia(ctx),
]
})
// .chain(std::iter::once(create_indent_trivia(ctx, shape)))
.collect();

let trailing_comments = end_parens
.trailing_trivia()
.filter(|token| trivia_util::trivia_is_comment(token))
.flat_map(|x| {
// Prepend a single space beforehand
vec![Token::new(TokenType::spaces(1)), x.to_owned()]
})
.collect();
let (leading_comments, trailing_comments) =
contained_span_comments(ctx, contained, shape);

format_expression(ctx, expression, shape)
.update_leading_trivia(FormatTriviaType::Append(leading_comments))
Expand Down Expand Up @@ -358,6 +337,37 @@ fn format_expression_internal(
}
}

fn contained_span_comments(
ctx: &Context,
contained_span: &ContainedSpan,
shape: Shape,
) -> (Vec<Token>, Vec<Token>) {
// Get the leading and trailing comments from contained span and append them onto the expression
let (start_parens, end_parens) = contained_span.tokens();
let leading_comments = start_parens
.leading_trivia()
.filter(|token| trivia_util::trivia_is_comment(token))
.flat_map(|x| {
vec![
create_indent_trivia(ctx, shape),
x.to_owned(),
create_newline_trivia(ctx),
]
})
// .chain(std::iter::once(create_indent_trivia(ctx, shape)))
.collect();

let trailing_comments = end_parens
.trailing_trivia()
.filter(|token| trivia_util::trivia_is_comment(token))
.flat_map(|x| {
// Prepend a single space beforehand
vec![Token::new(TokenType::spaces(1)), x.to_owned()]
})
.collect();
(leading_comments, trailing_comments)
}

/// Determines whether the provided [`Expression`] is a brackets string, i.e. `[[string]]`
/// We care about this because `[ [[string] ]` is invalid syntax if we remove the whitespace
pub fn is_brackets_string(expression: &Expression) -> bool {
Expand Down Expand Up @@ -1352,13 +1362,17 @@ fn format_hanging_expression_(

// If the context is for a prefix, we should always keep the parentheses, as they are always required
if use_internal_expression && !keep_parentheses {
let (leading_comments, trailing_comments) =
contained_span_comments(ctx, contained, shape);
format_hanging_expression_(
ctx,
expression,
lhs_shape,
expression_context,
lhs_range,
)
.update_leading_trivia(FormatTriviaType::Append(leading_comments))
.update_trailing_trivia(FormatTriviaType::Append(trailing_comments))
} else {
let contained = format_contained_span(ctx, contained, lhs_shape);

Expand Down
11 changes: 11 additions & 0 deletions tests/inputs/excess-parentheses-comments-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/1033

_ = {
("foo"),
("foo"),
-- ("foo")
-- ("foo")
-- ("foo")
("foo"),
("foo")
}
16 changes: 16 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/tests.rs
expression: "format(&contents, LuaVersion::Lua51)"
input_file: tests/inputs/excess-parentheses-comments-2.lua
---
-- https://github.com/JohnnyMorganz/StyLua/issues/1033

_ = {
"foo",
"foo",
-- ("foo")
-- ("foo")
-- ("foo")
"foo",
"foo",
}
Loading