Commit Graph

192 Commits

Author SHA1 Message Date
Kirill Bulatov
28293d370f Add docs and use better naming 2021-05-03 18:16:35 +03:00
Kirill Bulatov
1679a376f3 Resolve single assist only 2021-05-03 18:03:28 +03:00
Kirill Bulatov
e5cdcb8b12 Add a way to resolve certain assists 2021-05-03 17:14:49 +03:00
Aleksey Kladov
1a01a5ae19 internal: fix naming polarity
Type Constructors have *parameters*, when they are substituted with type
*arguments*, we have a type.
2021-04-30 11:55:59 +03:00
Aleksey Kladov
cb3ef552e8 internal: normalize name
All def types in hir are unsubstituted
2021-04-30 11:52:31 +03:00
Lukas Wirth
050c69c19d Split out merge_imports module from helpers::insert_use 2021-04-24 13:31:43 +02:00
bors[bot]
5cbde9f531
Merge #8591 #8638
8591: Remove SyntaxRewriter usage in insert_use in favor of mutable syntax trees r=matklad a=Veykril

Unfortunately changing `insert_use` to not use `SyntaxRewriter` creates a lot of changes since so much relies on that. But on the other hand this should be the biggest usage of `SyntaxRewriter` I believe.

8638: Remove SyntaxRewriter::from_fn r=Veykril a=Veykril



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-04-23 16:49:08 +00:00
Lukas Wirth
e6e4417bbb Remove SyntaxRewriter::from_fn 2021-04-23 18:36:43 +02:00
bors[bot]
85bab7539a
Merge #8317
8317: Convert tuple struct to named struct assist r=Veykril a=unexge

Closes https://github.com/rust-analyzer/rust-analyzer/issues/8192

Co-authored-by: unexge <unexge@gmail.com>
2021-04-23 13:37:48 +00:00
unexge
5e765895cf Add missing test case for "Convert to named struct" assist 2021-04-23 16:18:10 +03:00
unexge
97270dfb91 Stop iterating reference after made an edit in "Convert to named struct" assist 2021-04-23 13:08:07 +03:00
bors[bot]
0bb074aa26
Merge #8620
8620: Remove unnecessary braces for extracted block expression r=Veykril a=brandondong

This change addresses the first bullet point of https://github.com/rust-analyzer/rust-analyzer/issues/7839.

Specifically, when extracting block expressions, remove the unneeded extra braces inside the generated function.

Co-authored-by: Brandon <brandondong604@hotmail.com>
2021-04-22 15:22:43 +00:00
unexge
affd8d3518 Move reference editing logic into own function to make error handling more ease in "Convert to named struct" assist 2021-04-22 11:33:56 +03:00
Brandon
1713f4c7cd Remove unnecessary braces for extracted block expression 2021-04-21 23:39:35 -07:00
Lukas Wirth
d5c9de65c5 Don't filter equal nodes in reorder assists 2021-04-22 00:54:31 +02:00
unexge
6630266ce1 Add multi file test for "Convert to named struct" assist 2021-04-21 16:20:08 +03:00
unexge
96d694062b Remove unwraps in "Convert to named struct" assist 2021-04-21 16:01:13 +03:00
Comonad
09147c3303 Add support for fill match arms of boolean values
- Add support for boolean inside tuple
2021-04-21 19:33:45 +08:00
unexge
e0a60e71d7 Add larger example for "Convert to named struct" assist 2021-04-21 10:57:36 +03:00
unexge
53599d11f6 Fix incorrectly replacing method calls in "Convert to named struct" assist 2021-04-21 10:27:26 +03:00
Lukas Wirth
b290cd5782 Add cov_marks to insert_use tests 2021-04-20 19:34:43 +02:00
Lukas Wirth
2c8f1b5c30 Rewrite extract_struct_from_enum_variant assist 2021-04-20 17:36:42 +02:00
bors[bot]
86c2bb3c5b
Merge #8602
8602: Fix panic in `replace_derive_with_manual_impl` r=jonas-schievink a=jonas-schievink

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2021-04-20 14:27:08 +00:00
Jonas Schievink
c0ad9b3581 Follow testing style guide 2021-04-20 16:26:07 +02:00
Jonas Schievink
6624158969 Fix panic in replace_derive_with_manual_impl 2021-04-20 16:25:24 +02:00
Jonas Schievink
d699371f5f "Inline variable" when on a use of the variable 2021-04-20 16:16:23 +02:00
Lukas Wirth
fa20a5064b Remove SyntaxRewriter usage in insert_use in favor of ted 2021-04-20 02:09:12 +02:00
Lukas Wirth
e8744ed9bb Replace SyntaxRewriter usage with ted in reorder_impl assist 2021-04-20 02:08:21 +02:00
Jonas Schievink
ec05186378 Add autoimport test with inner items 2021-04-19 19:53:29 +02:00
bors[bot]
fc709c8b21
Merge #8583
8583: Simplify r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-04-19 15:55:29 +00:00
Lukas Wirth
c96c38edd3 Simplify 2021-04-19 17:11:30 +02:00
bors[bot]
e7a8977358
Merge #8524 #8527
8524: Fix extract function with partial block selection r=matklad a=brandondong

