-
Notifications
You must be signed in to change notification settings - Fork 898
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
Fix formatting of comments in empty structs #5171
Fix formatting of comments in empty structs #5171
Conversation
@ytmimi I have made the changes you suggested in this PR, sorry for the inconvenience I did not realize that branches changed. |
No inconvenience at all! Thank you very much for the work you put into your original solution, making these suggested changes, and for updating all those tests! Everything looks good to me and I'm happy to move forward. |
Thanks for the quick turn around! Would you mind adding a few more tests?
|
Ah sorry @calebcartwright I thought I had the original commit with tests but probably lost it when rebasing and changing brnach. Added them now. So formating of block comments changed from: struct Foo {
/* bar */
} to struct Foo {/* bar */} Let me know if that is ok. |
@Szymongib I believe that's the correct behavior! However, I do think there's an issue with some of the examples that include multi line block comments. Here are the examples you have: struct Struct5 {
/*
Comment block
with many lines.
*/}
struct Struct6(
/*
Comment block
with many lines.
*/);
struct Struct7 {
/*
Invalid
format
*/}
struct Struct8(
/*
Invalid
format
*/);
struct Struct10 {
/* bar
baz
*/} And here's what I think the issue is; On line 1380 we check if we need to push an indented newline before adding the content of the string, however on line 1387 before deciding to add a newline we do a similar but slightly different check. Lines 1380 to 1390 in 5056f4c
I think if we updated Line 1387 to include the + let is_multi_line = !is_single_line(s);
+ if is_multi_line || first_line_contains_single_line_comment(s) {
- if !is_single_line(s) || first_line_contains_single_line_comment(s)
let nested_indent_str = offset
.block_indent(context.config)
.to_string_with_newline(context.config);
result.push_str(&nested_indent_str);
}
result.push_str(s);
+ if is_multi_line || last_line_contains_single_line_comment(s)
- if last_line_contains_single_line_comment(s) {
result.push_str(&offset.to_string_with_newline(context.config));
}
} |
We actually cannot make such a "breaking" formatting change, at least not without version gating it. We should try to find a way of preserving this type of formatting (comment on next line), but if not it will have to be gated. We've got some other code (or at least PRs) with examples of maintaining that type of comment behavior that I'm sure we could dig up as reference info |
@calebcartwright correct me if my understanding is off, but your saying that we can't format single line block comments like this? struct Foo { struct Foo {/* bar */}
/* bar */ ---------->
} I just checked and on the current master Is my comment about the closing brace also something that would be considered breaking? |
Thanks @ytmimi. Indeed master version does format single-line comments like that, so this behavior does not change. struct Foo {/* bar */} |
My apologies as it seems I misunderstood, thank you both! So long as the bug fix doesn't introduce any idempotency issues for existing "valid" (see, non-buggy indentation) formatting it's fine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor question inline re: existing tests but otherwise LGTM and ready to merge
Thanks so much for sticking with this one @Szymongib, it will be a solid first contribution!
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed | ||
// Expand as needed, numbers should be ascending according to the stage | ||
// through the inclusion pipeline, or according to the descriptions | ||
// Expand as needed, numbers should be ascending according to the stage | ||
// through the inclusion pipeline, or according to the descriptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was it necessary to modify these files? It's typically a flag when we have to change existing inputs to tests, but I'm guessing this isn't actually necessary, perhaps wasn't even explicitly intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, I just thought we might want to remove the // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
part of the comment as the issue should be addressed with the PR. It makes sense to leave it as it is tho, so I will revert that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@calebcartwright are you trying to highlight that this file is the source
file and that we don't need to update the formatting since it'll get checked against the corresponding target
? Would we not want to remove the comment about issue 4854 to not confuse anyone down the line about whether or not the issue is fixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was really just speaking to changing of the indentation, I actually hadn't read the the comment text word for word. I'm okay with removing that first line though would prefer to defer that to a follow up PR if anyone is interested given how long this PR (and it's predecessor) have been basically ready, but would still be good in that potential follow up to leave the source
contents with incorrect indentation
Thanks again! |
Currently, comments in empty structs are formatted weirdly. This PR attempts to fix it by indenting all one-line comments to the same one-depth indentation.
Examples can be found in the issue and tests.
Fixes: #4854
Reopen of #4875 due to branch changes (apologies for inconveniences).