Commit Graph

228 Commits

Author SHA1 Message Date
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
Lukas Wirth
ec2895e956 Insert unnamed consts to ChildBySource DynMap 2021-04-09 17:14:48 +02:00
Brandon
c989287a34 Fix extract function's mutability of variables outliving the body 2021-04-08 20:58:29 -07:00
Brandon
24ab69c608 Add FIXME for macro case 2021-04-08 09:27:04 -07:00
bors[bot]
855a739ebf
Merge #8207
8207: Show dbg remove assist on empty contents r=edwin0cheng a=ivan770

Closes #8185

Co-authored-by: ivan770 <leshenko.ivan770@gmail.com>
Co-authored-by: ivan770 <ivan@ivan770.me>
2021-04-08 05:46:15 +00:00
Brandon
1ccfd0ceda Fix faulty assertion when extracting function with macro call 2021-04-07 21:43:38 -07:00
bors[bot]
d8ee25bb97
Merge #8339
8339: fix: extract variable works in guards r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-04-05 12:19:25 +00:00
Aleksey Kladov
30d6419bc9 fix: extract variable works in guards
closes #8336
2021-04-05 14:40:56 +03:00
bors[bot]
4a589b1c3a
Merge #8326
8326: Rewrite reorder fields assist to use mutable syntax trees r=matklad a=Veykril

This also instead uses `Either` to use the typed `RecordPat` and `RecordExpr` nodes, this unfortunately gives a bit of code duplication

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-04-05 11:23:12 +00:00
unexge
8d4be829e0 Add convert tuple struct to named struct assist 2021-04-04 20:52:43 +03:00
Lukas Wirth
df1320d8c4 Rewrite reorder fields assist to use mutable syntax trees 2021-04-03 17:22:16 +02:00
Graeme Coupar
ee03849017 Convert Into to From assist
This adds a "Convert Into to From" assist, useful since clippy has
recently started adding lints on every `Into`.

It covers converting the signature, and converting any `self`/`Self`
references within the body to the correct types.

It does assume that every instance of `Into` can be converted to a
`From`, which I _think_ is the case now.  Let me know if there's
something I'm not thinking of and I can try and make it smarter.
2021-04-03 15:48:35 +01:00
bors[bot]
75011bbccb
Merge #8210
8210: Implement "Extract type alias" assist r=jonas-schievink a=jonas-schievink



Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2021-03-31 12:26:57 +00:00
bors[bot]
55d7d71590
Merge #8267
8267: Adding gifs and screenshots for features in manual r=matklad a=MozarellaMan

For #6539

This includes most of gif or screenshot examples of most items in the "Features" header. With the exceptions of:

- **On Typing Assists** - couldn't get it to work for a demo, I'm probably missing something?
- **Structural search and replace** - looked to be already a visual example of the feature
- **Workspace symbol** - wasn't sure how best to show this, all of the examples maybe? Also wasn't sure of the best code example to show it off
- **Semantic Syntax Highlighting** - seemed obvious enough to not need a screenshot, but I could easily add this

