Skip to content
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

Pick up comments between visibility modifier and item name #4239

Merged
merged 3 commits into from
Jun 8, 2020

Conversation

ayazhafiz
Copy link
Contributor

I don't think this hurts to fix. #2781, which surfaced this issue, has
a number of comments relating to similar but slightly different issues
(i.e. dropped comments in other places). I can mark #2781 as closed and
then will open new issues for the comments that are not already resolved
or tracked.

Closes #2781

I don't think this hurts to fix. rust-lang#2781, which surfaced this issue, has
a number of comments relating to similar but slightly different issues
(i.e. dropped comments in other places). I can mark rust-lang#2781 as closed and
then will open new issues for the comments that are not already resolved
or tracked.

Closes rust-lang#2781
Comment on lines 2994 to 3021
let mut result = String::with_capacity(128);
let shape = Shape::indented(offset, context.config);

result.push_str(&format_visibility(context, vis));

// Check for a missing comment between the visibility and the item name.
let after_vis = vis.span.hi();
if let Some(before_item_name) = context
.snippet_provider
.opt_span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim())
{
if let Some(cmt) =
rewrite_missing_comment(mk_sp(after_vis, before_item_name), shape, context)
{
result.push_str(&cmt);
let need_newline = last_line_contains_single_line_comment(&result);
if need_newline {
result.push_str(&offset.to_string_with_newline(context.config));
} else if cmt.len() > 0 {
result.push(' ');
}
}
}

result.push_str(item_name);
result.push_str(&rewrite_ident(context, ident));

result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see if using combine_strs_with_missing_comments would be viable? Haven't had a chance to look at this too closely yet, but most cases of comments getting dropped between strings combined with format! can be resolved by using combine_strs_with_missing_comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tip! Updated to use combine_strs_with_missing_comments.

@calebcartwright calebcartwright merged commit 5941299 into rust-lang:master Jun 8, 2020
calebcartwright pushed a commit to calebcartwright/rustfmt that referenced this pull request Nov 29, 2020
…#4239)

* Pick up comments between visibility modifier and item name

I don't think this hurts to fix. rust-lang#2781, which surfaced this issue, has
a number of comments relating to similar but slightly different issues
(i.e. dropped comments in other places). I can mark rust-lang#2781 as closed and
then will open new issues for the comments that are not already resolved
or tracked.

Closes rust-lang#2781

* fixup! Pick up comments between visibility modifier and item name

* fixup! Pick up comments between visibility modifier and item name
calebcartwright pushed a commit that referenced this pull request Nov 29, 2020
* Pick up comments between visibility modifier and item name

I don't think this hurts to fix. #2781, which surfaced this issue, has
a number of comments relating to similar but slightly different issues
(i.e. dropped comments in other places). I can mark #2781 as closed and
then will open new issues for the comments that are not already resolved
or tracked.

Closes #2781

* fixup! Pick up comments between visibility modifier and item name

* fixup! Pick up comments between visibility modifier and item name
@karyon
Copy link
Contributor

karyon commented Oct 26, 2021

Backported in #4564

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A comment is removed after pub keyword in a struct.
4 participants