Skip to content

Commit 0476b39

Browse files
authored
Fixed comment formatting issue between assignment operator and rhs (#4440)
* Fixed comment formatting issue between assignment * rewrite_rhs_with_comments method * Add more tests * Added more test cases and optimized * Removed extra new lines * Removed accidentally pushed tmp file
1 parent 2927e89 commit 0476b39

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

Diff for: src/formatting/expr.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,32 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19851985
Some(lhs + &rhs)
19861986
}
19871987

1988+
pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
1989+
context: &RewriteContext<'_>,
1990+
lhs: S,
1991+
ex: &R,
1992+
shape: Shape,
1993+
rhs_tactics: RhsTactics,
1994+
between_span: Span,
1995+
allow_extend: bool,
1996+
) -> Option<String> {
1997+
let lhs = lhs.into();
1998+
let contains_comment = contains_comment(context.snippet(between_span));
1999+
let shape = if contains_comment {
2000+
shape.block_left(context.config.tab_spaces())?
2001+
} else {
2002+
shape
2003+
};
2004+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
2005+
2006+
if contains_comment {
2007+
let rhs = rhs.trim_start();
2008+
combine_strs_with_missing_comments(context, &lhs, &rhs, between_span, shape, allow_extend)
2009+
} else {
2010+
Some(lhs + &rhs)
2011+
}
2012+
}
2013+
19882014
fn choose_rhs<R: Rewrite>(
19892015
context: &RewriteContext<'_>,
19902016
expr: &R,

Diff for: src/formatting/items.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::formatting::{
1919
},
2020
expr::{
2121
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_expr,
22-
rewrite_assign_rhs_with, RhsTactics,
22+
rewrite_assign_rhs_with, rewrite_assign_rhs_with_comments, RhsTactics,
2323
},
2424
lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator},
2525
macros::{rewrite_macro, MacroPosition},
@@ -1878,14 +1878,22 @@ fn rewrite_static(
18781878
};
18791879

18801880
if let Some(expr) = static_parts.expr_opt {
1881+
let comments_lo = context.snippet_provider.span_after(static_parts.span, "=");
1882+
let expr_lo = expr.span.lo();
1883+
let comments_span = mk_sp(comments_lo, expr_lo);
1884+
18811885
let lhs = format!("{}{} =", prefix, ty_str);
1886+
18821887
// 1 = ;
18831888
let remaining_width = context.budget(offset.block_indent + 1);
1884-
rewrite_assign_rhs(
1889+
rewrite_assign_rhs_with_comments(
18851890
context,
18861891
&lhs,
18871892
&**expr,
18881893
Shape::legacy(remaining_width, offset.block_only()),
1894+
RhsTactics::Default,
1895+
comments_span,
1896+
true,
18891897
)
18901898
.and_then(|res| recover_comment_removed(res, static_parts.span, context))
18911899
.or_else(|| {

Diff for: tests/source/issue-4427.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const A: usize =
2+
// Some constant
3+
2;
4+
5+
const B: usize =
6+
/* constant */
7+
3;
8+
9+
const C : usize
10+
= /* foo */5;
11+
12+
const D: usize = // baz
13+
/* Some constant */
14+
/* ba */
15+
{ 3
16+
// foo
17+
};
18+
const E: usize= /* foo */5;
19+
const F: usize =
20+
{
21+
7
22+
};
23+
const G: usize = /* foooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000xx00 */ 5;
24+
const H: usize = /* asdfasdf */ match G > 1 {
25+
true => 1,
26+
false => 3,
27+
};
28+
29+
pub static FOO_BAR: Vec<u8> = //f
30+
{
31+
vec![]};

Diff for: tests/target/issue-4427.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const A: usize =
2+
// Some constant
3+
2;
4+
5+
const B: usize =
6+
/* constant */
7+
3;
8+
9+
const C: usize = /* foo */ 5;
10+
11+
const D: usize = // baz
12+
/* Some constant */
13+
/* ba */
14+
{
15+
3
16+
// foo
17+
};
18+
const E: usize = /* foo */ 5;
19+
const F: usize = { 7 };
20+
const G: usize =
21+
/* foooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000xx00 */
22+
5;
23+
const H: usize = /* asdfasdf */
24+
match G > 1 {
25+
true => 1,
26+
false => 3,
27+
};
28+
29+
pub static FOO_BAR: Vec<u8> = //f
30+
{ vec![] };

0 commit comments

Comments
 (0)