mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-10 02:12:40 +00:00
cleanups
This commit is contained in:
parent
edeb492782
commit
d9c9f6dc2c
4 changed files with 25 additions and 17 deletions
|
@ -139,12 +139,12 @@ fn generate_unique_lifetime_param_name(
|
||||||
|
|
||||||
fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) {
|
fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) {
|
||||||
let generic_param =
|
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);
|
type_params.add_generic_param(generic_param);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime {
|
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()
|
.syntax()
|
||||||
.descendants()
|
.descendants()
|
||||||
.find_map(ast::Lifetime::cast)
|
.find_map(ast::Lifetime::cast)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner};
|
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
|
// Assist: replace_impl_trait_with_generic
|
||||||
//
|
//
|
||||||
|
@ -17,30 +17,30 @@ pub(crate) fn replace_impl_trait_with_generic(
|
||||||
acc: &mut Assists,
|
acc: &mut Assists,
|
||||||
ctx: &AssistContext,
|
ctx: &AssistContext,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
|
let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
|
||||||
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
|
let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?;
|
||||||
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::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(
|
acc.add(
|
||||||
AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite),
|
AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite),
|
||||||
"Replace impl trait with generic",
|
"Replace impl trait with generic",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|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()
|
.generic_param_list()
|
||||||
.unwrap_or_else(|| make::generic_param_list(None))
|
.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
|
let new_type_fn = fn_
|
||||||
.replace_descendant::<ast::Type>(type_impl_trait.into(), make::ty(&generic_letter))
|
.replace_descendant::<ast::Type>(impl_trait_type.into(), make::ty(&type_param_name))
|
||||||
.with_generic_param_list(generic_param_list);
|
.with_generic_param_list(generic_param_list);
|
||||||
|
|
||||||
edit.replace_ast(type_fn.clone(), new_type_fn);
|
edit.replace_ast(fn_.clone(), new_type_fn);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use itertools::Itertools;
|
||||||
use stdx::to_lower_snake_case;
|
use stdx::to_lower_snake_case;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, NameOwner},
|
ast::{self, NameOwner},
|
||||||
match_ast, AstNode,
|
match_ast, AstNode, SmolStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
|
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
|
||||||
|
@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[
|
||||||
"iter_mut",
|
"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
|
/// Suggest name of variable for given expression
|
||||||
///
|
///
|
||||||
/// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name.
|
/// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name.
|
||||||
|
|
|
@ -475,8 +475,8 @@ pub fn param_list(
|
||||||
};
|
};
|
||||||
ast_from_text(&list)
|
ast_from_text(&list)
|
||||||
}
|
}
|
||||||
|
// FIXME: s/&str/ast:Name
|
||||||
pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
|
pub fn generic_param(name: &str, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
|
||||||
let bound = match ty {
|
let bound = match ty {
|
||||||
Some(it) => format!(": {}", it),
|
Some(it) => format!(": {}", it),
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue