Commit Graph

655 Commits

Author SHA1 Message Date
bors
b771de3fdc Auto merge of #15179 - ponyii:fix/default-values-of-const-params-are-ignored, r=HKalbasi
the "add missing members" assists: implemented substitution of default values of const params

To achieve this, I've made `hir::ConstParamData` store the default values
2023-08-15 10:17:43 +00:00
bors
7ca45dcf04 Auto merge of #15410 - alibektas:15240/invalid-demorgan, r=Veykril
internal : rewrite DeMorgan assist

fixes #15239 , #15240 . This PR is a rewrite of the DeMorgan assist that essentially rids of all the string manipulation and modifies syntax trees to apply demorgan on a binary expr. The main reason for the rewrite is that I wanted to use `Expr::needs_parens_in` method to see if the expr on which the assist is applied would still need the parens it had once the parent expression's operator had equal precedence with that of the expression. I used `.clone_(subtree|for_update)` left and right and probably more than I should have, so I would also be happy to hear how I could have prevented redundant cloning.
2023-08-15 07:29:28 +00:00
bors
c18ce9a08b Auto merge of #15429 - alibektas:deunwrap/generate_delegate_methods, r=Veykril
minor : Deunwrap generate_delegate_methods

#15398 subtask 8
2023-08-15 07:15:25 +00:00
bors
0b2a241b57 Auto merge of #15426 - alibektas:deunwrap/convert_to_guarded_return, r=Veykril
minor : Deunwrap convert_to_guarded_return

Closes subtask 12 of #15398
2023-08-15 07:00:03 +00:00
bors
1fde334195 Auto merge of #15433 - alibektas:deunwrap/remove_unused_imports, r=lnicola
minor : Deunwrap remove_unused_imports

#15398 Subtask 3
2023-08-11 11:14:45 +00:00
bors
de28baf874 Auto merge of #15424 - lnicola:remove-remove-dbg-unwrap, r=lnicola
minor: Remove `unwrap` from `Remove dbg!`

Part of #15398.
2023-08-11 07:56:10 +00:00
Ali Bektas
94b3481808 Deunwrap generate_derive 2023-08-10 01:46:51 +02:00
Ali Bektas
423b00a83a Deunwrap remove_unused_imports 2023-08-10 01:22:26 +02:00
Ali Bektas
a46eebbc9f Deunwrap wrap_return_type_in_result 2023-08-09 23:51:27 +02:00
Ali Bektas
c81a0681a2 Deunwrap generate_delegate_methods 2023-08-09 23:42:52 +02:00
Ali Bektas
ebf27058cd minor : Deunwrap convert_to_guarded_return 2023-08-09 17:17:43 +02:00
Laurențiu Nicola
b658f9a954 Remove unwrap from Remove dbg 2023-08-09 17:54:34 +03:00
bors
b6ee96c3b5 Auto merge of #15423 - alibektas:deunwrap/convert_named_struct_to_tuple_struct, r=lnicola
internal : Deunwrap convert_named_struct_to_tuple_struct

Replaces `unwrap`s with `?` for the mentioned assist.
2023-08-09 14:52:09 +00:00
Ali Bektas
4e4dda5f59 Deunwrap convert_named_struct_to_tuple_struct 2023-08-09 16:40:23 +02:00
ponyii
61cabe029f the "add missing members" assists: supported bracketed default const values 2023-08-08 21:57:55 +04:00
ponyii
4e2be8e959 the "add missing members" assists: implemented the transformation of const param default values 2023-08-08 21:57:55 +04:00
ponyii
4ebdc6f052 syntax update: the default value of ConstParam turned from Expr into ConstArg 2023-08-08 21:57:54 +04:00
ponyii
52b4392724 the "add missing members" assists: implemented substitution of default values of const params 2023-08-08 21:57:54 +04:00
Ali Bektas
17f3055803 Rewrite DeMorgan v2 2023-08-08 15:54:58 +02:00
Ali Bektas
ef5c6daf6e Rewrite DeMorgan without str manipulation. 2023-08-08 15:49:27 +02:00
bors
783130bd26 Auto merge of #15250 - lowr:fix/extract-fn-no-control-flow-with-tail-expr, r=Veykril
fix: don't use control flow when extracted fn contains tail expr of original fn

Fixes #10113
Fixes #15061
2023-08-08 13:38:49 +00:00
Ryo Yoshida
582917453b
Don't provide generate_default_from_new when impl self ty is missing
Also don't provide the assist when the `Default` trait can't be found.
2023-08-07 03:23:41 +09:00
bors
70fa270637 Auto merge of #15401 - Veykril:disabled-proc-macro, r=Veykril
internal: Turn unresolved proc macro expansions into missing expressions

Reduces the amount of type related errors one gets when proc macro expansion is disabled.
2023-08-05 18:01:46 +00:00
Lukas Wirth
042be329a7 Turn unresolved proc macro expansions into missing expressions 2023-08-05 20:00:37 +02:00
Laurențiu Nicola
622b18e579 Remove unwraps from Generate delegate trait 2023-08-05 19:23:56 +03:00
bors
12cb6e7327 Auto merge of #15377 - Veykril:extern-crate-decl, r=Veykril
Add ExternCrateDecl to HIR

Adding these doesn't really require much design effort as they represent a single import, unlike use trees which are one item that represent 0 or more imports.

We only resolve to this definition when actually resolving on the name or alias of an `extern crate name as alias` item, not usages yet as that requires far more changes that won't lead anywhere without giving it more thought. Nevertheless the changes slightly improve IDE things, an example being hover on the decl showing the merged doc comments for example.

cc https://github.com/rust-lang/rust-analyzer/issues/14079
2023-08-02 10:27:30 +00:00
Lukas Wirth
bcff166b3a Add ExternCrateDecl to HIR 2023-08-02 11:52:55 +02:00
bors
8e18b0f28c Auto merge of #15376 - Veykril:match-to-matches, r=Veykril
Allow match to matches assist to trigger on non-literal bool arms
2023-08-02 08:18:43 +00:00
Lukas Wirth
3c1c319c88 Allow match to matches assist to trigger on non-literal bool arms 2023-08-01 12:10:38 +02:00
bors
62dcf39ef0 Auto merge of #14723 - obsgolem:master, r=Veykril
Added remove unused imports assist

This resolves the most important part of #5131. I needed to make a couple of cosmetic changes to the search infrastructure to do this.

A few open questions:
* Should imports that don't resolve to anything be considered unused? I figured probably not, but it would be a trivial change to make if we want it.
* Is there a cleaner way to make the edits to the use list?
* Is there a cleaner way to get the list of uses that intersect the current selection?
* Is the performance acceptable? When testing this on itself, it takes a good couple seconds to perform the assist.
* Is there a way to hide the rustc diagnostics that overlap with this functionality?
2023-08-01 09:50:16 +00:00
bors
c71e1368fd Auto merge of #15269 - DropDemBits:structured-snippets-deferred-rendering, r=Veykril
internal: Defer structured snippet rendering to allow escaping snippet bits

Since we know exactly where snippets are, we can transparently escape snippet bits to the exact text edits that need it, and not have to do it for anything other text edits.

Also will eventually fix #11006 once all assists are migrated. This comes as a side-effect of text edits that don't have snippets get marked as having no insert formatting at all.
2023-08-01 09:18:46 +00:00
bors
f6bffa4dd3 Auto merge of #15345 - lowr:fix/add_missing_match_arms-upmap-failure, r=Veykril
Don't provide `add_missing_match_arms` assist when upmapping match arm list failed

Fixes #15310

We shouldn't provide the assist when we fail to find the original match arm list.

Note that this PR will temporarily make the assist not applicable when attribute macro operates on the match expression in question, just like the case in #15310, for most of the current stable toolchain users. This is because the sysroot-abi proc-macro-srv on the current stable [discards] spans for `Group` delimiters in some code paths, which the popular `proc-macro2` crate almost always calls, and it makes the identity of match arm list's brackets lost, leading to the upmapping failure. This has been fixed by #14960, which will land in the next stable, 1.71.

[discards]: 8ede3aae28/src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs (L231)
2023-08-01 08:58:43 +00:00
bors
efc5a813de Auto merge of #15050 - alibektas:14957, r=Veykril
bugfix :  skip doc(hidden) default members

fixes  #14957 . I have two questions :

1.  I am definitely looking for a more idiomatic way for the things I added in `crates/ide-assists/src/utils.rs`. See `FIXME` in that file.
2. Would it be actually better to change `DefaultMethods` to something like

```rust
enum DefaultMethods {
     Only( IgnoreHidden ( bool ) ) ,
     None
}
```

instead of adding a boolean to every function that calls `crates/ide-assists/src/utils.rs::filter_assoc_items`
2023-08-01 08:38:36 +00:00
Ryo Yoshida
b517aeeca5
Show TyKind::FnDef as a fn pointer in source code 2023-07-28 19:09:38 +09:00
Ryo Yoshida
fbec711ada
Don't provide add_missing_match_arms assist when upmapping match arm list failed 2023-07-27 16:43:01 +09:00
1Kinoti
65823b0c74 limit change_visibility assist to applicable items 2023-07-22 17:16:51 +03:00
bors
f45fdd9123 Auto merge of #15260 - DropDemBits:structured-snippet-migrate-3, r=lowr
internal: Migrate assists to the structured snippet API, part 3

Continuing from #15231

Migrates the following assists:
- `add_missing_match_arms`
- `fix_visibility`
- `promote_local_to_const`

The `add_missing_match_arms` changes are best reviewed commit-by-commit since they're relatively big changes compared to the rest of the commits.
2023-07-12 22:05:30 +00:00
DropDemBits
97a6fa58cd
internal: Defer rendering of structured snippets
This ensures that any assist using structured snippets won't
accidentally remove bits interpreted as snippet bits.
2023-07-12 01:50:35 -04:00
DropDemBits
89f7bf7411
Add SnippetEdit to be alongside source changes
Rendering of snippet edits is deferred to places using source change
2023-07-12 00:43:41 -04:00
DropDemBits
a9889a0f1c
fix the rest of the nits 2023-07-11 17:32:12 -04:00
DropDemBits
f9a144f0ed
Hoist out make::name_ref
`clone_for_update` is relatively cheap in comparison, since making a
node require parsing an entire source text

Adds a test to make sure that it doesn't crash when multiple uses are
present.
2023-07-11 17:28:17 -04:00
DropDemBits
99abcdc1eb
format let-else in promote_local_to_const 2023-07-10 17:49:27 -04:00
DropDemBits
11debaf61a
Migrate fix_visibility to mutable ast 2023-07-10 17:39:55 -04:00
Ryo Yoshida
595d9e6ebb
fix: don't use control flow when extracted fn contains tail expr of original fn 2023-07-10 21:55:13 +09:00
bors
2f6d545535 Auto merge of #15231 - DropDemBits:structured-snippet-migrate-2, r=lowr
internal: Migrate more assists to use the structured snippet API

Continuing from #14979

Migrates the following assists:
- `generate_derive`
- `wrap_return_type_in_result`
- `generate_delegate_methods`

As a bonus, `generate_delegate_methods` now generates the function and impl block at the correct indentation 🎉.
2023-07-10 10:57:24 +00:00
DropDemBits
27444eda56
Migrate promote_local_to_const to mutable ast 2023-07-09 18:49:29 -04:00
DropDemBits
6ab2788978
Migrate add_missing_match_arms to mutable ast
Requires a hack in order to work inside of macros
2023-07-09 18:31:38 -04:00
Josiah Bills
7a87a35f1a Added remove_unused_imports assist. 2023-07-09 17:44:27 -04:00
Josiah Bills
b9cef03230 Updated search to expose some more functions and to make search take the search scope by reference. 2023-07-09 17:30:21 -04:00
DropDemBits
35f2e82e0b
Don't show add_missing_match_arms when not needed
`does_not_fill_wildcard_with_wildcard`
and `does_not_fill_wildcard_with_partial_wildcard_and_wildcard`
both made no modifications to the code,
which is a problem for mutable ast porting as it generates a best-effort
minimal set of text edits,
and assists require at least one text edit.
2023-07-09 02:32:44 -04:00
DropDemBits
5fddf3ef6d
fix: Don't panic for different/missing delimiters 2023-07-08 13:43:30 -04:00
Omer Tuchfeld
9200d27a26 Stop inserting semicolon when extracting match arm
# Overview

