Commit graph

10265 commits

Author SHA1 Message Date
bors[bot]
8eee9149e8
Merge #7848
7848: Bump cargo_metadata r=matklad a=lnicola



Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
2021-03-02 12:31:15 +00:00
Laurențiu Nicola
b20708f6ee Bump cargo_metadata 2021-03-02 14:27:29 +02:00
Aleksey Kladov
3038579c8e Switch from pico-args to xflags 2021-03-02 15:08:20 +03: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
bors[bot]
b7fa6dfabc
Merge #7844
7844: Fix ProcMacroClient dropped too early in cli r=edwin0cheng a=edwin0cheng

Fix #7843

bors r+

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2021-03-02 05:20:32 +00:00
Edwin Cheng
877521325f Fix ProcMacroClient dropped too early in cli 2021-03-02 13:14:05 +08:00
bors[bot]
477c1ac105
Merge #7827
7827: Fix proc macro TokenStream::from_str token ids r=vlad20012 a=vlad20012

To be honest, I don't know what it changes from a user perspective.

Internally, this fixes spans (token ids) of a `TokenStream` parsed from a string:

```rust
#[proc_macro_derive(FooDerive)]
pub fn foo_derive(item: TokenStream) -> TokenStream {
    "fn foo() {}".parse().unwrap()
}
```

Previously, `TokenStream` was constructed from tokens with incremental ids (that conflicted with call-site tokens). Now they are `-1`.

Co-authored-by: vlad20012 <beskvlad@gmail.com>
2021-03-02 00:01:14 +00:00
John Renner
f5cde97aae Apply edits 2021-03-01 11:41:22 -08:00
bors[bot]
a6ee8e9e76
Merge #7829 #7833
7829: Bump deps r=matklad a=lnicola

Unfortunately, this brings a bunch of proc macros dep because `cargo-metadata` went full-in on `derive-builder`. I'm not sure what we can do here..

7833: Use chalk_ir::Mutability r=Veykril a=Veykril



Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-01 19:31:01 +00:00
Lukas Wirth
7072f59fc6 Use chalk_ir::Mutability 2021-03-01 19:57:36 +01:00
Jonas Schievink
0dcec31553 Fix find_path when inner items are present 2021-03-01 19:39:17 +01:00
vlad20012
a157f19bf5
Fix proc macro TokenStream::from_str token ids 2021-03-01 19:55:30 +03:00
Laurențiu Nicola
63e8bdb8a2 Bump chalk 2021-03-01 18:17:03 +02:00
Laurențiu Nicola
a445584029 Use an unversioned profile dependency in mbe 2021-03-01 18:11:12 +02:00
bors[bot]
f3139d46b0
Merge #7778
7778: Fix lowering trailing self paths in UseTrees r=Veykril a=Veykril

Noticed that hovering over `self` in a use tree like `use foo::bar::{self}` showing documentation and such for the current module instead of `bar`.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-01 14:50:58 +00:00
Lukas Wirth
5d121cdb45 Introduce Ty::Alias 2021-03-01 14:24:00 +01:00
bors[bot]
cda13d5461
Merge #7823
7823: Being Ty::InferenceVar closer to chalk equivalent r=flodiebold a=Veykril



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-01 12:57:26 +00:00
Lukas Wirth
4b7fc69315 Assert index relationship between type_variable_table and var_unification_table 2021-03-01 13:54:17 +01:00
Lukas Wirth
11a1f13cd1 Being Ty::InferenceVar closes to chalk equivalent 2021-03-01 12:41:29 +01:00
bors[bot]
1c7b2b8c07
Merge #7822
7822: Paperover a bug in cargo-workspace for publish RA r=lnicola a=edwin0cheng

r? @lnicola 

See also https://github.com/pksunkara/cargo-workspaces/issues/39

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2021-03-01 11:29:47 +00:00
Edwin Cheng
43f52a965c Paperover a bug in cargo-workspace for publish RA 2021-03-01 19:23:45 +08:00
bors[bot]
4e5c496199
Merge #7819 #7820
7819: Speedup heavy tests r=matklad a=matklad

bors r+
🤖

7820: Update vscode README with a small features list r=matklad a=Veykril

Nothing grande but I figured this is a bit better than showing almost nothing 

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-03-01 11:15:19 +00:00
Lukas Wirth
91ff47ef6a Update vscode README with a small features list 2021-03-01 11:08:26 +01:00
Aleksey Kladov
4d14617636 Speedup heavy tests
We used to skip sysroot, but this logic got bitrotted, reinstalling.
2021-03-01 12:14:16 +03:00
Dániel Buga
cb39f2df94 Clean up unused field warning 2021-03-01 09:12:42 +01:00
Dániel Buga
2f7b03b01c Remove redundant semicolon 2021-03-01 09:05:14 +01:00
bors[bot]
5df3ee8274
Merge #7816
7816: Lift Ty::Fn into a struct r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-28 22:54:07 +00:00
Lukas Wirth
407196b8c0 Lift FnPointer into a struct 2021-02-28 23:53:21 +01:00
Lukas Wirth
ca7cd41a48 Fix lowering trailing self paths in UseTrees 2021-02-28 23:44:49 +01:00
cynecx
2cdd1ff1b5 hir_ty: use default type generic for box expressions 2021-02-28 21:18:30 +01:00
Lukas Wirth
a3fd2faba5 Remove Substs from Ty::ForeignType 2021-02-28 20:44:09 +01:00
Lukas Wirth
0e995adcf6 Turn Ty::Tuple variant into a tuple-variant 2021-02-28 20:39:43 +01:00
bors[bot]
2fc137b70f
Merge #7813
7813: Inline TypeCtor into Ty r=flodiebold a=Veykril

This removes the `ApplicationTy` variant from `Ty` bringing the representation a lot closer to chalk's `TyKind`.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-28 19:16:51 +00:00
Lukas Wirth
faf2dd49e4 Fix code_model::Type::walk not walking all types 2021-02-28 20:15:56 +01:00
Aleksey Kladov
406d96c7d4 Use consistent naming for assist 2021-02-28 21:14:34 +03:00
Lukas Wirth
a3f5491a1a Inline TypeCtor into Ty 2021-02-28 19:13:37 +01:00
Aleksey Kladov
aa04e3bbb2 Don't spam loop-rewriting assist
The more focused the assist, the better!
2021-02-28 21:08:51 +03:00
Edwin Cheng
cff2201c30 NFA parser for mbe matcher 2021-03-01 00:11:14 +08:00
bors[bot]
803ff2e55e
Merge #7804
7804: Introduce TypeCtor::Scalar r=lnicola a=Veykril

