Skip to content

Commit 432e09e

Browse files
davidBar-Oncalebcartwright
authored andcommitted
Add the use of rewrite_assign_rhs_with_comments to 1.x
1 parent 4b0ed96 commit 432e09e

File tree

5 files changed

+152
-44
lines changed

5 files changed

+152
-44
lines changed

src/comment.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
977977

978978
pub(crate) trait FindUncommented {
979979
fn find_uncommented(&self, pat: &str) -> Option<usize>;
980+
fn find_last_uncommented(&self, pat: &str) -> Option<usize>;
980981
}
981982

982983
impl FindUncommented for str {
@@ -1002,6 +1003,19 @@ impl FindUncommented for str {
10021003
None => Some(self.len() - pat.len()),
10031004
}
10041005
}
1006+
1007+
fn find_last_uncommented(&self, pat: &str) -> Option<usize> {
1008+
if let Some(left) = self.find_uncommented(pat) {
1009+
let mut result = left;
1010+
// add 1 to use find_last_uncommented for &str after pat
1011+
while let Some(next) = self[(result + 1)..].find_last_uncommented(pat) {
1012+
result += next + 1;
1013+
}
1014+
Some(result)
1015+
} else {
1016+
None
1017+
}
1018+
}
10051019
}
10061020

10071021
// Returns the first byte position after the first comment. The given string

src/expr.rs

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use rustc_span::{BytePos, Span};
99
use crate::chains::rewrite_chain;
1010
use crate::closures;
1111
use crate::comment::{
12-
combine_strs_with_missing_comments, comment_style, contains_comment, recover_comment_removed,
13-
rewrite_comment, rewrite_missing_comment, CharClasses, FindUncommented,
12+
combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment,
13+
rewrite_missing_comment, CharClasses, FindUncommented,
1414
};
1515
use crate::config::lists::*;
1616
use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
@@ -829,38 +829,16 @@ impl<'a> ControlFlow<'a> {
829829
let comments_lo = context
830830
.snippet_provider
831831
.span_after(self.span, self.connector.trim());
832-
let missing_comments = if let Some(comment) =
833-
rewrite_missing_comment(mk_sp(comments_lo, expr.span.lo()), cond_shape, context)
834-
{
835-
if !self.connector.is_empty() && !comment.is_empty() {
836-
if comment_style(&comment, false).is_line_comment() || comment.contains("\n") {
837-
let newline = &pat_shape
838-
.indent
839-
.block_indent(context.config)
840-
.to_string_with_newline(context.config);
841-
// An extra space is added when the lhs and rhs are joined
842-
// so we need to remove one space from the end to ensure
843-
// the comment and rhs are aligned.
844-
let mut suffix = newline.as_ref().to_string();
845-
if !suffix.is_empty() {
846-
suffix.truncate(suffix.len() - 1);
847-
}
848-
format!("{}{}{}", newline, comment, suffix)
849-
} else {
850-
format!(" {}", comment)
851-
}
852-
} else {
853-
comment
854-
}
855-
} else {
856-
"".to_owned()
857-
};
858-
859-
let result = format!(
860-
"{}{}{}{}",
861-
matcher, pat_string, self.connector, missing_comments
832+
let comments_span = mk_sp(comments_lo, expr.span.lo());
833+
return rewrite_assign_rhs_with_comments(
834+
context,
835+
&format!("{}{}{}", matcher, pat_string, self.connector),
836+
expr,
837+
cond_shape,
838+
RhsTactics::Default,
839+
comments_span,
840+
true,
862841
);
863-
return rewrite_assign_rhs(context, result, expr, cond_shape);
864842
}
865843

866844
let expr_rw = expr.rewrite(context, cond_shape);
@@ -1899,14 +1877,13 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18991877
rewrite_assign_rhs_with(context, lhs, ex, shape, RhsTactics::Default)
19001878
}
19011879

1902-
pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
1880+
pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
19031881
context: &RewriteContext<'_>,
1904-
lhs: S,
1882+
lhs: &str,
19051883
ex: &R,
19061884
shape: Shape,
19071885
rhs_tactics: RhsTactics,
19081886
) -> Option<String> {
1909-
let lhs = lhs.into();
19101887
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
19111888
shape.indent.width()
19121889
} else {
@@ -1918,22 +1895,67 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19181895
offset: shape.offset + last_line_width + 1,
19191896
..shape
19201897
});
1921-
let rhs = choose_rhs(
1898+
let has_rhs_comment = if let Some(offset) = lhs.find_last_uncommented("=") {
1899+
lhs.trim_end().len() > offset + 1
1900+
} else {
1901+
false
1902+
};
1903+
1904+
choose_rhs(
19221905
context,
19231906
ex,
19241907
orig_shape,
19251908
ex.rewrite(context, orig_shape),
19261909
rhs_tactics,
1927-
)?;
1910+
has_rhs_comment,
1911+
)
1912+
}
1913+
1914+
pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
1915+
context: &RewriteContext<'_>,
1916+
lhs: S,
1917+
ex: &R,
1918+
shape: Shape,
1919+
rhs_tactics: RhsTactics,
1920+
) -> Option<String> {
1921+
let lhs = lhs.into();
1922+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
19281923
Some(lhs + &rhs)
19291924
}
19301925

