2020-10-15 10:27:50 -05:00
|
|
|
use ide_db::defs::{Definition, NameRefClass};
|
2022-01-07 07:53:42 -06:00
|
|
|
use itertools::Itertools;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{ast, AstNode, SyntaxKind, T};
|
2020-05-19 17:07:00 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
2020-06-28 17:36:05 -05:00
|
|
|
AssistId, AssistKind,
|
2020-05-19 17:07:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Assist: add_turbo_fish
|
|
|
|
//
|
|
|
|
// Adds `::<_>` to a call of a generic method or function.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn make<T>() -> T { todo!() }
|
|
|
|
// fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
// let x = make$0();
|
2020-05-19 17:07:00 -05:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn make<T>() -> T { todo!() }
|
|
|
|
// fn main() {
|
|
|
|
// let x = make::<${0:_}>();
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-11-06 15:21:56 -06:00
|
|
|
let ident = ctx.find_token_syntax_at_offset(SyntaxKind::IDENT).or_else(|| {
|
2020-07-18 07:48:28 -05:00
|
|
|
let arg_list = ctx.find_node_at_offset::<ast::ArgList>()?;
|
2021-03-20 07:44:12 -05:00
|
|
|
if arg_list.args().next().is_some() {
|
2020-07-18 07:48:28 -05:00
|
|
|
return None;
|
|
|
|
}
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(add_turbo_fish_after_call);
|
|
|
|
cov_mark::hit!(add_type_ascription_after_call);
|
2020-07-18 07:48:28 -05:00
|
|
|
arg_list.l_paren_token()?.prev_token().filter(|it| it.kind() == SyntaxKind::IDENT)
|
|
|
|
})?;
|
2020-05-19 17:07:00 -05:00
|
|
|
let next_token = ident.next_token()?;
|
|
|
|
if next_token.kind() == T![::] {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(add_turbo_fish_one_fish_is_enough);
|
2020-05-19 17:07:00 -05:00
|
|
|
return None;
|
|
|
|
}
|
2021-01-30 09:19:21 -06:00
|
|
|
let name_ref = ast::NameRef::cast(ident.parent()?)?;
|
2020-10-15 10:27:50 -05:00
|
|
|
let def = match NameRefClass::classify(&ctx.sema, &name_ref)? {
|
2020-05-19 17:07:00 -05:00
|
|
|
NameRefClass::Definition(def) => def,
|
2021-07-11 06:55:24 -05:00
|
|
|
NameRefClass::FieldShorthand { .. } => return None,
|
2020-05-19 17:07:00 -05:00
|
|
|
};
|
|
|
|
let fun = match def {
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Function(it) => it,
|
2020-05-19 17:07:00 -05:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let generics = hir::GenericDef::Function(fun).params(ctx.sema.db);
|
|
|
|
if generics.is_empty() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(add_turbo_fish_non_generic);
|
2020-05-19 17:07:00 -05:00
|
|
|
return None;
|
|
|
|
}
|
2021-03-02 08:28:53 -06:00
|
|
|
|
|
|
|
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
|
|
|
|
if let_stmt.colon_token().is_none() {
|
|
|
|
let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end();
|
2021-03-11 04:36:45 -06:00
|
|
|
let semi_pos = let_stmt.syntax().last_token()?.text_range().end();
|
|
|
|
|
2021-03-02 08:28:53 -06:00
|
|
|
acc.add(
|
|
|
|
AssistId("add_type_ascription", AssistKind::RefactorRewrite),
|
|
|
|
"Add `: _` before assignment operator",
|
|
|
|
ident.text_range(),
|
2021-03-11 04:36:45 -06:00
|
|
|
|builder| {
|
|
|
|
if let_stmt.semicolon_token().is_none() {
|
|
|
|
builder.insert(semi_pos, ";");
|
|
|
|
}
|
|
|
|
match ctx.config.snippet_cap {
|
|
|
|
Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"),
|
|
|
|
None => builder.insert(type_pos, ": _"),
|
|
|
|
}
|
2021-03-02 08:28:53 -06:00
|
|
|
},
|
|
|
|
)?
|
|
|
|
} else {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(add_type_ascription_already_typed);
|
2021-03-02 08:28:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 19:02:16 -06:00
|
|
|
let number_of_arguments = generics
|
|
|
|
.iter()
|
2022-01-07 06:40:41 -06:00
|
|
|
.filter(|param| {
|
|
|
|
matches!(param, hir::GenericParam::TypeParam(_) | hir::GenericParam::ConstParam(_))
|
|
|
|
})
|
2022-01-06 19:02:16 -06:00
|
|
|
.count();
|
2021-12-28 12:41:34 -06:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Add `::<>`",
|
|
|
|
ident.text_range(),
|
|
|
|
|builder| match ctx.config.snippet_cap {
|
2021-12-28 12:41:34 -06:00
|
|
|
Some(cap) => {
|
2022-01-07 07:17:21 -06:00
|
|
|
let snip = format!("::<{}>", get_snippet_fish_head(number_of_arguments));
|
2021-12-28 12:41:34 -06:00
|
|
|
builder.insert_snippet(cap, ident.text_range().end(), snip)
|
2021-12-28 12:52:12 -06:00
|
|
|
}
|
2021-12-28 12:41:34 -06:00
|
|
|
None => {
|
2022-01-07 07:53:42 -06:00
|
|
|
let fish_head = std::iter::repeat("_").take(number_of_arguments).format(", ");
|
2021-12-28 12:41:34 -06:00
|
|
|
let snip = format!("::<{}>", fish_head);
|
|
|
|
builder.insert(ident.text_range().end(), snip);
|
2021-12-28 12:52:12 -06:00
|
|
|
}
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 07:17:21 -06:00
|
|
|
/// This will create a snippet string with tabstops marked
|
|
|
|
fn get_snippet_fish_head(number_of_arguments: usize) -> String {
|
2022-01-07 07:53:42 -06:00
|
|
|
let mut fish_head = (1..number_of_arguments)
|
|
|
|
.format_with("", |i, f| f(&format_args!("${{{}:_}}, ", i)))
|
|
|
|
.to_string();
|
2022-01-07 07:17:21 -06:00
|
|
|
|
|
|
|
// tabstop 0 is a special case and always the last one
|
|
|
|
fish_head.push_str("${0:_}");
|
|
|
|
fish_head
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:07:00 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-02 08:28:53 -06:00
|
|
|
use crate::tests::{check_assist, check_assist_by_label, check_assist_not_applicable};
|
2020-05-19 17:07:00 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_function() {
|
|
|
|
check_assist(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
make$0();
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
make::<${0:_}>();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-28 12:41:34 -06:00
|
|
|
#[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() {
|
2022-01-07 07:53:42 -06:00
|
|
|
make::<${1:_}, ${0:_}>();
|
2021-12-28 12:41:34 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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() {
|
2022-01-07 07:53:42 -06:00
|
|
|
make::<${1:_}, ${2:_}, ${3:_}, ${4:_}, ${5:_}, ${6:_}, ${0:_}>();
|
2021-12-28 12:41:34 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-18 07:48:28 -05:00
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_after_call() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(add_turbo_fish_after_call);
|
2020-07-18 07:48:28 -05:00
|
|
|
check_assist(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
make()$0;
|
2020-07-18 07:48:28 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
make::<${0:_}>();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:07:00 -05:00
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_method() {
|
|
|
|
check_assist(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn make<T>(&self) -> T {}
|
|
|
|
}
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
S.make$0();
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn make<T>(&self) -> T {}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
S.make::<${0:_}>();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_one_fish_is_enough() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(add_turbo_fish_one_fish_is_enough);
|
2020-05-19 17:07:00 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
make$0::<()>();
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_non_generic() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(add_turbo_fish_non_generic);
|
2020-05-19 17:07:00 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make() -> () {}
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
make$0();
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|
2021-03-02 08:28:53 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_type_ascription_function() {
|
|
|
|
check_assist_by_label(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x = make$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x: ${0:_} = make();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"Add `: _` before assignment operator",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_type_ascription_after_call() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(add_type_ascription_after_call);
|
2021-03-02 08:28:53 -06:00
|
|
|
check_assist_by_label(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x = make()$0;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x: ${0:_} = make();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"Add `: _` before assignment operator",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_type_ascription_method() {
|
|
|
|
check_assist_by_label(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn make<T>(&self) -> T {}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x = S.make$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn make<T>(&self) -> T {}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x: ${0:_} = S.make();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"Add `: _` before assignment operator",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_type_ascription_already_typed() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(add_type_ascription_already_typed);
|
2021-03-02 08:28:53 -06:00
|
|
|
check_assist(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x: () = make$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x: () = make::<${0:_}>();
|
|
|
|
}
|
2020-05-19 17:07:00 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2021-03-11 04:36:45 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_type_ascription_append_semicolon() {
|
|
|
|
check_assist_by_label(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x = make$0()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T>() -> T {}
|
|
|
|
fn main() {
|
|
|
|
let x: ${0:_} = make();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"Add `: _` before assignment operator",
|
|
|
|
);
|
|
|
|
}
|
2022-01-06 19:02:16 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_function_lifetime_parameter() {
|
|
|
|
check_assist(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<'a, T, A>(t: T, a: A) {}
|
|
|
|
fn main() {
|
|
|
|
make$0(5, 2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<'a, T, A>(t: T, a: A) {}
|
|
|
|
fn main() {
|
2022-01-07 07:53:42 -06:00
|
|
|
make::<${1:_}, ${0:_}>(5, 2);
|
2022-01-06 19:02:16 -06:00
|
|
|
}
|
2022-01-06 19:09:32 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_turbo_fish_function_const_parameter() {
|
|
|
|
check_assist(
|
|
|
|
add_turbo_fish,
|
|
|
|
r#"
|
|
|
|
fn make<T, const N: usize>(t: T) {}
|
|
|
|
fn main() {
|
|
|
|
make$0(3);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn make<T, const N: usize>(t: T) {}
|
|
|
|
fn main() {
|
2022-01-07 07:53:42 -06:00
|
|
|
make::<${1:_}, ${0:_}>(3);
|
2022-01-06 19:09:32 -06:00
|
|
|
}
|
2022-01-06 19:02:16 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|