`TypeCtor::Int(..) | TypeCtor::Float(..) | TypeCtor::Char | TypeCtor::Bool` => `TypeCtor::Scalar(..)`, in this case we can actually just straight up use `chalk_ir::Scalar` already since its just a POD without any IDs or anything.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-28 13:36:44 +00:00
bors[bot]
7f57a01b3d
Merge #7808
7808: Fix assert split exprs on comma r=edwin0cheng a=edwin0cheng

bors r+

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2021-02-28 12:53:21 +00:00
Edwin Cheng
30402beeb2 Fix assert split exprs on comma 2021-02-28 20:46:24 +08:00
Vincent Esche
c4e2f32d73 Fixed remaining references to AnalysisChange (now: Change)
(The type was renamed/moved in 8716c4cec3)
2021-02-28 12:57:41 +01:00
Aleksey Kladov
05729fd3c4 For unresolved macros, hightlight only the last segment 2021-02-28 14:29:10 +03:00
Lukas Wirth
7c2dd85a32 Use chalk_ir::Scalar directly 2021-02-28 11:06:37 +01:00
Edwin Cheng
bf8bc5c882 Fix non-latin characters doc comment for mbe 2021-02-28 13:49:08 +08:00
bors[bot]
e0437f899c
Merge #7802
7802: Fix builtin macros split exprs on comma r=edwin0cheng a=edwin0cheng

Fixes #7640

bors r+

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2021-02-28 05:07:09 +00:00
Edwin Cheng
f5bf1a9650 Fix builtin macros split exprs on comma 2021-02-28 13:06:17 +08:00
Jonas Schievink
6990b89b26 Restrict visibilities to the containing DefMap 2021-02-28 04:47:38 +01:00
Lukas Wirth
5183c9f083 Introduce TypeCtor::Scalar 2021-02-28 01:20:04 +01:00
bors[bot]
f682627da4
Merge #7797
7797: Format generated lints and features manually r=matklad a=lnicola

As `quote` and `rustfmt` leave them on a single line, which makes running `grep` in the repository quite annoying.

Also removes a dead `gen_features.rs` file (`gen_lint_completions.rs` does the same thing).

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
2021-02-27 14:56:24 +00:00
bors[bot]
a8cf346b97
Merge #7566
7566: Add benchmark tests for mbe r=matklad a=edwin0cheng

This PR add more real world tests dumped from `rust-analyzer analysis-stats .` to benchmark its performance.

cc #7513

r? @matklad 

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2021-02-27 14:39:22 +00:00
Laurențiu Nicola
0fb01367f5 Format generated features manually instead of relying on rustfmt 2021-02-27 16:25:06 +02:00
Domantas Jadenkus
558bcf4e0b generate try_into instead of into 2021-02-27 12:21:57 +02:00
Domantas Jadenkus
d4b6cbe5b6 rename generate_enum_match_method file to match assist name 2021-02-27 12:21:56 +02:00
Domantas Jadenkus
f098a2b31b move into_ and as_ generation to a separate file 2021-02-27 12:21:06 +02:00
Domantas Jadenkus
6f25fef36a cargo fmt 2021-02-27 12:06:00 +02:00
Domantas Jadenkus
e0f08fcc20 add generate_enum_as_method assist 2021-02-27 12:06:00 +02:00
Domantas Jadenkus
642786986f deduplicate some 2021-02-27 12:05:59 +02:00
Domantas Jadenkus
2dcd5d7a7c add generate_enum_into_method assist 2021-02-27 12:05:59 +02:00
Domantas Jadenkus
4ab285a8e5 make generate_enum_is_method work on any variants 2021-02-27 12:05:59 +02:00
Domantas Jadenkus
f67586066d rename existing assist to generate_enum_is_method 2021-02-27 12:05:59 +02:00
Denys Zadorozhnyi
eb3c23588d
Disable "Flip comma" assist inside a macro call 2021-02-26 19:12:53 +02:00
Sanchith Hegde
3eaec3cd33
docs(ide_assists): Change importMergeBehaviour to importMergeBehavior 2021-02-26 12:54:50 +05:30
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
Edwin Cheng
23dbf36c7d Add benchmark test for mbe 2021-02-25 05:47:13 +08:00
bors[bot]
dc14c432f5
Merge #7741
7741: Add convert_for_to_iter_for_each assist r=mattyhall a=mattyhall

Implements one direction of #7681 

I wonder if this tries to guess too much at the right thing here. A common pattern is:

```rust
let col = vec![1, 2, 3];
for v in &mut col {
  *v *= 2;
}
// equivalent to:
col.iter_mut().for_each(|v| *v *= 2);
```

I've tried to detect this case by checking if the expression after the `in` is a (mutable) reference and if not inserting iter()/iter_mut(). This is just a convention used in the stdlib however, so could sometimes be wrong. I'd be happy to make an improvement for this, but not sure what would be best. A few options spring to mind:
1. Only allow this for types that are known to have iter/iter_mut (ie stdlib types)
2. Try to check if iter/iter_mut exists and they return the right iterator type
3. Don't try to do this and just add `.into_iter()` to whatever is after `in`

