turbo fish supports multiple type arguments

This commit is contained in:
Jeroen Vannevel 2021-12-28 18:41:34 +00:00
parent 355a4bdb88
commit e299fd5475

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);