Migrate add_turbo_fish to mutable ast

`add_type_ascription` is still left as-is since it's a different assist
This commit is contained in:
DropDemBits 2023-07-11 00:02:46 -04:00
parent 02c7b8b9ba
commit cc4e06f04b
No known key found for this signature in database
GPG key ID: 7FE02A6C1EDFA075

View file

@ -1,6 +1,9 @@
use either::Either;
use ide_db::defs::{Definition, NameRefClass}; use ide_db::defs::{Definition, NameRefClass};
use itertools::Itertools; use syntax::{
use syntax::{ast, AstNode, SyntaxKind, T}; ast::{self, make, HasArgList},
ted, AstNode,
};
use crate::{ use crate::{
assist_context::{AssistContext, Assists}, assist_context::{AssistContext, Assists},
@ -25,21 +28,45 @@ use crate::{
// } // }
// ``` // ```
pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let ident = ctx.find_token_syntax_at_offset(SyntaxKind::IDENT).or_else(|| { let turbofish_target =
let arg_list = ctx.find_node_at_offset::<ast::ArgList>()?; ctx.find_node_at_offset::<ast::PathSegment>().map(Either::Left).or_else(|| {
if arg_list.args().next().is_some() { let callable_expr = ctx.find_node_at_offset::<ast::CallableExpr>()?;
if callable_expr.arg_list()?.args().next().is_some() {
return None; return None;
} }
cov_mark::hit!(add_turbo_fish_after_call); cov_mark::hit!(add_turbo_fish_after_call);
cov_mark::hit!(add_type_ascription_after_call); cov_mark::hit!(add_type_ascription_after_call);
arg_list.l_paren_token()?.prev_token().filter(|it| it.kind() == SyntaxKind::IDENT)
match callable_expr {
ast::CallableExpr::Call(it) => {
let ast::Expr::PathExpr(path) = it.expr()? else {
return None;
};
Some(Either::Left(path.path()?.segment()?))
}
ast::CallableExpr::MethodCall(it) => Some(Either::Right(it)),
}
})?; })?;
let next_token = ident.next_token()?;
if next_token.kind() == T![::] { let already_has_turbofish = match &turbofish_target {
Either::Left(path_segment) => path_segment.generic_arg_list().is_some(),
Either::Right(method_call) => method_call.generic_arg_list().is_some(),
};
if already_has_turbofish {
cov_mark::hit!(add_turbo_fish_one_fish_is_enough); cov_mark::hit!(add_turbo_fish_one_fish_is_enough);
return None; return None;
} }
let name_ref = ast::NameRef::cast(ident.parent()?)?;
let name_ref = match &turbofish_target {
Either::Left(path_segment) => path_segment.name_ref()?,
Either::Right(method_call) => method_call.name_ref()?,
};
let ident = name_ref.ident_token()?;
let def = match NameRefClass::classify(&ctx.sema, &name_ref)? { let def = match NameRefClass::classify(&ctx.sema, &name_ref)? {
NameRefClass::Definition(def) => def, NameRefClass::Definition(def) => def,
NameRefClass::FieldShorthand { .. } | NameRefClass::ExternCrateShorthand { .. } => { NameRefClass::FieldShorthand { .. } | NameRefClass::ExternCrateShorthand { .. } => {
@ -91,33 +118,38 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
AssistId("add_turbo_fish", AssistKind::RefactorRewrite), AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
"Add `::<>`", "Add `::<>`",
ident.text_range(), ident.text_range(),
|builder| { |edit| {
builder.trigger_signature_help(); edit.trigger_signature_help();
match ctx.config.snippet_cap {
Some(cap) => { let new_arg_list = match turbofish_target {
let fish_head = get_snippet_fish_head(number_of_arguments); Either::Left(path_segment) => {
let snip = format!("::<{fish_head}>"); edit.make_mut(path_segment).get_or_create_generic_arg_list()
builder.insert_snippet(cap, ident.text_range().end(), snip)
} }
None => { Either::Right(method_call) => {
let fish_head = std::iter::repeat("_").take(number_of_arguments).format(", "); edit.make_mut(method_call).get_or_create_generic_arg_list()
let snip = format!("::<{fish_head}>"); }
builder.insert(ident.text_range().end(), snip); };
let fish_head = get_fish_head(number_of_arguments).clone_for_update();
// Note: we need to replace the `new_arg_list` instead of being able to use something like
// `GenericArgList::add_generic_arg` as `PathSegment::get_or_create_generic_arg_list`
// always creates a non-turbofish form generic arg list.
ted::replace(new_arg_list.syntax(), fish_head.syntax());
if let Some(cap) = ctx.config.snippet_cap {
for arg in fish_head.generic_args() {
edit.add_placeholder_snippet(cap, arg)
} }
} }
}, },
) )
} }
/// This will create a snippet string with tabstops marked /// This will create a turbofish generic arg list corresponding to the number of arguments
fn get_snippet_fish_head(number_of_arguments: usize) -> String { fn get_fish_head(number_of_arguments: usize) -> ast::GenericArgList {
let mut fish_head = (1..number_of_arguments) let args = (0..number_of_arguments).map(|_| make::type_arg(make::ty_placeholder()).into());
.format_with("", |i, f| f(&format_args!("${{{i}:_}}, "))) make::turbofish_generic_arg_list(args)
.to_string();
// tabstop 0 is a special case and always the last one
fish_head.push_str("${0:_}");
fish_head
} }
#[cfg(test)] #[cfg(test)]