Co-authored-by: Matt Hall <matthew@quickbeam.me.uk>
2021-02-24 19:24:22 +00:00
Matt Hall
a28e862825 Address further review comments
* Use known names for iter/iter_mut method (simplifies checking if the
  method exists
* Extract code to check assist with fixtures to function
2021-02-24 19:23:12 +00:00
Lukas Wirth
694f7a7e9f Add tests for apply_demorgan 2021-02-24 11:58:37 +01:00
lbrande
9db970ee08 De Morgan's Law assist now correctly inverts <, <=, >, >=. 2021-02-24 11:58:37 +01:00
lbrande
f7a4a87de2 De Morgan's Law assist now correctly parenthesizes binary expressions. 2021-02-24 11:58:37 +01:00
Lukas Wirth
9e8f4d31a4 Pickup ConstReference patterns in FindUsages 2021-02-23 23:31:53 +01:00
Matt Hall
98a626450d Address review comments
* Move code to build replacement into closure
* Look for iter/iter_mut methods on types behind reference
2021-02-23 19:19:48 +00:00
Matt Hall
506293ca43 Add convert_for_to_iter_for_each assist 2021-02-23 18:10:01 +00:00
Jonas Schievink
338823f73a is_visible_from_def_map: handle block expressions 2021-02-23 17:56:16 +01:00
Jonas Schievink
cf456d72db Add test 2021-02-23 14:54:01 +01:00
Chetan Khilosiya
eb6cfa7f15 7526: Renamed create ssr to ide_ssr. 2021-02-23 00:59:16 +05:30
Chetan Khilosiya
e4756cb4f6 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
Jonas Bushart
bd525c9d20 Add isize to the list of suffixed integers in typed_integer
The missing `isize` in `typed_integers` seems to just be an oversight.

Might fix: #7751
2021-02-22 14:48:56 +01:00
Aleksey Kladov
58be1edfbb Make more common assist easier to ues 2021-02-22 16:07:08 +03:00
Laurențiu Nicola
d03cfe141c Add test from #7273 2021-02-21 19:22:14 +02:00
Laurențiu Nicola
48ae948b22 Bump deps 2021-02-21 19:13:11 +02:00
Scott McMurray
0fe44d099a Stop mixing Result and Option with ? in inline_local_variable
Depending on the discussion in RFC#3058 this might not end up being necessary, but I think it's a reasonable change regardless.
2021-02-20 14:45:30 -08:00
bors[bot]
62bc753f8b
Merge #7732
7732: Don't lower TypeBound::Lifetime as GenericPredicate::Error r=flodiebold a=Veykril

Basically we just discard the typebound for now instead when lowering to `GenericPredicate`. I think this shouldn't have any other side effects?

Fixes #7683(hopefully for real this time)

I also played around with introducing `GenericPredicate::LifetimeOutlives` and `GenericPredicate::TypeOutlives`(see b9d6904845) but that won't fix this issue(at least not for now) due to lifetime predicate mismatches when resolving methods so I figure this is a good way to fix it for now.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-20 20:17:14 +00:00
Jeremy Kolb
1f3c637a1d Update tests 2021-02-20 14:59:05 -05:00
Lukas Wirth
0799288f01 Don't write trailing whitespace when formatting empty GenericPredicates 2021-02-20 20:43:04 +01:00
Lukas Wirth
9e5192d917 Don't lower TypeBound::Lifetime as GenericPredicate::Error 2021-02-20 19:08:20 +01:00
Matt Hall
8497b5bd75 Use format_to! to make logic a bit nicer 2021-02-20 17:11:04 +00:00
Matt Hall
b963893349 Fix #7712 retain visibility extracting mod to file 2021-02-20 16:51:16 +00:00
Laurențiu Nicola
4c589aded0 Remove documentation of obsolete extend selection command 2021-02-20 17:18:53 +02:00
bors[bot]
1349f6a791
Merge #7726
7726: feat(completion): add doc(hidden) completion for attributes r=lnicola a=bnjjj



Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
2021-02-20 14:18:34 +00:00
Benjamin Coenen
cf32dbd9bf feat(completion): add doc(hidden) completion for attributes
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
2021-02-20 15:16:05 +01:00
Benjamin Coenen
d8559588c0 fix(assist): display where predicates when we want to generate impl
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
2021-02-20 15:05:01 +01:00
Kirill Bulatov
4fe5786c0c Consider import prefix config settings during flyimports 2021-02-20 13:53:50 +02:00
Laurențiu Nicola
c1d37f030f Add test 2021-02-20 12:43:52 +02:00
Laurențiu Nicola
af4148970a Fix incorrect missing field diagnostic with box patterns 2021-02-20 12:36:17 +02:00
Luciano Bestia
864fb063a0 rustfmt 1.4.30-stable 2021-02-18 16:51:21 +01:00
Aleksey Kladov
3db64a400c rename completion -> ide_completion
We don't have completion-related PRs in flight, so lets do it
2021-02-17 17:53:31 +03: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
bors[bot]
4054525c41
Merge #7699
7699: Implement ast::AstNode for NameLike and move it to node_ext r=matklad a=Veykril

With this `search`(and 2 other modules) don't necessarily go through 3 calls of `find_node_at_offset_with_descend` to find the correct node. Also makes the code that searches for NameLikes a bit easier on the eyes imo, though that can be fixed with just a helper function as well so its not that relevant.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-17 13:45:27 +00:00
bors[bot]
056601b41f
Merge #7705
7705: Show hover info of the definition of ConstReference patterns instead of its type r=Veykril a=Veykril

Closes #7671
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-17 13:17:15 +00:00
Lukas Wirth
51dda8ea8e Show hover info of the definition of ConstReference patterns instead of its type 2021-02-17 14:14:58 +01:00
Lukas Wirth
e1dbf43cf8 Replace usage of ast::NameOrNameRef with ast::NameLike 2021-02-17 14:02:34 +01:00
Michał Muskała
528a0bcf9b Avoid transmitting unchanged diagnostics
Reading through the code for diagnostics and observing debug logs, I noticed
that diagnostics are transmitted after every change for every opened file,
even if they haven't changed (especially visible for files with no diagnostics).

This change avoids marking files as "changed" if diagnostics are the same to what
was already sent before. This will only work if diagnostics are always produced in
the same order, but from my limited testing it seems this is the case.
2021-02-17 12:45:17 +01:00
bors[bot]
b3c848ee28
Merge #7703
7703: Allow comments between newlines in chaining hints r=Veykril a=unratito

Currently, chaining hints are not generated if there are comments between newlines, which is a very common pattern:

```rust
let vec = (0..10)
    // Multiply by 2
    .map(|x| x * 2)
    // Add 3
    .map(|x| x + 3)
    .collect::<Vec<i32>>();
```

Besides, it seems a bit weird that this piece of code generates a chaining hint:

```rust
let vec = (0..10)



    .collect::<Vec<i32>>();
```

But this one doesn't:

```rust
let vec = (0..10)

    // This is a comment

    .collect::<Vec<i32>>();
```


Co-authored-by: Paco Soberón <unratito@gmail.com>
2021-02-17 01:24:25 +00:00
bstrie
3c922072ae Remove use of deprecated std::collections::Bound 2021-02-16 18:22:14 -05:00
Paco Soberón
e0eb80eaee Allow comments between newlines in chaining hints 2021-02-17 00:09:56 +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]
054caa81c5
Merge #7690
7690: Extract `fn load_workspace(…)` from `fn load_cargo(…)` r=matklad a=regexident

