11136: Turbo fish assist supports multiple type arguments r=matklad a=Vannevelj

This fixes #11135 (changelog: bug).

I've only started using Rust a few days ago but saw this issue on the top of the list when I looked at this repo. I based myself on [this blog post](https://techblog.tonsser.com/posts/what-is-rusts-turbofish) to understand what a "turbo fish" is so let me know if I missed anything.

Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
This commit is contained in:
bors[bot] 2022-01-01 16:42:22 +00:00 committed by GitHub
commit 3f2edc72cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
use ide_db::defs::{Definition, NameRefClass};
use itertools::Itertools;
use syntax::{ast, AstNode, SyntaxKind, T};
use crate::{
@ -77,13 +78,22 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
}
}
let number_of_arguments = generics.len();
let fish_head = std::iter::repeat("_").take(number_of_arguments).collect_vec().join(",");
acc.add(
AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
"Add `::<>`",
ident.text_range(),
|builder| match ctx.config.snippet_cap {
Some(cap) => builder.insert_snippet(cap, ident.text_range().end(), "::<${0:_}>"),
None => builder.insert(ident.text_range().end(), "::<_>"),
Some(cap) => {
let snip = format!("::<${{0:{}}}>", fish_head);
builder.insert_snippet(cap, ident.text_range().end(), snip)
}
None => {
let snip = format!("::<{}>", fish_head);
builder.insert(ident.text_range().end(), snip);
}
},
)
}
@ -113,6 +123,44 @@ fn main() {
);
}
#[test]
fn add_turbo_fish_function_multiple_generic_types() {
check_assist(
add_turbo_fish,
r#"
fn make<T, A>() -> T {}
fn main() {
make$0();
}
"#,
r#"
fn make<T, A>() -> T {}
fn main() {
make::<${0:_,_}>();
}
"#,
);
}
#[test]
fn add_turbo_fish_function_many_generic_types() {
check_assist(
add_turbo_fish,
r#"
fn make<T, A, B, C, D, E, F>() -> T {}
fn main() {
make$0();
}
"#,
r#"
fn make<T, A, B, C, D, E, F>() -> T {}
fn main() {
make::<${0:_,_,_,_,_,_,_}>();
}
"#,
);
}
#[test]
fn add_turbo_fish_after_call() {
cov_mark::check!(add_turbo_fish_after_call);