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

Add doc gen to the generate_enum_match_method assist #7570

Merged
merged 1 commit into from
Feb 5, 2021

Conversation

yoshuawuyts
Copy link
Member

Implements a small extension to #7562, generating default comments. I wasn't sure if this would fit the goals of Rust-Analyzer, so I chose to split it into a separate PR. This is especially useful when writing code in a codebase which uses #![warn(missing_docs)] lint, as many production-grade libraries do.

The comments we're generating here are similar to the ones found on Option::is_some and Result::is_err. I briefly considered only generating these for pub types, but they seem small and unobtrusive enough that they're probably useful in the general case. Thanks!

Example

input

pub(crate) enum Variant {
    Undefined,
    Minor, // cursor here
    Major,
}

output

pub(crate) enum Variant {
    Undefined,
    Minor,
    Major,
}

impl Variant {
    /// Returns `true` if the variant is [`Minor`].
    pub(crate) fn is_minor(&self) -> bool {
        matches!(self, Self::Minor)
    }
}

Future Directions

This opens up the path to adding an assist for generating these comments on existing is_ methods. This would make it both easy to document new code, and update existing code with documentation.

@yoshuawuyts yoshuawuyts force-pushed the enum-matches-default-comments branch from d77c0f5 to d90bd63 Compare February 5, 2021 15:00
@matklad
Copy link
Member

matklad commented Feb 5, 2021

bors d+

Yeah, it's unclear if we want this long term, and in which shape exactly. But this indeed seems unobtrusive, and we wouldn't know if we didn't try, so I am onboard.

@bors
Copy link
Contributor

bors bot commented Feb 5, 2021

✌️ yoshuawuyts can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@yoshuawuyts
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 5, 2021

@bors bors bot merged commit 855b00c into rust-lang:master Feb 5, 2021
@yoshuawuyts yoshuawuyts deleted the enum-matches-default-comments branch February 5, 2021 15:32
bors bot added a commit that referenced this pull request Feb 10, 2021
7617: Add getter/setter assists r=Veykril a=yoshuawuyts

This patch makes progress towards the design outlined in #5943, and includes a small refactor which closes #7607. All together this patch does 4 things:

- Adds a `generate_getter` assist.
- Adds a `generate_getter_mut` assist.
- Adds a `generate_setter` assist.
- Moves the `generate_impl_text` function from `generate_new` into `utils` (which closes #7607).

## Design Notes

I've chosen to follow the [Rust API guidelines on getters](https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter) as closely as possible. This deliberately leaves "builder pattern"-style setters out of scope.

Also, similar to #7570 this assist generates doc comments. I think this should work well in most cases, and for the few where it doesn't it's probably easily edited. This makes it slightly less correct than the #7570 implementation, but I think this is still useful enough to include for many of the same reasons.

The reason why this PR contains 3 assists, rather than 1, is because each of them is so similar to the others that it felt more noisy to do them separately than all at once. The amount of code added does not necessarily reflect that, but hope that still makes sense.

## Examples

**Input**
```rust
struct Person {
    name: String,     // <- cursor on "name"
}
```

**generate getter**
```rust
struct Person {
    name: String,
}

impl Person {
    /// Get a reference to the person's name.
    fn name(&self) -> &String {
        &self.name
    }
}
```

**generate mut getter**
```rust
struct Person {
    name: String,
}

impl Person {
    /// Get a mutable reference to the person's name.
    fn name_mut(&mut self) -> &mut String {
        &mut self.name
    }
}
```

**generate setter**
```rust
struct Person {
    name: String,
}

impl Person {
    /// Set the person's name.
    fn set_name(&mut self, name: String) {
        self.name = name;
    }
}
```


Co-authored-by: Yoshua Wuyts <[email protected]>
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

Successfully merging this pull request may close these issues.

2 participants