Unfortunately in https://github.com/rust-analyzer/rust-analyzer/pull/7595 I forgot to `pub use` (rather than just `use`) the newly introduced `LoadCargoConfig`.

So this PR fixes this now.

It also:

- splits up `fn load_cargo` into a "workspace loading" and a "project loading" phase
- adds a `progress: &dyn Fn(String)` to allow third-parties to provide CLI progress updates, too

The motivation behind both of these is the fact that rust-analyzer currently does not support caching.
As such any third-party making use of `ra_ap_…` needs to providing a caching layer itself.
Unlike for rust-analyzer itself however a common use-pattern of third-parties is to analyze a specific target (`--lib`/`--bin <BIN>`/…) from a specific package (`--package`). The targets/packages of a crate can be obtained via `ProjectWorkspace::load(…)`, which currently is performed inside of `fn load_cargo`, effectively making the returned `ProjectWorkspace` inaccessible to the outer caller. With this information one can then provide early error handling via CLI (in case of ambiguities or invalid arguments, etc), instead of `fn load_cargo` failing with a possibly obscure error message. It also allows for annotating the persisted caches with its specific associated package/target selector and short-circuit quickly if a matching cache is found on disk, significantly cutting load times.

Before:

```rust
pub struct LoadCargoConfig {
    pub cargo_config: &CargoConfig,
    pub load_out_dirs_from_check: bool,
    pub with_proc_macro: bool,
}

pub fn load_cargo(
    root: &Path,
    config: &LoadCargoConfig
) -> Result<(AnalysisHost, vfs::Vfs)> {
    // ...
}
```

After:

```rust
pub fn load_workspace(
    root: &Path,
    config: &CargoConfig,
    progress: &dyn Fn(String),
) -> Result<ProjectWorkspace> {
        // ...
}

pub struct LoadCargoConfig {
    pub load_out_dirs_from_check: bool,
    pub with_proc_macro: bool,
}

pub fn load_cargo(
    ws: ProjectWorkspace,
    config: &LoadCargoConfig,
    progress: &dyn Fn(String),
) -> Result<(AnalysisHost, vfs::Vfs)> {
    // ...
}
```


Co-authored-by: Vincent Esche <regexident@gmail.com>
2021-02-16 18:24:28 +00:00
Jonas Schievink
50d18eb0a6 Use cfg(target_arch) instead of a Cargo feature
Not that WASM works right now anyways...
2021-02-16 18:50:06 +01:00
bors[bot]
c9672a0539
Merge #7657
7657: utf8 r=matklad a=matklad

- Prepare for utf-8 offsets
- reduce code duplication in tests
- Make utf8 default, implement utf16 in terms of it
- Make it easy to add additional context for offset conversion
- Implement utf8 offsets

closes #7453

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-02-16 16:34:22 +00:00
Aleksey Kladov
1fcf687657 Fix bitrotted module name 2021-02-16 19:17:32 +03:00
Aleksey Kladov
f3d56b89c5 Enable offset-encoding capability 2021-02-16 19:17:32 +03:00
Aleksey Kladov
c8b9ec8e62 Implement utf8 offsets 2021-02-16 19:17:32 +03:00
Aleksey Kladov
0025836f26 Make it easy to add additional context for offset conversion 2021-02-16 19:17:32 +03:00
Aleksey Kladov
95209aa3f8 Make utf8 default, implement utf16 in terms of it 2021-02-16 19:17:32 +03:00
Aleksey Kladov
00cc778c8c reduce code duplication in tests 2021-02-16 19:17:32 +03:00
Aleksey Kladov
2cb4ac9eb4 Prepare for utf-8 offsets 2021-02-16 19:17:32 +03:00
kjeremy
f9bb398cc5 Fix a few clippy::perf warnings 2021-02-16 10:55:34 -05:00
Vincent Esche
1a44168260 Split pub fn cargo_load into pub fn load_workspace_at and pub fn load_workspace 2021-02-16 16:37:52 +01:00
bors[bot]
cc49502ab4
Merge #7695
7695: Bump lsp-types r=matklad a=kjeremy

Nothing to see here...

Co-authored-by: kjeremy <kjeremy@gmail.com>
2021-02-16 14:32:33 +00:00
kjeremy
88a5e1d036 Bump lsp-types 2021-02-16 09:21:07 -05: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
ivan770
66de90b85a
Actually fix runnable order 2021-02-15 16:09:50 +02:00
bors[bot]
7435b9e98c
Merge #7661
7661: Start LSP 3.17 support r=kjeremy a=kjeremy

Companion to https://github.com/gluon-lang/lsp-types/pull/199 which <strike>has not been merged yet</strike>  has been merged.

This doesn't opt into any 3.17 functionality yet.

Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
2021-02-14 19:52:38 +00:00
Jeremy Kolb
8105418b25 Start LSP 3.17 support 2021-02-14 14:51:29 -05:00
Michał Muskała
e640fa967c Simplify find_crlf
This is both simpler to read and compiles to better code:
https://rust.godbolt.org/z/MxKodv
2021-02-14 20:20:14 +01:00
bors[bot]
425c56aa52
Merge #7676
7676: Make it clear which client-side commands we use r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-02-14 16:40:44 +00:00
Aleksey Kladov
9852537809 Make it clear which client-side commands we use 2021-02-14 19:36:44 +03:00
bors[bot]
a0322defc3
Merge #7656
7656: Implement constructor usage search for almost all items r=matklad a=Veykril

This PR moves the filering for enum constructors to the HIR, with this unprefixed variants as well as when the enum has been renamed via use will then still show up properly.
We now walk the ast of the `NameRef` up until we find a `PathExpr`(which also handles `CallExpr` for tuple-type structs and variants already) or a `RecordExpr`. For enum search we then take the `path` out of that expression and do a resolution on it to compare it with the definition enum.
With this PR we now support searching for all constructor literals, Unit-, Tuple- and Record-Structs, Unit-, Tuple- and Record-Variants as well as Unions.

