From b7f2154b498c018c7fa5fa9b9ec97197d0ba5b7c Mon Sep 17 00:00:00 2001 From: yuvraj wale Date: Mon, 11 Mar 2024 20:37:29 +0530 Subject: [PATCH 1/4] add:`rewrite_with_parens`in `PatKind::Paren` --- src/patterns.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/patterns.rs b/src/patterns.rs index 0fa6edaa5d7..75fcf624bf2 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -271,9 +271,17 @@ impl Rewrite for Pat { PatKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Pat) } - PatKind::Paren(ref pat) => pat - .rewrite(context, shape.offset_left(1)?.sub_width(1)?) - .map(|inner_pat| format!("({})", inner_pat)), + PatKind::Paren(ref pat) => { + overflow::rewrite_with_parens( + context, + "", + std::iter::once(&**pat), // iterator with a single element + shape, + self.span, + context.config.max_width(), + Some(SeparatorTactic::Never), // no separator needed for a single pattern + ) + }, } } } From 88cfed174004271c84430870963f0cccfd411100 Mon Sep 17 00:00:00 2001 From: yuvraj wale Date: Fri, 15 Mar 2024 12:43:03 +0530 Subject: [PATCH 2/4] add: manual comment recovery --- src/patterns.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/patterns.rs b/src/patterns.rs index 75fcf624bf2..b1230a984f1 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -272,16 +272,12 @@ impl Rewrite for Pat { rewrite_macro(mac, None, context, shape, MacroPosition::Pat) } PatKind::Paren(ref pat) => { - overflow::rewrite_with_parens( - context, - "", - std::iter::once(&**pat), // iterator with a single element - shape, - self.span, - context.config.max_width(), - Some(SeparatorTactic::Never), // no separator needed for a single pattern - ) - }, + let inner_pat = pat.rewrite(context, shape)?; + let pre_snippet = context.snippet(mk_sp(self.span.lo(), pat.span.lo())); + let post_snippet = context.snippet(mk_sp(pat.span.hi(), self.span.hi())); + let result = format!("{}{}{}", pre_snippet, inner_pat, post_snippet); + Some(result) + } } } } From 930b412fb36c91539a469e4260b3c78decb46baf Mon Sep 17 00:00:00 2001 From: yuvraj wale Date: Sat, 16 Mar 2024 19:16:15 +0530 Subject: [PATCH 3/4] add: test cases --- .../comments_in_paren/comments_in_paren.rs | 54 +++++++++++++++++++ .../comments_in_paren/comments_in_paren.rs | 43 +++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 tests/source/comments_in_paren/comments_in_paren.rs create mode 100644 tests/target/comments_in_paren/comments_in_paren.rs diff --git a/tests/source/comments_in_paren/comments_in_paren.rs b/tests/source/comments_in_paren/comments_in_paren.rs new file mode 100644 index 00000000000..1aeab77f185 --- /dev/null +++ b/tests/source/comments_in_paren/comments_in_paren.rs @@ -0,0 +1,54 @@ +// rustfmt-wrap_comments: true + +// Ensure comment preservation in parenthesis +fn main() { + let (/* */ () | () /* */) = (); + let (/**/ () | () /**/) = (); + let (/*comment*/ () | () /*comment*/) = (); + let (/*multi-line + comment*/ () | () /*multi-line + comment */) = (); + let (/*comment with new line + */ + () | () + /*comment with new line + */ + ) = (); +} + +// Ensure proper handling of line comments +fn line_comments() { + let (// Before + () + | + () + // After + ) = (); +} + +// Ensure proper handling of block comments with new lines +fn block_comments_with_new_lines() { + let (/* Before + * with new line */ + () + | + () + /* After + * with new line */ + ) = (); +} + +// Ensure inner pattern is getting formatted properly +// whilst preserving comments in outer parenthesis +fn inner_pat_formatting() { + let (/*comment*/ + ( + + ) + + + | ( + + ) + /*comment*/) = (); +} diff --git a/tests/target/comments_in_paren/comments_in_paren.rs b/tests/target/comments_in_paren/comments_in_paren.rs new file mode 100644 index 00000000000..483b3e1e8ce --- /dev/null +++ b/tests/target/comments_in_paren/comments_in_paren.rs @@ -0,0 +1,43 @@ +// rustfmt-wrap_comments: true + +// Ensure comment preservation in parenthesis +fn main() { + let (/**/ () | () /**/) = (); + let (/**/ () | () /**/) = (); + let (/*comment*/ () | () /*comment*/) = (); + let (/*multi-line + comment*/ () | () /*multi-line + comment */) = (); + let (/*comment with new line + */ + () | () + /*comment with new line + */ + ) = (); +} + +// Ensure proper handling of line comments +fn line_comments() { + let (// Before + () | () + // After + ) = (); +} + +// Ensure proper handling of block comments with new lines +fn block_comments_with_new_lines() { + let (/* Before + * with new line */ + () | () + /* After + * with new line */ + ) = (); +} + +// Ensure inner pattern is getting formatted properly +// whilst preserving comments in outer parenthesis +fn inner_pat_formatting() { + let (/*comment*/ + () | () + /*comment*/) = (); +} From be1585cf731381db386131186ec11e33b077c1e4 Mon Sep 17 00:00:00 2001 From: yuvraj wale Date: Sat, 16 Mar 2024 19:26:03 +0530 Subject: [PATCH 4/4] fix: test case errors --- tests/target/comments_in_paren/comments_in_paren.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/target/comments_in_paren/comments_in_paren.rs b/tests/target/comments_in_paren/comments_in_paren.rs index 483b3e1e8ce..eb45ad46b05 100644 --- a/tests/target/comments_in_paren/comments_in_paren.rs +++ b/tests/target/comments_in_paren/comments_in_paren.rs @@ -2,7 +2,7 @@ // Ensure comment preservation in parenthesis fn main() { - let (/**/ () | () /**/) = (); + let (/* */ () | () /* */) = (); let (/**/ () | () /**/) = (); let (/*comment*/ () | () /*comment*/) = (); let (/*multi-line