Commit graph

211 commits

Author SHA1 Message Date
Laurențiu Nicola
88cee24c6c Enable thread-local coverage marks 2021-03-15 16:02:50 +02:00
Lukas Wirth
6e6d75bbdc Attach trivia to ast::Union nodes 2021-03-14 11:11:01 +01: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
kjeremy
08e0e9976d cargo update and lexer 2021-03-10 13:47:12 -05:00
Luiz Carlos Mourão Paes de Carvalho
61fb16577b feat: add expr_for_loop to make in syntax 2021-03-09 23:54:35 -03:00
bors[bot]
21913d0fdb
Merge #7873 #7933
7873: Consider unresolved qualifiers during flyimport r=matklad a=SomeoneToIgnore

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

Takes unresolved qualifiers into account, providing better completions (or none, if the path is resolved or do not match).

Does not handle cases when both path qualifier and some trait has to be imported: there are many extra issues with those (such as overlapping imports, for instance) that will require large diffs to address.

Also does not do a fuzzy search on qualifier, that requires some adjustments in `import_map` for better queries and changes to the default replace range which also seems relatively big to include here.

![qualifier_completion](https://user-images.githubusercontent.com/2690773/110040808-0af8dc00-7d4c-11eb-83db-65af94e843bb.gif)


7933: Improve compilation speed r=matklad a=matklad

bors r+
🤖

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-03-09 11:58:48 +00:00
Aleksey Kladov
867fdf8f03 Improve compilation speed 2021-03-09 14:54:50 +03:00
Kirill Bulatov
778deb38fe Better strip turbofishes 2021-03-08 23:59:39 +02:00
Laurențiu Nicola
fc9eed4836 Use upstream cov-mark 2021-03-08 22:19:44 +02:00
Aleksey Kladov
abb6b8f14c Use the same name in xtask and test utils 2021-03-08 21:45:06 +03:00
kjeremy
41d1b4cd26 Update lexer 2021-03-02 15:33:22 -05:00
bors[bot]
2183d65c97
Merge #7777
7777: Implement line<->block comment assist r=Veykril a=djrenren

Fixes: https://github.com/rust-analyzer/rust-analyzer/issues/6515

Co-authored-by: John Renner <john@jrenner.net>
2021-03-02 08:04:38 +00:00
kjeremy
d42730b76e bump crates 2021-02-25 10:34:48 -05:00
John Renner
9eecba4dbf Implement line<->block comment assist 2021-02-24 17:13:00 -08:00
Lukas Wirth
694f7a7e9f Add tests for apply_demorgan 2021-02-24 11:58:37 +01:00
Laurențiu Nicola
48ae948b22 Bump deps 2021-02-21 19:13:11 +02:00
Laurențiu Nicola
af4148970a Fix incorrect missing field diagnostic with box patterns 2021-02-20 12:36:17 +02:00
Lukas Wirth
2887426da0 Revert "Replace usage of ast::NameOrNameRef with ast::NameLike"
This reverts commit e1dbf43cf8.
2021-02-17 15:00:44 +01:00
Lukas Wirth
e1dbf43cf8 Replace usage of ast::NameOrNameRef with ast::NameLike 2021-02-17 14:02:34 +01:00
Lukas Wirth
e52bdc55ef Implement ast::AstNode for NameLike and move it to node_ext 2021-02-16 19:27:08 +01:00
bors[bot]
b7a6d830be
Merge #7687
7687: Specialization for async traits r=matklad a=arnaudgolfouse

Fixes #7669.

Adapting the parser seemed to be all that was needed, but I am not very experienced with the codebase. Is this enough ?

Co-authored-by: Arnaud <arnaud.golfouse@laposte.net>
2021-02-16 14:16:47 +00:00
bors[bot]
88e8b0a5fa
Merge #7620
7620: Support control flow in `extract_function` assist r=matklad a=cpud36

Support `return`ing from outer function, `break`ing and `continue`ing outer loops when extracting function.

# Example
Transforms
```rust
fn foo() -> i32 {
  let items = [1,2,3];
  let mut sum = 0;
  for &item in items {
    <|>if item == 42 {
      break;
    }<|>
    sum += item;
  }
  sum
}
```
Into 
```rust
fn foo() -> i32 {
  let items = [1,2,3];
  let mut sum = 0;
  for &item in items {
    if fun_name(item) {
      break;
    }
    sum += item;
  }
  sum
}

fn fun_name(item: i32) -> bool {
  if item == 42 {
    return true;
  }
  false
}
```

![add_explicit_type_infer_type](https://user-images.githubusercontent.com/4218373/107544222-0fadf280-6bdb-11eb-9625-ed6194ba92c0.gif)

# Features

Supported variants
- break and function does not return => uses `bool` and plain if
- break and function does return => uses `Option<T>` and matches on it
- break with value and function does not return => uses `Option<T>` and if let
- break with value and function does return => uses `Result<T, U>` and matches on t
- same for `return` and `continue`(but we can't continue with value)

Assist does handle nested loops and nested items(like functions, modules, impls)

Try `expr?` operator is allowed together with `return Err(_)` and `return None`.
`return expr` is not allowed.

# Not supported
## Mixing `return` with `break` or `continue`
If we have e.g. a `return` and a `break` in the selected code, it is unclear what the produced code should look like.
We can try `Result<T, Option<U>>` or something like that, but it isn't idiomatic, nor it is established. Otherwise, implementation
is relatively simple.

## `break` with label
Not sure how to handle different labels for multiple `break`s.

[edit] implemented try `expr?`

Co-authored-by: Vladyslav Katasonov <cpud47@gmail.com>
2021-02-16 14:01:09 +00:00
Arnaud
95d239da99 Specialization for async traits 2021-02-15 18:33:12 +01:00
Lukas Wirth
7b64622780 Don't rename field record patterns directly 2021-02-13 23:47:21 +01:00
Vladyslav Katasonov
9eb19d92dd allow try expr? when extacting function 2021-02-13 22:04:52 +03:00
Vladyslav Katasonov
f345d1772a handle return, break and continue when extracting function 2021-02-13 22:04:52 +03:00
Lukas Wirth
d644728d82 Refactor reference searching to work with the ast 2021-02-12 18:58:28 +01:00
Aleksey Kladov
61f15b72ac Add parsing benchmark 2021-02-09 21:52:34 +03:00
Aleksey Kladov
4b1279d0b1 Infra for "unit" benchmarking 2021-02-09 20:25:39 +03:00
kjeremy
0c3b38695a Update crates
Pulls in https://github.com/rust-lang/chalk/pull/682
2021-02-08 11:38:51 -05:00
Aleksey Kladov
7022ea52b5 AdtDef -> Adt 2021-02-07 14:15:02 +03:00
bors[bot]
ac5958485e
Merge #7535
7535: Extract function assist r=cpud36 a=cpud36

This PR adds `extract function/method` assist. closes #5409.

# Supported features
Assist should support extracting from expressions(`1`, `2 + 2`, `loop { }`) and from a series of statements, e.g.:
```rust
foo();
$0bar();
baz();$0
quix();
```
Assist also supports extracting parameters, like:
```rust
fn foo() -> i32 {
  let n = 1;
  $0n + 1$0
}

// -
fn foo() -> i32 {
  let n = 1;
  fun_name(n)
}

fn fun_name(n: i32) -> i32 {
  n + 1
}
```

Extracting methods also generally works.

Assist allows referencing outer variables, both mutably and immutably, and handles handles access to variables local to extracted function:
```rust
fn foo() {
  let mut n = 1;
  let mut m = 2;
  let mut moved_v = Vec::new();
  let mut ref_mut_v = Vec::new();
  $0
  n += 1;
  let k = 1;
  moved_v.push(n);
  let r = &mut m;
  ref_mut_v.push(*r);
  let h = 3;
  $0
  n = ref_mut_v.len() + k;
  n -= h + m;
}

// -
fn foo() {
  let mut n = 1;
  let mut m = 2;
  let mut moved_v = Vec::new();
  let mut ref_mut_v = Vec::new();
 
  let (k, h) =  fun_name(&mut n, moved_v, &mut m, &mut ref_mut_v);

  n = ref_mut_v.len() + k;
  n -= h + m;
}

fn fun_name(n: &mut i32, mut moved_v: Vec<i32>, m: &mut i32, ref_mut_v: &mut Vec<i32>) -> (i32, i32) {
  *n += 1;
  let k = 1;
  moved_v.push(*n);
  let r = m;
  ref_mut_v.push(*r);
  let h = 3;
  (k, h)
}
```

So we handle both input and output paramters

# Showcase

![extract_cursor_in_range_3](https://user-images.githubusercontent.com/4218373/106980190-c9870800-6770-11eb-83d9-3d36b2550ff6.gif)
![fill_match_arms_discard_wildcard](https://user-images.githubusercontent.com/4218373/106980197-cbe96200-6770-11eb-96b0-14c27894fac0.gif)
![ide_db_helpers_handle_kind](https://user-images.githubusercontent.com/4218373/106980201-cdb32580-6770-11eb-9e6e-6ac8155d65ac.gif)
![ide_db_imports_location_local_query](https://user-images.githubusercontent.com/4218373/106980205-cf7ce900-6770-11eb-8516-653c8fcca807.gif)

# Working with non-`Copy` types

Consider the following example:
```rust
fn foo() {
  let v = Vec::new();
  $0
  let n = v.len();
  $0
  let is_empty = v.is_empty();
}
```
`v` must be a parameter to extracted function. 
The question is, what type should it have.
It could be `v: Vec<i32>`, or `v: &Vec<i32>`. 
The former is incorrect for `Vec<i32>`, but the later is silly for `i32`.

To resolve this we need to know if the type implements `Copy` trait.

I didn't find any api available from assists to query this. 
`hir_ty::method_resolution::implements` seems relevant, but is isn't publicly re-exported from `hir`.

# Star(`*`) token and pointer dereference

If I understand correctly, in order to create expression like `*p`, one should use `ast::make::expr_prefix(T![*], ...)`, which
in turn calls `token(T![*])`.

`token` does not have star in `tokens::SOURCE_FILE`, so this panics.
I had to add `*` to `SOURCE_FILE` to make it work.

Correct me if this is not intended way to do this.

# Lowering access `value -> mut ref -> shared ref`

Consider the following example:
```rust
fn foo() {
  let v = Vec::new();
  $0 let n = v.len(); $0
}
```
`v` is not used after extracted function body, so both `v: &Vec<i32>` and `v: Vec<i32>` would work.
Currently the later would be chosen.

We can however check the body of extracted function and conclude that `v: &Vec<i32>` is sufficient.
Using `v: &Vec<i32>`(that is a minimal required access level) might be a better default.
I am unsure.

# Cleanup
The assist seems to be reasonably handling most of common cases.
If there are no concerns with code it produces(i.e. with test cases), I will start cleaning up

[edit]
added showcase


Co-authored-by: Vladyslav Katasonov <cpud47@gmail.com>
2021-02-05 02:55:56 +00:00
Vladyslav Katasonov
f102616aae allow modifications of vars from outer scope inside extracted function
It currently allows only directly setting variable.
No `&mut` references or methods.
2021-02-03 23:45:03 +03:00
Edwin Cheng
e73ffbf1e5 Add cargo file tidy test 2021-02-03 22:01:09 +08:00
Laurențiu Nicola
6b60206669 Bump rustc_lexer 2021-02-02 17:40:01 +02:00
Lukas Wirth
6c2ce55150 Fix ast::String::value not properly escaping in some cases 2021-01-30 16:31:19 +01:00
Laurențiu Nicola
efafcf2428 Bump deps 2021-01-27 14:22:19 +02:00
Aleksey Kladov
1df711b95c ⬆️ rowan 2021-01-25 12:32:35 +03:00
Lukas Wirth
70d43c3faf Add validation for mutable const items 2021-01-24 02:17:41 +01:00
kjeremy
f006517857 Up lexer 2021-01-21 09:31:06 -05:00
Aleksey Kladov
3429b32ad1 ⬆️ rowan
It now stores text inline with tokens
2021-01-20 14:04:53 +03:00
Aleksey Kladov
46b4f89c92 . 2021-01-20 01:56:11 +03:00
Aleksey Kladov
cd21b0e9c1 ⬆️ rowan 2021-01-19 22:11:42 +03:00
Lukas Wirth
b26002410b Parse impl const Trait 2021-01-18 20:18:02 +01:00
Jonas Schievink
872bf09381 Add MacroType syntax 2021-01-18 17:56:35 +01:00
bors[bot]
9daba961f2
Merge #7291
7291: Wrap remaining self/super/crate in Name{Ref} r=matklad a=Veykril

That should be the remaining special casing for `self` 🎉 

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-01-18 16:13:06 +00:00
Aleksey Kladov
b38414c7f4 When building an item-tree, keep fewer nodes in memory 2021-01-16 23:07:28 +03:00
Lukas Wirth
98718e0544 Wrap remaining self/super/crate in Name{Ref} 2021-01-15 22:18:43 +01:00
unexge
a3a722de9f Add Unmerge Use assist 2021-01-15 22:14:51 +03:00
bors[bot]
8a869e870a
Merge #7288
7288: Handle self/super/crate in PathSegment as NameRef r=matklad a=Veykril

Wrapping self/super/crate in NameRef as per https://github.com/rust-analyzer/rust-analyzer/pull/7261#issuecomment-760023172



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-01-15 18:40:47 +00:00