Commit Graph

306 Commits

Author SHA1 Message Date
rami3l
cfa914958c refactor: use generate_trait_impl_text_intransitive for From-like traits 2022-12-17 22:59:30 +08:00
rami3l
12b05d2416 fix: add generic TypeBoundList in generated derivable impl 2022-12-14 19:18:05 +08:00
Maybe Waffle
7ed0871ff6 Fix "needs parens" check in remove_parentheses assist 2022-12-13 00:06:00 +00:00
bors
4596847a88 Auto merge of #13746 - feniljain:fix_extract_function, r=jonas-schievink
fix: make make_body respect comments in extract_function

Possible fix for #13621

### Points to help in review:

- Earlier we were only considering statements in a block expr and hence comments were being ignored, now we handle tokens hence making it aware of comments and then preserving them using `hacky_block_expr_with_comments`

Seems like I am not able to attach output video, github is glitching for it :(
2022-12-12 14:51:03 +00:00
bors
e7dff7491a Auto merge of #13726 - feniljain:fix_assists, r=jonas-schievink
feat: allow unwrap block in let initializers

Possible fix for #13679

### Points to help in review:

- I just added a parent case for let statements and it seems everything else was in place already, so turned out to be a small fix
2022-12-12 14:06:45 +00:00
bors
3a7215b92e Auto merge of #13732 - rami3l:fix/gen-partial-eq, r=jonas-schievink
fix: add fallback case in generated `PartialEq` impl

Partially fixes #13727.

When generating `PartialEq` implementations for enums, the original code can already generate the following fallback case:

```rs
_ => std::mem::discriminant(self) == std::mem::discriminant(other),
```

However, it has been suppressed in the following example for no good reason:

```rs
enum Either<T, U> {
    Left(T),
    Right(U),
}

impl<T, U> PartialEq for Either<T, U> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Left(l0), Self::Left(r0)) => l0 == r0,
            (Self::Right(l0), Self::Right(r0)) => l0 == r0,
            // _ => std::mem::discriminant(self) == std::mem::discriminant(other),
            // ^ this completes the match arms!
        }
    }
}
```

This PR has removed that suppression logic.

~~Of course, the PR could have suppressed the fallback case generation for single-variant enums instead, but I believe that this case is quite rare and should be caught by `#[warn(unreachable_patterns)]` anyway.~~

After this fix, when the enum has >1 variants, the following fallback arm will be generated :

* `_ => false,` if we've already gone through every case where the variants of `self` and `other` match;
* The original one (as stated above) in other cases.

---

Note: The code example is still wrong after the fix due to incorrect trait bounds.
2022-12-12 13:52:49 +00:00
feniljain
d7183fb5d0 fix: make make_body respect comments in extract_function 2022-12-09 18:30:30 +05:30
Maybe Waffle
ba6f0befc8 Simplify remove_parentheses's implementation 2022-12-08 18:54:08 +00:00
Maybe Waffle
8d42439a7d Move precedence handling to crates/syntax 2022-12-08 18:46:30 +00:00
Maybe Waffle
2870b01ec0 Explicitly say that the assist removes *redundant* parentheses 2022-12-08 18:22:57 +00:00
rami3l
57fb18e3bd fix: refine fallback case in generated PartialEq impl 2022-12-07 10:25:17 +08:00
Maybe Waffle
ab061945a1 Consider expression precedense in remove_parentheses assist 2022-12-06 19:11:24 +00:00
Maybe Waffle
5f79279b48 Add remove_parentheses assist 2022-12-06 16:18:25 +00:00
rami3l
fed74c8b71 fix: add fallback case in generated PartialEq impl 2022-12-06 21:54:53 +08:00
feniljain
4794572e36 feat: allow unwrap block in let initializers 2022-12-05 09:00:16 +05:30
Ryo Yoshida
6d4538734e
Add move_const_to_impl assist 2022-12-03 01:22:00 +09:00
Ryo Yoshida
8e03f18e37
fix: check if range contains tail expression 2022-11-27 00:31:02 +09:00
Ryo Yoshida
822c61f559
refactor: remove unnecessary stuff 2022-11-27 00:01:16 +09:00
bors
e668eca632 Auto merge of #13647 - nyz93:fix/tuple-to-named-struct, r=Veykril
fix: tuple to named struct inside macros

seems to fix #13634
2022-11-25 09:41:21 +00:00
bors
5e3ad5ddf6 Auto merge of #13592 - MihailMihov:trait_impl_assist, r=Veykril
Add assist to generate trait impl's

resolves #13553

This pull request adds a `generate_trait_impl` assist, which generates trait impl's for a type. It is almost the same as the one to generate impl's and I also reduced the trigger range to only outside the `RecordFieldList`. Also moved all the tests into separate test functions. A few of the old tests seemed redundant, so I didn't port them.
2022-11-24 20:44:33 +00:00
bors
63a676eedf Auto merge of #13576 - Bben01:supress_missing_impl_inside_block, r=jonas-schievink
Suppress "Implement default members" inside contained items

Fixes #13561
2022-11-24 15:16:36 +00:00
Mihail Mihov
469f620b06 Combine generate_impl and generate_trait_impl into a single file 2022-11-21 22:58:43 +02:00
Mihail Mihov
0bd11f8171 Reduce trigger range of generate_impl assist and update tests 2022-11-21 22:27:26 +02:00
Mihail Mihov
ecb15ca717 Add assist to generate trait impl's 2022-11-21 22:27:26 +02:00
Bben01
95b4a7487b Suppress "Implement default members" inside contained items 2022-11-21 22:17:04 +02:00
Nyikos Zoltán
23cfe0702d fix: tuple to named struct inside macros
seems to fix #13634
2022-11-19 20:08:01 +01:00
bors
2d9ed4fd48 Auto merge of #13641 - DesmondWillowbrook:fix-move-format-string, r=Veykril
fix: format expression parsing edge-cases

- Handle positional arguments with formatting options (i.e. `{:b}`). Previously copied `:b` as an argument, producing broken code.

- Handle indexed positional arguments (`{0}`) ([reference](https://doc.rust-lang.org/std/fmt/#positional-parameters)). Previously copied over `0` as an argument.

Note: the assist also breaks when named arguments are used (`"{name}$0", name = 2 + 2` is converted to `"{}"$0, name`. I'm working on fix for that as well.
2022-11-19 12:46:29 +00:00
bvanjoi
a4f071afd5 fix(assists): remove item_const which had default value when implement missing members` 2022-11-19 19:38:53 +08:00
Kartavya Vashishtha
a3f8fd71df
fix: format expression parsing edge-cases
handle positional arg with formatting

handle indexed positional args
2022-11-19 15:00:25 +05:30
Jonas Schievink
cd6459e7b3 Make "Remove dbg!()" assist work on selections 2022-11-17 17:39:31 +01:00
Ryo Yoshida
19306c070d
Fix tests that depended on loose visibility restriction 2022-11-11 20:31:46 +09:00
Ryo Yoshida
e75afebeb2
Resolve invisible defs in fix_visibility assist 2022-11-11 20:31:44 +09:00
Lukas Wirth
ffd7bf8bf9 Bump Cargo rust-version fields to latest stable 2022-11-07 12:59:51 +01:00
bors
d3d3806565 Auto merge of #12991 - TiddoLangerak:extract-method-from-trait-into-impl-root, r=Veykril
Feat: extracted method from trait impl is placed in existing impl

**Before**

https://user-images.githubusercontent.com/1759192/183872883-3b0eafd2-d1dc-440e-9e66-38e3372f8b64.mp4

**After**

https://user-images.githubusercontent.com/1759192/183875769-87f34c7d-52f0-4dfc-9766-f591ee738ebb.mp4

Previously, when triggering a method extraction from within an impl trait block, then this would always create a new impl block for
the struct, even if there already is one. Now, if there is already an existing trait-less impl block, then it'll put the extracted method in there.

**Caveats**:
- It currently requires the target impl block to be non-empty. This limitation is because the current architecture takes a `node_to_insert_after` as reference for where to insert the extracted function. An empty impl block doesn't have such a reference node, since it's empty. It seems that supporting this requires a much larger and more complex change.
- This is my first contribution in rust, so apologies for any beginner mistakes.
2022-11-07 11:07:12 +00:00
Lukas Wirth
f24fbc2027 rustfmt 2022-11-07 11:58:57 +01:00
bors
25b1d6f3f9 Auto merge of #13435 - DropDemBits:assists-format-args-capture-pt3, r=Veykril
Migrate assists to format args captures, part 3

Continuation of https://github.com/rust-lang/rust-analyzer/pull/13379

Migrates:

- `inline_call`
- `inline_local_variable`
- `introduce_named_lifetime`
- `merge_match_arms`
- `move_from_mod_rs`
- `move_guard`
- `move_module_to_file`
- `move_to_mod_rs`
- `number_representation`
- `qualify_method_call`
- `qualify_path`
- `raw_string`
- `remove_dbg`
- `replace_derive_with_manual_impl`
- `replace_or_with_or_else`
- `replace_turbofish_with_explicit_type`
- `unwrap_tuple`
- `wrap_return_type_in_result`
2022-11-05 12:41:23 +00:00
bors
afe8f6b922 Auto merge of #13379 - DropDemBits:ide-assists-format-args-capture, r=Veykril
internal: Migrate `ide_assists::utils` and `ide_assists::handlers` to use format arg captures (part 1)

This not only serves as making future migration to mutable syntax trees easier, it also finds out what needs to be migrated in the first place.

~~Aside from the first commit, subsequent commits are structured to only deal with one file/handler at a time.~~

This is the first of 3 PRs, migrating:

Utils:

- `gen_trait_fn_body`
- `render_snippet`
- `ReferenceConversion`
  - `convert_type`
  - `getter`

Handlers:

- `add_explicit_type`
- `add_return_type`
- `add_turbo_fish`
- `apply_demorgan`
- `auto_import`
- `convert_comment_block`
- `convert_integer_literal`
- `convert_into_to_from`
- `convert_iter_for_each_to_for`
- `convert_let_else_to_match`
- `convert_tuple_struct_to_named_struct`
- `convert_two_arm_bool_match_to_matches_macro`
- `destructure_tuple_binding`
- `extract_function`
- `extract_module`
- `extract_struct_from_enum_variant`
- `extract_type_alias`
- `extract_variable`
- `fix_visibility`
2022-11-05 12:29:06 +00:00
bors
df3877037e Auto merge of #13454 - justinmmott:master, r=flodiebold
Fixed local shadowing the caller's argument issue

fix https://github.com/rust-lang/rust-analyzer/issues/12536
2022-11-05 10:05:52 +00:00
bors
6c3ab563de Auto merge of #13527 - unexge:use-let-else-stmt-in-convert-to-guarded-return-assist, r=jonas-schievink
Use let-else statements in `Convert to guarded return` assist

Follow up for https://github.com/rust-lang/rust-analyzer/pull/13516, addresses remaining part of https://github.com/rust-lang/rust-analyzer/issues/13254#issuecomment-1250408527
2022-11-02 11:06:54 +00:00
bors
af1f48deab Auto merge of #13359 - feniljain:feat-must-use-option, r=Veykril
feat: add config for inserting must_use in `generate_enum_as_method`

Should fix #13312

Didn't add a test because I was not sure on how to add test for a specific configuration option, tried to look for the usages for other `AssistConfig` variants but couldn't find any in `tests`. If there is a way to test this, do point me towards it.

I tried to extract the formatting string as a common `template_string` and only have if-else for that, but it didn't compile :(

Also it seems these tests are failing:

```
test config::tests::generate_config_documentation ... FAILED
test config::tests::generate_package_json_config ... FAILED
```

Can you also point me to how to correct these 😅  ( I guess there is some command to automatically generate these? )
2022-11-02 10:50:08 +00:00
feniljain
691ce306df
fix: indentation after inserting #must_use
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2022-11-02 16:09:12 +05:30
unexge
62a6cdfe46 Use let-else statements in Convert to guarded return assist 2022-11-01 23:02:10 +00:00
bors
a8e97bcf3c Auto merge of #13508 - koka831:fix/13492, r=jonas-schievink
fix: async trait method for `unnecessary_async`

Fix https://github.com/rust-lang/rust-analyzer/issues/13492
2022-11-01 16:38:40 +00:00
Jonas Schievink
72d5b456e1 Fix doc test 2022-11-01 17:23:32 +01:00
Jonas Schievink
9f1bb17a1b Import option in the tests 2022-11-01 17:18:13 +01:00
unexge
f0a14346ee Update auto generated tests 2022-10-30 00:00:53 +01:00
unexge
48efc9d303 Add Convert match to let-else assist 2022-10-29 23:45:13 +01:00
koka
cf90e4f32b
Simplify the procedure
fix: remove unused import
2022-10-30 00:59:20 +09:00
koka
4a7f5cac9d
fix: async trait method for unnecessary_async 2022-10-29 01:14:44 +09:00
feniljain
4bf9b9b003 refactor: remove repetitive string interpolation and doc changes 2022-10-24 21:12:31 +05:30
feniljain
c4bdb8e516 feat: add config for inserting must_use in generate_enum_as_method 2022-10-24 13:18:24 +05:30
Cameron
3dd2f99c49 fix tests 2022-10-22 06:23:23 +01:00
Cameron
5b56d7bd90 remove duplicate mod definition 2022-10-22 05:43:57 +01:00
Cameron
9ea57f19be add wrapping/checked/saturating assist 2022-10-22 05:41:28 +01:00
Justin Mott
e4ef0e5df9 addressed https://github.com/rust-lang/rust-analyzer/issues/12536 2022-10-21 13:28:59 -04:00
feniljain
5bff6c55de feat: add multiple getters mode in generate_getter
This commit adds two modes to generate_getter action.
First, the plain old working on single fields.
Second, working on a selected range of fields.
2022-10-20 16:47:23 +05:30
bors
a2e4f783a2 Auto merge of #13399 - DropDemBits:assists-format-args-capture-pt2, r=Veykril
Migrate assists to format args captures, part 2

Continuation of #13379

Migrates:

- `generate_constant`
- `generate_default_from_enum_variant`
- `generate_default_from_new`
- `generate_delegate_methods`
- `generate_deref`
- `generate_documentation_template`
- `generate_enum_is_method`
- `generate_enum_projection_method`
- `generate_from_impl_for_enum`
- `generate_function`
- `generate_getter`
- `generate_impl`
- `generate_new`
- `generate_setter`
2022-10-17 09:54:07 +00:00
Volkan Sagcan
dd4d3f0a09 fix #13105: Ignore auto-import assist on parameter names 2022-10-15 18:00:32 +02:00
bors
855cd5c280 Auto merge of #13418 - lnicola:bump-deps, r=lnicola
Bump deps
2022-10-15 11:27:12 +00:00
Laurențiu Nicola
cbce0cda08 Bump anyhow, arbitrary, itertools, semver, serde 2022-10-15 12:52:34 +03:00
DropDemBits
b1909a80af Migrate assists to format args captures, part 3 2022-10-12 02:28:41 -04:00
DropDemBits
d7fb8d51d6 Migrate assists to format args captures, part 2 2022-10-12 02:23:35 -04:00
DropDemBits
d439fb2bc8 Migrate assists to format args captures, part 1 2022-10-10 15:56:21 -04:00
DropDemBits
0f46f2773a Migrate most of ide_assists::utils to format arg capture 2022-10-10 10:47:54 -04:00
bors
3a69435af7 Auto merge of #13333 - feniljain:fix_assists, r=Veykril
fix(generate_method): correct method indentation inside generated impl and change gen loc

should fix #10619
2022-10-10 08:36:18 +00:00
bors
dcf1d71bb8 Auto merge of #13303 - jplatte:convert-named-struct-to-tuple-struct, r=Veykril
Add convert_named_struct_to_tuple_struct assist

Closes #11643, since the assist for converting in the other direction is already there (I based most of the implementation and all of the tests on it).
2022-10-10 08:24:37 +00:00
DropDemBits
bfe6ec9b77 Add {TypeParam, ConstParam}::remove_default
Also includes a drive-by refactor of `utils::generate_impl_text_inner`,
since that's what drove this change
2022-10-09 18:46:31 -04:00
DropDemBits
75f641799e Add GenericParamList::to_generic_args 2022-10-09 18:44:23 -04:00
bors
5b7e40014e Auto merge of #13321 - Veykril:format-str-args, r=Veykril
Fix move_format_string_arg being tokentree unaware

Fixes https://github.com/rust-lang/rust-analyzer/issues/13261
2022-09-30 22:42:40 +00:00
Lukas Wirth
3ad0334718 Fix move_format_string_arg being tokentree unaware 2022-10-01 00:42:16 +02:00
Jonas Platte
a3d79b5172
Update outside test and generated code 2022-09-27 20:40:51 +02:00
Jonas Platte
2c666a08b0
Add convert_named_struct_to_tuple_struct assist 2022-09-27 20:39:48 +02:00
bors
ad752bd521 Auto merge of #13301 - Veykril:empty-assist-source-changes, r=Veykril
Make assist tests panic again on empty source changes
2022-09-27 15:54:54 +00:00
Lukas Wirth
f5fe6b157f Make assist tests panic again on empty source changes 2022-09-27 17:48:00 +02:00
harudagondi
c2dc32c48e return None instead of assert 2022-09-21 09:11:02 +08:00
harudagondi
ed0cf1c5fa Add functionality to unwrap tuple declarations 2022-09-17 15:57:45 +08:00
feniljain
37ff07e1ff fix(generate_module): generate new impl near its ADT 2022-09-15 19:33:19 +05:30
feniljain
d0f2db3bf4 fix(generate_method): correct method indentation inside generated impl 2022-09-15 17:22:44 +05:30
bors
125d43cb2c Auto merge of #13227 - Veykril:core-pref, r=Veykril
Restructure `find_path` into a separate functions for modules and non-module items

Follow up to https://github.com/rust-lang/rust-analyzer/pull/13212
Also renames `prefer_core` imports config to `prefer_no_std` and changes the behavior of no_std path searching by preferring `core` paths `over` alloc

This PR turned into a slight rewrite, so it unfortunately does a few more things that I initially planned to (including a bug fix for enum variant paths)
2022-09-13 13:16:57 +00:00
Lukas Wirth
a8ecaa1979 Restructure find_path into a separate functions for modules and non-module items
Also renames `prefer_core` imports config to `prefer_no_std` and changes the behavior of no_std path searching by preferring `core` paths `over` alloc
2022-09-13 15:15:27 +02:00
bors
f64c95600c Auto merge of #13216 - DesmondWillowbrook:move_format_string_arg, r=DesmondWillowbrook
New assist: move_format_string_arg

The name might need some improving.

```rust
fn main() {
    print!("{x + 1}");
}
```
to
```rust
fn main() {
    print!("{}"$0, x + 1);
}
```

fixes #13180

ref to #5988 for similar work

* extracted `format_like`'s parser to it's own module in `ide-db`
* reworked the parser's API to be more direct
* added assist to extract expressions in format args
2022-09-12 15:50:42 +00:00
Kartavya Vashishtha
54e9324e93
Update crates/ide-assists/src/handlers/move_format_string_arg.rs
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2022-09-12 05:45:11 -07:00
Kartavya Vashishtha
fb5ae9906b
suggest ExtractRefactor if no expressions found
Added `Ident` variant to arg enum.
2022-09-11 10:39:25 +05:30
Kartavya Vashishtha
a5cbee4d11
remove false positive 2022-09-10 21:04:25 +05:30
Kartavya Vashishtha
cc7200891b
new lint: move_format_string_arg
The name might need some improving.

extract format_like's parser to it's own module in ide-db

reworked the parser's API to be more direct

added assist to extract expressions in format args
2022-09-10 20:13:46 +05:30
Kartavya Vashishtha
2584d48508
wip 2022-09-10 20:13:46 +05:30
Lukas Wirth
7d19971666 Add config to unconditionally prefer core imports over std
Fixes https://github.com/rust-lang/rust-analyzer/issues/12979
2022-09-09 20:04:56 +02:00
ice1000
364d9c4910 Fmt 2022-09-02 21:18:36 +00:00
ice1000
68eabf1bf1 Fix test 2022-09-02 21:18:36 +00:00
ice1000
a695e900f6 Create trait Removable, replace ted APIs with builder APIs 2022-09-02 21:18:36 +00:00
ice1000
37e20decad Address comments 2022-09-02 21:18:36 +00:00
ice1000
277df02ff5 This should work, but I got mysterious errors 2022-09-02 21:18:36 +00:00
ice1000
79e5c366cd Extract shared logic 2022-09-02 21:18:36 +00:00
ice1000
fcc61337a8 Remove alias definition naively 2022-09-02 21:18:36 +00:00
Tiddo Langerak
d5e6aa39c1
Pre-cast impl nodes to ast::Impl in find_non_trait_impl 2022-09-01 08:43:41 +03:00
bors
7f38581372 Auto merge of #13120 - Austaras:master, r=jonas-schievink
turn `unwrap_or` into `unwrap_or_else` and vice versa

closes #12983
2022-08-31 13:55:29 +00:00
bors
79a578ae84 Auto merge of #13005 - pocket7878:convert-two-arm-bool-match-to-matches-macro, r=jonas-schievink
feature: Assist to turn match into matches! invocation

Resolves #12510

This PR adds an assist, which convert 2-arm match that evaluates to a boolean into the equivalent matches! invocation.
2022-08-31 13:47:40 +00:00
bors
cf05b7db4d Auto merge of #13051 - DropDemBits:attrs-and-comments-on-enum-variant, r=jonas-schievink
fix: Only move comments when extracting a struct from an enum variant

Motivating example:

```rs
#[derive(Debug, thiserror::Error)]
enum Error {
    /// Some explanation for this error
    #[error("message")]
    $0Woops {
        code: u32
    }
}
```
now becomes
```rs
/// Some explanation for this error
#[derive(Debug, thiserror::Error)]
struct Woops{
    code: u32
}

#[derive(Debug, thiserror::Error)]
enum Error {
    #[error("message")]
    Woops(Woops)
}
```
(the `thiserror::Error` derive being copied and the struct formatting aren't ideal, though those are issues for another day)
2022-08-31 13:34:43 +00:00
Pocket7878
7464b6dbc4 feature: Check if first_arm bool and second_arm bool is inverted or not. 2022-08-31 18:47:45 +09:00
Masato Sogame
5a1b45dcc1 feature: Simplfy branch check logics
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2022-08-31 18:47:45 +09:00