Skip to content

Commit 95f5966

Browse files
committed
Fix removal of generic param from list
This removes an existing generic param from the `GenericParamList`. It also considers to remove the extra colon & whitespace to the previous sibling. * change order to get all param types first and mark them as mutable before the first edit happens * add helper function to remove a generic parameter * fix test output
1 parent 59f8827 commit 95f5966

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

crates/ide-assists/src/handlers/replace_named_generic_with_impl.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use syntax::{
2-
ast::{
3-
self,
4-
make::{self, impl_trait_type},
5-
HasGenericParams, HasName, HasTypeBounds,
6-
},
2+
ast::{self, make::impl_trait_type, HasGenericParams, HasName, HasTypeBounds},
73
ted, AstNode,
84
};
95

@@ -27,7 +23,7 @@ pub(crate) fn replace_named_generic_with_impl(
2723
// finds `<P: AsRef<Path>>`
2824
let type_param = ctx.find_node_at_offset::<ast::TypeParam>()?;
2925

30-
// The list of type bounds / traits for generic name `P`
26+
// The list of type bounds / traits: `AsRef<Path>`
3127
let type_bound_list = type_param.type_bound_list()?;
3228

3329
// returns `P`
@@ -67,24 +63,26 @@ pub(crate) fn replace_named_generic_with_impl(
6763
let type_param = edit.make_mut(type_param);
6864
let fn_ = edit.make_mut(fn_);
6965

70-
// Replace generic type in `<P: AsRef<Path>>` to `<P>`
71-
let new_ty = make::ty(&type_param_name.to_string()).clone_for_update();
72-
ted::replace(type_param.syntax(), new_ty.syntax());
66+
// get all params
67+
let param_types = params
68+
.iter()
69+
.filter_map(|param| match param.ty() {
70+
Some(ast::Type::PathType(param_type)) => Some(edit.make_mut(param_type)),
71+
_ => None,
72+
})
73+
.collect::<Vec<_>>();
7374

7475
if let Some(generic_params) = fn_.generic_param_list() {
76+
generic_params.remove_generic_param(ast::GenericParam::TypeParam(type_param));
7577
if generic_params.generic_params().count() == 0 {
7678
ted::remove(generic_params.syntax());
7779
}
7880
}
7981

80-
// Replace generic type parameter: `foo(p: P)` -> `foo(p: impl AsRef<Path>)`
81-
let new_bounds = impl_trait_type(type_bound_list).clone_for_update();
82-
83-
for param in params {
84-
if let Some(ast::Type::PathType(param_type)) = param.ty() {
85-
let param_type = edit.make_mut(param_type).clone_for_update();
86-
ted::replace(param_type.syntax(), new_bounds.syntax());
87-
}
82+
// get type bounds in signature type: `P` -> `impl AsRef<Path>`
83+
let new_bounds = impl_trait_type(type_bound_list);
84+
for param_type in param_types.iter().rev() {
85+
ted::replace(param_type.syntax(), new_bounds.clone_for_update().syntax());
8886
}
8987
},
9088
)

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ impl ast::GenericParamList {
236236
}
237237
}
238238

239+
/// Removes the existing generic param
240+
pub fn remove_generic_param(&self, generic_param: ast::GenericParam) {
241+
if let Some(previous) = generic_param.syntax().prev_sibling() {
242+
if let Some(next_token) = previous.next_sibling_or_token() {
243+
ted::remove_all(next_token..=generic_param.syntax().clone().into());
244+
}
245+
} else if let Some(next) = generic_param.syntax().next_sibling() {
246+
if let Some(next_token) = next.prev_sibling_or_token() {
247+
ted::remove_all(generic_param.syntax().clone().into()..=next_token);
248+
}
249+
} else {
250+
ted::remove(generic_param.syntax());
251+
}
252+
}
253+
239254
/// Constructs a matching [`ast::GenericArgList`]
240255
pub fn to_generic_args(&self) -> ast::GenericArgList {
241256
let args = self.generic_params().filter_map(|param| match param {

0 commit comments

Comments
 (0)