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

/// # #![rustfmt::skip] behaves weird #6098

Open
max-ishere opened this issue Feb 27, 2024 · 7 comments
Open

/// # #![rustfmt::skip] behaves weird #6098

max-ishere opened this issue Feb 27, 2024 · 7 comments
Labels
a-comments a-rustfmt::skip only-with-option requires a non-default option value to reproduce

Comments

@max-ishere
Copy link

I just wanted to keep specific formatting in my list. However it wasn't so easy...

skip visible in the HTML

Source code

//! ```rust
//! #![rustfmt::skip]
//! let _ = [
//!     1, 2, 3,
//!     4, 5,
//! ];
//! ```

Rendered doc

#![rustfmt::skip]
let _ = [
    1, 2, 3,
    4, 5,
];

This works, but I want to hide the formatting attribute

Hiding the skip

Before save:

//! ```rust
//! # #![rustfmt::skip]
//! let _ = [
//!     1, 2, 3,
//!     4, 5,
//! ];
//! ```

On save the list is formatted:

//! ```rust
//! # #![rustfmt::skip]
//! let _ = [1, 2, 3, 4, 5];
//! ```

Another way to hide skip with a block

Source code

//! ```rust
//! # #![rustfmt::skip] {
//! let _ = [
//!     1, 2, 3,
//!     4, 5
//! ];
//! }
//! ```

No difference on save, but its a new scope so now this variable is not avaliable to unformatted code.

Rendered HTML

let _ = [
    1, 2, 3,
    4, 5
];
}

Contains trailing }. If you hide the closing brace rustfmt will format the list.

Using nofmt::pls!{}

Source code

//! ```rust
//! let _ = nofmt::pls! {[
//!     1, 2, 3,
//!     4, 5
//! ]};
//! ```

Rendered doc

let _ = nofmt::pls! {[
    1, 2, 3,
    4, 5
]};

Hiding nofmt::pls!{}

Source code

//! ```rust
//! let _ =
//! # nofmt::pls! {
//! [
//!     1, 2, 3,
//!     4, 5
//! ];
//! # }
//! ```

No change on save.

Rendered doc

let _ =
[
    1, 2, 3,
    4, 5
];

But this just looks weird, I want to try to put the open bracket on the same line as =. I don't want the doc to look completely unformatted, just a little bit in a subtle way so that my list is more readable.

Making nofmt::pls!{} more hidden

Source code

//! ```rust
//! let _ = [
//! # nofmt::pls! {
//!     1, 2, 3,
//!     4, 5
//! # }
//! ];
//! ```

On save:

//! ```rust
//! let _ = [
//! # nofmt::pls! {
//!     1, 2, 3, 4, 5, //#### # }
//! ];
//! ```

Ouch... I think this is realted to #6025

Versions

rustfmt 1.7.0-nightly (3246e79 2024-02-19)
cargo 1.78.0-nightly (7b7af3077 2024-02-17
@max-ishere max-ishere changed the title /// # #![rustfmt::skipp] behaves weird /// # #![rustfmt::skip] behaves weird Feb 27, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Mar 4, 2024

@max-ishere Thanks for reaching out. Currently there's no good way to skip formatting in a code block when using format_code_in_doc_comments I gave a brief explanation about what rustfmt is looking for in order to determine if it should reformat code in doc comments in #5623 (comment). Maybe some of that advice will be useful.

Writing //! # #![rustfmt::skip] to hide the skip attribute is unfortunately not going to work. Rustfmt needs to hack around any lines that start with //! # since they don't parse as valid rust code so internally rustfmt replaces them with a comment before formatting so the #![rustfmt::skip] attribute is never actually seen by rustfmt.

for now I'll link the tracking issue #3348

@ytmimi ytmimi added a-comments only-with-option requires a non-default option value to reproduce a-rustfmt::skip labels Mar 4, 2024
@max-ishere
Copy link
Author

For now I will leave the macro in, I think it may be the easiest solution for a list. I don't actually want to disable all formatting, just in the list so the ```ignore won't work. Also it's a doctest so it would have to be one of the rust code attrs anyway.

@ytmimi
Copy link
Contributor

ytmimi commented Mar 5, 2024

I think a more complete solution would involve working with the rustdoc team to add an attribute that both tools understand as "rustfmt should skip this, but rustdoc should not".

@max-ishere
Copy link
Author

@ytmimi not following with your suggestion...

@max-ishere
Copy link
Author

Either way I hope the usecase is clear, mark something as do not format, but hide that marker in the docs. At the moment the nofmt macro is an ok alternative.

@max-ishere
Copy link
Author

max-ishere commented Mar 12, 2024

I think a more complete solution would involve working with the rustdoc team to add an attribute that both tools understand as "rustfmt should skip this, but rustdoc should not".

Oh i see, like ```nofmt. That wont solve my usecase, but could work I guess.

@max-ishere
Copy link
Author

And to add to my previous comment, it has to be something you can enable for just one line, because all doc comments are formatted by rustfmt in my project and have some rules like line length 80 I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments a-rustfmt::skip only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants