Skip to content

Commit d9c9f6d

Browse files
committed
cleanups
1 parent edeb492 commit d9c9f6d

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

crates/ide_assists/src/handlers/introduce_named_lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ fn generate_unique_lifetime_param_name(
139139

140140
fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) {
141141
let generic_param =
142-
make::generic_param(format!("'{}", new_lifetime_param), None).clone_for_update();
142+
make::generic_param(&format!("'{}", new_lifetime_param), None).clone_for_update();
143143
type_params.add_generic_param(generic_param);
144144
}
145145

146146
fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime {
147-
make::generic_param(format!("'{}", new_lifetime_param), None)
147+
make::generic_param(&format!("'{}", new_lifetime_param), None)
148148
.syntax()
149149
.descendants()
150150
.find_map(ast::Lifetime::cast)

crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner};
22

3-
use crate::{AssistContext, AssistId, AssistKind, Assists};
3+
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
44

55
// Assist: replace_impl_trait_with_generic
66
//
@@ -17,30 +17,30 @@ pub(crate) fn replace_impl_trait_with_generic(
1717
acc: &mut Assists,
1818
ctx: &AssistContext,
1919
) -> Option<()> {
20-
let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
21-
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
22-
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?;
20+
let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
21+
let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?;
22+
let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?;
2323

24-
let impl_trait_ty = type_impl_trait.type_bound_list()?;
24+
let type_bound_list = impl_trait_type.type_bound_list()?;
2525

26-
let target = type_fn.syntax().text_range();
26+
let target = fn_.syntax().text_range();
2727
acc.add(
2828
AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite),
2929
"Replace impl trait with generic",
3030
target,
3131
|edit| {
32-
let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string();
32+
let type_param_name = suggest_name::generic_parameter(&impl_trait_type);
3333

34-
let generic_param_list = type_fn
34+
let generic_param_list = fn_
3535
.generic_param_list()
3636
.unwrap_or_else(|| make::generic_param_list(None))
37-
.append_param(make::generic_param(generic_letter.clone(), Some(impl_trait_ty)));
37+
.append_param(make::generic_param(&type_param_name, Some(type_bound_list)));
3838

39-
let new_type_fn = type_fn
40-
.replace_descendant::<ast::Type>(type_impl_trait.into(), make::ty(&generic_letter))
39+
let new_type_fn = fn_
40+
.replace_descendant::<ast::Type>(impl_trait_type.into(), make::ty(&type_param_name))
4141
.with_generic_param_list(generic_param_list);
4242

43-
edit.replace_ast(type_fn.clone(), new_type_fn);
43+
edit.replace_ast(fn_.clone(), new_type_fn);
4444
},
4545
)
4646
}

crates/ide_assists/src/utils/suggest_name.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use stdx::to_lower_snake_case;
77
use syntax::{
88
ast::{self, NameOwner},
9-
match_ast, AstNode,
9+
match_ast, AstNode, SmolStr,
1010
};
1111

1212
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
@@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[
5757
"iter_mut",
5858
];
5959

60+
pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr {
61+
let c = ty
62+
.type_bound_list()
63+
.and_then(|bounds| bounds.syntax().text().char_at(0.into()))
64+
.unwrap_or('T');
65+
c.encode_utf8(&mut [0; 4]).into()
66+
}
67+
6068
/// Suggest name of variable for given expression
6169
///
6270
/// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name.

crates/syntax/src/ast/make.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ pub fn param_list(
475475
};
476476
ast_from_text(&list)
477477
}
478-
479-
pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
478+
// FIXME: s/&str/ast:Name
479+
pub fn generic_param(name: &str, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
480480
let bound = match ty {
481481
Some(it) => format!(": {}", it),
482482
None => String::new(),

0 commit comments

Comments
 (0)