Skip to content

keep comment appearing between parameter's name and its type #3491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2019
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
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ pub fn wrap_struct_field(
}

pub fn struct_lit_field_separator(config: &Config) -> &str {
colon_spaces(config.space_before_colon(), config.space_after_colon())
colon_spaces(config)
}

pub fn rewrite_field(
Expand Down
55 changes: 43 additions & 12 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const DEFAULT_VISIBILITY: ast::Visibility = source_map::Spanned {
};

fn type_annotation_separator(config: &Config) -> &str {
colon_spaces(config.space_before_colon(), config.space_after_colon())
colon_spaces(config)
}

// Statements of the form
Expand Down Expand Up @@ -1695,10 +1695,7 @@ fn rewrite_static(
static_parts: &StaticParts<'_>,
offset: Indent,
) -> Option<String> {
let colon = colon_spaces(
context.config.space_before_colon(),
context.config.space_after_colon(),
);
let colon = colon_spaces(context.config);
let mut prefix = format!(
"{}{}{} {}{}{}",
format_visibility(context, static_parts.vis),
Expand Down Expand Up @@ -1828,6 +1825,42 @@ fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
}
}

/// Recover any missing comments between the argument and the type.
///
/// # Returns
///
/// A 2-len tuple with the comment before the colon in first position, and the comment after the
/// colon in second position.
fn get_missing_arg_comments(
context: &RewriteContext<'_>,
pat_span: Span,
ty_span: Span,
shape: Shape,
) -> (String, String) {
let missing_comment_span = mk_sp(pat_span.hi(), ty_span.lo());

let span_before_colon = {
let missing_comment_span_hi = context
.snippet_provider
.span_before(missing_comment_span, ":");
mk_sp(pat_span.hi(), missing_comment_span_hi)
};
let span_after_colon = {
let missing_comment_span_lo = context
.snippet_provider
.span_after(missing_comment_span, ":");
mk_sp(missing_comment_span_lo, ty_span.lo())
};

let comment_before_colon = rewrite_missing_comment(span_before_colon, shape, context)
.filter(|comment| !comment.is_empty())
.map_or(String::new(), |comment| format!(" {}", comment));
let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
.filter(|comment| !comment.is_empty())
.map_or(String::new(), |comment| format!("{} ", comment));
(comment_before_colon, comment_after_colon)
}

impl Rewrite for ast::Arg {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
if let Some(ref explicit_self) = self.to_self() {
Expand All @@ -1838,13 +1871,11 @@ impl Rewrite for ast::Arg {
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;

if !is_empty_infer(&*self.ty, self.pat.span) {
if context.config.space_before_colon() {
result.push_str(" ");
}
result.push_str(":");
if context.config.space_after_colon() {
result.push_str(" ");
}
let (before_comment, after_comment) =
get_missing_arg_comments(context, self.pat.span, self.ty.span, shape);
result.push_str(&before_comment);
result.push_str(colon_spaces(context.config));
result.push_str(&after_comment);
let overhead = last_line_width(&result);
let max_width = shape.width.checked_sub(overhead)?;
let ty_str = self
Expand Down
5 changes: 1 addition & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,7 @@ where
}

fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
colon_spaces(
context.config.space_before_colon(),
context.config.space_after_colon(),
)
colon_spaces(context.config)
}

impl Rewrite for ast::WherePredicate {
Expand Down
4 changes: 3 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ fn is_valid_str(snippet: &str, max_width: usize, shape: Shape) -> bool {
}

#[inline]
pub fn colon_spaces(before: bool, after: bool) -> &'static str {
pub fn colon_spaces(config: &Config) -> &'static str {
let before = config.space_before_colon();
let after = config.space_after_colon();
match (before, after) {
(true, true) => " : ",
(true, false) => " :",
Expand Down
3 changes: 3 additions & 0 deletions tests/target/issue-2976.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn a(_ /*comment*/: u8 /* toto */) {}
fn b(/*comment*/ _: u8 /* tata */) {}
fn c(_: /*comment*/ u8) {}