-
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
Comments handling for BinOp and Lists and for Multiline #4518
Comments handling for BinOp and Lists and for Multiline #4518
Conversation
Thank you for the PR @davidBar-On. To be fully transparent, this is unlikely to be thoroughly reviewed and considered for merging any time soon. We've got limited bandwidth on the rustfmt team at the moment, and for the foreseeable future this PR would require too much of that bandwidth for something that's primarily adding binop comment support. If you believe supporting binop comments is going to require such a widespread impact on formatting then I feel the best tactical path forward for the time being is likely going to be maintaining the status quo. I mentioned a couple things in #4417 that I want to reiterate here:
That being said, as it relates to the guidelines you enumerated in the PR description I do see several issues:
Context matters here, which is why rustfmt handles comments differently depending on the context. For example, consider a function call with exaggerated line breaks for illustrative purposes: foo(a, b,
/* c */
d, e);
This is not a complete/sufficient characterization of the formatting rules. Other factors like position/width limits and potentially indent style are also relevant considerations.
This is an incorrect articulation of expected/target behavior, but technically a correct description of current behavior due to a known bug that incorrectly defaults to visual alignment for these comments (#4108). We want to fix the bug, not codify the buggy behavior as correct.
This formatting (indentation in particular) is explicitly incorrect. It's another case where I'm not sure if this is what you are actually suggesting, or if somehow what you've posted is out of sync with what you intended to suggest. Finally, but quite important, is rustfmt's stability guarantee. Updated versions of rustfmt are not supposed to modify existing rustfmt-ed code, with exceptions in a few cases such as iterative formatting improvements for new syntax and rustfmt bug fixes. We cannot arbitrarily change comment handling rules such that users that |
I fully understand. When submitting the PR I thought it may be useful for testing comments formatting approaches, but now I understand this is not realistic (except maybe for myself). Taking this into account, thanks for still taking the time for answering.
O.k. I will dig through the changes I did and see if and what specific concerns may be raised as separate issues.
This is strange. I assume you refer to
The code shown (repeated below) is what I suggested. Since the code starting fn main() {
x = 6; /* Assignment comment */
/* Code commented out:
let y = 8;
let z = 9;
let u = 20;
*/
}
I understand. Since I don't have enough knowledge to know what may be the impact of a modification on the rustfmt users, I will make sure to first discuss any suggestion. |
I'd be quite surprised if GH's rendering of the snippet I referenced from the issue description varies by browser/OS, but for reference here's what I see in Firefox on Linux. Note that none of the block members are indented |
Ok, My mistake. This is also what I see. Sorry for that. I through you are referring to the commented code. For some reason I didn't notice that the |
No worries! It can happen pretty easily both when typing directly in GitHub as well as copy/paste. In a lot of projects the formatting of a snippet in GH doesn't really matter all that much, but obviously in a case like rustfmt every single space (or lackthereof) carries significant meaning 😄 |
Closing the PR per the discussion. I will check current rustfmt output for the target test files added in this PR. In cases where output is different than what I expected I will open a new issue to discuss. |
This PR was indented to be for BinOp comments handling (a replacement to the previous try - #4417). However, because the changes touch general used functions, other features are affected, mainly general handling of Multiline comments and handling of Lists comments.
Some test cases files are added to test the changes. In addition, some changes were done to existing test cases so that the expected formatting output will be according to the changes done.
A main change is adding
combine_strs_with_comments()
which was split fromcombine_strs_with_missing_comments()
, and allow general handling of string-comments combining, not just span-comments. In addition some parameters were added to allow more flexibility with the comments indentation. The multiline comments handling changes were mainly done inlight_rewrite_comment()
.As many changes are include in this PR, I understand that it may be difficult to test them to allow merging with the mainline. However, at least this PR can be used for to decide about the desired formatting of different cases. Once the output is agreed, it may be possible to merge subsets of these changes.
The main guidelines used for the comments indentations are:
Comments are trimmed and original prefix/suffix white spaces are ignore (except for line start and end for the above formatting).
Multiline comments are appended to a line or starts new line based on the width of the longest line in the comment (and not the first line width).
Comments at the end of a List items (Array, function parameters, match items, etc.), are aligned to the "Block" of items they belong to - consecutive items that include post-line one-line comments (not multiline). E.g. formatting output example:
For comments that follow each other, the first line of the first comment is formatted and indented as the code. The internal (non-first) comment lines indentation is:
//
" or "/*
", or '*
' that follow at least one whitespace (i.e. "*
"), the trimmed lines are indented as the first line, except that a ' ' is added before the '*
' to the "*
" lines.Formatting output example: