rust/crates/ide_assists/src/handlers/add_turbo_fish.rs

165 lines
3.5 KiB
Rust
Raw Normal View History

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:19:59 -05:00
use test_utils::mark;
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<()> {
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>()?;
if arg_list.args().count() > 0 {
return None;
}
mark::hit!(add_turbo_fish_after_call);
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![::] {
2020-05-19 17:19:59 -05:00
mark::hit!(add_turbo_fish_one_fish_is_enough);
2020-05-19 17:07:00 -05:00
return None;
}
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,
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() {
2020-05-19 17:19:59 -05:00
mark::hit!(add_turbo_fish_non_generic);
2020-05-19 17:07:00 -05:00
return None;
}
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 {
use crate::tests::{check_assist, check_assist_not_applicable};
use super::*;
2020-05-19 17:19:59 -05:00
use test_utils::mark;
2020-05-19 17:07:00 -05:00
#[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() {
mark::check!(add_turbo_fish_after_call);
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() {
2020-05-19 17:19:59 -05:00
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() {
2020-05-19 17:19:59 -05:00
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
}
"#,
);
}
}