1926+
pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
1927+
context: &RewriteContext<'_>,
1928+
lhs: S,
1929+
ex: &R,
1930+
shape: Shape,
1931+
rhs_tactics: RhsTactics,
1932+
between_span: Span,
1933+
allow_extend: bool,
1934+
) -> Option<String> {
1935+
let lhs = lhs.into();
1936+
let contains_comment = contains_comment(context.snippet(between_span));
1937+
let shape = if contains_comment {
1938+
shape.block_left(context.config.tab_spaces())?
1939+
} else {
1940+
shape
1941+
};
1942+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
1943+
1944+
if contains_comment {
1945+
let rhs = rhs.trim_start();
1946+
combine_strs_with_missing_comments(context, &lhs, &rhs, between_span, shape, allow_extend)
1947+
} else {
1948+
Some(lhs + &rhs)
1949+
}
1950+
}
1951+
19311952
fn choose_rhs<R: Rewrite>(
19321953
context: &RewriteContext<'_>,
19331954
expr: &R,
19341955
shape: Shape,
19351956
orig_rhs: Option<String>,
19361957
rhs_tactics: RhsTactics,
1958+
has_rhs_comment: bool,
19371959
) -> Option<String> {
19381960
match orig_rhs {
19391961
Some(ref new_str)
@@ -1950,13 +1972,14 @@ fn choose_rhs<R: Rewrite>(
19501972
.indent
19511973
.block_indent(context.config)
19521974
.to_string_with_newline(context.config);
1975+
let before_space_str = if has_rhs_comment { "" } else { " " };
19531976

19541977
match (orig_rhs, new_rhs) {
19551978
(Some(ref orig_rhs), Some(ref new_rhs))
19561979
if wrap_str(new_rhs.clone(), context.config.max_width(), new_shape)
19571980
.is_none() =>
19581981
{
1959-
Some(format!(" {}", orig_rhs))
1982+
Some(format!("{}{}", before_space_str, orig_rhs))
19601983
}
19611984
(Some(ref orig_rhs), Some(ref new_rhs))
19621985
if prefer_next_line(orig_rhs, new_rhs, rhs_tactics) =>
@@ -1966,10 +1989,11 @@ fn choose_rhs<R: Rewrite>(
19661989
(None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
19671990
(None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
19681991
let shape = shape.infinite_width();
1969-
expr.rewrite(context, shape).map(|s| format!(" {}", s))
1992+
expr.rewrite(context, shape)
1993+
.map(|s| format!("{}{}", before_space_str, s))
19701994
}
19711995
(None, None) => None,
1972-
(Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
1996+
(Some(orig_rhs), _) => Some(format!("{}{}", before_space_str, orig_rhs)),
19731997
}
19741998
}
19751999
}

src/items.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::comment::{
1717
use crate::config::lists::*;
1818
use crate::config::{BraceStyle, Config, IndentStyle, Version};
1919
use crate::expr::{
20-
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with, RhsTactics,
20+
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
21+
rewrite_assign_rhs_with_comments, RhsTactics,
2122
};
2223
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
2324
use crate::macros::{rewrite_macro, MacroPosition};
@@ -1822,14 +1823,22 @@ fn rewrite_static(
18221823
};
18231824

18241825
if let Some(expr) = static_parts.expr_opt {
1826+
let comments_lo = context.snippet_provider.span_after(static_parts.span, "=");
1827+
let expr_lo = expr.span.lo();
1828+
let comments_span = mk_sp(comments_lo, expr_lo);
1829+
18251830
let lhs = format!("{}{} =", prefix, ty_str);
1831+
18261832
// 1 = ;
18271833
let remaining_width = context.budget(offset.block_indent + 1);
1828-
rewrite_assign_rhs(
1834+
rewrite_assign_rhs_with_comments(
18291835
context,
1830-
lhs,
1836+
&lhs,
18311837
&**expr,
18321838
Shape::legacy(remaining_width, offset.block_only()),
1839+
RhsTactics::Default,
1840+
comments_span,
1841+
true,
18331842
)
18341843
.and_then(|res| recover_comment_removed(res, static_parts.span, context))
18351844
.map(|s| if s.ends_with(';') { s } else { s + ";" })

tests/source/issue-4427.rs

Lines changed: 31 additions & 0 deletions
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![]};

tests/target/issue-4427.rs

Lines changed: 30 additions & 0 deletions
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)