**Reproduction:**
```rust
fn foo() {
    let n = 1;
    let mut v = $0n * n;$0
    v += 1;
}
```
1. Select the snippet ($0) and use the "Extract into function" assist.
2. Extracted function is incorrect and does not compile:
```rust
fn foo() {
    let n = 1;
    let mut v = fun_name(n);
    v += 1;
}

fn fun_name(n: i32) {}
```
3. Omitting the ending semicolon from the selection fixes the extracted function:
```rust
fn fun_name(n: i32) -> i32 {
    n * n
}
```

**Cause:**
- When `extraction_target` uses a block extraction (semicolon case) instead of an expression extraction (no semicolon case), the user selection is directly used as the TextRange.
- However, the existing function extraction logic for blocks requires that the TextRange spans from start to end of complete statements to work correctly.
- For example:
```rust
fn foo() {
    let m = 2;
    let n = 1;
    let mut v = m $0* n;
    let mut w = 3;$0
    v += 1;
    w += 1;
}
```
produces
```rust
fn foo() {
    let m = 2;
    let n = 1;
    let mut v = m let mut w = fun_name(n);
    v += 1;
    w += 1;
}

fn fun_name(n: i32) -> i32 {
    let mut w = 3;
    w
}
```
- The user selected TextRange is directly replaced by the function call which is now in the middle of another statement. The extracted function body only contains statements that were fully covered by the TextRange and so the `* n` code is deleted. The logic for calculating variable usage and outlived variables for the function parameters and return type respectively search within the TextRange and so do not include `m` or `v`.

**Fix:**
- Only extract full statements when using block extraction. If a user selected part of a statement, extract that full statement.

8527: Switch introduce_named_lifetime assist to use mutable syntax tree  r=matklad a=iDawer

This extends `GenericParamsOwnerEdit` trait with `get_or_create_generic_param_list` method

Co-authored-by: Brandon <brandondong604@hotmail.com>
Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
2021-04-19 13:09:18 +00:00
bors[bot]
65dd942fa1
Merge #8565
8565: Fill match arms assist: add remaining arms for tuple of enums r=iDawer a=iDawer

Fix for #8493

However, the assist is still flaky and does not use `hir_ty::diagnostics::match_check`

Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
2021-04-19 11:32:22 +00:00
Dawer
9222d3b0fb Unindent test according to the style guide. 2021-04-19 16:24:09 +05:00
bors[bot]
3f432730df
Merge #8467
8467: Adds impl Deref assist r=jhgg a=jhgg

This PR adds a new `generate_deref` assist that automatically generates a deref impl for a given struct field.

Check out this gif:

![2021-04-11_00-33-33](https://user-images.githubusercontent.com/5489149/114296006-b38e1000-9a5d-11eb-9112-807c01b8fd0a.gif)

--

I have a few Q's:
 - [x] Should I write more tests, if so, what precisely should I test for?
 - [x] I have an inline question on line 65, can someone provide guidance? :) 
 - [x] I can implement this for `ast::TupleField` too. But should it be a separate assist fn, or should I try and jam both into the `generate_deref`?
 - [x] I want to follow this up with an assist on `impl $0Deref for T {` which would automatically generate a `DerefMut` impl that mirrors the Deref as well, however, I could probably use some pointers on how to do that, since I'll have to reach into the ast of `fn deref` to grab the field that it's referencing for the `DerefMut` impl. 

