Fixes#8607.
This commit changes the auto-import functionality and causes it to add
imports after any leading comments, which are commonly license headers.
This does not affect comments on items as they're considered part of the
item itself and not separate.
11236: internal: Remove `InFile` wrapping from `DynMap` keys r=Veykril a=Veykril
We already store a `DynMap` per `(Container, HirFileId)` pair, so the `InFile` keys are already guruanteed to always be of the same file id
bors r+
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
11107: Fix generic type substitution in impl trait with assoc type r=pnevyk a=pnevyk
Fixes#11045
The path transform now detects if a type parameter that is being substituted has an associated type. In that case it is necessary (or safe in general case) to fully qualify the substitution with a trait which the associated type belongs to.
This PR also fixes the previous wrong behavior of the substitution that could create an invalid tree `PATH_TYPE -> PATH_TYPE -> ...`.
Co-authored-by: Petr Nevyhoštěný <petr.nevyhosteny@gmail.com>
11145: feat: add config to use reasonable default expression instead of todo! when filling missing fields r=Veykril a=bnjjj
Use `Default::default()` in struct fields when we ask to fill it instead of putting `todo!()` for every fields
before:
```rust
pub enum Other {
One,
Two,
}
pub struct Test {
text: String,
num: usize,
other: Other,
}
fn t_test() {
let test = Test {<|>};
}
```
after:
```rust
pub enum Other {
One,
Two,
}
pub struct Test {
text: String,
num: usize,
other: Other,
}
fn t_test() {
let test = Test {
text: String::new(),
num: 0,
other: todo!(),
};
}
```
Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
Co-authored-by: Coenen Benjamin <benjamin.coenen@hotmail.com>
11220: Turbo fish assist: don't include lifetime parameters r=Veykril a=Vannevelj
Fixes#11219
The issue talks about three different types of params: type, const & lifetime. I wasn't entirely sure which ones are intended to be included here so I've gone for the type & const params (i.e. exclude lifetime).
I've added a test case for both a lifetime param and a const param. I'm still making my way through the rust book (chapter 7, yay) so I'm not too sure yet what these are but my testing shows that this approach generates code that compiles.
Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
11225: internal: Cleanup doc and attribute handling r=Veykril a=Veykril
(very vague PR title but as I tried to fix the mentioned issue I ran into more and more subtle things that were interwoven)
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/11215
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
11194: fix(gen-doc-assist): remove lifetimes in description of `new` r=Veykril a=numero-744
From wrong behavior:
```rust
/// Creates a new [`MyGenericStruct<'a, T>`].
```
to correct behavior:
```rust
/// Creates a new [`MyGenericStruct<T>`].
```
But I feel like there is a better way to implement it. Do you know if there is an existing function that could do the work of `lifetimes_removed()` below?
Co-authored-by: Côme ALLART <come.allart@etu.emse.fr>
11218: fix: Correct has_ref detection, avoiding duplicate &mut insertion on parameter completion r=Veykril a=weirane
The original code fails to detect there's a ref in scenarios such as `&mut s` and `& s` because `WHITESPACE` and `IDENT` got reversed.
Closes#11199.
Co-authored-by: Wang Ruochen <wrc@ruo-chen.wang>
11193: feat: Add config to replace specific proc-macros with dummy expanders r=Veykril a=Veykril
With this one can specify proc-macros from crates to expand into their input as a (temporary) workaround for the current completion problems with some of the bigger attribute proc-macros like `async_trait`.
This could've been done by just not expanding these macros, but that would require fiddling with nameres. I felt like this approach was simpler to pull off while also keeping the behaviour of the attributes/proc-macro in that they still expand instead of being dead syntax to us.
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/11052
Usage(`async_trait` as example):
```jsonc
"rust-analyzer.procMacro.dummies": {
"async-trait": [ // crate name(as per its cargo.toml definition, not the dependency name)
"async_trait" // exported proc-macro name
]
},
```
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
11204: fix: `replace_qualified_name_with_use` does not use full item path for replacements r=Veykril a=Veykril
bors r+
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
11195: Correctly pass through reference modifiers when extracting a variable r=Veykril a=Vannevelj
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10034
This will parse the field expression and look at whether it is marked `&` or `&mut` and include a modifier if appropriate. The original issue only mentions `&mut params` but I've found that this issue also occurs for `&mut locals` as well as `¶ms` and `&locals` so I've also added tests for them.
I'd definitely be interested in hearing where I can make my code more idiomatic for Rust.
11202: fix: Fix `apply_demorgan` assist hanging for certain binary expressions r=Veykril a=Veykril
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10963
bors r+
Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
11201: fix: Fix completions not considering ancestor items for attribute search r=Veykril a=Veykril
Turns out we never filled the `CompletionContext` with the attribute expansion of attributed impls and traits when typing in the assoc items, as we were only considering the assoc item to have an attribute to expand.
bors r+
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
11190: fix(completions): improve fn_param r=dbofmmbt a=dbofmmbt
- insert commas around when necessary
- only suggest `self` completions when param list is empty
- stop suggesting completions for identifiers which are already on the param list
Closes#11085
Co-authored-by: Eduardo Canellas <eduardocanellas98@gmail.com>
- insert commas around when necessary
- only suggest `self` completions when param list is empty
- stop suggesting completions for identifiers which are already on the param list
11184: Correctly pass through mutable parameter references when extracting a function r=Veykril a=Vannevelj
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10277
I have based this investigation based on my understanding of [the Borrowing chapter](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html) but I wasn't able to debug the test runs or see it in action in an IDE. I'll try to figure out how to do that for future PRs but for now, the tests seem to confirm my understanding. I'll lay out my hypothesis below.
Here we define the parameters for the to-be-generated function:
7409880a07/crates/ide_assists/src/handlers/extract_function.rs (L882)
Three values in particular are important here: `requires_mut`, `is_copy` and `move_local`. These will in turn be used here to determine the kind of parameter:
7409880a07/crates/ide_assists/src/handlers/extract_function.rs (L374-L381)
and then here to determine what transformation is needed for the calling argument:
7409880a07/crates/ide_assists/src/handlers/extract_function.rs (L383-L390)
which then gets transformed here:
7409880a07/crates/syntax/src/ast/make.rs (L381-L383)
What I believe is happening is that
* `requires_mut` is `false` (it already is marked as mutable),
* `is_copy` is `false` (`Foo` does not implement `Copy`), and
* `move_local` is `false` (it has further usages)
According to the pattern matching in `fn kind()`, that would lead to `ParamKind::SharedRef` which in turn applies a transformation that prepends `&`.
However if I look at the chapter on borrowing then we only need to mark an argument as a reference if we actually own it. In this case the value is passed through as a reference parameter into the current function which means we never had ownership in the first place. By including the additional check for a reference parameter, `move_local` now becomes `true` and the resulting parameter is now `ParamKind::Value` which will avoid applying any transformations. This was further obscured by the fact that you need further usages of the variable or `move_local` would be considered `true` after all.
I didn't follow it in depth but it appears this idea applies for both the generated argument and the generated parameter.
There are existing tests that account for `&mut` values but they refer to local variables for which we do have ownership and as such they didn't expose this issue.
Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
11061: Support "move if to guard" for if else chains r=weirane a=weirane
The idea is to first parse the if else chain into a vector of `(Condition, BlockExpr)`s until we reach an iflet branch, an else branch, or the end (the tail). Then add the match arms with guard for the vector, and add the tail with no if guard.
Because the whole original match arm is replaced and the generated code doesn't have redundent commas, I removed redundent commas in some test cases.
Closes#11033.
Co-authored-by: Wang Ruochen <wrc@ruo-chen.wang>