There is one shortcoming due to how the search is triggered. Unit Variants constructors can't be searched as we have no position for it to kick off the search(since a comma doesn't have to exist for the last variant).

Closes #2549 though it doesn't implement it as outlined in the issue since the reference kind was removed recently, though I believe the approach taken here is better personally.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-14 16:17:03 +00:00
bors[bot]
d50a37d3aa
Merge #7643 #7663
7643: Automatically detect the rustc-src directory (fixes #3517) r=matklad a=bnjbvr

If the configured rustcSource was not set, then try to automatically
detect a source for the sysroot rustc directory.

I wasn't sure how to do it in the case of the project.json file, though.

7663: Tolerate spaces in nix binary patching r=matklad a=CertainLach

If path to original file contains space (I.e on code insiders, where
default data directory is ~/Code - Insiders/), then there is syntax
error evaluating src arg.

Instead pass path as str, and coerce to path back in nix expression

Co-authored-by: Benjamin Bouvier <public@benj.me>
Co-authored-by: Yaroslav Bolyukin <iam@lach.pw>
2021-02-14 15:42:07 +00:00
bors[bot]
63c5c92856
Merge #7668
7668: Finalize rename infra rewrite r=matklad a=Veykril

This should be the final PR in regards to rewriting rename stuff, #4290.

It addresses 3 things:
	- Currently renaming import aliases causes some undesired behavior(see #5198) which is why this PR causes us to just return an error if an attempt at renaming an alias is made for the time being. Though this only prevents it from happening when the alias import is renamed, so its not too helpful.
	- Fixes #6898
	- If we are inside a macro file simply rename the input name node as there isn't really a way to do any of the fancy shorthand renames and similar things as for that we would have to exactly know what the macro generates and what not.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-14 15:29:00 +00:00
Lukas Wirth
7b64622780 Don't rename field record patterns directly 2021-02-13 23:47:21 +01:00
Domantas Jadenkus
150ed80c51 strip type parameter defaults when generating impl generics 2021-02-13 23:59:51 +02:00
bors[bot]
84c9970db5
Merge #7664
7664: refactor impl generation in assists r=Veykril a=jDomantas

Follow-up to #7659: all impl generation in assists (at least what I found) is now done through `utils::{generate_impl_text, generate_trait_impl_text}`.

Co-authored-by: Domantas Jadenkus <djadenkus@gmail.com>
2021-02-13 21:37:35 +00:00
bors[bot]
7f751d6413
Merge #7665
7665: Don't classify attribute macros as their path unless it's a function with the proc_macro_attribute attribute r=Veykril a=Veykril

bors r+
Closes #6389

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-13 21:12:49 +00:00
Lukas Wirth
403a63d2b4 Don't classify attribute macros as their path unless it's a function with the proc_macro_attribute attribute 2021-02-13 22:11:31 +01:00
Domantas Jadenkus
7f100fff92 cargo fmt 2021-02-13 22:51:48 +02:00
Domantas Jadenkus
36e8a55bee use generate_impl_text in replace_derive_with_manual_impl 2021-02-13 22:51:07 +02:00
Domantas Jadenkus
3364ac8b11 use generate_impl_text in generate_impl 2021-02-13 22:45:22 +02:00
Lukas Wirth
9b04506924 Fallback to renaming input NameRef node for macros when inside macro 2021-02-13 21:41:04 +01:00
Domantas Jadenkus
ff7ea7c308 use generate_impl_text in generate_from_impl 2021-02-13 22:27:57 +02:00
Lukas Wirth
1901172841 Prevent aliases from being renamed for now 2021-02-13 20:49:04 +01:00
Vladyslav Katasonov
37a8cec638 expose hir::Type::type_paramters
Used to get E parameter from `Result<T, E>`
2021-02-13 22:04:52 +03:00
Vladyslav Katasonov
4be260d693 allow try expr? with return None in extracted function 2021-02-13 22:04:52 +03: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
Vladyslav Katasonov
0ad3d7938f migrate body span to {parent,text_range}
This simplifies api as we are not duplicating code from syntax crate
2021-02-13 22:04:52 +03:00
Lukas Wirth
ed1f2b58f7 Group generate getter/setter assist 2021-02-13 19:58:58 +01:00
bors[bot]
205e72f34d
Merge #7596
7596: Move CodeLens to ide crate r=ivan770 a=ivan770

Closes #7579

Co-authored-by: ivan770 <leshenko.ivan770@gmail.com>
2021-02-13 17:50:55 +00:00
Benjamin Bouvier
4a6e602c94 Allow automatically detect the rustc-src directory (fixes #3517).
If the configured rustcSource is set to "discover", try to automatically
detect a source from the sysroot rustc directory.
2021-02-13 18:20:46 +01:00
ivan770
4c2b201b7d
Make annotations tests similar to those in runnables 2021-02-13 19:18:13 +02:00
Lukas Wirth
8ac6041bcf Only use HIR when searching for enum constructors, otherwise fall back to AST 2021-02-13 17:57:14 +01:00
Luciano Bestia
790bda1f85 corrected no newline at end of file 2021-02-13 17:57:05 +01:00
Luciano Bestia
a28f9545e1 removed logging stuff 2021-02-13 17:50:52 +01:00
Luciano
75015b6eaa
Update crates/ide/src/folding_ranges.rs
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-13 17:46:26 +01:00
ivan770
ee049b256a
Improve runnable annotations order, fix incorrect ignore detection 2021-02-13 15:47:53 +02:00
Domantas Jadenkus
b07f530e3a cargo fmt 2021-02-13 15:34:40 +02:00
ivan770
c46b32c449
Added annotation tests 2021-02-13 15:27:04 +02:00
Domantas Jadenkus
375c13cd44 make it work for record-style variants 2021-02-13 14:57:06 +02:00
Domantas Jadenkus
6799088579 update doc 2021-02-13 14:46:41 +02:00
Domantas Jadenkus
3a4d273ac2 handle generic enums 2021-02-13 14:46:28 +02:00
Domantas Jadenkus
e63116c74f allow any field type 2021-02-13 14:29:26 +02:00
Domantas Jadenkus
fee05b0491 use Self instead of enum name 2021-02-13 14:27:38 +02:00
ivan770
02ad828c6d
Fix incorrect references annotation 2021-02-13 13:22:12 +02:00
ivan770
185da286d2
Moved CodeLens to ide crate 2021-02-13 13:07:47 +02:00
Lukas Wirth
c395dd1032 Implement constructor usage search for almost all items
For all struct kinds, unions and enums, as well as for record- and
tuple-variants but not for unit-variants, as these have no trailing
character we can anchor the search to. Functionality wise it is
implemented though.
2021-02-12 21:30:55 +01:00
Lukas Wirth
fd6cf4d566 Update references test output 2021-02-12 19:00:37 +01:00
Lukas Wirth
c5be0b2589 Use NameLike in FileReference directly as its not exported from ide anymore 2021-02-12 18:58:29 +01:00
Lukas Wirth
d644728d82 Refactor reference searching to work with the ast 2021-02-12 18:58:28 +01:00
Lukas Wirth
43ccbf4360 Remove unused ReferenceSearchResult functions 2021-02-12 18:58:28 +01:00
Yoshua Wuyts
ac959b82b3 Add find_impl_block_end assist helper 2021-02-12 17:59:08 +01:00
Florian Diebold
a7387cae2c Fix slow tests sometimes failing
In some situations we reloaded the workspace in the tests after having reported
to be ready. There's two fixes here:
1. Add a version to the VFS config and include that version in progress reports,
so that we don't think we're done prematurely;
2. Delay status transitions until after changes are applied. Otherwise the last
change during loading can potentially trigger a workspace reload, if it contains
interesting changes.
2021-02-12 16:31:16 +01:00
bors[bot]
dee5aba43a
Merge #7644
7644: Primitive completion r=jonas-schievink a=jonas-schievink

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

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2021-02-12 13:18:30 +00:00
bors[bot]
a044ac980c
Merge #7638 #7648
7638: libloading 0.7 r=kjeremy a=kjeremy

See https://docs.rs/libloading/0.7.0/libloading/changelog/r0_7_0/index.html

7648: fix nightly warning `legacy_derive_helpers` r=lnicola a=peddermaster2

With a recent nightly (e.g. 2021-02-10) a warning comes up. This PR reorders the attributes to fix the warning.

See https://github.com/rust-lang/rust/issues/79202

Co-authored-by: kjeremy <kjeremy@gmail.com>
Co-authored-by: Peter Wischer <peter.wischer@lwl.org>
2021-02-12 13:01:44 +00:00
Peter Wischer
f18fc5a0ae fix nightly warning legacy_derive_helpers
see https://github.com/rust-lang/rust/issues/79202
2021-02-12 13:57:38 +01:00
Jonas Schievink
70f388cedb Pin Rust to 1.49.0 on CI 2021-02-12 13:05:56 +01:00
Jonas Schievink
e938d769d9 Complete builtin type paths 2021-02-11 19:52:46 +01:00
Jonas Schievink
216dc856c5 Wrap BuiltinType in code model 2021-02-11 19:52:33 +01:00
kjeremy
61092bd900 libloading 0.7
See https://docs.rs/libloading/0.7.0/libloading/changelog/r0_7_0/index.html
2021-02-11 10:07:49 -05:00
Jonas Schievink
925c51aef3 Add test for #1165
Closes #1165
2021-02-10 18:42:35 +01:00
Jonas Schievink
2382dce2d3 Slightly expand test 2021-02-10 17:37:15 +01:00
Jonas Schievink
3c5734712a Add more tests 2021-02-10 15:59:20 +01:00
Jonas Schievink
a00fa0c06f Put the old resolver back 2021-02-10 15:12:45 +01:00
Jonas Schievink
244d8e37f1 Update fixed tests 2021-02-10 14:48:52 +01:00
Jonas Schievink
e837df8479 infer: update resolver when descending into block 2021-02-10 14:41:54 +01:00
bors[bot]
5e39d7a680
Merge #7617
7617: Add getter/setter assists r=Veykril a=yoshuawuyts

This patch makes progress towards the design outlined in https://github.com/rust-analyzer/rust-analyzer/issues/5943, and includes a small refactor which closes https://github.com/rust-analyzer/rust-analyzer/issues/7607. All together this patch does 4 things:

- Adds a `generate_getter` assist.
- Adds a `generate_getter_mut` assist.
- Adds a `generate_setter` assist.
- Moves the `generate_impl_text` function from `generate_new` into `utils` (which closes #7607).

## Design Notes

I've chosen to follow the [Rust API guidelines on getters](https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter) as closely as possible. This deliberately leaves "builder pattern"-style setters out of scope.

Also, similar to https://github.com/rust-analyzer/rust-analyzer/pull/7570 this assist generates doc comments. I think this should work well in most cases, and for the few where it doesn't it's probably easily edited. This makes it slightly less correct than the #7570 implementation, but I think this is still useful enough to include for many of the same reasons.

The reason why this PR contains 3 assists, rather than 1, is because each of them is so similar to the others that it felt more noisy to do them separately than all at once. The amount of code added does not necessarily reflect that, but hope that still makes sense.

## Examples

**Input**
```rust
struct Person {
    name: String,     // <- cursor on "name"
}
```

**generate getter**
```rust
struct Person {
    name: String,
}

impl Person {
    /// Get a reference to the person's name.
    fn name(&self) -> &String {
        &self.name
    }
}
```

**generate mut getter**
```rust
struct Person {
    name: String,
}

impl Person {
    /// Get a mutable reference to the person's name.
    fn name_mut(&mut self) -> &mut String {
        &mut self.name
    }
}
```

**generate setter**
```rust
struct Person {
    name: String,
}

impl Person {
    /// Set the person's name.
    fn set_name(&mut self, name: String) {
        self.name = name;
    }
}
```


Co-authored-by: Yoshua Wuyts <yoshuawuyts+github@gmail.com>
2021-02-10 10:32:36 +00:00
bors[bot]
ff5ef2830c
Merge #7591 #7622
7591: Fix/no floating promises r=matklad a=sahandevs

closes #3515

- added `@typescript-eslint/no-floating-promises: error` rule
- changed `"no-console": ["error"]` to `"no-console": ["error", { allow: ["warn", "error"] }]` (we at least log the error messages of the floating promises)
- fixed lint/compile errors

7622: Resolve TupleStructPat in SourceAnalyzer::resolve_path r=Veykril a=Veykril

Closes #7594
bors r+

Co-authored-by: Sahandevs <sahandevs@gmail.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-10 08:44:53 +00:00
Lukas Wirth
a195711df0 Resolve TupleStructPat in SourceAnalyzer::resolve_path 2021-02-10 09:37:22 +01:00
Yoshua Wuyts
bfa99f20ff Add #[track_caller] to assist tests 2021-02-10 00:16:00 +01:00
Yoshua Wuyts
e8d7bcc355 Add getter/setter assists
Finish implementing `generate_setter` assists

Make `generate_impl_text` util generic

generate getter methods

Fix getter / setter naming

It's now in-line with the Rust API naming guidelines: https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter

apply clippy

Improve examples
2021-02-10 00:08:41 +01:00
Lukas Wirth
5454559c0a Show qualified variant pattern completions 2021-02-09 21:35:02 +01:00
bors[bot]
5d4ae1c8e3
Merge #7616
7616: Show `Self` pattern and Self-prefixed enum-variant completions r=Veykril a=Veykril

![jDfQXNE0qZ](https://user-images.githubusercontent.com/3757771/107413514-1ff99b00-6b11-11eb-88b3-126cd106b514.gif)
![JpogVIgloq](https://user-images.githubusercontent.com/3757771/107413519-212ac800-6b11-11eb-8282-51115468dccc.gif)

Variant pattern completions are to be done still.

Closes #6549, at least that should address all that's left from that issue from what I can see.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-09 19:38:39 +00:00
bors[bot]
4f9a5287bf
Merge #7615
7615: Add parsing benchmark r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2021-02-09 18:53:27 +00:00
Aleksey Kladov
61f15b72ac Add parsing benchmark 2021-02-09 21:52:34 +03:00
Lukas Wirth
e92180a1d8 Show Self pattern completions for Adts if inside impls 2021-02-09 19:47:21 +01:00
Lukas Wirth
2f171ca78d Show completions for enum variant with self prefix if inside impl 2021-02-09 19:04:39 +01:00
bors[bot]
96a9ab7250
Merge #7614
7614: Unleash inner item resolution to users r=jonas-schievink a=jonas-schievink

![Peek 2021-02-09 17-30](https://user-images.githubusercontent.com/1786438/107394800-8627f300-6afc-11eb-8662-ed07226bc58f.gif)


Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2021-02-09 18:01:04 +00:00
Jonas Schievink
85906aa304 Remove unneeded return 2021-02-09 18:40:05 +01:00
Aleksey Kladov
4b1279d0b1 Infra for "unit" benchmarking 2021-02-09 20:25:39 +03:00
Jonas Schievink
51f68863b1 Remove pub 2021-02-09 17:36:10 +01:00
Jonas Schievink
e16d9dc5bd Use block_def_map in Resolver
This required a few changes to not bail out immediately if a
`ModuleScope` doesn't resolve a path.

The `LocalItemsScope` hack is now removed.
2021-02-09 17:27:04 +01:00
Jonas Schievink
34ad3d629a Teach find_path about inner items 2021-02-09 17:25:03 +01:00
Jonas Schievink
7067a22e1c Add another block def map test 2021-02-09 17:24:43 +01:00
Jonas Schievink
b9c213da7a Make with_ancestor_maps public 2021-02-09 17:23:25 +01:00
Jonas Schievink
27f77060e2 Add TestDB::module_at_position 2021-02-09 17:22:57 +01:00
Jonas Schievink
1956286368 Add expression scopes for blocks 2021-02-09 17:11:44 +01:00
Aleksey Kladov
9ea2c96ddd restore accidentally deleted test 2021-02-09 18:29:15 +03:00
Aleksey Kladov
ef8f38efc6 More precise navigation to parent 2021-02-09 18:21:09 +03:00
Aleksey Kladov
2ac20b05f1 Cleanup tests 2021-02-09 18:13:11 +03:00
Aleksey Kladov
2dc67c85b9 Cleanup test 2021-02-09 17:48:25 +03:00
Aleksey Kladov
5ddfde3038 Modernize tests 2021-02-09 17:23:35 +03:00
bors[bot]
876c4519e3
Merge #7602
7602: Check for dyn impls in method resolution r=flodiebold a=Veykril

Fixes #6777

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-08 18:22:35 +00:00
Lukas Wirth
965d31ce5b Check for dyn impls in method resolution 2021-02-08 19:13:54 +01:00
kjeremy
0c3b38695a Update crates
Pulls in https://github.com/rust-lang/chalk/pull/682
2021-02-08 11:38:51 -05:00
Vincent Esche
8f461ffe8f Consolidate fn load_cargo(…) parameters into struct LoadCargoConfig { … } 2021-02-08 11:30:16 +01:00
Vincent Esche
6d9c13c710 Add config: &CargoConfig parameter to fn load_cargo(…) 2021-02-08 11:00:55 +01:00
Gowri
6b947567e1 chore: swap order of placeholders 2021-02-08 18:22:46 +10:30
Gowri
498dfc45d4 ci: update relevant test case expected values 2021-02-08 10:16:20 +10:30
Gowri
7b92a55ff7 fix: add for keyword in completion #7588 2021-02-08 09:57:16 +10:30
Aleksey Kladov
7022ea52b5 AdtDef -> Adt 2021-02-07 14:15:02 +03:00
petr-tik
f4e3eceb6f Add a semantic token type for char literals
The LSP spec doesn't recognise character literals, so
had to extend the suported types to our own custom type
2021-02-05 23:46:39 +00:00
Jonas Schievink
c312ab51d0 Test super resolution too 2021-02-05 19:25:50 +01:00
Jonas Schievink
997bd97b77 Fix resolution of self module within blocks 2021-02-05 19:24:03 +01:00
bors[bot]
80ab753d7e
Merge #7572
7572: Add `find_or_create_impl_block` to assist utils r=matklad a=yoshuawuyts

This is another continuation of https://github.com/rust-analyzer/rust-analyzer/pull/7562, introducing a small util to either find an `impl` block, or create a new one if none exists. I copied this code from the `generate_new` assist into https://github.com/rust-analyzer/rust-analyzer/pull/7562, and this unifies both into a helper.

It doesn't feel super polished in its current state, but my hope is that this is enough of a starting point that it can be expanded on later. For example something that would be useful would be a flag which either returns the index of the start of the block, or the end of the block.

Anyway, I hope this is useful. Thanks!

Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
2021-02-05 17:18:02 +00:00
Yoshua Wuyts
2cf1612669 Add find_or_create_impl_block to assist utils 2021-02-05 17:19:31 +01:00
bors[bot]
737e7a7b75
Merge #7574
7574: Remove various redundant clones r=kjeremy a=yoshuawuyts

I noticed when running clippy through RA that there are a few instances where `clone` is called where it's not actually needed. I figured a small patch to remove these might be welcome here.

Thanks!

Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
2021-02-05 16:04:50 +00:00
Yoshua Wuyts
79d103d5b4 Remove redundant clones 2021-02-05 16:57:26 +01:00
bors[bot]
2a75594ee8
Merge #7505
7505: Widen Highlights root range to covering element r=Veykril a=Veykril

There have been a few issues about/containing spurious syntax highlighting panics, which all seem to come from the `rust_analyzer::handlers::handle_semantic_tokens_range` request, which I believe this to be the cause of as the text range we want to highlight here is currently potentially smaller than that of the covering element, so we might highlight something that is inside the covering element, but outside of the text range we wish to highlight causing the assert to fail.
Unfortunately this isn't really easy to test since I have yet to find a reproducible cause(#7504 doesn't work for me cause I can't seem to checkout the given commit).

See #7504, #7298, #7299 and #7416, all of those contain an assertion failure in syntax highlighting, but only in the range request.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-05 15:31:25 +00:00
bors[bot]
855b00cbe6
Merge #7570 #7571
7570: Add doc gen to the `generate_enum_match_method` assist r=yoshuawuyts a=yoshuawuyts

Implements a small extension to https://github.com/rust-analyzer/rust-analyzer/pull/7562, generating default comments. I wasn't sure if this would fit the goals of Rust-Analyzer, so I chose to split it into a separate PR. This is especially useful when writing code in a codebase which uses `#![warn(missing_docs)]` lint, as many production-grade libraries do.

The comments we're generating here are similar to the ones found on [`Option::is_some`](https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some) and [`Result::is_err`](https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err). I briefly considered only generating these for `pub` types, but they seem small and unobtrusive enough that they're probably useful in the general case. Thanks!

## Example

__input__
```rust
pub(crate) enum Variant {
    Undefined,
    Minor, // cursor here
    Major,
}
```

__output__
```rust
pub(crate) enum Variant {
    Undefined,
    Minor,
    Major,
}

impl Variant {
    /// Returns `true` if the variant is [`Minor`].
    pub(crate) fn is_minor(&self) -> bool {
        matches!(self, Self::Minor)
    }
}
```

## Future Directions

This opens up the path to adding an assist for generating these comments on existing `is_` methods. This would make it both easy to document new code, and update existing code with documentation.

7571: Cleanup decl_check r=Veykril a=Veykril

bors r+

Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2021-02-05 15:17:41 +00:00
Lukas Wirth
eeb5bfcfab Cleanup decl_check 2021-02-05 16:09:45 +01:00
Yoshua Wuyts
d90bd63536 Add doc gen to the generate_enum_match_method assist 2021-02-05 16:00:07 +01:00
bors[bot]
b89fef5220
Merge #7562
7562: add `generate_enum_match` assist r=matklad a=yoshuawuyts

This adds a `generate_enum_match` assist, which generates `is_` variants for enums (e.g. `Option::{is_none,is_some}` in std). This is my first attempt at contributing to Rust-Analyzer, so I'm not sure if I've gotten everything right. Thanks!

## Example

**Input**
```rust
pub(crate) enum Variant {
    Undefined,
    Minor, // cursor here
    Major,
}
```
**Output**
```rust
pub(crate) enum Variant {
    Undefined,
    Minor,
    Major,
}

impl Variant {
    pub(crate) fn is_minor(&self) -> bool {
        matches!(self, Self::Minor)
    }
}
```

## Future Directions

I made this as a stepping stone for some of the more involved refactors (e.g. #5944). I'm not sure yet how to create, use, and test `window.showQuickPick`-based asssists in RA. But once that's possible, it'd probably be nice to be able to generate match methods in bulk through the quickpick UI rather than one-by-one:

```
[x] Select enum members to generate methods for. (3 selected) [ OK ]
---------------------------------------------------------------------------
[x] Undefined
[x] Minor
[x] Major
```

Co-authored-by: Yoshua Wuyts <yoshuawuyts+github@gmail.com>
Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
2021-02-05 14:28:25 +00:00
bors[bot]
5009958847
Merge #7568
7568: Fix merging of `segment_index` in path resolution r=jonas-schievink a=jonas-schievink

This caused associated item lookup to fail when modifying `resolver.rs` to handle block expressions with inner items.

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2021-02-05 14:16:12 +00:00
Jonas Schievink
6239fe4730 Fix merging of segment_index in path resolution 2021-02-05 15:14:33 +01:00
Lukas Wirth
486c7175fe Remove unnecessary allocs in case_conv 2021-02-05 15:02:39 +01:00
Yoshua Wuyts
dfd751303e Move find_struct_impl to assist utils 2021-02-05 14:36:07 +01:00
Luciano Bestia
084b21bc36 simple comparison instead of regex 2021-02-05 14:32:03 +01:00
Yoshua Wuyts
13d663dd16 add generate-enum-match assist 2021-02-05 11:28:11 +01: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
7eaa3e56a0 allow extracted body to be indented(dedent it) 2021-02-05 05:44:08 +03:00
Vladyslav Katasonov
876ca60316 allow transitive &mut access for fields in extract_function 2021-02-05 04:35:41 +03:00
Vladyslav Katasonov
271c1cb013 add tests for extracting if/match/while/for exprs 2021-02-05 02:30:34 +03:00
Vladyslav Katasonov
4dc2a42500 document extract_function assist implementation 2021-02-05 02:14:32 +03:00
Vladyslav Katasonov
0ff74467c0 use &T for non copy params of extracted function
Use shared ref if param is not `T: Copy` and is used after body
2021-02-05 01:41:40 +03:00
Jonas Schievink
474df093a9 Avoid using ModPath's fields directly 2021-02-04 22:42:21 +01:00
Vladyslav Katasonov
d9b122858b split extract_function into pieces and order them 2021-02-05 00:35:28 +03:00
Jonas Schievink
5d99ba1d9a Make ModPath's representation private 2021-02-04 20:49:24 +01:00