Commit Graph

684 Commits

Author SHA1 Message Date
Lukas Wirth
35620306a6 internal: Move proc-macro knowledge out of base-db 2023-12-18 12:37:18 +01:00
bors
dd07f1f2fb Auto merge of #15887 - jinohkang-theori:fix/ide-assists/convert_tuple_struct_to_named_struct/handle_refs_inside_macro_invocations, r=Veykril
Fix incorrectly replacing references in macro invocation in "Convert to named struct" assist

Fixes #15630.

Complements #13647 (same assist but missed this one), #14920 (inverse action assist).
2023-12-13 21:30:18 +00:00
roife
38b5dcb53c fix: self type replacement with macros 2023-12-14 00:42:33 +08:00
roife
3449222ab1 fix: self type replacement in inline-function 2023-12-13 23:13:55 +08:00
roife
5070534d32 fix: testcases in generate_delegate_trait 2023-12-13 11:42:45 +08:00
roife
59aa791fe6 fix: rewrite code_action generate_delegate_trait 2023-12-13 11:22:42 +08:00
DropDemBits
1506435f65
Update various_resolve_strategies test
The weird disjoint `Indel`s are likely an artifact of the tree diffing
algorithm we use.
2023-12-10 20:33:32 -05:00
DropDemBits
c486637ec5
Migrate replace_is_method_with_if_let_method to mutable ast 2023-12-10 18:39:43 -05:00
DropDemBits
316269901f
Migrate generate_function to mutable ast 2023-12-10 18:39:43 -05:00
DropDemBits
1e1761e9ae
Migrate extract_variable to mutable ast 2023-12-10 18:37:51 -05:00
roife
63d2f353d9 fix: fix 'introduce_named_generic' for impl inside types 2023-12-09 17:11:48 +08:00
bors
6bbb2ac304 Auto merge of #15705 - rmehri01:14485_fix_delegate_self_references, r=Veykril
fix: resolve Self type references in delegate method assist

This PR makes the delegate method assist resolve any `Self` type references in the parameters or return type. It also works across macros such as the `uint_impl!` macro used for `saturating_mul` in the issue example.

Closes #14485
2023-12-08 11:31:34 +00:00
Ryan Mehri
7e768cbe70 fix: prefer keeping Self if it is in the same impl def 2023-12-08 12:30:14 +01:00
Ryan Mehri
f4349ff26e fix: preserve where clause in delegate method 2023-12-08 12:29:34 +01:00
Ryan Mehri
934358e95c fix: resolve Self type references in delegate method assist 2023-12-08 12:29:34 +01:00
Matheus Cardoso
3d9221291f flip binexpr works for lhs cmp 2023-12-08 11:36:30 +01:00
Matheus Cardoso
e18b89452e Flip binexpr works for lhs binexpr 2023-12-08 11:36:30 +01:00
bors
49dd3804c0 Auto merge of #16009 - werifu:fix-extract-function, r=Veykril
fix: bug in extract_function.rs

There is a little bug in extract_function: It appends `use path::to::ControlFlow;` if the function created contains string "ControlFlow".

 A case below (also in the test named `does_not_import_control_flow` which will fail in the original code)

<img width="322" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/53432474/4b80bb58-0cfd-4d56-b64c-d9649eed336e">
<img width="391" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/53432474/3d7262f4-8a4c-44ea-822d-304b8b23fe28">

Now I have changed the condition determining whether adding import statement. Only when the new function body contains ControlFlow::Break or ControlFlow::Continue can the import statement be added.

Last related PR: https://github.com/rust-lang/rust-analyzer/pull/10309
2023-12-07 17:28:41 +00:00
Lukas Wirth
634d588fd7 Simplify 2023-12-06 14:36:39 +01:00
Lukas Wirth
d2cd30007c Implicit format args support 2023-12-05 17:07:00 +01:00
Lukas Wirth
5b8e386bae Improve macro descension API 2023-12-05 17:06:57 +01:00
Igor Matuszewski
a7224c998d Don't explicitly warn against semicolon_in_expressions_from_macros
This has been warn-by-default for two years now and has already been
added to the future-incompat lints in 1.68.
2023-12-05 11:35:09 +01:00
werifu
05e8b926e6 fix: bug in extract_function: should not import ControlFlow in some cases 2023-12-04 16:23:18 +08:00
Lukas Wirth
81410ab500 Cleanup FileId stuff 2023-12-02 19:32:53 +01:00
Lukas Wirth
c43078f99d Re-implement InFile wrappers as type aliases over generic InFileWrapper 2023-11-28 10:55:40 +01:00
Lukas Wirth
890eb17b4e Replace ID based TokenMap with proper relative text-ranges / spans 2023-11-28 10:55:39 +01:00
bors
4ab67291fa Auto merge of #15857 - Young-Flash:fix, r=Veykril
fix: remove parenthesis should ensure space

