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

[Feature Request] Add mechanism to skip formatting specific comments #5727

Open
ytmimi opened this issue Mar 28, 2023 · 4 comments
Open

[Feature Request] Add mechanism to skip formatting specific comments #5727

ytmimi opened this issue Mar 28, 2023 · 4 comments

Comments

@ytmimi
Copy link
Contributor

ytmimi commented Mar 28, 2023

rustfmt usually leaves comments alone, but will edit comments when wrap_comments=true is enabled, Additionally rustfmt sometimes removes leading / trailing spaces. However, in some cases its desirable to prevent modifications for specific comments. The #[rustfmt::skip] attribute exists to prevent rustfmt from modifying annotated code, and although this will sometimes prevent comments from being modified it's not ideal because it also prevents the code from being formatted.

Related Issues where modifying comments isn't desirable:

If anyone has ideas for how this mechanism could work please comment them down bellow.

@calebcartwright
Copy link
Member

The only way I can think of for supporting this is something along the lines of what I mentioned in #5440 (comment)

Basically, we establish some kind of marker/keyword/magic phrase/whatever that rustfmt will recognize and support, and if found in the supported position (or positions, not sure) then rustfmt will do nothing.

I suggested rustfmt::avert-your-eyes somewhat cheekily, but the important part of the idea is the notion of a marker (e.g. similar to // @generated which we already support for different purposes) and can bikeshed on the actual ident of the marker later

@ytmimi
Copy link
Contributor Author

ytmimi commented Jun 19, 2023

My thoughts, for doc comments at least, is that the marker should be an HTML comment so it won't mess with rustdoc output.

We could define something like this:

const SKIP_DOC_COMMENT_MARKER: &'static str = "<!--- rustfmt::skip --->"

There's probably more scenarios that we'd need to think through, but I also wonder if we'd need to handle the case where users want to prevent rustfmt from modifying their markdown, but also want to continue formatting their code blocks. The way I've defined it in my proof of concept branch is to skip the doc comment entirely if the marker is present.

@calebcartwright
Copy link
Member

I don't have an opinion or preference one way or the other about the specifics of the marker approach. As long as it works and is practical in any and all comment types I'd be content

@ytmimi
Copy link
Contributor Author

ytmimi commented Jun 19, 2023

I'll think on this a little more. I like the idea of keeping with rustfmt::skip as the marker for consistency with attribute skips. If we eventually added a markdown parser for doc comments then the marker could still be rustfmt::skip, but we'd just check for an HTML comment with rustfmt::skip.

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

No branches or pull requests

2 participants