1 use ide_db::defs::{Definition, NameRefClass};
2 use syntax::{ast, AstNode, SyntaxKind, T};
5 assist_context::{AssistContext, Assists},
9 // Assist: add_turbo_fish
11 // Adds `::<_>` to a call of a generic method or function.
14 // fn make<T>() -> T { todo!() }
21 // fn make<T>() -> T { todo!() }
23 // let x = make::<${0:_}>();
26 pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
27 let ident = ctx.find_token_syntax_at_offset(SyntaxKind::IDENT).or_else(|| {
28 let arg_list = ctx.find_node_at_offset::<ast::ArgList>()?;
29 if arg_list.args().next().is_some() {
32 cov_mark::hit!(add_turbo_fish_after_call);
33 cov_mark::hit!(add_type_ascription_after_call);
34 arg_list.l_paren_token()?.prev_token().filter(|it| it.kind() == SyntaxKind::IDENT)
36 let next_token = ident.next_token()?;
37 if next_token.kind() == T![::] {
38 cov_mark::hit!(add_turbo_fish_one_fish_is_enough);
41 let name_ref = ast::NameRef::cast(ident.parent()?)?;
42 let def = match NameRefClass::classify(&ctx.sema, &name_ref)? {
43 NameRefClass::Definition(def) => def,
44 NameRefClass::FieldShorthand { .. } => return None,
47 Definition::Function(it) => it,
50 let generics = hir::GenericDef::Function(fun).params(ctx.sema.db);
51 if generics.is_empty() {
52 cov_mark::hit!(add_turbo_fish_non_generic);
56 if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
57 if let_stmt.colon_token().is_none() {
58 let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end();
59 let semi_pos = let_stmt.syntax().last_token()?.text_range().end();
62 AssistId("add_type_ascription", AssistKind::RefactorRewrite),
63 "Add `: _` before assignment operator",
66 if let_stmt.semicolon_token().is_none() {
67 builder.insert(semi_pos, ";");
69 match ctx.config.snippet_cap {
70 Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"),
71 None => builder.insert(type_pos, ": _"),
76 cov_mark::hit!(add_type_ascription_already_typed);
80 let number_of_arguments = generics
82 .filter(|param| matches!(param, hir::GenericParam::TypeParam(_) | hir::GenericParam::ConstParam(_)))
84 let fish_head = std::iter::repeat("_").take(number_of_arguments).collect::<Vec<_>>().join(",");
87 AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
90 |builder| match ctx.config.snippet_cap {
92 let snip = format!("::<${{0:{}}}>", fish_head);
93 builder.insert_snippet(cap, ident.text_range().end(), snip)
96 let snip = format!("::<{}>", fish_head);
97 builder.insert(ident.text_range().end(), snip);
105 use crate::tests::{check_assist, check_assist_by_label, check_assist_not_applicable};
110 fn add_turbo_fish_function() {
129 fn add_turbo_fish_function_multiple_generic_types() {
133 fn make<T, A>() -> T {}
139 fn make<T, A>() -> T {}
148 fn add_turbo_fish_function_many_generic_types() {
152 fn make<T, A, B, C, D, E, F>() -> T {}
158 fn make<T, A, B, C, D, E, F>() -> T {}
160 make::<${0:_,_,_,_,_,_,_}>();
167 fn add_turbo_fish_after_call() {
168 cov_mark::check!(add_turbo_fish_after_call);
187 fn add_turbo_fish_method() {
193 fn make<T>(&self) -> T {}
202 fn make<T>(&self) -> T {}
212 fn add_turbo_fish_one_fish_is_enough() {
213 cov_mark::check!(add_turbo_fish_one_fish_is_enough);
214 check_assist_not_applicable(
226 fn add_turbo_fish_non_generic() {
227 cov_mark::check!(add_turbo_fish_non_generic);
228 check_assist_not_applicable(
240 fn add_type_ascription_function() {
241 check_assist_by_label(
252 let x: ${0:_} = make();
255 "Add `: _` before assignment operator",
260 fn add_type_ascription_after_call() {
261 cov_mark::check!(add_type_ascription_after_call);
262 check_assist_by_label(
273 let x: ${0:_} = make();
276 "Add `: _` before assignment operator",
281 fn add_type_ascription_method() {
282 check_assist_by_label(
287 fn make<T>(&self) -> T {}
296 fn make<T>(&self) -> T {}
299 let x: ${0:_} = S.make();
302 "Add `: _` before assignment operator",
307 fn add_type_ascription_already_typed() {
308 cov_mark::check!(add_type_ascription_already_typed);
314 let x: () = make$0();
320 let x: () = make::<${0:_}>();
327 fn add_type_ascription_append_semicolon() {
328 check_assist_by_label(
339 let x: ${0:_} = make();
342 "Add `: _` before assignment operator",
347 fn add_turbo_fish_function_lifetime_parameter() {
351 fn make<'a, T, A>(t: T, a: A) {}
357 fn make<'a, T, A>(t: T, a: A) {}
359 make::<${0:_,_}>(5, 2);
366 fn add_turbo_fish_function_const_parameter() {
370 fn make<T, const N: usize>(t: T) {}
376 fn make<T, const N: usize>(t: T) {}