close https://github.com/rust-lang/rust-analyzer/issues/15844
2023-11-27 16:14:31 +00:00
David Tolnay
b68f5311b5
Replace option.map(cond) == Some(true) with option.is_some_and(cond) 2023-11-24 09:06:44 -08:00
Young-Flash
bd5a63b208 move parentheses judge logic into builder 2023-11-22 14:11:00 +08:00
Young-Flash
1cbda612bc chore: add use case for PathSegment::qualifying_trait 2023-11-17 21:31:33 +08:00
Laurențiu Nicola
cb8434e594 Bump either 2023-11-15 13:07:36 +02:00
Laurențiu Nicola
f66df10f87 Bump itertools 2023-11-15 12:53:56 +02:00
bors
535eb0da9d Auto merge of #15874 - DropDemBits:structured-snippet-migrate-4, r=Veykril
internal: Migrate assists to the structured snippet API, part 4

Continuing from #15260

Migrates the following assists:
- `add_turbo_fish`
- `add_type_ascription`
- `destructure_tuple_binding`
- `destructure_tuple_binding_in_subpattern`

I did this a while ago, but forgot to make a PR for the changes until now. 😅
2023-11-15 09:54:45 +00:00
DropDemBits
3f99a56fae
Fix panic in add_type_ascription
Assist wasn't applicable when the let statement was missing a pattern
before, so we should do the same now.
2023-11-14 18:43:48 -05:00
Jinoh Kang
0beba7cfc4 fix: Fix incorrectly replacing references in macro invocation in "Convert to named struct" assist 2023-11-14 16:01:59 +09:00
DropDemBits
787ca888e3
Add IdentPat::set_pat
Needed so that the `tuple_pat` node gets added to the syntax tree,
which is required as we're using structured snippets.
2023-11-13 20:41:06 -05:00
DropDemBits
6f68cd3394
Remove unwraps from destructure_tuple_binding 2023-11-13 18:42:58 -05:00
DropDemBits
4aaa592a9a
Migrate destructure_tuple_binding to mutable ast
Due to the way the current tree mutation api works, we need to collect
changes before we can apply them to the real syntax tree, and also can only
switch to a file once.

`destructure_tuple_binding_in_sub_pattern` also gets migrated even
though can't be used.
2023-11-11 21:07:19 -05:00
DropDemBits
f3dcc67dfa
Migrate add_type_ascription 2023-11-11 21:05:27 -05:00
DropDemBits
cc4e06f04b
Migrate add_turbo_fish to mutable ast
`add_type_ascription` is still left as-is since it's a different assist
2023-11-11 21:05:26 -05:00
Lukas Wirth
ba61766217 Add config for preferring / ignoring prelude modules in find_path 2023-11-11 14:56:38 +01:00
Young-Flash
be62e0bf08 fix: remove parenthesis should ensure space 2023-11-09 18:33:49 +08:00
Ali Bektas
b0101da116 Ignore doc(hidden) attr if no body is present 2023-11-09 02:12:53 +01:00
Young-Flash
b84940b199 make generate_mut_trait_impl assist trigged for std trait only 2023-11-05 12:27:10 +08:00
Young-Flash
8d8d12120d feat: add generate_mut_trait_impl assist 2023-11-05 12:27:10 +08:00
bors
99e94d2938 Auto merge of #15788 - Young-Flash:import_anonymously, r=lnicola
feat: import trait with alias

