rust/crates
bors[bot] 5e39d7a680
Merge #7617
7617: Add getter/setter assists r=Veykril a=yoshuawuyts

This patch makes progress towards the design outlined in https://github.com/rust-analyzer/rust-analyzer/issues/5943, and includes a small refactor which closes https://github.com/rust-analyzer/rust-analyzer/issues/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 https://github.com/rust-analyzer/rust-analyzer/pull/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 <yoshuawuyts+github@gmail.com>
2021-02-10 10:32:36 +00:00
..
assists Merge #7617 2021-02-10 10:32:36 +00:00
base_db Fix warnings when running cargo doc --document-private-items 2021-01-18 16:44:40 -05:00
cfg
completion Show qualified variant pattern completions 2021-02-09 21:35:02 +01:00
flycheck Make logger-based debugging more pleasant 2021-01-28 17:07:53 +03:00
hir Resolve TupleStructPat in SourceAnalyzer::resolve_path 2021-02-10 09:37:22 +01:00
hir_def Remove unneeded return 2021-02-09 18:40:05 +01:00
hir_expand add more counts 2021-01-27 12:39:19 +03:00
hir_ty Infra for "unit" benchmarking 2021-02-09 20:25:39 +03:00
ide Resolve TupleStructPat in SourceAnalyzer::resolve_path 2021-02-10 09:37:22 +01:00
ide_db Make ModPath's representation private 2021-02-04 20:49:24 +01:00
mbe Make sure normal dependencies always have version 2021-02-03 12:51:07 +00:00
parser Add validation for mutable const items 2021-01-24 02:17:41 +01:00
paths Document paths items 2021-01-22 15:38:33 +01:00
proc_macro_api Remove redundant clones 2021-02-05 16:57:26 +01:00
proc_macro_srv Use non-deprecated memmap2 crate 2021-02-02 10:25:17 -05:00
proc_macro_test
profile add more counts 2021-01-27 12:39:19 +03:00
project_model Async Loading outdir and proc-macro 2021-01-29 01:04:14 +08:00
rust-analyzer Consolidate fn load_cargo(…) parameters into struct LoadCargoConfig { … } 2021-02-08 11:30:16 +01:00
ssr . 2021-01-20 01:56:11 +03:00
stdx Cleanup decl_check 2021-02-05 16:09:45 +01:00
syntax Add parsing benchmark 2021-02-09 21:52:34 +03:00
test_utils Add parsing benchmark 2021-02-09 21:52:34 +03:00
text_edit
toolchain
tt
vfs Document vfs private items 2021-01-12 18:01:47 +01:00
vfs-notify