Extracting a match arm value that has type unit into a function, when a
comma already follows the match arm value, results in an invalid (syntax
error) semicolon added between the newly generated function's generated
call and the comma.

# Example

Running this extraction

```rust
fn main() {
    match () {
        _ => $0()$0,
    };
}
```

would lead to

```rust
fn main() {
    match () {
        _ => fun_name();,
    };
}

fn fun_name() {
}
```

# Issue / Fix details

This happens because when there is no comma, rust-analyzer would simply
add the comma and wouldn't even try to evaluate whether it needs to add
a semicolon. But when the comma is there, it proceeds to evaluate
whether it needs to add a semicolon and it looks like the evaluation
logic erroneously ignores the possibility that we're in a match arm.
IIUC it never makes sense to add a semicolon when we're extracting from
a match arm value, so I've adjusted the logic to always decide against
adding a semicolon when we're in a match arm
2023-07-08 15:41:24 +02:00
DropDemBits
2eb506462e
refactor: simplify generate_delegate_method
Can actually split out adding the functions from  getting the impl to
update or create thanks to being able to refer to the impl ast node.

FIXME Context:
Unfortunately we can't adjust the indentation of the newly added function
inside of `ast::AssocItemList::add_item` since for some reason the `todo!()`
placeholder generated by `add_missing_impl_members` and
`replace_derive_with_manual_impl` gets indented weirdly.
2023-07-07 20:08:32 -04:00
DropDemBits
09a3bd5899
Match indentation of generated delegate method 2023-07-07 20:06:10 -04:00
DropDemBits
58e2053327
Migrate generate_delegate_methods to mutable ast 2023-07-07 20:06:10 -04:00
DropDemBits
f8b6b4cc0b
Migrate wrap_return_type_in_result to mutable ast 2023-07-07 20:06:10 -04:00
DropDemBits
25088fca9d
Migrate generate_derive to mutable ast 2023-07-07 20:06:09 -04:00
Ali Bektas
915ddb05fa HiddenDocAttr becomes DocHiddenAttr 2023-07-07 14:15:15 +02:00
bors
954a341008 Auto merge of #15226 - alibektas:15109, r=Veykril
assist : add enum to glob_import_expand

fixes #15109
2023-07-07 10:17:55 +00:00
Ali Bektas
d2693aabd6 assist : add enum to glob_import_expand 2023-07-07 02:40:35 +02:00
bors
54c2ee9fab Auto merge of #15219 - alibektas:15080, r=Veykril
Unify getter and setter assists