![import_trait_with_alias](https://github.com/rust-lang/rust-analyzer/assets/71162630/81601160-fe55-46e3-ab8d-b2705e1aa696)

cc `@Veykril`

close https://github.com/rust-lang/rust-analyzer/issues/15684
2023-10-31 14:33:00 +00:00
Young-Flash
929544ef28 use check_assist_by_label to pick assist 2023-10-31 22:13:07 +08:00
Young-Flash
7186a28717 chore: add unapplicable test for extract_variable without select 2023-10-28 10:06:09 +08:00
Young-Flash
00cdbe6c96 feat: make extract_variable assist in place 2023-10-27 21:16:34 +08:00
bors
2f6961aaaf Auto merge of #15780 - Young-Flash:auto_import, r=lnicola
fix: import trait if needed for `unqualify_method_call` assist

before:

![before](https://github.com/rust-lang/rust-analyzer/assets/71162630/66fda67d-afcb-453f-91a9-7e85993c3d2a)

after:

![after](https://github.com/rust-lang/rust-analyzer/assets/71162630/72ffbda4-1615-4413-836e-480eb52e9728)

follow up https://github.com/rust-lang/rust-analyzer/pull/13825
2023-10-23 16:56:22 +00:00
Young-Flash
45ee88f9cb fix: remove unwrap 2023-10-23 23:12:07 +08:00
Young-Flash
4f5f7e2800 feat: import trait with alias 2023-10-22 21:39:00 +08:00
Young-Flash
a7f77d89a9 fix: auto import trait if needed 2023-10-19 17:34:17 +08:00
Young-Flash
1a0fe58d81 refactor: change generated variable name 2023-10-17 17:34:11 +08:00
Young-Flash
bc34e8f1ad feat: make cursor select at _tmp 2023-10-14 11:05:38 +08:00
Young-Flash
5bbca22720 update mod order to adapt alphabetically sorted 2023-10-13 00:09:13 +08:00
Young-Flash
506b1e515b feat: add replace_is_ok_with_if_let_ok assist 2023-10-12 23:26:42 +08:00
Young-Flash
3605bb38ff fix CI: generate doctest 2023-10-12 11:50:44 +08:00
Young-Flash
a7fada4650 add replace_is_some_with_if_let_some assist 2023-10-11 17:44:27 +08:00
bors
ab62c0186f Auto merge of #15696 - rmehri01:14293_tuple_return_type_to_struct, r=Veykril
feat: implement tuple return type to tuple struct assist

This PR implements the `convert_tuple_return_type_to_struct` assist, for converting the return type of a function or method from a tuple to a tuple struct. Additionally, it moves the `to_camel_case` and `char_has_case` functions from `case_conv` to `stdx` so that they can be used similar to `to_lower_snake_case`.

[tuple_return_type_to_tuple_struct.webm](https://github.com/rust-lang/rust-analyzer/assets/52933714/2803ff58-fde3-4144-9495-7c7c7e139075)

Currently, the assist puts the struct definition above the function, or above the nearest `impl` or `trait` if applicable and only rewrites literal tuples that are returned in the body of the function. Additionally, it only attempts to rewrite simple tuple pattern usages with the corresponding tuple struct pattern but does so across files and modules.

I think that this is sufficient for the majority of use cases but I could be wrong. One thing I'm still not sure how to approach is handling `Self` and generics/lifetimes in the tuple type to be extracted. I was thinking of either manually figuring out what lifetimes and generics are in scope and using them (sort of similar to the `generate_function` assist) or maybe using `ctx.sema.resolve_type` and `generic_params` on `hir::Type` but this seems to not deal with lifetimes.

Closes #14293
2023-10-09 08:14:42 +00:00
bors
695c612489 Auto merge of #15641 - alibektas:15598/fix_into_to_from, r=Veykril
fix: preceding QualifiedPathType for into_to_from assist

fixes #15598
2023-10-05 08:25:58 +00:00
Ryan Mehri
9ba8dbc902 style: clean up magic number for finding pattern usages 2023-10-04 08:04:59 -07:00
bors
7e9da40078 Auto merge of #15700 - rmehri01:15694_iterator_demorgan, r=Veykril
feat: add assist for applying De Morgan's law to `Iterator::all` and `Iterator::any`

This PR adds an assist for transforming expressions of the form `!iter.any(|x| predicate(x))` into `iter.all(|x| !predicate(x))` and vice versa.

[IteratorDeMorgans.webm](https://github.com/rust-lang/rust-analyzer/assets/52933714/aad1a299-6620-432b-9106-aafd2a7fa9f5)

Closes #15694
2023-10-04 11:08:44 +00:00
Lukas Wirth
c266387e13
Replace unwrap with expect 2023-10-04 13:06:23 +02:00
Ryan Mehri
34d3490198 feat: add assist for applying De Morgan's law to iterators 2023-10-01 21:30:10 -07:00
Ryan Mehri
146a7cc490 fix: allow more kinds of if let patterns in guarded return assist 2023-10-01 11:48:10 -07:00
Ryan Mehri
2611fbf623 implement basic version of convert_tuple_return_type_to_struct assist 2023-09-30 17:07:01 -07:00
bors
87e2c310f9 Auto merge of #15667 - rmehri01:bool_to_enum_top_level, r=Veykril
fix: make bool_to_enum assist create enum at top-level

This pr makes the `bool_to_enum` assist create the `enum` at the next closest module block or at top-level, which fixes a few tricky cases such as with an associated `const` in a trait or module:

```rust
trait Foo {
    const $0BOOL: bool;
}

impl Foo for usize {
    const BOOL: bool = true;
}

fn main() {
    if <usize as Foo>::BOOL {
        println!("foo");
    }
}
```

Which now properly produces:

```rust
#[derive(PartialEq, Eq)]
enum Bool { True, False }

trait Foo {
    const BOOL: Bool;
}

impl Foo for usize {
    const BOOL: Bool = Bool::True;
}

fn main() {
    if <usize as Foo>::BOOL == Bool::True {
        println!("foo");
    }
}
```

I also think it's a bit nicer, especially for local variables, but didn't really know to do it in the first PR :)
2023-09-29 10:20:11 +00:00
Ryan Mehri
1b3e5b2105 style: simplify node_to_insert_before 2023-09-28 10:09:13 -07:00
bors
3b1b58c225 Auto merge of #15662 - rmehri01:fix_panic_with_return_in_match, r=Veykril
fix: panic with wrapping/unwrapping result return type assists

With the `wrap_return_type_in_result` assist, the following code results in a panic (note the lack of a semicolon):

```rust
fn foo(num: i32) -> $0i32 {
    return num
}

=>

thread 'handlers::wrap_return_type_in_result::tests::wrap_return_in_tail_position' panicked at crates/syntax/src/ted.rs:137:41:
called `Option::unwrap()` on a `None` value
```

I think this is because it first walks the body expression to change any `return` expressions and then walks all tail expressions, resulting in the `return num` being changed twice since it is both a `return` and in tail position. This can also happen when a `match` is in tail position and `return` is used in a branch for example. Not really sure how big of an issue this is in practice though since this seems to be the only case that is impacted and can be reduced to just `num` instead of `return num`.

This also occurs with the `unwrap_result_return_type` assist but panics with the following instead:

```
thread 'handlers::unwrap_result_return_type::tests::wrap_return_in_tail_position' panicked at /rustc/3223b0b5e8dadda3f76c3fd1a8d6c5addc09599e/library/alloc/src/string.rs:1766:29:
assertion failed: self.is_char_boundary(n)
```
2023-09-26 14:18:33 +00:00
Ryan Mehri
73150c3f36 fix: wrap method call exprs in parens 2023-09-25 21:44:16 -07:00
Ryan Mehri
bce4be9478 fix: make bool_to_enum assist create enum at top-level 2023-09-25 21:01:54 -07:00
Milo
85ead6ec27 remove other unwraps 2023-09-25 11:48:23 +00:00
Milo
f64eecd2e2 fix one 2023-09-25 11:30:21 +00:00
Ryan Mehri
7306504b82 fix panic with wrapping/unwrapping result return type assists 2023-09-24 16:00:55 -07:00
Ali Bektas
fc258de5a3 Make QualPathTy case readable 2023-09-22 21:23:03 +02:00
bors
8139e8e072 Auto merge of #15425 - alibektas:deunwrap/convert_comment_block, r=Veykril
minor : Deunwrap convert_comment_block and desugar_doc_comment

Closes subtask 13 of #15398 . I still don't know a more idiomatic way for the for loops I added, any suggestion would make me happy.
2023-09-22 15:47:55 +00:00
bors
59bcbafc95 Auto merge of #15594 - alibektas:deunwrap/add_missing_match_arms, r=Veykril
Deunwrap add_missing_match_arms

Last subtask of #15398
2023-09-22 15:31:30 +00:00
Ali Bektas
132a6ce8fc Omit QualPathTy when possible 2023-09-22 14:04:17 +02:00
Ali Bektas
622e1a8d88 Add a test case to add_missing_match_arms
Although it doesn't panic now, further changes to how we recover from incomplete syntax
may cause this assist to panic. To mitigate this a test case has been added.
2023-09-22 13:51:19 +02:00
Ali Bektas
0a91a54794 v4 2023-09-22 13:32:20 +02:00
Ali Bektas
695a1349fa Fix doctest 2023-09-22 10:46:21 +02:00
Ali Bektas
8ad536f2d1 Make path start with a QualifiedPathType 2023-09-22 10:46:21 +02:00
bors
df75809a85 Auto merge of #15484 - rmehri01:14779_bool_to_enum_assist, r=Veykril
feat: Bool to enum assist

This adds the `bool_to_enum` assist, which converts the type of boolean local variables, fields, constants and statics to a new `enum` type, making it easier to distinguish the meaning of `true` and `false` by renaming the variants.

Closes #14779
2023-09-22 07:19:12 +00:00
bors
2ededa2f14 Auto merge of #15432 - alibektas:deunwrap/inline_call, r=Veykril
minor : Deunwrap inline call

#15398 subtask 4. There is still one instance of unwrap, which I found pretty hard to change.
2023-09-22 07:03:02 +00:00
Lukas Wirth
93562dd5bd Use parent + and_then instead of ancestors 2023-09-22 08:53:24 +02:00
Ryan Mehri
ea11846490 fix parens when inlining closure in body of function 2023-09-21 21:55:10 -07:00
Ryan Mehri
60f7473c99 fix parens when inlining closure local variables 2023-09-21 21:31:15 -07:00
Kevin Reid
cac796acb3 Give unmerge_use a label explaining what it will affect. 2023-09-16 13:29:03 -07:00
bors
9d0ccf01a1 Auto merge of #15597 - rmehri01:fix_promote_local_field_shorthand, r=HKalbasi
Field shorthand overwritten in promote local to const assist

Currently, running `promote_local_to_const` on the following:

```rust
struct Foo {
    bar: usize,
}

fn main() {
    let $0bar = 0;
    let foo = Foo { bar };
}
```

Results in:

```rust
struct Foo {
    bar: usize,
}

fn main() {
    const BAR: usize = 0;
    let foo = Foo { BAR };
}
```

But instead should be something like:

```rust
struct Foo {
    bar: usize,
}

fn main() {
    const BAR: usize = 0;
    let foo = Foo { bar: BAR };
}
```
2023-09-16 16:48:21 +00:00
Ryan Mehri
cd0a89ac4f fix: field shorthand overwritten in promote local to const assist 2023-09-11 10:59:23 -07:00
Ali Bektas
145a101fe8 Deunwrap add_missing_match_arms 2023-09-11 14:09:19 +02:00
Ali Bektas
893e19137e Make assist lazy again 2023-09-11 13:33:26 +02:00
Ali Bektas
0863024b1a Make assist lazy again 2023-09-11 13:31:42 +02:00
Ryan Mehri
25b1b3e753 feat: add support for other ADT types and destructuring patterns 2023-09-10 22:21:12 -07:00
Ali Bektas
2fdf7e4b75 v3 2023-09-10 23:15:37 +02:00
Ali Bektas
a66dbd11ed v2 2023-09-10 23:15:05 +02:00
Ali Bektas
b316bccc97 replace for loops with sth more idiomatic 2023-09-10 23:15:05 +02:00
Ali Bektas
9c6257138d Deunwrap convert_comment_block 2023-09-10 23:15:05 +02:00
Ali Bektas
0f1673c6f1 v3 2023-09-10 23:00:19 +02:00