Commit graph

483 commits

Author SHA1 Message Date
Yoshua Wuyts
1cf7af7032 wip enum record/tuple generation 2021-08-10 22:08:14 +02:00
Yoshua Wuyts
65ce87cad8 gen PartialEq for basic enums 2021-08-10 21:05:23 +02:00
Yoshua Wuyts
f8a64c044a gen PartialEq for structs 2021-08-10 19:49:06 +02:00
Yoshua Wuyts
3cad2c4feb add partial_eq gen tests 2021-08-10 19:48:50 +02:00
bors[bot]
e1dcec0e02
Merge #9846
9846: feat: Generate default trait fn impl when generating `Clone` r=Veykril a=yoshuawuyts

Implements a default trait function body when generating the `Clone` trait for a type. Thanks!

r? `@\veykril`

Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
2021-08-10 13:18:09 +00:00
Yoshua Wuyts
2f866974d9 gen clone for tuple enums 2021-08-10 15:07:13 +02:00
Yoshua Wuyts
e0adb39de3 gen clone for record enums 2021-08-10 14:43:20 +02:00
Lukas Wirth
7e6eb67f0d Substitute generic types in inline_call 2021-08-10 14:39:56 +02:00
Yoshua Wuyts
7ddc26aea1 add clone generation for structs and bare enums 2021-08-10 13:55:26 +02:00
Yoshua Wuyts
7d7a50daf7 Add clone generation tests 2021-08-10 13:20:24 +02:00
Lukas Wirth
b7d7dd6163 Implement bool_then_to_if assist 2021-08-10 13:17:45 +02:00
Yoshua Wuyts
4b5139e8a5 impl gen hash for structs 2021-08-10 12:37:10 +02:00
Yoshua Wuyts
ec2535e9ce impl gen hash for enums 2021-08-10 12:37:10 +02:00
Yoshua Wuyts
326890753c implement feedback from review 2021-08-10 12:21:48 +02:00
Yoshua Wuyts
d6b788a9ee Add trait codegen to add_missing_impl_members assist 2021-08-10 10:27:52 +02:00
Yoshua Wuyts
4d2e25a034 move trait body gen code to utils 2021-08-09 21:47:44 +02:00
bors[bot]
8e4fb4f518
Merge #9804
9804: Generate method from call r=matklad a=mahdi-frms

Needs a bit of refactoring. Tests also should be added.

Co-authored-by: mahdi-frms <mahdif1380@outlook.com>
2021-08-09 17:09:13 +00:00
bors[bot]
63427afb69
Merge #9825
9825: Generate default impl when converting #[derive(Default)] to manual impl r=Veykril a=yoshuawuyts

Similar to https://github.com/rust-analyzer/rust-analyzer/pull/9814, but for `#[derive(Default)]`. Thanks!

## Follow-up steps
I've added the tests inside `handlers/replace_derive_with_manual_impl.rs` again, but I'm planning a follow-up PR to extract these to `utils/` so we can share them between assists - and maybe even add another assist just for the purpose of testing these impls (e.g. `generate_default_trait_body`).

The step after _that_ is likely to fill out the remaining traits, so we can make it so whenever RA auto-completes a trait which also can be derived, we provide a default function body.


Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
2021-08-09 16:59:10 +00:00
mahdi-frms
05a789c09d refactor method generation assist 2021-08-09 21:06:24 +04:30
mahdi-frms
9217f6dcf7 method gen assist usable in all of expression 2021-08-09 21:06:24 +04:30
mahdi-frms
311dc5b04f add test for method generation assist 2021-08-09 21:06:24 +04:30
mahdi-frms
3c31f3831d One assist for function and method generation 2021-08-09 21:06:24 +04:30
mahdi-frms
02f5b5e0e2 method generation assist: store owned ast nodes 2021-08-09 21:06:24 +04:30
mahdi-frms
6240b2dae2 generate method adds pub keyword 2021-08-09 21:06:24 +04:30
mahdi-frms
d38f36e5af generate method assist uses existing impl blocks 2021-08-09 21:06:24 +04:30
mahdi-frms
99570f32d8 refactor: use single next space 2021-08-09 21:06:24 +04:30
mahdi-frms
b777e498fe refactor: use single fn_args 2021-08-09 21:06:24 +04:30
mahdi-frms
9ca73528ee generate method assist 2021-08-09 21:06:24 +04:30
Yoshua Wuyts
13749e782e move code around 2021-08-09 18:27:01 +02:00
Yoshua Wuyts
457eede795 Add gen default for tuple structs 2021-08-09 17:40:50 +02:00
Yoshua Wuyts
423860cd7e Add default body when implementing Default by hand 2021-08-09 17:12:18 +02:00
bors[bot]
caac771439
Merge #9735
9735: Add assist to sort members alphabetically. r=matklad a=vsrs