Co-authored-by: jake <jh@discordapp.com>
2021-04-19 04:54:04 +00:00
jake
3d1ca786f6 implement field stuff too 2021-04-18 21:51:17 -07:00
Dawer
8d588efc2b Return to the status quo in #8129 2021-04-18 20:17:30 +05:00
Dawer
51d65caed4 Prevent adding useless match arms 2021-04-18 16:54:09 +05:00
Lukas Wirth
58a6ec549d Add some more error messages to fixture failure cases 2021-04-17 21:34:14 +02:00
Dawer
76285f16de Test fill-match-arms assist: partial with wildcards 2021-04-17 15:20:29 +05:00
Dawer
edbb1797fb Fill partial match arms for a tuple of enums 2021-04-17 01:09:09 +05:00
Dawer
8965be3d0e Fill match arms for a tuple of a single enum. 2021-04-16 17:22:11 +05:00
Brandon
ffefbf2ba4 Fix extract function with partial block selection 2021-04-14 21:34:01 -07:00
Dawer
144afa55a6 Switch introduce_named_lifetime assist to use mutable syntax tree 2021-04-15 01:56:19 +05:00
bors[bot]
e6728a8cd3
Merge #8415
8415: Fix faulty assertion when extracting function with macro call r=matklad a=brandondong

**Reproduction:**
```rust
fn main() {
    let n = 1;
    let k = n * n;
    dbg!(n);
}
```
1. Select the second and third lines of the main function. Use the "Extract into function" code assist.
2. Panic occurs in debug, error is logged in release: "[ERROR ide_assists::handlers::extract_function] assertion failed: matches!(path, ast :: Expr :: PathExpr(_))".
3. Function generates successfully on release where the panic was bypassed.
```rust
fn fun_name(n: i32) {
    let k = n * n;
    dbg!(n);
}
```

**Cause:**
- The generated function will take `n` as a parameter. The extraction logic needs to search the usages of `n` to determine whether it is used mutably or not. The helper `path_element_of_reference` is called for each usage but the second usage is a macro call and fails the `Expr::PathExpr(_)` match assertion.
- The caller of `path_element_of_reference` does implicitly assume it to be a `Expr::PathExpr(_)` in how it looks at its parent node for determining whether it is used mutably. This logic will not work for macros.
- I'm not sure if there are any other cases besides macros where it could be something other than a `Expr::PathExpr(_)`. I tried various examples and could not find any.

**Fix:**
- Update assertion to include the macro case.
- Add a FIXME to properly handle checking if a macro usage requires mutable access. For now, return false instead of running the existing logic that is tailored for `Expr::PathExpr(_)`'s.

Co-authored-by: Brandon <brandondong604@hotmail.com>
2021-04-13 11:39:03 +00:00
Aleksey Kladov
db2a989565 internal: don't use #[should_panic] for tests 2021-04-13 12:21:59 +03:00
Aleksey Kladov
460f0ef669 internal: unfork code paths for unresolved and resolved assist 2021-04-13 10:59:15 +03:00
Brandon
09a78caca4 Add macro test 2021-04-11 11:12:02 -07:00
jake
a624e2ea8d Adds impl Deref assist 2021-04-11 00:42:05 -07:00
bors[bot]
a8a25863f6
Merge #8436
8436: Fix extract function's mutability of variables outliving the body r=matklad a=brandondong

**Reproduction:**
```rust
fn main() {
    let mut k = 1;
    let mut j = 2;
    j += 1;
    k += j;
}
```
1. Select the first to third lines of the main function. Use the "Extract into function" code assist.
2. The output is the following which does not compile because the outlived variable `k` is declared as immutable:
```rust
fn main() {
    let (k, j) = fun_name();
    k += j;
}

fn fun_name() -> (i32, i32) {
    let mut k = 1;
    let mut j = 2;
    j += 1;
    (k, j)
}
```
3. We would instead expect the output to be:
```rust
fn main() {
    let (mut k, j) = fun_name();
    k += j;
}
```

**Fix:**
- Instead of declaring outlived variables as immutable unconditionally, check for any mutable usages outside of the extracted function.

Co-authored-by: Brandon <brandondong604@hotmail.com>
2021-04-10 21:35:05 +00:00