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
83 matches!(param, hir::GenericParam::TypeParam(_) | hir::GenericParam::ConstParam(_))
88 AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
91 |builder| match ctx.config.snippet_cap {
93 let snip = format!("::<{}>", get_snippet_fish_head(number_of_arguments));
94 builder.insert_snippet(cap, ident.text_range().end(), snip)
98 std::iter::repeat("_").take(number_of_arguments).collect::<Vec<_>>().join(", ");
99 let snip = format!("::<{}>", fish_head);
100 builder.insert(ident.text_range().end(), snip);
106 /// This will create a snippet string with tabstops marked
107 fn get_snippet_fish_head(number_of_arguments: usize) -> String {
108 let mut fish_head = String::new();
110 while i < number_of_arguments {
111 fish_head.push_str(&format!("${{{}:_}},", i));
115 // tabstop 0 is a special case and always the last one
116 fish_head.push_str("${0:_}");
122 use crate::tests::{check_assist, check_assist_by_label, check_assist_not_applicable};
127 fn add_turbo_fish_function() {
146 fn add_turbo_fish_function_multiple_generic_types() {
150 fn make<T, A>() -> T {}
156 fn make<T, A>() -> T {}
158 make::<${1:_},${0:_}>();
165 fn add_turbo_fish_function_many_generic_types() {
169 fn make<T, A, B, C, D, E, F>() -> T {}
175 fn make<T, A, B, C, D, E, F>() -> T {}
177 make::<${1:_},${2:_},${3:_},${4:_},${5:_},${6:_},${0:_}>();
184 fn add_turbo_fish_after_call() {
185 cov_mark::check!(add_turbo_fish_after_call);
204 fn add_turbo_fish_method() {
210 fn make<T>(&self) -> T {}
219 fn make<T>(&self) -> T {}
229 fn add_turbo_fish_one_fish_is_enough() {
230 cov_mark::check!(add_turbo_fish_one_fish_is_enough);
231 check_assist_not_applicable(
243 fn add_turbo_fish_non_generic() {
244 cov_mark::check!(add_turbo_fish_non_generic);
245 check_assist_not_applicable(
257 fn add_type_ascription_function() {
258 check_assist_by_label(
269 let x: ${0:_} = make();
272 "Add `: _` before assignment operator",
277 fn add_type_ascription_after_call() {
278 cov_mark::check!(add_type_ascription_after_call);
279 check_assist_by_label(
290 let x: ${0:_} = make();
293 "Add `: _` before assignment operator",
298 fn add_type_ascription_method() {
299 check_assist_by_label(
304 fn make<T>(&self) -> T {}
313 fn make<T>(&self) -> T {}
316 let x: ${0:_} = S.make();
319 "Add `: _` before assignment operator",
324 fn add_type_ascription_already_typed() {
325 cov_mark::check!(add_type_ascription_already_typed);
331 let x: () = make$0();
337 let x: () = make::<${0:_}>();
344 fn add_type_ascription_append_semicolon() {
345 check_assist_by_label(
356 let x: ${0:_} = make();
359 "Add `: _` before assignment operator",
364 fn add_turbo_fish_function_lifetime_parameter() {
368 fn make<'a, T, A>(t: T, a: A) {}
374 fn make<'a, T, A>(t: T, a: A) {}
376 make::<${1:_},${0:_}>(5, 2);
383 fn add_turbo_fish_function_const_parameter() {
387 fn make<T, const N: usize>(t: T) {}
393 fn make<T, const N: usize>(t: T) {}
395 make::<${1:_},${0:_}>(3);