All the gifs/pngs are hosted in this [comment](https://github.com/rust-analyzer/rust-analyzer/issues/6539#issuecomment-809574840). Please let me know if any of them aren't suitable (and why) and I'll improve it! Or if you don't like the theme/font

Co-authored-by: Ayomide Bamidele <48062697+MozarellaMan@users.noreply.github.com>
2021-03-31 10:01:56 +00:00
Ayomide Bamidele
276022682b Gifs and screenshots for features in manual 2021-03-31 00:08:10 +01:00
Lukas Wirth
c2a63b97a8 Rename target_ty to self_ty 2021-03-29 17:47:47 +02:00
ivan770
f9d17c6d7c
Apply test style fixes to all empty remove_dbg tests 2021-03-29 16:00:09 +02:00
ivan770
8e5611442e
Update crates/ide_assists/src/handlers/remove_dbg.rs
Apply standard test style fixes

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-03-29 16:52:30 +03:00
Jonas Schievink
3c6c1c99b4 Don't use snippets 2021-03-29 13:23:07 +02:00
Jonas Schievink
8c1092455e Use find_node_at_range 2021-03-29 13:17:49 +02:00
Jonas Schievink
b494e47920 Snippet support in extract_type_alias 2021-03-27 18:53:13 +01:00
ivan770
665266bf66
Replace empty dbg with unit in letexprs, better removal in blocks 2021-03-27 17:34:35 +02:00
bors[bot]
ae92014319
Merge #8213
8213: Added support for const generics in impl generation r=Veykril a=ivan770

Closes #8211

Co-authored-by: ivan770 <leshenko.ivan770@gmail.com>
2021-03-27 10:00:37 +00:00
ivan770
5a2ef8d0ca
Added support for const generics in impl generation 2021-03-27 11:37:39 +02:00
ivan770
0a5badbcba
Replace match on option with if 2021-03-27 11:17:17 +02:00
ivan770
c7cd0aff4a
Remove dbg expression and newline as whole 2021-03-27 11:10:20 +02:00
Jonas Schievink
e39979aa91 Implement "Extract type alias" assist 2021-03-26 19:39:20 +01:00
cynecx
5ff3299dd6 syntax: return owned string instead of leaking string 2021-03-26 18:30:59 +01:00
ivan770
2292ff64f1
Show dbg remove assist on empty contents 2021-03-26 16:15:26 +02:00
hi-rustin
eef9bdb441 refine comment style of tests 2021-03-24 19:28:50 +08:00
bors[bot]
776b1ebcb4
Merge #8168
8168: correct `convert to guard return` let_stmt r=Veykril a=hi-rustin

close https://github.com/rust-analyzer/rust-analyzer/issues/8074

Co-authored-by: hi-rustin <rustin.liu@gmail.com>
2021-03-24 10:55:25 +00:00
hi-rustin
e992acf078 correct convert to guard return let_stmt
fix

fix

add check
2021-03-24 11:52:44 +08:00
Aleksey Kladov
8b4240e026 Cleanup 2021-03-23 19:59:33 +03:00
Aleksey Kladov
e33959a888 Simplify code
changelog: skip
2021-03-23 19:41:15 +03:00
Aleksey Kladov
860e069d4d Use styleguide conforming import for ast nodes 2021-03-23 17:44:17 +03:00
Aleksey Kladov
7352f50ec2 Unify test style
changelog skip
2021-03-23 17:38:51 +03:00
Aleksey Kladov
b83c7eedcc Tweak assits API to fit mutable syntax trees
changelog: skip
2021-03-23 17:31:19 +03:00
Aleksey Kladov
9cbf09ec4f rewrite merge use trees assist to use muatable syntax trees
changelog internal
2021-03-22 20:47:46 +03:00
Matthias Krüger
ae7e55c1dd clippy::complexity simplifications related to Iterators 2021-03-21 13:13:34 +01:00
Kirill Bulatov
eaa4fcbbde Less reallocations 2021-03-21 11:45:37 +02:00
Kirill Bulatov
56a7d246d5 Disable unqualified assoc items completion for now 2021-03-20 23:08:44 +02:00
Kirill Bulatov
879432452d Docs 2021-03-20 22:55:34 +02:00
Kirill Bulatov
a631108d2d Do not query item search by name eagerly 2021-03-20 22:33:54 +02:00
bors[bot]
5cc8ad0c4a
Merge #8119
8119:  Don't return a SourceChange on WillRenameFiles when nothing gets refactored r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-20 12:58:28 +00:00
Lukas Wirth
d84912483d Fix add_life_to_type label typo 2021-03-20 13:44:12 +01:00
Aleksey Kladov
ba72308588 simplify
changelog skip
2021-03-19 21:00:20 +03:00
Lukas Wirth
c34a9f10b1 Cleanup qualify_path 2021-03-19 13:12:00 +01:00
Lukas Wirth
34464ede3f Fix associated items not being appended to paths in import_assets 2021-03-18 21:36:52 +01:00
Matthias Krüger
ff5f90d8ae use simpler .map(|x| y) instead of .and_then(|x| Some(y)) for Options. (clippy::bind_instead_of_map) 2021-03-17 02:36:29 +01:00
Matthias Krüger
048dad8c2e don't clone types that are copy (clippy::clone_on_copy) 2021-03-17 01:56:31 +01:00
Aleksey Kladov
d733c9bdad Move more bounds
changelog: skip
2021-03-16 22:28:04 +03:00
Aleksey Kladov
f5a81ec468 Upgrade rowan
Notably, new rowan comes with support for mutable syntax trees.
2021-03-16 16:10:49 +03:00
Chetan Khilosiya
714836959b 7709: Added the check for return type of len function. 2021-03-16 01:16:59 +05:30
Chetan Khilosiya
0c2d4a8a77 7709: Updated the implementation.
The get function from impl method is updated.
and now same method used to get len and is_empty function.
2021-03-15 22:48:50 +05:30
Chetan Khilosiya
2bf3802f21 7709: Added the assist to generate is_empty function
the assist will be shown when the len function is implemented.
is_empty internally uses len function.
2021-03-15 21:31:52 +05:30
Laurențiu Nicola
88cee24c6c Enable thread-local coverage marks 2021-03-15 16:02:50 +02:00
Jake Goulding
63155d66f5 Allow applying De Morgan's law to multiple terms at once 2021-03-12 10:19:54 -05:00
bors[bot]
c0459c5357
Merge #7956
7956: Add assist to convert for_each into for loops r=Veykril a=SaiintBrisson

This PR resolves #7821.
Adds an assist to that converts an `Iterator::for_each` into a for loop: 

```rust
fn main() {
    let vec = vec![(1, 2), (2, 3), (3, 4)];
    x.iter().for_each(|(x, y)| {
        println!("x: {}, y: {}", x, y);
    })
}
```
becomes
```rust
fn main() {
    let vec = vec![(1, 2), (2, 3), (3, 4)];
    for (x, y) in x.iter() {
        println!("x: {}, y: {}", x, y);
    });
}
```

Co-authored-by: Luiz Carlos Mourão Paes de Carvalho <luizcarlosmpc@gmail.com>
Co-authored-by: Luiz Carlos <luizcarlosmpc@gmail.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-12 14:45:04 +00:00
Lukas Wirth
6d35c67b6e Fix convert_iter_for_each_to_for doctest 2021-03-12 15:42:53 +01:00
Luiz Carlos Mourão Paes de Carvalho
e505752442 fix: generated test fixture 2021-03-12 08:53:57 -03:00
Luiz Carlos
7a9230acdf
fix: replace doc-comments with normal comments
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-12 08:44:03 -03:00
Luiz Carlos Mourão Paes de Carvalho
f67861310c refactor: refactored and reduced assist code 2021-03-12 08:06:50 -03:00
Conrad Ludgate
233820d780
fix: add semicolon after type ascription 2021-03-11 10:36:45 +00:00
Luiz Carlos Mourão Paes de Carvalho
6236b1eaf8 fix: remove semicolon 2021-03-10 15:43:57 -03:00
Kirill Bulatov
94bb9cb9ee Fix labels for single import assists 2021-03-10 11:30:25 +02:00