Skip to content

Pick up comments between trait where clause and open block #4292

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 1 commit into from
Jun 30, 2020
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
36 changes: 20 additions & 16 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,24 +1155,28 @@ pub(crate) fn format_trait(
result.push_str(&where_indent.to_string_with_newline(context.config));
}
result.push_str(&where_clause_str);
}
let pre_block_span = if !generics.where_clause.predicates.is_empty() {
mk_sp(generics.where_clause.span.hi(), item.span.hi())
} else {
let item_snippet = context.snippet(item.span);
if let Some(lo) = item_snippet.find('/') {
// 1 = `{`
let comment_hi = body_lo - BytePos(1);
let comment_lo = item.span.lo() + BytePos(lo as u32);
if comment_lo < comment_hi {
match recover_missing_comment_in_span(
mk_sp(comment_lo, comment_hi),
Shape::indented(offset, context.config),
context,
last_line_width(&result),
) {
Some(ref missing_comment) if !missing_comment.is_empty() => {
result.push_str(missing_comment);
}
_ => {}
item.span
};
let pre_block_snippet = context.snippet(pre_block_span);
if let Some(lo) = pre_block_snippet.find('/') {
// 1 = `{`
let comment_hi = body_lo - BytePos(1);
let comment_lo = pre_block_span.lo() + BytePos(lo as u32);
if comment_lo < comment_hi {
match recover_missing_comment_in_span(
mk_sp(comment_lo, comment_hi),
Shape::indented(offset, context.config),
context,
last_line_width(&result),
) {
Some(ref missing_comment) if !missing_comment.is_empty() => {
result.push_str(missing_comment);
}
_ => {}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/source/issue-4289.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait MyTrait
where
Self: Clone, // comment on first constraint
Self: Eq, // comment on last constraint
{
}

trait MyTrait2 where Self: Clone, /* another comment */ {}
12 changes: 12 additions & 0 deletions tests/target/issue-4289.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait MyTrait
where
Self: Clone, // comment on first constraint
Self: Eq, // comment on last constraint
{
}

trait MyTrait2
where
Self: Clone, /* another comment */
{
}