Skip to content

Commit 8e06851

Browse files
scampitopecongiro
authored andcommitted
keep comment appearing between parameter's name and its type (#3491)
1 parent 7650f0b commit 8e06851

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ pub fn wrap_struct_field(
16911691
}
16921692

16931693
pub fn struct_lit_field_separator(config: &Config) -> &str {
1694-
colon_spaces(config.space_before_colon(), config.space_after_colon())
1694+
colon_spaces(config)
16951695
}
16961696

16971697
pub fn rewrite_field(

src/items.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const DEFAULT_VISIBILITY: ast::Visibility = source_map::Spanned {
3636
};
3737

3838
fn type_annotation_separator(config: &Config) -> &str {
39-
colon_spaces(config.space_before_colon(), config.space_after_colon())
39+
colon_spaces(config)
4040
}
4141

4242
// Statements of the form
@@ -1695,10 +1695,7 @@ fn rewrite_static(
16951695
static_parts: &StaticParts<'_>,
16961696
offset: Indent,
16971697
) -> Option<String> {
1698-
let colon = colon_spaces(
1699-
context.config.space_before_colon(),
1700-
context.config.space_after_colon(),
1701-
);
1698+
let colon = colon_spaces(context.config);
17021699
let mut prefix = format!(
17031700
"{}{}{} {}{}{}",
17041701
format_visibility(context, static_parts.vis),
@@ -1828,6 +1825,42 @@ fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
18281825
}
18291826
}
18301827

1828+
/// Recover any missing comments between the argument and the type.
1829+
///
1830+
/// # Returns
1831+
///
1832+
/// A 2-len tuple with the comment before the colon in first position, and the comment after the
1833+
/// colon in second position.
1834+
fn get_missing_arg_comments(
1835+
context: &RewriteContext<'_>,
1836+
pat_span: Span,
1837+
ty_span: Span,
1838+
shape: Shape,
1839+
) -> (String, String) {
1840+
let missing_comment_span = mk_sp(pat_span.hi(), ty_span.lo());
1841+
1842+
let span_before_colon = {
1843+
let missing_comment_span_hi = context
1844+
.snippet_provider
1845+
.span_before(missing_comment_span, ":");
1846+
mk_sp(pat_span.hi(), missing_comment_span_hi)
1847+
};
1848+
let span_after_colon = {
1849+
let missing_comment_span_lo = context
1850+
.snippet_provider
1851+
.span_after(missing_comment_span, ":");
1852+
mk_sp(missing_comment_span_lo, ty_span.lo())
1853+
};
1854+
1855+
let comment_before_colon = rewrite_missing_comment(span_before_colon, shape, context)
1856+
.filter(|comment| !comment.is_empty())
1857+
.map_or(String::new(), |comment| format!(" {}", comment));
1858+
let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
1859+
.filter(|comment| !comment.is_empty())
1860+
.map_or(String::new(), |comment| format!("{} ", comment));
1861+
(comment_before_colon, comment_after_colon)
1862+
}
1863+
18311864
impl Rewrite for ast::Arg {
18321865
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
18331866
if let Some(ref explicit_self) = self.to_self() {
@@ -1838,13 +1871,11 @@ impl Rewrite for ast::Arg {
18381871
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
18391872

18401873
if !is_empty_infer(&*self.ty, self.pat.span) {
1841-
if context.config.space_before_colon() {
1842-
result.push_str(" ");
1843-
}
1844-
result.push_str(":");
1845-
if context.config.space_after_colon() {
1846-
result.push_str(" ");
1847-
}
1874+
let (before_comment, after_comment) =
1875+
get_missing_arg_comments(context, self.pat.span, self.ty.span, shape);
1876+
result.push_str(&before_comment);
1877+
result.push_str(colon_spaces(context.config));
1878+
result.push_str(&after_comment);
18481879
let overhead = last_line_width(&result);
18491880
let max_width = shape.width.checked_sub(overhead)?;
18501881
let ty_str = self

src/types.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,7 @@ where
378378
}
379379

380380
fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
381-
colon_spaces(
382-
context.config.space_before_colon(),
383-
context.config.space_after_colon(),
384-
)
381+
colon_spaces(context.config)
385382
}
386383

387384
impl Rewrite for ast::WherePredicate {

src/utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ fn is_valid_str(snippet: &str, max_width: usize, shape: Shape) -> bool {
376376
}
377377

378378
#[inline]
379-
pub fn colon_spaces(before: bool, after: bool) -> &'static str {
379+
pub fn colon_spaces(config: &Config) -> &'static str {
380+
let before = config.space_before_colon();
381+
let after = config.space_after_colon();
380382
match (before, after) {
381383
(true, true) => " : ",
382384
(true, false) => " :",

tests/target/issue-2976.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn a(_ /*comment*/: u8 /* toto */) {}
2+
fn b(/*comment*/ _: u8 /* tata */) {}
3+
fn c(_: /*comment*/ u8) {}

0 commit comments

Comments
 (0)