Skip to content

Commit 69b9660

Browse files
committed
fix: refactor syntax_editor_add_generic_param
Signed-off-by: Tarek <[email protected]>
1 parent 776b2d7 commit 69b9660

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,11 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
5959
let new_ty = make.ty(&type_param_name);
6060

6161
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
62-
fn_.syntax_editor_add_generic_param(&mut editor, type_param.into());
62+
let generic_param = syntax::ast::GenericParam::from(type_param);
63+
fn_.syntax_editor_add_generic_param(&mut editor, generic_param.clone());
6364

6465
if let Some(cap) = ctx.config.snippet_cap {
65-
if let Some(generic_param) =
66-
fn_.generic_param_list().and_then(|it| it.generic_params().last())
67-
{
68-
editor.add_annotation(generic_param.syntax(), edit.make_tabstop_before(cap));
69-
}
66+
editor.add_annotation(generic_param.syntax(), edit.make_tabstop_before(cap));
7067
}
7168

7269
editor.add_mappings(make.finish_with_mappings());

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl GenericParamsOwnerEdit for ast::Fn {
5656
}
5757

5858
impl ast::Fn {
59+
/// Adds a new generic param to the function using `SyntaxEditor`
5960
pub fn syntax_editor_add_generic_param(
6061
&self,
6162
editor: &mut SyntaxEditor,
@@ -65,23 +66,44 @@ impl ast::Fn {
6566
Some(generic_param_list) => match generic_param_list.generic_params().last() {
6667
Some(_last_param) => {
6768
// There exists a generic param list and it's not empty
68-
let mut params = generic_param_list
69-
.generic_params()
70-
.map(|param| param.clone())
71-
.collect::<Vec<_>>();
72-
params.push(new_param.into());
73-
let new_param_list = make::generic_param_list(params);
74-
editor.replace(
75-
generic_param_list.syntax(),
76-
new_param_list.syntax().clone_for_update(),
69+
let position = generic_param_list.r_angle_token().map_or_else(
70+
|| crate::syntax_editor::Position::last_child_of(self.syntax()),
71+
crate::syntax_editor::Position::before,
7772
);
73+
74+
if let Some(last_param) = generic_param_list.generic_params().last() {
75+
if last_param
76+
.syntax()
77+
.next_sibling_or_token()
78+
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
79+
{
80+
editor.insert(
81+
crate::syntax_editor::Position::after(last_param.syntax()),
82+
new_param.syntax().clone(),
83+
);
84+
editor.insert(
85+
crate::syntax_editor::Position::after(last_param.syntax()),
86+
make::token(SyntaxKind::WHITESPACE),
87+
);
88+
editor.insert(
89+
crate::syntax_editor::Position::after(last_param.syntax()),
90+
make::token(SyntaxKind::COMMA),
91+
);
92+
} else {
93+
let elements = vec![
94+
make::token(SyntaxKind::COMMA).into(),
95+
make::token(SyntaxKind::WHITESPACE).into(),
96+
new_param.syntax().clone().into(),
97+
];
98+
editor.insert_all(position, elements);
99+
}
100+
};
78101
}
79102
None => {
80103
// There exists a generic param list but it's empty
81104
let position = crate::syntax_editor::Position::after(
82105
generic_param_list.l_angle_token().unwrap(),
83106
);
84-
85107
editor.insert(position, new_param.syntax());
86108
}
87109
},
@@ -96,9 +118,12 @@ impl ast::Fn {
96118
} else {
97119
crate::syntax_editor::Position::last_child_of(self.syntax())
98120
};
99-
100-
let new_param_list = make::generic_param_list(once(new_param.clone()));
101-
editor.insert(position, new_param_list.syntax().clone_for_update());
121+
let elements = vec![
122+
make::token(SyntaxKind::L_ANGLE).into(),
123+
new_param.syntax().clone().into(),
124+
make::token(T![>]).into(),
125+
];
126+
editor.insert_all(position, elements);
102127
}
103128
}
104129
}

0 commit comments

Comments
 (0)