This PR combines what previously have been two different files into a single file. I want to talk about the reasons why I did this. The issue that prompted this PR ( and before I forget : this pr fixes #15080 ) mentions an interesting behavior. We combine these two assists into an assist group and the order in which the assists are listed in this group changes depending on the text range of the selected area. The reason for that is that VSCode prioritizes actions that have a bigger impact in a smaller area and until now generate setter assist was only possible to be invoked for a single field whereas you could generate multiple getters for the getter assist. So I used the latter's infra to make former applicable to multiple fields, hence the unification. So this PR solves in essence

1. Make `generate setter` applicable to multiple fields
2. Provide a consistent order of the said assists in listing.
2023-07-06 12:22:39 +00:00
Ali Bektas
1829d550ac Unify getter and setter assists 2023-07-06 12:27:45 +02:00
bors
aa91eda902 Auto merge of #15152 - alibektas:14987, r=Veykril,lowr
assist : generate trait from impl

fixes #14987 . As the name suggests this assist is used to generate traits from inherent impls while adapting the original impl to fit to the newly generated trait. I made some decisions regarding when the assist should be applicable. These are surely open to discussion. I looking forward to any feedback.

![generate_trait_from_impl_v1](https://github.com/rust-lang/rust-analyzer/assets/20956650/05d4dda5-604a-4108-8b82-9b60bd45894a)
2023-07-05 16:56:37 +00:00
Ali Bektas
8edb3e192a Minor changes 2023-07-05 16:45:54 +02:00
Ali Bektas
30cbba20de Add snippet insert if client supports it 2023-07-05 13:41:54 +02:00
Ali Bektas
18ea9245c6 v2 2023-07-04 19:32:48 +02:00
Ali Bektas
8a2c5d215b Still in need of more test cases 2023-07-04 19:27:50 +02:00
Ali Bektas
0df1153277 bugfix : skip doc(hidden) default members
fixes  #14957
2023-07-04 19:27:50 +02:00
Ali Bektas
03423116ad Generate trait from impl v2 2023-07-04 17:33:45 +02:00
bors
daba334611 Auto merge of #15206 - Veykril:let-else-fmt, r=Veykril
internal: Format let-else

As nightly finally got support for it I went ahead and formatted r-a with the latest nightly, then with the latest stable (in case other stuff changed)
2023-07-03 18:41:59 +00:00
Lukas Wirth
69cd3c30ac Format let-else 2023-07-03 20:34:09 +02:00
bors
691600a885 Auto merge of #15181 - lowr:patch/import-map-purge-unused, r=Veykril
Clean up `ImportMap`

There are several things in `hir_def::import_map` that are never used. This PR removes them and restructures the code. Namely:

- Removes `Query::name_only`, because it's *always* true.
  - Because of this, we never took advantage of storing items' full path. This PR removes `ImportPath` and changes `ImportInfo` to only store items' name, which should reduce the memory consumption to some extent.
- Removes `SearchMode::Contains` for `Query` because it's never used.
- Merges `Query::assoc_items_only` and `Query::exclude_import_kinds` into `Query::assoc_mode`, because the latter is never used besides filtering associated items out.

Best reviewed one commit at a time. I made sure each commit passes full test suite. I can squash the first three commits if needed.
2023-07-03 14:37:55 +00:00
Ryo Yoshida
8cd4e9f7ec
Merge assoc_items_only and exclude_import_kinds into assoc_mode 2023-06-30 23:37:10 +09:00
Ryo Yoshida
4e793e7859
Use anonymous lifetime where possible 2023-06-29 23:27:28 +09:00
Ali Bektas
20c877a700 Update crates/ide-assists/src/handlers/generate_trait_from_impl.rs
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2023-06-28 12:38:54 +02:00
Ali Bektas
677151e4e1 Version 1 2023-06-27 18:03:36 +02:00
bors
f0e00ed599 Auto merge of #14948 - alibektas:14386, r=Veykril
feature : assist delegate impl

This PR ( fixes #14386 ) introduces a new IDE assist that generates a trait impl for a struct that delegates a field. This is a draft because the current `ide_db::path_transform::PathTransform` produces some unwanted results when it deals with extern crates, an example of which I attach as a GIF.

GIFs :
1. A general case
![14386-functional](https://github.com/rust-lang/rust-analyzer/assets/20956650/22114959-caa6-45ec-a154-b4b2f458f6b1)

2. A case where `ide_db::path_transform::PathTransform` fails to correctly resolve a property ( take `Allocator` as an example ) to its full path, thus causing an error to occur. ( Not to even mention that resolving this causes another error `use of unstable library feature 'allocator_api'` to occur
![14386-erroneous](https://github.com/rust-lang/rust-analyzer/assets/20956650/922ca715-594e-4168-a579-7c5c006f93aa)
2023-06-22 10:44:01 +00:00
bors
f99348578a Auto merge of #15074 - oxalica:fix/inline-async-fn, r=lowr
Correctly handle inlining of async fn

Fixes #10198
2023-06-19 15:46:55 +00:00
oxalica
52f1ce17aa Correctly handle inlining of async fn 2023-06-19 17:14:17 +08:00
bors
a1b536ec6f Auto merge of #15054 - ponyii:fix/implement-missing-members-do-not-transform-const-params, r=lowr
fix: implement missing members doesn't transform const params and default types

Fixes https://github.com/rust-lang/rust-analyzer/issues/13363
2023-06-18 09:30:13 +00:00
Ali Bektas
4ed1197662 Version 3
Remove scope_for_def calls as the definition have been removed entirely.
As a result of this change the problem with false path resolutions has been solved.
2023-06-17 21:27:52 +02:00
Ali Bektas
19a9de72f6 Version 2 2023-06-17 13:52:34 +02:00
Ali Bektas
59cc783a3b Correct errors produced by CI 2023-06-17 13:52:34 +02:00
Ali Bektas
8123a39c82 Generate delegate trait 2023-06-17 13:52:34 +02:00
Lukas Wirth
bd093d1ccd Sort methods in generate_delegate_methods listing 2023-06-16 18:41:25 +02:00
ponyii
7e08933a26 the "implement missing members" assist's const transformation patched 2023-06-16 20:34:44 +04:00
ponyii
b07490ffe9 made the add_missing_impl_members and add_missing_default_members assists transform default generic types 2023-06-14 17:37:34 +04:00
ponyii
5ce65a1d92 the "implement missing members" assist's const transformation implemented 2023-06-12 20:16:22 +04:00
bors
dcd31550e2 Auto merge of #14932 - HKalbasi:dev, r=HKalbasi
Lower const params with a bad id

cc #7434

This PR adds an `InTypeConstId` which is a `DefWithBodyId` and lower const generic parameters into bodies using it, and evaluate them with the mir interpreter. I think this is the last unimplemented const generic feature relative to rustc stable.

But there is a problem: The id used in the `InTypeConstId` is the raw `FileAstId`, which changes frequently. So these ids and their bodies will be invalidated very frequently, which is bad for incremental analysis.

Due this problem, I disabled lowering for local crates (in library crate the id is stable since files won't be changed). This might be overreacting (const generic expressions are usually small, maybe it would be better enabled with bad performance than disabled) but it makes motivation for doing it in the correct way, and it splits the potential panic and breakages that usually comes with const generic PRs in two steps.

Other than the id, I think (at least I hope) other parts are in the right direction.
2023-06-12 08:49:02 +00:00
Ryo Yoshida
ed8c58a3b1
Remove commented out conflicts 2023-06-11 17:14:36 +09:00
Ryo Yoshida
008f5065d1
fix(assist): derive source scope from syntax node to be transformed 2023-06-11 15:25:36 +09:00
hkalbasi
f8594f78bb Use ConstArg instead of Expr for AstId of InTypeConstId 2023-06-11 00:39:28 +03:30
bors
95228d23bb Auto merge of #14875 - ponyii:fix/implement-missing-members-do-not-transform-lifetimes, r=Veykril
fix: implemeted lifetime transformation fot assits

A part of https://github.com/rust-lang/rust-analyzer/issues/13363
I expect to implement transformation of const params in a separate PR

Other assists and a completion affected:
- `generate_function` currently just ignores lifetimes and, consequently, is not affected
- `inline_call` and `replace_derive_with...` don't seem to need lifetime transformation
- `trait_impl` (a completion) is fixed and tested
2023-06-10 13:22:50 +00:00
bors
60d952e902 Auto merge of #14979 - DropDemBits:structure-snippets-migrate-1, r=Veykril
internal: Migrate some assists to use the structured snippet API

Migrates the following assists:

- `add_missing_impl_members`
- `extract_type_alias`

As an additional requirement, these assists are also migrated to use the mutable AST API, since otherwise there would be overlapping `Indel` spans
2023-06-09 19:26:01 +00:00
Ryo Yoshida
32768fe310
Infer return type for async function in generate_function 2023-06-09 00:32:52 +09:00
Viktor Lott
eef716d5f2 fix: use render_eval instead of inlined expr 2023-06-06 12:08:11 +02:00
Viktor Lott
2d4cb780b6 fix: Use render_eval for all builtins 2023-06-06 00:34:00 +02:00
Viktor Lott
8103a10a78 update assist to include more literals 2023-06-06 00:34:00 +02:00
Viktor Lott
094c1e7b86 feat: inline const expr as static str 2023-06-06 00:34:00 +02:00
hkalbasi
5531d46c95 Emit '_ for lifetime generics in HirDisplay 2023-06-05 10:55:47 +03:30
DropDemBits
dd5f05590e
Migrate extract_type_alias to mutable ast 2023-06-05 00:24:12 -04:00
DropDemBits
e6e72bf9d5
Migrate add_missing_impl_members to mutable ast
`replace_derive_with_manual_impl` was slightly since it used
`add_trait_assoc_items_to_impl`
(which was also used by `add_missing_impl_members`)
2023-06-05 00:23:10 -04:00
ponyii
fe8f862757 made the add_missing_impl_members and add_missing_default_members assists transform lifetimes 2023-06-03 21:12:11 +04:00
Sebastian Ziebell
c0e9b57371 Improve assist to filter invalid params
The change updates the logic to determine if a function parameter is
valid for replacing the type param with the trait implementation.

First all usages are determined, to check if they are used outside the function
parameter list. If an outside reference is found, e.g. in body, return type or
where clause, the assist is skipped. All remaining usages only appear in the
function param list. For each usage the param type is checked to see if
it's valid.

**Please note** the logic currently follows a heuristic and may not cover
all existing parameter declarations.

* determine valid usage references by checking ancestors (on AST level)
* split test into separate ones
2023-06-02 12:46:01 +02:00
bors
d2b3caa5b5 Auto merge of #14929 - Veykril:hover-hex, r=Veykril
Add render configs for memory layout hovers

Closes https://github.com/rust-lang/rust-analyzer/issues/14918
2023-05-30 16:47:41 +00:00
Lukas Wirth
3c862507b9 Add render configs for memory layout hovers 2023-05-30 18:36:06 +02:00
bors
e33c0b4b5e Auto merge of #14842 - alibektas:internal/ast-make-improve, r=Veykril
Improve ast::make

Add `ty_alias` and make `impl_trait` , `fn` and `impl_` have more coverage.
2023-05-30 16:09:46 +00:00
bors
51c3ab5b85 Auto merge of #14920 - lowr:fix/overhaul-named-struct-to-tuple-struct, r=Veykril
Fix edits for `convert_named_struct_to_tuple_struct`

Two fixes:
- When replacing syntax nodes, macro files weren't taken into account. Edits were simply made for `node.syntax().text_range()`, which would be wrong range when `node` is inside a macro file.
- We do ancestor node traversal for every struct name reference to find record expressions/patterns to edit, but we didn't verify that expressions/patterns do actually refer to the struct we're operating on.

Best reviewed one commit at a time.

Fixes #13780
Fixes #14927
2023-05-30 11:45:22 +00:00
Ryo Yoshida
033e6ac57a
Verify name references more rigidly
Previously we didn't verify that record expressions/patterns that were
found did actually point to the struct we're operating on. Moreover,
when that record expressions/patterns had missing child nodes, we would
continue traversing their ancestor nodes.
2023-05-29 20:10:54 +09:00
Ryo Yoshida
ab9347542c
Consider macro files when replacing nodes 2023-05-29 19:52:31 +09:00
hkalbasi
51368793b4 MIR episode 6 2023-05-28 23:25:15 +03:30
Sebastian Ziebell
e78df83e2f Integrate feedback
* pass in existing `Semantics` object to function
* pass in `Definition` for param type
* refactor iterator to use `flatten`
2023-05-26 13:43:12 +02:00
Sebastian Ziebell
ce1c85317f Check param is not referenced in function
This checks the type param is referenced neither in the function body
nor as a return type.

* add tests
2023-05-26 13:24:29 +02:00
Sebastian Ziebell
95f59668e6 Fix removal of generic param from list
This removes an existing generic param from the `GenericParamList`. It
also considers to remove the extra colon & whitespace to the previous
sibling.

* change order to get all param types first and mark them as mutable
  before the first edit happens
* add helper function to remove a generic parameter
* fix test output
2023-05-26 13:13:15 +02:00
Sebastian Ziebell
59f8827a6f Implement assist to replace named generic with impl
This adds a new assist named "replace named generic with impl" to move
the generic param type from the generic param list into the function
signature.

```rust
fn new<T: ToString>(input: T) -> Self {}
```

becomes

```rust
fn new(input: impl ToString) -> Self {}
```

The first step is to determine if the assist can be applied, there has
to be a match between generic trait param & function paramter types.

* replace function parameter type(s) with impl
* add new `impl_trait_type` function to generate the new trait bounds with `impl` keyword  for use in the
  function signature
2023-05-26 13:13:11 +02:00
bors
21e5dc2af9 Auto merge of #14881 - Veykril:name-display, r=Veykril
internal: Replace Display impl for Name

This allows us to plug in interner for `Name`s that live inside the database.
2023-05-25 12:04:16 +00:00
bors
a512774fd9 Auto merge of #14752 - ponyii:fix/generate_derive_breaks_indentation, r=Veykril
fix: assists no longer break indentation

Fixes https://github.com/rust-lang/rust-analyzer/issues/14674

These are _ad hoc_ patches for a number of assists that can produce incorrectly indented code, namely:
- generate_derive
- add_missing_impl_members
- add_missing_default_members

Some general solution is required in future, as the same problem arises in many other assists, e.g.
- replace_derive_with...
- generate_default_from_enum...
- generate_default_from_new
- generate_delegate_methods
(the list is incomplete)
2023-05-24 18:58:07 +00:00
Lukas Wirth
c7ef6c25b7 internal: Replace Display impl for Name 2023-05-24 20:55:12 +02:00
alibektas
51ec2ced69 Improve ast::make
Add `ty_alias` and make `impl_trait` , `fn` and `impl_` have more coverage.
2023-05-23 03:12:57 +02:00
Ryo Yoshida
7b70988d48
fix: consider all tokens in macro expr when analyzing locals
Also consider `self` token along with ordinary identifiers.
2023-05-21 20:07:36 +09:00
Lukas Wirth
4b577e2bc8 Support c string literals 2023-05-18 11:06:05 +02:00
ponyii
49f1092f14 add_missing_impl_members and add_missing_default_members break indentation no longer 2023-05-10 22:27:05 +04:00
ponyii
e07d6382de add_missing_impl_members no longer breaks indentation 2023-05-10 16:35:48 +04:00
ponyii
a0db4781dc generate_derive no longer breaks indentation 2023-05-10 16:35:43 +04:00
hkalbasi
36c9d5ce17 Fix pattern type mismatch in tuples 2023-05-04 16:03:36 +03:30
Jake Heinz
a86c431f45 fix: generate delegate methods filters out functions that already exist
on the struct's impls
2023-05-02 03:36:35 +00:00
cui fliter
41632fac86 fix some typos
Signed-off-by: cui fliter <imcusg@gmail.com>
2023-05-01 14:53:25 +08:00
unexge
a2ab7ee3bd Address code review comments 2023-04-27 03:03:13 +01:00
unexge
734fe66f71 Handle nested types in unwrap_result_return_type assist 2023-04-26 22:07:06 +01:00
bors
2400b36a2e Auto merge of #14577 - jsoref:spelling, r=lnicola
Spelling

This PR corrects misspellings identified by the [check-spelling action](https://github.com/marketplace/actions/check-spelling).

The misspellings have been reported at https://github.com/jsoref/rust-analyzer/actions/runs/4699991040#summary-12751355796

The action reports that the changes in this PR would make it happy: https://github.com/jsoref/rust-analyzer/actions/runs/4699991284#summary-12751356293

closes #14567
2023-04-19 14:05:40 +00:00
Josh Soref
bc7d84c3ce Spelling
* a rule
* access
* after
* amount
* annotations
* assignment
* assist
* associated
* attribute
* borrowed
* built-in type
* clarification
* command
* const
* constructor
* corresponding
* counterparts
* curlies
* dependencies
* deterministic
* diagnostic
* duplicates
* edge
* edited
* efficient
* elsewhere
* execution
* expression
* extensions
* extracted
* fill
* github
* helper
* heuristic
* incomplete
* indent end
* inlay
* invocation
* lifetime
* looking
* maybe
* move
* mutability
* mutable
* necessarily
* necessary
* negative
* nonexistent
* occurred
* offsets
* offsetted
* overridden
* parameters
* params
* params_and_where_preds_in_scope
* paredit
* parent
* parentheses
* prepended if
* punctuation
* receive
* receiver
* referring
* repeated
* representing
* semantically
* separately
* shouldnot
* siblings
* similar
* something's
* statement
* struct
* structure
* surprise
* the
* this
* transparent
* unimplemented
* unnamed
* unnecessary
* unneeded
* unreachable
* unterminated
* utilities
* variant
* variants
* visibility
* work around (v)
* workaround

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2023-04-19 09:45:55 -04:00
bors
5111207d9a Auto merge of #14591 - justahero:gh-14516, r=Veykril
Restrict "sort items" assist for traits & impls

This restricts the "sort items alphabetically" assist when the selection is inside a `Impl` or `Trait` node & intersects with one of the associated items.

It re-orders the conditional checks of AST nodes in the `sort_items` function to check for more specific nodes first before checking `Trait` or `Impl` nodes. The `AssistContext` is passed into the `add_sort_methods_assist` function to check if the selection intersects with any inner items, e.g. associated const or type alias, function. In this case the assist does not apply.

Fixes: #14516
2023-04-17 12:45:31 +00:00
Sebastian Ziebell
c1712e55c6 Restrict "sort items" assist inside Impl & Trait
This fixes the applicability of the "sort items alphabetically" assist
when the selection is inside a `Trait` or `Impl`. It's now tested if the
selection is inside or overlaps with an inner node, e.g. associated
const or type alias, function.
2023-04-17 12:36:32 +02:00
Lukas Wirth
a2a3fecae3 Option begone part 2 2023-04-16 19:20:48 +02:00
Ryo Yoshida
ac03de773f
Add flag to disallow opaque types for DisplayTarget::SourceCode 2023-04-12 19:03:48 +09:00
bors
5d41affc77 Auto merge of #14547 - Veykril:extract_adjust, r=Veykril
fix: Fix receiver adjustments for extract_variable assist
2023-04-11 12:01:47 +00:00
Lukas Wirth
72d47144e8 fix: Fix receiver adjustments for extract_variable assist 2023-04-11 14:01:23 +02:00
bors
f9f443076a Auto merge of #14540 - AmrDeveloper:disallow_extract_fun_single_brace, r=Veykril
Fix allow extracting function from single brace of block expression

Fix allow extracting function when selecting either `{` or `}`

Fix #14514
2023-04-11 10:10:25 +00:00
AmrDeveloper
5ded22065e Make all kind of braces not applicable in function extraction 2023-04-11 11:22:52 +02:00
bors
fa3db447d7 Auto merge of #14536 - ClSlaid:feat/reorder-impl-items/not-applicative-editing-assoc-items, r=Veykril
fix: restrict applicable range of `reorder-impl-trait-items`

This PR should complete the need for restricting the applicable range of `reorder-impl-trait-items`.

When the cursor is in the associated items of the `impl` range, the assist will be disabled.

Fix: #14515

## Showcases
Note: If there is any available `code-action` (`ide-assist`) available, a lightbulb icon from `lspsaga` will show in the left.

- cursor in `impl` headers
![code action available](https://user-images.githubusercontent.com/44747719/230756854-7b236018-cfa8-4005-b589-2996ec42917f.png)
Code action is available. And it is reordering impl items.
![code action detail](https://user-images.githubusercontent.com/44747719/230756971-341c7fbc-f2ba-4715-a1e5-b1add984d4dd.png)

- cursor in `impl` associated items
![code action unavailable](
https://user-images.githubusercontent.com/44747719/230756906-bee7784e-bd9d-49b2-801b-743c94b4af54.png)
2023-04-11 07:36:42 +00:00
hkalbasi
59b6f2d9f2 Compute closure captures 2023-04-10 23:04:34 +03:30
AmrDeveloper
2afc124cb9 Fix allow extracting function from single brace of block expression 2023-04-09 23:44:31 +02:00
蔡略
475aa2839f refactor: correct test sample
giving a sorted file is useless

Signed-off-by: 蔡略 <cailue@bupt.edu.cn>
2023-04-09 00:24:25 +08:00
蔡略
e90e1901ef feat: restrict applicable range of reorder-impl-trait-items 2023-04-09 00:14:48 +08:00
Lukas Wirth
8e7c104b3a Move hir_def::adt to hir_def::data::adt 2023-04-06 19:23:29 +02:00
bors
ea22d245b6 Auto merge of #14499 - DropDemBits:drive-by-fixmes, r=Veykril
minor: Fix some simple FIXMEs

Each FIXME fix has been split into its own commit, since they're all pretty independent changes.

(Forgot to open a PR for this a few days ago, oops)
2023-04-05 20:42:55 +00:00
bors
e8bad53d63 Auto merge of #14455 - jplatte:convert-nested-function-to-closure, r=Veykril
Convert nested function to closure assist

Continuation of / closes #13467.
Resolves #13230.

r? `@Veykril`
2023-04-05 17:28:46 +00:00
Jonas Platte
bc704e127d
Address another round of review comments 2023-04-05 18:48:21 +02:00
bors
da9c0bd0a7 Auto merge of #14442 - DropDemBits:structured-snippet-api, r=Veykril
internal: Implement Structured API for snippets

Fixes #11638 (including moving the cursor before the generated type parameter)

Adds `add_tabstop_{before,after}` for inserting tabstop snippets before & after nodes, and `add_placeholder_snippet` for wrapping nodes inside placeholder nodes.

Currently, the snippets are inserted into the syntax tree in `SourceChange::commit` so that snippet bits won't interfere with syntax lookups before completing a `SourceChange`.

It would be preferable if snippet rendering was deferred to after so that rendering can work directly with text ranges, but have left that for a future PR (it would also make it easier to finely specify which text edits have snippets in them).

Another possible snippet variation to support would be a group of placeholders (i.e. placeholders with the same tabstop number) so that a generated item and its uses can be renamed right as it's generated, which is something that is technically supported by the current snippet hack in VSCode, though it's not clear if that's a thing that is officially supported.
2023-04-05 14:33:30 +00:00
Ryo Yoshida
0df9fb22d8
fix: insert whitespace between text and pound
`text#`, `text"..."`, and `text'...'` are reserved syntax since Rust
2021. Note that the latter two are already handled correctly.
2023-04-05 20:33:17 +09:00
Jake Heinz
f6b0c19c56 dedupe by name 2023-04-04 06:57:02 +00:00
Jake Heinz
50b427c0d1 use crate from autoderef ty 2023-04-04 06:54:26 +00:00
Jake Heinz
94e6a6642c assist: autoderef in generate delegate methods 2023-04-04 06:50:20 +00:00
DropDemBits
d689fd30bc
Remove outdated comment in sourcegen
`CommentBlock::extract` does handle blank lines in comment blocks
2023-03-31 14:43:24 -04:00
Jonas Platte
d01a38ce3f
Address second round of review comments 2023-03-31 13:53:57 +02:00
Jonas Platte
0e11d507e1
Address review comment 2023-03-31 12:20:08 +02:00
Mathew Horner
3973d1aaa7
Add assist to convert nested function to closure 2023-03-31 12:16:49 +02:00
Lukas Wirth
e244942209 internal: Set Durability to HIGH for enable_proc_attr_macros input 2023-03-30 15:11:22 +02:00
Lukas Wirth
8ea1afce28 Simplify 2023-03-28 16:32:26 +02:00
DropDemBits
369f4774dd
fix: Move cursor before generated generic 2023-03-26 18:10:41 -04:00
hkalbasi
b7b9ae59a0 desugar ? operator 2023-03-17 13:08:35 +03:30
Lukas Wirth
802d7ea335 internal: Prioritize remove-dbg assist over inline-macro 2023-03-15 14:19:50 +01:00
Ryo Yoshida
01bf0725b1
fix: don't replace SyntaxToken with SyntaxNode 2023-03-15 17:18:09 +09:00
Lukas Wirth
9fb9ee3b6a internal: Rename AstDatabase to ExpandDatabase 2023-03-13 16:35:41 +01:00
hkalbasi
811190b913 fix multiple definition binding in match to let-else 2023-03-09 00:34:31 +03:30
bors
44ff3c407a Auto merge of #14232 - HKalbasi:mir, r=Veykril
MIR episode 2

This PR adds:
1. `need-mut` and `unused-mut` diagnostics
2. `View mir` command which shows MIR for the body under cursor, useful for debugging
3. MIR lowering for or-patterns and for-loops
2023-03-07 09:49:49 +00:00
bors
31c12ec282 Auto merge of #14266 - Veykril:generalize-eager-lazy, r=Veykril
feature: Make replace_or_with_or_else assists more generally applicable
2023-03-06 21:41:46 +00:00
Lukas Wirth
199bc82ce8 feature: Make replace_or_with_or_else assists more generally applicable 2023-03-06 22:17:30 +01:00
hkalbasi
61ad6a96ad Add BindingId 2023-03-06 21:09:08 +03:30
Lukas Wirth
7be48ac32c Adjust replace_match_with_if_let applicability range 2023-03-05 16:52:07 +01:00
Lukas Wirth
b8276481e7 Fix extract_variable test 2023-03-04 14:58:58 +01:00
bors
6756294aa0 Auto merge of #14184 - lowr:feat/trait-alias-def, r=Veykril
Handle trait alias definitions

Part of #2773

This PR adds a bunch of structs and enum variants for trait aliases. Trait aliases should be handled as an independent item because they are semantically distinct from traits.

I basically started by adding `TraitAlias{Id, Loc}` to `hir_def::item_tree` and iterated adding necessary stuffs until compiler stopped complaining what's missing. Let me know if there's still anything I need to add.

I'm opening up this PR for early review and stuff. I'm planning to add tests for IDE functionalities in this PR, but not type-related support, for which I put FIXME notes.
2023-03-03 15:45:18 +00:00
Ryo Yoshida
29c957f973
Lower and handle trait aliases in HIR 2023-03-04 00:24:07 +09:00
Ryo Yoshida
bda2af71c6
feat: allow generate_function to generate in different local crate 2023-03-03 18:40:13 +09:00
Ryo Yoshida
943de55214
Fix typo 2023-03-03 01:12:39 +09:00
Ryo Yoshida
de4a8961dc
Support removing nested dbg!()s in remove_dbg 2023-03-01 19:06:05 +09:00
bors
a0be16b0b2 Auto merge of #14040 - HKalbasi:mir, r=HKalbasi
Beginning of MIR

This pull request introduces the initial implementation of MIR lowering and interpreting in Rust Analyzer.

The implementation of MIR has potential to bring several benefits:
- Executing a unit test without compiling it: This is my main goal. It can be useful for quickly testing code changes and print-debugging unit tests without the need for a full compilation (ideally in almost zero time, similar to languages like python and js). There is a probability that it goes nowhere, it might become slower than rustc, or it might need some unreasonable amount of memory, or we may fail to support a common pattern/function that make it unusable for most of the codes.
- Constant evaluation: MIR allows for easier and more correct constant evaluation, on par with rustc. If r-a wants to fully support the type system, it needs full const eval, which means arbitrary code execution, which needs MIR or something similar.
- Supporting more diagnostics: MIR can be used to detect errors, most famously borrow checker and lifetime errors,  but also mutability errors and uninitialized variables, which can be difficult/impossible to detect in HIR.
- Lowering closures: With MIR we can find out closure capture modes, which is useful in detecting if a closure implements the `FnMut` or `Fn` traits, and calculating its size and data layout.

But the current PR implements no diagnostics and doesn't support closures. About const eval, I removed the old const eval code and it now uses the mir interpreter. Everything that is supported in stable rustc is either implemented or is super easy to implement. About interpreting unit tests, I added an experimental config, disabled by default, that shows a `pass` or `fail` on hover of unit tests (ideally it should be a button similar to `Run test` button, but I didn't figured out how to add them). Currently, no real world test works, due to missing features including closures, heap allocation, `dyn Trait` and ... so at this point it is only useful for me selecting what to implement next.

The implementation of MIR is based on the design of rustc, the data structures are almost copy paste (so it should be easy to migrate it to a possible future stable-mir), but the lowering and interpreting code is from me.
2023-02-28 09:12:19 +00:00
hkalbasi
cd67589f63 beginning of MIR 2023-02-27 23:45:54 +03:30
Lukas Wirth
9e5fa74279 Simplify 2023-02-27 15:51:45 +01:00
Azriel Hoh
a6f54d6c5f Conditionally add snippet marker. 2023-02-16 10:50:27 +13:00
Lukas Wirth
3c0f20a7bd internal: Enable smallvec's union feature 2023-02-14 17:01:01 +01:00
lh123
626dc651ff fix: Insert spaces when inlining macros 2023-02-10 14:22:56 +08:00
bors
57ea9826b5 Auto merge of #13991 - vasilev-alex:add-braces-assist, r=Veykril
feat: add braces assist

This assist adds support for adding braces where it may be appropriate (e.g. lambda expressions)

![ex](https://user-images.githubusercontent.com/4973437/213783924-7c8a8ab5-6a52-4d80-837c-cf2a9b56f061.gif)
2023-02-07 17:12:01 +00:00
Ryo Yoshida
493cabbde7
Treat scope info retrieval failure as assist failure 2023-02-02 17:47:11 +09:00
Ryo Yoshida
3edde6fcc1
Support generic function in generate_function assist 2023-01-31 21:05:25 +09:00
Ryo Yoshida
0df70d37fc
Minor refactoring 2023-01-31 21:05:04 +09:00
Jonas Schievink
6321b25a21 Apply the assist to itself 2023-01-30 17:52:15 +01:00
Jonas Schievink
1f20804b6e Improve "match to let-else" assist 2023-01-30 17:50:02 +01:00
Jonas Schievink
e7a2d13ff6 Fix "add missing impl members" assist for impls inside blocks 2023-01-27 16:51:55 +01:00
Lukas Wirth
6829190611 Handle boolean scrutinees in match <-> if let replacement assists better 2023-01-27 12:33:40 +01:00
bors
6fd5769996 Auto merge of #14011 - bvanjoi:fix-unwrap-block, r=jonas-schievink
fix(ide-assists): unwrap block when it parent is let stmt

fix #13990
2023-01-25 15:04:03 +00:00
bvanjoi
90b1222b0b fix(ide-assists): unwrap block when it parent is let stmt 2023-01-25 10:25:59 +08:00
Ryo Yoshida
01d8b8947c
Don't escape non-snippets in assist 2023-01-23 00:22:46 +09:00
Alex Vasilev
2a4837089e fixes 2023-01-21 23:42:11 +05:30
Alex Vasilev
dd9dcca7a2 assist id fix 2023-01-21 00:47:02 +05:30
Alex Vasilev
1ab58b190e cargo test 2023-01-21 00:42:29 +05:30
Alex Vasilev
ddb2f8760b typo fix 2023-01-21 00:30:38 +05:30
Alex Vasilev
69ffbe25a1 feat: add braces assist 2023-01-21 00:17:23 +05:30
bvanjoi
8fa69f9f7d feat: array match 2023-01-20 10:59:30 +08:00
Laurențiu Nicola
210757769d Fix replace_arith label 2023-01-17 16:31:34 +02:00