Supports traits, impls, structs, unions and enums (including inner struct variants). Does not support modules yet.

```rust
en┃um Animal {
  Dog(String, f64),
  Cat { weight: f64, name: String },
}
```
->
```rust
enum Animal {
  Cat { weight: f64, name: String },
  Dog(String, f64),
}
```
---
```rust
enum Animal {
  Dog(String, f64),
  Cat {┃ weight: f64, name: String },
}
```
->
```rust
enum Animal {
  Dog(String, f64),
  Cat { name: String, weight: f64 },
}
```
---
More samples in docs 0b7835619a/crates/ide_assists/src/handlers/sort_items.rs (L12-L83).

Relates #6110


Co-authored-by: vsrs <vit@conrlab.com>
2021-08-09 10:21:14 +00:00
bors[bot]
5664a2b0b3
Merge #9814
9814: Generate default impl when converting `#[derive(Debug)]` to manual impl r=yoshuawuyts a=yoshuawuyts

This patch makes it so when you convert `#[derive(Debug)]` to a manual impl, a default body is provided that's equivalent to the original output of `#[derive(Debug)]`. This should make it drastically easier to write custom `Debug` impls, especially when all you want to do is quickly omit a single field which is `!Debug`.

This is implemented for enums, record structs, tuple structs, empty structs - and it sets us up to implement variations on this in the future for other traits (like `PartialEq` and `Hash`).

Thanks!

## Codegen diff
This is the difference in codegen for record structs with this patch:
```diff
struct Foo {
    bar: String,
}

impl fmt::Debug for Foo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        todo!();
+        f.debug_struct("Foo").field("bar", &self.bar).finish()
    }
}
```

Co-authored-by: Irina Shestak <shestak.irina@gmail.com>
Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Co-authored-by: Yoshua Wuyts <yoshuawuyts+github@gmail.com>
2021-08-08 22:30:37 +00:00
Yoshua Wuyts
59cdb51ef3 Remove unwraps 2021-08-09 00:29:38 +02:00
Yoshua Wuyts
f64aacc0c1 Use minicore 2021-08-08 18:58:42 +02:00
Lukas Wirth
3b7c713af3 Implement if_to_bool_then assist 2021-08-08 17:56:34 +02:00
Yoshua Wuyts
dcbf385ffc use make::name_ref 2021-08-08 16:31:28 +02:00
Yoshua Wuyts
e26ba72333 Update replace_derive_with_manual_impl.rs 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
a2e5fc659d Improve naming and add comments 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
720508a2df dedup struct debug impl code 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
cc3ff1b486 Fix enum debug indent level 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
4b7ae9fedc generate Debug for enums 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
a1f2c7adcd rename variables
According to https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/style.md#variable-naming
2021-08-08 16:26:25 +02:00
Yoshua Wuyts
aa09141a8a finish debug_struct impls 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
fd7236c791 debug for record field structs 2021-08-08 16:26:25 +02:00
Yoshua Wuyts
e2ab2e12a0 wip 2021-08-08 16:24:54 +02:00
Irina Shestak
f2e547d587 impl Debug def from trait 2021-08-08 16:24:54 +02:00
Lukas Wirth
c4a119f433 Simplify 2021-08-07 22:16:15 +02:00
Lukas Wirth
b6d574642d extract_type_alias extracts generics correctly 2021-08-05 02:54:06 +02:00
Lukas Wirth
01413dd7d4 extract_function does not move locals defined outside of loops 2021-08-04 20:00:45 +02:00