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

Rustdoc should support inline or same-line doc comments for enums #3306

Open
bvargin-dev opened this issue Aug 25, 2022 · 3 comments
Open

Rustdoc should support inline or same-line doc comments for enums #3306

bvargin-dev opened this issue Aug 25, 2022 · 3 comments

Comments

@bvargin-dev
Copy link

Hi,

As suggested in an old and closed but still active discussion, I open another thread because it looks interesting to have the capability to have inline comments.
My use case concerns a CLI which should parse the arguments, so in order to not reinvent the wheel I have tried with Clap .

Clap looks to use the rust documentation to display the sub-command comments.
Unfortunately, it is not possible today to align the action with the comment.
Indeed it becomes more readable when the comment is on the same line (like it is displayed with the sub-command "help").

_// Here an example with sub-commands from GIT_
enum Action {
    Clone,  //< Display file's information rights (size,rights).
    Init,   //< Create an empty Git repository or reinitialize an existing one
    Add     //< Add file contents to the index
}

For this use case, the comment should be "short and pithy" but it does not look to be an anti pattern as discussed in this thread.

Thanks in advance for your feedback and the work you're realizing.

@ThomasMontanoGames
Copy link

This would be especially useful for embedded libraries that have a large amount of register definitions. Readability would be significantly improved if the doc comments could be inlined.

@kennytm
Copy link
Member

kennytm commented Aug 21, 2024

Unfortunately, it is not possible today to align the action with the comment.

Note that Rust Style Guide specifically avoids visual-alignment (rust-lang/style-team#8, rust-lang/rustfmt#4108) because it is not diff-friendly (https://doc.rust-lang.org/stable/style-guide/principles.html). So your example if properly formatted (i.e. after 4108 is fixed) should look like

enum Action {
    Clone, //< Display file's information rights (size,rights).
    Init, //< Create an empty Git repository or reinitialize an existing one
    Add, //< Add file contents to the index
}

//<

Using a new syntax //< is going to break plenty of existing use of the same sequence that are not intended to be a doc-comment (in particular those, for whatever reason, embeds XML into the comment like //<!-- why -->). Sure you could require a new edition, but #2374's //! does not even need to break compatibility.

For this use case,

In practice when you #[derive(clap::*)] you don't just write the doc comment you also bring in other attributes for precise control such as:

#[derive(Subcommand)]
enum Action {
    #[command(arg_required_else_help = true)]
    Clone { //< Clones repos
        remote: String, //< The remote to clone
    },

    #[command(arg_required_else_help = true)]
    Add { //< adds things
        #[arg(required = true)]
        path: Vec<PathBuf>, //< Stuff to add
    },
}

Considering this the clap usage does not look particular easier to read compared with the existing /// style as those #[arg] requires breaking the line anyway.

@SOF3
Copy link

SOF3 commented Oct 4, 2024

@ThomasMontanoGames If it is really so bad for you, you could always do /** Xxx */ Xxx,. Of course rustfmt will still move the attribute to the line above and this is less readable, but this is the current alternative if you insist that it has very significant impact.

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

No branches or pull requests

4 participants