rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Backport PR #4658 and Fix issue #4551 - multilines comment indentation

Open davidBar-On opened this issue 3 years ago • 3 comments
trafficstars

Fixes #4551

Backport #4658 and suggested fix for #4551. The backport and the suggested fix are submitted in one PR, since fixing #4551 requires both, and if only one will be merged, then the issue will not be fixed.

The backport of #4658 handles the case when in the per-formatted code a tab precedes the * at the beginning of a second and following lines of a comment. Copying the tab into the target code sometimes cased wrong indentation, so the tab is replaced with space.

The additional fix for #4551 is for version Two to properly handle the indentation of the second and following lines of a multiline comment, when the first comment line is in the same line of the previous statement. In version Two, these lines were indented according to the statement indentation and the fix indents them to the first line.

davidBar-On avatar Aug 01 '22 14:08 davidBar-On

As mentioned in the Comments page of the rust reference we can have nested block comments. For example:

fn main() {
let x = 0; /* start of outer comment
/* start of nested comment
 * end of nested comment
 */
* end of outer comment
*/
}

Out of curiosity I was wondering if this would also be aligned by this PR?

ytmimi avatar Aug 08 '22 01:08 ytmimi

No. the PR doesn't handle nested comments. The current code only check for * at the beginning of a (trimmed) line inside a comment, and adds one space before the *. Therefore this code is formatted as (note the the opening of the nested comment is not indented by a space since it does not start with *):

fn main() {
    let x = 0; /* start of outer comment
               /* start of nested comment
                * end of nested comment
                */
                * end of outer comment
                */
}

I assume that the desired formatting to indent the internal comment by two spaces instead of one, i.e.:

fn main() {
    let x = 0; /* start of outer comment
                /* start of nested comment
                 * end of nested comment
                 */
                * end of outer comment
                */
}

If this is correct, I can try to enhance the code to take the /* and */ into account.

davidBar-On avatar Aug 08 '22 19:08 davidBar-On

I assume that the desired formatting to indent the internal comment by two spaces instead of one

That sounds good, but It's tough to say because the Comment section of the style guide doesn't give any prescription for how to handle nested block comments.

I was just curiosity that made me ask if this PR also handled nested comments. I'd say nested comments are out of scope for the backport so we should ignore nested comments for now, and revisit them if anyone runs into an issue with nested comments in the future.

ytmimi avatar Aug 08 '22 20:08 ytmimi