2020-10-15 10:27:50 -05:00
|
|
|
use ide_db::defs::{Definition, NameRefClass};
|
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,
|
2020-08-08 13:14:18 -05:00
|
|
|
NameRefClass::ExternCrate(_) | NameRefClass::FieldShorthand { .. } => return None,
|
2020-05-19 17:07:00 -05:00
|
|
|
};
|
|
|
|
let fun = match def {
|
|
|
|
Definition::ModuleDef(hir::ModuleDef::Function(it)) => it,
|
|
|
|
_ => 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-05-19 17:07:00 -05:00
|
|
|
Some(cap) => builder.insert_snippet(cap, ident.text_range().end(), "::<${0:_}>"),
|
|
|
|
None => builder.insert(ident.text_range().end(), "::<_>"),
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
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:_}>();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
2020-05-19 17:07:00 -05:00
|
|
|
}
|