This commit is contained in:
Aleksey Kladov 2021-05-09 17:58:03 +03:00
parent edeb492782
commit d9c9f6dc2c
4 changed files with 25 additions and 17 deletions

View file

@ -139,12 +139,12 @@ fn generate_unique_lifetime_param_name(
fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) {
let generic_param =
make::generic_param(format!("'{}", new_lifetime_param), None).clone_for_update();
make::generic_param(&format!("'{}", new_lifetime_param), None).clone_for_update();
type_params.add_generic_param(generic_param);
}
fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime {
make::generic_param(format!("'{}", new_lifetime_param), None)
make::generic_param(&format!("'{}", new_lifetime_param), None)
.syntax()
.descendants()
.find_map(ast::Lifetime::cast)

View file

@ -1,6 +1,6 @@
use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner};
use crate::{AssistContext, AssistId, AssistKind, Assists};
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
// Assist: replace_impl_trait_with_generic
//
@ -17,30 +17,30 @@ pub(crate) fn replace_impl_trait_with_generic(
acc: &mut Assists,
ctx: &AssistContext,
) -> Option<()> {
let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?;
let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?;
let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?;
let impl_trait_ty = type_impl_trait.type_bound_list()?;
let type_bound_list = impl_trait_type.type_bound_list()?;
let target = type_fn.syntax().text_range();
let target = fn_.syntax().text_range();
acc.add(
AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite),
"Replace impl trait with generic",
target,
|edit| {
let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string();
let type_param_name = suggest_name::generic_parameter(&impl_trait_type);
let generic_param_list = type_fn
let generic_param_list = fn_
.generic_param_list()
.unwrap_or_else(|| make::generic_param_list(None))
.append_param(make::generic_param(generic_letter.clone(), Some(impl_trait_ty)));
.append_param(make::generic_param(&type_param_name, Some(type_bound_list)));
let new_type_fn = type_fn
.replace_descendant::<ast::Type>(type_impl_trait.into(), make::ty(&generic_letter))
let new_type_fn = fn_
.replace_descendant::<ast::Type>(impl_trait_type.into(), make::ty(&type_param_name))
.with_generic_param_list(generic_param_list);
edit.replace_ast(type_fn.clone(), new_type_fn);
edit.replace_ast(fn_.clone(), new_type_fn);
},
)
}

View file

@ -6,7 +6,7 @@ use itertools::Itertools;
use stdx::to_lower_snake_case;
use syntax::{
ast::{self, NameOwner},
match_ast, AstNode,
match_ast, AstNode, SmolStr,
};
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[
"iter_mut",
];
pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr {
let c = ty
.type_bound_list()
.and_then(|bounds| bounds.syntax().text().char_at(0.into()))
.unwrap_or('T');
c.encode_utf8(&mut [0; 4]).into()
}
/// Suggest name of variable for given expression
///
/// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name.

View file

@ -475,8 +475,8 @@ pub fn param_list(
};
ast_from_text(&list)
}
pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
// FIXME: s/&str/ast:Name
pub fn generic_param(name: &str, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
let bound = match ty {
Some(it) => format!(": {}", it),
None => String::new(),