Commit graph

748 commits

Author SHA1 Message Date
龙方淞
8f5e8e0a13 Update crates/ide_assists/src/handlers/move_from_mod_rs.rs
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-09-28 10:20:35 +08:00
longfangsong
0049b5b0bc initial commit 2021-09-28 10:20:29 +08:00
Lukas Wirth
b6ed91a6de Rename *Owner traits to Has* 2021-09-27 12:54:24 +02:00
Aleksey Kladov
2bf81922f7 internal: more reasonable grammar for blocks
Consider these expples

        { 92 }
  async { 92 }
    'a: { 92 }
   #[a] { 92 }

Previously the tree for them were

  BLOCK_EXPR
    { ... }

  EFFECT_EXPR
    async
    BLOCK_EXPR
      { ... }

  EFFECT_EXPR
    'a:
    BLOCK_EXPR
      { ... }

  BLOCK_EXPR
    #[a]
    { ... }

As you see, it gets progressively worse :) The last two items are
especially odd. The last one even violates the balanced curleys
invariant we have (#10357) The new approach is to say that the stuff in
`{}` is stmt_list, and the block is stmt_list + optional modifiers

  BLOCK_EXPR
    STMT_LIST
      { ... }

  BLOCK_EXPR
    async
    STMT_LIST
      { ... }

  BLOCK_EXPR
    'a:
    STMT_LIST
      { ... }

  BLOCK_EXPR
    #[a]
    STMT_LIST
      { ... }
2021-09-26 19:16:09 +03:00
bors[bot]
c51a3c78cf
Merge #10358
10358: internal: Remove inherent methods from ast nodes that do non-syntactic complex tasks  r=Veykril a=Veykril



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-09-26 14:49:25 +00:00
Lukas Wirth
151afdfe5c Remove inherent methods from ast node that carry semantic meaning 2021-09-26 16:49:03 +02:00
bors[bot]
cd7b26c6eb
Merge #10352
10352: feat: Implement inline callers assist r=Veykril a=Veykril

Fixes #7242

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-09-26 14:02:25 +00:00
Lukas Wirth
215a077ee4 Remove imports when inlining all calls in a file 2021-09-26 16:01:54 +02:00
Lukas Wirth
79c70d0ad3 Simplify 2021-09-26 14:56:43 +02:00
Lukas Wirth
1a50f904ef Reject recursive calls in inline_call 2021-09-26 14:55:03 +02:00
Lukas Wirth
1ccb21a0ca feat: Implement inline callers assist 2021-09-25 18:39:43 +02:00
bors[bot]
d401f2a062
Merge #10211
10211: assists: Promote module to folder r=jonas-schievink a=longfangsong

Close part of #10143.

This PR adds a assist to promote module to directory, which means make a .rs file module into a directory style module with the same name.

![未命名(1)](https://user-images.githubusercontent.com/13777628/132958377-14555d6f-a64a-4b9b-9154-90a3b86fd685.gif)


Co-authored-by: longfangsong <longfangsong@icloud.com>
2021-09-25 15:32:00 +00:00
longfangsong
22abbe86f3 Address comments 2021-09-25 22:48:57 +08:00
Aleksey Kladov
d72f7cf3af internal: add => () rule; emphasize n_items rule 2021-09-25 14:10:25 +03:00
zhoufan
8690cfb868 Change the style of the code 2021-09-22 06:32:23 +08:00
zhoufan
7912d3a4b3 Generate function assist creates bad param names for const/static item args 2021-09-22 00:15:57 +08:00
Lukas Wirth
ba84b91e78 Add a mirror function-like proc-macro expander for tests 2021-09-21 14:55:54 +02:00
Lukas Wirth
8b1e8197fe Merge iter_for_each_to_for and for_to_iter_for_each assists modules 2021-09-21 10:34:11 +02:00
Lukas Wirth
8b2be8572f Rename some assists 2021-09-21 00:54:09 +02:00
longfangsong
cd599ec202 Address comments 2021-09-20 21:37:18 +08:00
rainy-me
11fbafdee3 Narrow "if-let to match" assist available range 2021-09-18 05:28:52 +09:00
bors[bot]
d44779f8a5
Merge #10260
10260: fix: fix names generation in `Generate function` r=Veykril a=iDawer

- Improve fn name computation (close #10176).
- Handle tuple indexing expressions in argument position (should close  #10241)

Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
2021-09-17 18:35:36 +00:00
Andrzej Głuszak
11a56f886b assists: turn while into loop 2021-09-16 22:20:27 +02:00
Dawer
1d94e234d5 Handle tuple indexing expression in argument position in Generate function 2021-09-16 23:45:41 +05:00
Dawer
4dc33140a3 Imrove fn name computation in Generate function 2021-09-16 21:03:37 +05:00
Lukas Wirth
747f2d1719 fix: Do not wrap inlined local in parens when at block tail position 2021-09-14 18:29:34 +02:00
Lukas Wirth
a044175412 Simplify 2021-09-13 18:50:19 +02:00
Aleksey Kladov
46cdde75f8 internal: document tribal knowledge of how to assist 2021-09-13 14:19:10 +03:00
Aleksey Kladov
076c972e3b internal: prevent possible bugs when adding magical comments 2021-09-13 13:43:13 +03:00
longfangsong
cafc7e3501 Fix doc test 2021-09-12 11:20:28 +08:00
longfangsong
3edc25dc26 Add docs strings 2021-09-12 10:53:56 +08:00
longfangsong
fede1a3beb Add promote_mod_file assist 2021-09-12 10:45:14 +08:00
Dawer
535761e63f minor: update test 2021-09-04 15:19:44 +05:00
Dawer
3d9d10be39 fix: use placeholder as default type in Extract into function. 2021-09-04 14:25:17 +05:00
Dawer
a6c650edf6 fix: use placeholder as default type in Generate function. 2021-09-04 14:24:54 +05:00
Yotam Ofek
dd9433cc63
Update crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-09-03 21:02:48 +03:00
Yotam Ofek
0d453cc2be Fix replacing for loops over ranges with for_each. 2021-09-03 19:43:40 +03:00
Lukas Wirth
8e8ea537ab Deduplicate imports for qualify_path 2021-09-01 21:51:28 +02:00
Daiki Ihara
4d005e529b Fix extract_function with macro arg 2021-09-01 11:11:57 +02:00
DropDemBits
3bafb5f025
feat: Use enum's visibility for extracted struct fields 2021-08-30 21:33:19 -04:00
DropDemBits
95bf89d3c8
fix: Keep comments & attrs from enum variant 2021-08-30 21:33:12 -04:00
DropDemBits
681efe6dcb
feat: Properly indent extracted struct and enum 2021-08-30 21:26:21 -04:00
bors[bot]
d250aa77ec
Merge #10015
10015: internal: more declarative re-indentation API r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-08-30 08:34:49 +00:00
Aleksey Kladov
78365c64c8 internal: slightly improve compile times
As per style guide, avoid monomorphisations
2021-08-29 12:53:56 +03:00
Lukas Wirth
5c3a93ca9e Remove incorrect assertion in extract_function 2021-08-27 19:58:57 +02:00
Aleksey Kladov
d1cd81f387 internal: more declarative re-indentation API 2021-08-24 18:18:43 +03:00
bors[bot]
6287d388c0
Merge #9944
9944: internal: introduce in-place indenting API r=matklad a=iDawer

Introduce `edit_in_place::Indent` that uses mutable tree API and intended to replace `edit::AstNodeEdit`.

Closes #9903 

Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
2021-08-24 13:11:50 +00:00
bors[bot]
43021525e5
Merge #9994
9994: add static method generation assist r=matklad a=mahdi-frms

Adds feature: #9948

Will soon send a GIF for the changelog.

Co-authored-by: mahdi-frms <mahdif1380@outlook.com>
2021-08-24 09:26:52 +00:00
bors[bot]
ced65f77c4
Merge #10001
10001: Sort enum variant r=Veykril a=vsrs

A small fix to the problem noted by `@lnicola` :

> ![sort-fields](https://user-images.githubusercontent.com/308347/129513196-4ffc7937-be58-44d4-9ec7-ba8745dcb460.gif)
> 
> (note the slight inconsistency here: to sort the variants of `Animal` I have to select the enum name, but to sort the fields of `Cat` I have to select the fields themselves)



Co-authored-by: vsrs <vit@conrlab.com>
2021-08-23 22:19:50 +00:00
vsrs
7a8f881a09 Sort enum variant 2021-08-23 20:31:54 +03:00
mahdi-frms
cf5f1c102c add static method generation assist 2021-08-23 12:14:23 +04:30
bors[bot]
107b2f1890
Merge #9972
9972: refactor : function generation assists r=Veykril a=mahdi-frms

Separated code generation from finding position for generated code. This will be ground work for introducing static associated function generation.

Co-authored-by: mahdi-frms <mahdif1380@outlook.com>
2021-08-22 14:53:41 +00:00
mahdi-frms
c1edbfbacc better assist label 2021-08-22 12:31:16 +04:30
mahdi-frms
50923ad741 func gen: seperate generation form position(3) 2021-08-21 19:13:14 +04:30
mahdi-frms
1ac9400100 func gen: seperate generation form position(2) 2021-08-21 17:31:37 +04:30
bors[bot]
4aa2a44a55
Merge #9962
9962: Add empty-body check to replace_match_with_if_let and re-prioritize choices r=elkowar a=elkowar

This PR changes some behaviour of the `replace_match_with_if_let` ide-assist.
Concretely, it makes two changes:

it introduces a check for empty expression bodies. This means that checks of the shape
```rs
match x {
  A => {}
  B => {
    println!("hi");
  }
}
```
will prefer to use the B branch as the first (and only) variant.

It also reprioritizes the importance of "happy" and "sad" patterns.
Concretely, if there are reasons to prefer having the sad pattern be the first (/only) pattern,
it will follow these.
This means that in the case of 
```rs
match x {
  Ok(_) => {
    println!("Success");
  }
  Err(e) => {
    println!("Failure: {}", e);
  }
}
```
the `Err` variant will correctly be used as the first expression in the generated if.
Up until now, the generated code was actually invalid, as it would generate
```rs
if let Ok(_) = x {
  println!("Success");
} else {
  println!("Failure: {}", e);
}
```
where `e` in the else branch is not defined.


Co-authored-by: elkowar <5300871+elkowar@users.noreply.github.com>
2021-08-21 10:12:17 +00:00
elkowar
e47c9743cf
Fix smaller nitpicks 2021-08-21 12:02:21 +02:00
elkowar
7cff930393
Use NameClass::classify to check for ConstReference 2021-08-21 11:11:27 +02:00
elkowar
75f0701211
Add heuristic to determine type of IdentPat, make check for empty expressions correct 2021-08-21 11:00:43 +02:00
mahdi-frms
87439b1d6a func gen: seperate generation form position(1) 2021-08-21 13:29:15 +04:30
mahdi-frms
e2c233a42b simplify 2021-08-21 11:52:07 +04:30
elkowar
d6695286ee
Add empty-body check to replace_match_with_if_let and re-prioritize choices 2021-08-20 15:20:54 +02:00
bors[bot]
59aa091866
Merge #9855
9855: feature: Destructure Tuple Assist r=Veykril a=Booksbaum

Part of #8673. This PR only handles tuples, not TupleStruct and RecordStruct.

Code Assist to destructure a tuple into its items:
![Destructure_Tuple_Assist](https://user-images.githubusercontent.com/15612932/129020107-775d7c94-dca7-4d1f-a0a2-cd63cabf4132.gif)



* Should work in nearly all pattern positions, like let assignment, function parameters, match arms, for loops, and nested variables (`if let Some($0t) = Some((1,2))`)  
  -> everywhere `IdentPat` is allowed
  * Exception: If there's a sub-pattern (``@`):`
    ```rust
    if let t @ (1..=3, 1..=3) = ... {}
    //     ^
    ```
    -> `t` must be a `Name`; `TuplePat` (`(_0, _1)`) isn't allowed
    * inside subpattern is ok:
      ```rust
      let t @ (a, _) = ((1,2), 3);
      //       ^
      ```
      ->
      ```rust
      let t @ ((_0, _1), _) = ((1,2), 3);
      ```
* Assist triggers only at tuple declaration, not tuple usage.  
  (might be useful especially when it creates a sub-pattern (after ``@`)` and only changes the usage under cursor -- but not part of this PR).

### References
References can be destructured:
```rust
let t = &(1,2);
//  ^
let v = t.0;
```
->
```rust
let (_0, _1) = &(1,2);
let v = _0;
```
BUT: `t.0` and `_0` have different types (`i32` vs. `&i32`) -> `v` has now a different type.

I think that's acceptable: I think the destructure assist is mostly used in simple, immediate scopes and not huge existing code.

Additional Notes:
* `ref` has same behaviour (-> `ref` is kept for items)
  ```rust
  let ref t = (1,2);
  //      ^
  ```
  ->
  ```rust
  let (ref _0, ref _1) = (1,2);
  ```
* Rust IntelliJ Plugin: doesn't trigger with `&` or `ref` at all 

### mutable
```rust
let mut t = (1,2);
//      ^
```
->
```rust
let (mut _0, mut _1) = (1,2);
```
and
```rust
let t = &mut (1,2);
//  ^
```
->
```rust
let (_0, _1) = &mut (1,2);
```
Again: with reference (`&mut`), `t.0` and `_0` have different types (`i32` vs `&mut i32`).  
And there's an additional issue with `&mut` and assignment:
```rust
let t = &mut (1,2);
//  ^
t.0 = 9;
```
->
```rust
let (_0, _1) = &mut (1,2);
_0 = 9;
//   ^
//   mismatched types
//   expected `&mut {integer}`, found integer
//   consider dereferencing here to assign to the mutable borrowed piece of memory
```
But I think that's quite a niche use case, so I don't catch that (`*_0 = 9;`)

Additional Notes:
* Rust IntelliJ Plugin: removes the `mut` (`let mut t = ...` -> `let (_0, _1) = ...`), doesn't trigger with `&mut`

### Binding after ``@``
Destructure tuple in sub-pattern is implemented:
```rust
let t = (1,2);
//  ^
let v = t.0;
let f = t.into();
```
->
```rust
let t @ (_0, _1) = (1,2);
let v = _0;
let f = t.into();
```
BUT: Bindings after ``@`` aren't currently in stable and require `#![feature(bindings_after_at)]` (though should be generally [available quite soon](https://github.com/rust-lang/rust/pull/85305#event-5072889913) (with `1.56.0`)).  
But I don't know how to check for an enabled feature -> Destructure tuple in sub-pattern [isn't enabled](a4ee6c7954/crates/ide_assists/src/handlers/destructure_tuple_binding.rs (L32)) yet.

* When Destructure in sub-pattern is enabled there are two assists:
  * `Destructure tuple in place`:
    ```rust
    let t = (1,2);
    //  ^
    ```
    ->
    ```rust
    let (_0, _1) = (1,2);
    let v = _0;
    let f = /*t*/.into();
    ```
  * `Destructure tuple in sub-pattern`:
    ```rust
    let t = (1,2);
    //  ^
    let v = t.0;
    let f = t.into();
    ```
    ->
    ```rust
    let t @ (_0, _1) = (1,2);
    let v = _0;
    let f = t.into();
    ```
* When Destructure in sub-pattern is disabled, only the first one is available and just named `Destructure tuple`

<br/>
<br/>

### Caveats
* Unlike in #8673 or IntelliJ rust plugin, I'm not leaving the previous tuple name at function calls.  
  **Reasoning**: It's not too unlikely the tuple variable shadows another variable. Destructuring the tuple while leaving the function call untouched, results in still a valid function call -- but now with another variable:
  ```rust
  let t = (8,9);
  let t = (1,2);
  //  ^
  t.into()
  ```
  => Destructure Tuple
  ```rust
  let t = (8,9);
  let (_0, _1) = (1,2);
  t.into()
  ```
  `t.into()` is still valid -- using the first tuple.  
  Instead I comment out the tuple usage, which results in invalid code -> must be handled by user:
  ```rust
  /*t*/.into()
  ```
  * (though that might be a biased decision: For testing I just declared a lot of `t`s and quite ofen in lines next to each other...)
  * Issue: there are some cases that results in still valid code:
    * macro that accept the tuple as well as no arguments:
      ```rust
      macro_rules! m {
          () => { "foo" };
          ($e:expr) => { $e; "foo" };
      }
      let t = (1,2);
      m!(t);
      m!(/*t*/);
      ```
      -> both calls are valid ([test](a4ee6c7954/crates/ide_assists/src/handlers/destructure_tuple_binding.rs (L1474)))  
    * Probably with tuple as return value. Changing the return value most likely results in an error -- but in another place; not where the tuple usage was. 

  -> not sure that's the best way....  
  Additional the tuple name surrounded by comment is more difficult to edit than just the name.
* Code Assists don't support snippet placeholder, and rust analyzer just the first `$0` -> unfortunately no editing of generated tuple item variables. Cursor (`$0`) is placed on first generated item.

<br/>
<br/>

### Issues
* Tuple index usage in macro calls aren't converted:
  ```rust
  let t = (1,2);
  //  ^
  let v = t.0;
  println!("{}", t.0);
  ```
  ->
  ```rust
  let (_0, _1) = (1,2);
  let v = _0;
  println!("{}", /*t*/.0);
  ```
  ([tests](a4ee6c7954/crates/ide_assists/src/handlers/destructure_tuple_binding.rs (L1294)))
  * Issue is:  
    [name.syntax()](a4ee6c7954/crates/ide_assists/src/handlers/destructure_tuple_binding.rs (L242-L244)) in each [usage](a4ee6c7954/crates/ide_assists/src/handlers/destructure_tuple_binding.rs (L108-L113)) of a tuple is syntax & text_range in its file.  
    EXCEPT when tuple usage is in a macro call (`m!(t.0)`), the macro is expanded and syntax (and range) is based on that expanded macro, not in actual file.  
    That leads to several things:
    * I cannot differentiate between calling the macro with the tuple or with tuple item:
      ```rust
      macro_rules! m {
          ($t:expr, $i:expr) => { $t.0 + $i };
      }
      let t = (1,2);
      m!(t, t.0);
      ```
      -> both `t` usages are resolved as tuple index usage
    * Range of resolved tuple index usage is in expanded macro, not in actual file  
     -> don't know where to replace index usage

    -> tuple items passed into a macro are ignored, and only the tuple name itself is handled (uncommented)
* I'm not checking if the generated names conflict with already existing variables.
  ```rust
  let _0 = 42;            // >-|
  let t = (1,2);          //   |
  let v = _0;             // <-|
  //  ^ 42
  ```
  => deconstruct tuple
  ```rust
  let _0 = 42;
  let (_0, _1) = (1,2);     // >-|
  let v = _0;               // <-|
  //  ^ now 1
  ```
  * I tried to get the scope at tuple declaration and its usages. And then iterate all names with [`process_all_names`](145b51f9da/crates/hir/src/semantics.rs (L935)). But that doesn't find all local names for declarations (`let t = (1,2)`) (for usages it does)
  * This isn't unique to this Code Assist, but happen in others too (like `extract into variable` or `extract into function`). But here a name conflict is more likely (when destructuring multiple tuples, for examples nested ones (`let t = ((1,2),3)` -> `let (_0, _1) = ...` -> `let ((_0, _1), _1) = ...` -> error))
  * IntelliJ rust plugin does handle this (-> name is `_00`)

Co-authored-by: BooksBaum <15612932+Booksbaum@users.noreply.github.com>
2021-08-19 15:19:06 +00:00
BooksBaum
8a9feeddd3
Remove match_ast usage 2021-08-19 14:17:23 +02:00
BooksBaum
2c27adc0a3
Remove match_ast! macro call
Add node about uncommenting tuple in macro call
2021-08-19 14:09:52 +02:00
Yoshua Wuyts
586d6fca8c Fix codegen for is_method documentation
Previously the generated paths were invalid. This fixes that.
2021-08-19 13:31:16 +02:00
Dawer
08694dc1ef minor: address review comments 2021-08-18 19:04:10 +05:00
Dawer
98847e4d5b internal: use the new edit_in_place::Indent API 2021-08-18 18:08:14 +05:00
BooksBaum
b1ebb82f32
Add deref (*) handling for usages of ref tuples 2021-08-17 19:23:07 +02:00
BooksBaum
384fae7fcd
Switch order of assists
Destructure in sub-pattern before Destructure in place to favor the
first one
2021-08-17 19:23:06 +02:00
BooksBaum
d0cf28322a
Add generated doctest 2021-08-17 19:23:06 +02:00
BooksBaum
5b9f8e7e8e
Fix errors from rebase master
Note:
2nd Assist description is moved down: generated doc tests extracts now
all tests (previously only the first one). But it uses the first
`Assist` name -- which is the wrong one for the 2nd test. And 2nd assist
is currently disabled -> would fail anyway.
2021-08-17 19:23:06 +02:00
BooksBaum
b441aa2046
Cleanup according to style-requirements in tests 2021-08-17 19:23:06 +02:00
BooksBaum
45ef57bd23
Fix: different assist ids in doc and code 2021-08-17 19:23:05 +02:00
BooksBaum
0d2490f785
Handle tuple in macro call
Only tuple name is handled (uncommented), not tuple index
2021-08-17 19:23:05 +02:00
BooksBaum
450a9c5b12
Add destructure in sub-pattern (after @) 2021-08-17 19:23:05 +02:00
BooksBaum
3e4051938d
Handle mut & ref 2021-08-17 19:23:04 +02:00
BooksBaum
478b2988c0
Add Destructure Tuple 2021-08-17 19:22:57 +02:00
Laurențiu Nicola
c9f448a834 Fix some clippy lints 2021-08-16 22:04:26 +03:00
Yoshua Wuyts
067dc660ae implement feedback from review 2021-08-16 18:23:51 +02:00
Yoshua Wuyts
9374d526da fix debug tuple structs 2021-08-16 17:39:08 +02:00
Yoshua Wuyts
b67378f53e fix debug record structs 2021-08-16 12:58:06 +02:00
Aleksey Kladov
3c49a9f079 minor: move functionality to a better place 2021-08-14 20:43:28 +03:00
Aleksey Kladov
e300f58d2c internal: remove one more usage of old editing API. 2021-08-14 20:17:16 +03:00
Aleksey Kladov
a26b1c3923 internal: remove old editing API 2021-08-14 19:45:47 +03:00
bors[bot]
5d7ff6efe3
Merge #9894
9894: internal: use standard test style r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-08-14 15:59:47 +00:00
Aleksey Kladov
c3bbeb947c internal: use standard test style 2021-08-14 18:53:21 +03:00
Lukas Wirth
37ad9cb2a5 Don't use non cache syntaxnodes in generate_function for lookups 2021-08-14 17:51:11 +02:00
Aleksey Kladov
a78decc466 internal: make assist naming consistent 2021-08-14 18:39:56 +03:00
Aleksey Kladov
dc17b35e62 internal: remove a remnant of old editing infra 2021-08-14 18:24:42 +03:00
Aleksey Kladov
90357a9090 internal: merge hir::BinaryOp and ast::BinOp 2021-08-14 18:10:01 +03:00
Aleksey Kladov
6df00f8495 internal: make naming consistent 2021-08-14 17:01:28 +03:00
Aleksey Kladov
beca92b245 internal: make invert binary op more robust
Previously, we only inverted comparison operators (< and the like) if
the type implemented Ord. This doesn't make sense: if `<` works, then
`>=` will work as well!

Extra semantic checks greatly reduce robustness and predictability of
the assist, it's better to keep things simple.
2021-08-14 16:40:00 +03:00
bors[bot]
43af7920af
Merge #9871
9871: Jump to generated func r=mahdi-frms a=rylev

Worked on this with `@yoshuawuyts.`

We thought we ran into an issue with the `generate_function` assist where the code was not being generated in a certain situations. However, it wasn't actually a bug just a very confusing implementation where the cursor is not moved to the newly generated function. This happened when the return type was successfully inferred (and not unit). The function would be generated, but selection would not be changed.

This can be very confusing: If the function is generated somewhat far from where the assist is being invoked, the user never sees that the code was generated (nor are they given the chance to actually implement the function body). 

This PR makes it so that the cursor is _always_ moved to somewhere in the newly generated function. In addition, if we can infer unit as the type, then we do not still generate `-> ()` as the return type. Instead, we simply omit the return type.

This means the selection will move to either one of two places:
* A generated `-> ()` return type when we cannot successfully infer the return type.
* The `todo!()` body when we can successfully infer the return type.

Co-authored-by: Ryan Levick <me@ryanlevick.com>
2021-08-13 11:19:16 +00:00
Ryan Levick
1cc43ab7f4 Add documentation on how whether a generated return type should be focused or not 2021-08-13 11:46:08 +02:00
Jonas Schievink
d568e7686a Support if let match guards 2021-08-13 00:25:14 +02:00
bors[bot]
baf1494374
Merge #9807
9807: Implicit `Sized` bounds r=iDawer a=iDawer

This should close #8984 

`hir_ty`:
- Type parameters, associated types and `impl Trait` are `Sized` by deafault except `Self` in a trait.
- Implicit `Sized` bound is added to end of predicate list. It does not check if such bound is present already. Also it does not track the bound is implicit.
- Allowed ambiguous unsize coercion if Chalk returns definite guidance.
- Allowed ambiguous autoderef if Chalk returns definite guidance.

`hir_def`:
- `ItemTree` pretty printing shows `?Sized` bounds.

`HirDisplay`:
- `impl Trait` with weird bounds rendered correctly.
- `Sized`/`?Sized` bounds are not shown if they are default.

### Perf
`./target/rust-analyzer-baseline_8a843113 -q analysis-stats --memory-usage .`
```
Database loaded:     1.63s, 287minstr, 91mb
  crates: 38, mods: 741, decls: 15914, fns: 11835
Item Collection:     26.80s, 73ginstr, 338mb
  exprs: 318994, ??ty: 398 (0%), ?ty: 435 (0%), !ty: 174
Inference:           50.28s, 116ginstr, 516mb
Total:               77.08s, 189ginstr, 855mb
```

`./target/rust-analyzer-sized-fixed_ambig_coercion-de074fe6 -q analysis-stats --memory-usage .`
```
Database loaded:     1.63s, 287minstr, 91mb
  crates: 38, mods: 741, decls: 15914, fns: 11835
Item Collection:     26.95s, 73ginstr, 338mb
  exprs: 318994, ??ty: 398 (0%), ?ty: 435 (0%), !ty: 166
Inference:           96.39s, 234ginstr, 543mb
Total:               123.33s, 307ginstr, 881mb
```


Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
2021-08-12 17:55:29 +00:00
Dawer
6c366ade00 Clean up 2021-08-12 21:20:28 +05:00
Ryan Levick
a5c46e5a88 Factor out return type handling for both function and method 2021-08-12 16:05:32 +02:00
Ryan Levick
0834e05045 Always fall back to focusing on generated function body 2021-08-12 14:56:39 +02:00
Ryan Levick
e8de34b6b3 Use term on label for generating method impl 2021-08-12 14:01:15 +02:00
bors[bot]
1376ece497
Merge #9863
9863: feat: Generate default trait fn impl when generating `PartialEq` r=yoshuawuyts a=yoshuawuyts

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

r? `@veykril`

Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
2021-08-12 10:18:02 +00:00
Yoshua Wuyts
97ec6a27ec add make::ext::path_from_idents 2021-08-12 12:17:09 +02:00
Yoshua Wuyts
df19895ba6 implement feedback from review 2021-08-12 11:50:01 +02:00
Yoshua Wuyts
8ed24029c1 rm remaining todo comment 2021-08-11 23:30:00 +02:00
Yoshua Wuyts
2fff019b6b improve codegen 2021-08-11 20:33:13 +02:00
Yoshua Wuyts
6c4a94b03a finishing touches 2021-08-11 20:04:05 +02:00
Yoshua Wuyts
c4e10c81b9 gen partialeq for tuple enums 2021-08-11 20:01:19 +02:00
Yoshua Wuyts
741e27b414 dedup PartialEq for Record enums 2021-08-11 19:40:47 +02:00
Lukas Wirth
6045059916 extract_variable handles selection ranges better 2021-08-11 17:52:09 +02:00
Yoshua Wuyts
fc17eb482d gen PartialEq for Record enums
Co-Authored-By: Ryan Levick <rylev@users.noreply.github.com>
2021-08-11 17:15:12 +02:00
bors[bot]
f438dbb100
Merge #9860
9860: fix: Fix extract_function tagging params as mut unnecessarily r=Veykril a=Veykril

Fixes #9822
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-08-11 15:10:26 +00:00
Lukas Wirth
6d960ab032 Fix extract_function tagging params as mut unnecessarily 2021-08-11 17:09:51 +02:00
Aleksey Kladov
629c68e80d internal: document that ascription is preferred to a turbo fish 2021-08-11 14:16:15 +03:00
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
Dawer
0f6621fbfa Fix tests. 2021-08-04 20:20:14 +05:00
Lukas Wirth
29c4ae6f9a extract_assist is aware of the expression owner 2021-08-03 20:47:51 +02:00
Lukas Wirth
9edaf0cad8 extract_function is const aware 2021-08-03 18:43:28 +02:00
Lukas Wirth
e62ce6f61a Reorganize functions in extract_function assist 2021-08-03 18:24:33 +02:00
Lukas Wirth
027a99fc70 Do no tear comments apart in extract_function assist 2021-08-03 17:39:49 +02:00
Lukas Wirth
b96f1adf5c Give TypeInfo fields and methods more appropriate names 2021-08-03 17:28:51 +02:00
Lukas Wirth
8afa2722b2 Revise TypeInfo::ty usage 2021-08-03 17:24:43 +02:00
Lukas Wirth
25ff7171c4 Introduce TypeInfo 2021-08-03 16:41:53 +02:00
vsrs
cc33b0dc82 Update the docs 2021-08-02 20:24:18 +03:00
vsrs
6031dee145 Enable assist only if an item is selected 2021-08-02 19:58:41 +03:00
vsrs
e71b239d37 fix generated tests 2021-08-02 19:49:36 +03:00
vsrs
0088f84c88 Allow several samples in "// Assist:" comments. 2021-08-02 19:49:36 +03:00
vsrs
580e5e277a Some comment tests 2021-08-02 19:49:36 +03:00
vsrs
4fa66f1e1e Sort struct enum variant fields 2021-08-02 19:49:36 +03:00
vsrs
6f8888cfe7 Sort enums 2021-08-02 19:49:36 +03:00
vsrs
2c7e99b209 Sort unions 2021-08-02 19:49:36 +03:00
vsrs
b31bf74c13 cargo fmt 2021-08-02 19:49:36 +03:00
vsrs
0694f18636 Sort struct fields 2021-08-02 19:49:36 +03:00
vsrs
643f3931f7 Sort trait and impl methods 2021-08-02 19:49:36 +03:00
Lukas Wirth
432bb222c3 Simplify inline_local_variable assist 2021-08-02 15:27:47 +02:00
bors[bot]
93873b0ce1
Merge #9739
9739: generate function assist favors deref cmpt types r=matklad a=mahdi-frms

Fixes #9713

Although that's still not relying on sematic info.

Co-authored-by: mahdi-frms <mahdif1380@outlook.com>
2021-07-31 15:08:29 +00:00
Lukas Wirth
b363755901 Fix hover range for derive inputs 2021-07-31 14:59:31 +02:00
Lukas Wirth
7c7c4543da Replace useless types 2021-07-31 14:29:15 +02:00
Lukas Wirth
778e6e8ba8 add_explicit_type is applicable for closure parameters 2021-07-31 14:04:34 +02:00
mahdi-frms
a5edf6de79 generate function assist favors deref cmpt types 2021-07-31 16:34:09 +04:30
bors[bot]
1f817833e7
Merge #9727
9727: internal: Simplify extract_function assist r=Veykril a=Veykril

also fixes #7839(blocked on #9728)

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-07-30 14:56:50 +00:00
Lukas Wirth
3e351cc0ba Add trailing comma when extracting match arm expressions into functions 2021-07-30 16:48:13 +02:00
bors[bot]
4cc961007f
Merge #9731
9731: feat: Add `replace_char_with_string` assist r=Veykril a=Veykril

Adds the counterpart for the `replace_string_with_char` assist and fixes the assist not escaping the `'` in the string `"'"` when transforming that to a char.
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-07-30 14:47:38 +00:00
Lukas Wirth
17a47a830b Add replace_char_with_string assist 2021-07-30 16:46:06 +02:00
bors[bot]
0a3ac7a96c
Merge #9728
9728: fix: Attach comma token to MATCH_ARM instead of MATCH_ARM_LIST r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-07-30 14:11:37 +00:00
Lukas Wirth
82c1e61887 Fix assists assuming comma belonging to MATCH_ARM_LIST 2021-07-30 16:01:26 +02:00
Lukas Wirth
322513b06c Reorder definitions in extract_function 2021-07-29 22:10:40 +02:00
Lukas Wirth
b21f66fce3 Simplify extract_function assist 2021-07-29 22:07:56 +02:00
bors[bot]
8232804d3e
Merge #9706
9706: minor: perf and grammar fixes r=lnicola a=lnicola



Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
2021-07-29 16:14:10 +00:00
Lukas Wirth
b537cb186e Use more strictly typed syntax nodes for analysis in extract_function assist 2021-07-29 17:26:37 +02:00
Lukas Wirth
2b461c50d7 Refine extraction targets of extract_function assist 2021-07-29 15:45:05 +02:00