fix: refactor syntax_editor_add_generic_param

Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
Tarek 2024-12-03 23:47:52 +02:00
parent 54f7ed13f5
commit 70ef2f23c2
2 changed files with 41 additions and 19 deletions

View file

@ -59,14 +59,11 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
let new_ty = make.ty(&type_param_name); let new_ty = make.ty(&type_param_name);
editor.replace(impl_trait_type.syntax(), new_ty.syntax()); editor.replace(impl_trait_type.syntax(), new_ty.syntax());
fn_.syntax_editor_add_generic_param(&mut editor, type_param.into()); let generic_param = syntax::ast::GenericParam::from(type_param);
fn_.syntax_editor_add_generic_param(&mut editor, generic_param.clone());
if let Some(cap) = ctx.config.snippet_cap { if let Some(cap) = ctx.config.snippet_cap {
if let Some(generic_param) = editor.add_annotation(generic_param.syntax(), edit.make_tabstop_before(cap));
fn_.generic_param_list().and_then(|it| it.generic_params().last())
{
editor.add_annotation(generic_param.syntax(), edit.make_tabstop_before(cap));
}
} }
editor.add_mappings(make.finish_with_mappings()); editor.add_mappings(make.finish_with_mappings());

View file

@ -56,6 +56,7 @@ impl GenericParamsOwnerEdit for ast::Fn {
} }
impl ast::Fn { impl ast::Fn {
/// Adds a new generic param to the function using `SyntaxEditor`
pub fn syntax_editor_add_generic_param( pub fn syntax_editor_add_generic_param(
&self, &self,
editor: &mut SyntaxEditor, editor: &mut SyntaxEditor,
@ -65,23 +66,44 @@ impl ast::Fn {
Some(generic_param_list) => match generic_param_list.generic_params().last() { Some(generic_param_list) => match generic_param_list.generic_params().last() {
Some(_last_param) => { Some(_last_param) => {
// There exists a generic param list and it's not empty // There exists a generic param list and it's not empty
let mut params = generic_param_list let position = generic_param_list.r_angle_token().map_or_else(
.generic_params() || crate::syntax_editor::Position::last_child_of(self.syntax()),
.map(|param| param.clone()) crate::syntax_editor::Position::before,
.collect::<Vec<_>>();
params.push(new_param.into());
let new_param_list = make::generic_param_list(params);
editor.replace(
generic_param_list.syntax(),
new_param_list.syntax().clone_for_update(),
); );
if let Some(last_param) = generic_param_list.generic_params().last() {
if last_param
.syntax()
.next_sibling_or_token()
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
{
editor.insert(
crate::syntax_editor::Position::after(last_param.syntax()),
new_param.syntax().clone(),
);
editor.insert(
crate::syntax_editor::Position::after(last_param.syntax()),
make::token(SyntaxKind::WHITESPACE),
);
editor.insert(
crate::syntax_editor::Position::after(last_param.syntax()),
make::token(SyntaxKind::COMMA),
);
} else {
let elements = vec![
make::token(SyntaxKind::COMMA).into(),
make::token(SyntaxKind::WHITESPACE).into(),
new_param.syntax().clone().into(),
];
editor.insert_all(position, elements);
}
};
} }
None => { None => {
// There exists a generic param list but it's empty // There exists a generic param list but it's empty
let position = crate::syntax_editor::Position::after( let position = crate::syntax_editor::Position::after(
generic_param_list.l_angle_token().unwrap(), generic_param_list.l_angle_token().unwrap(),
); );
editor.insert(position, new_param.syntax()); editor.insert(position, new_param.syntax());
} }
}, },
@ -96,9 +118,12 @@ impl ast::Fn {
} else { } else {
crate::syntax_editor::Position::last_child_of(self.syntax()) crate::syntax_editor::Position::last_child_of(self.syntax())
}; };
let elements = vec![
let new_param_list = make::generic_param_list(once(new_param.clone())); make::token(SyntaxKind::L_ANGLE).into(),
editor.insert(position, new_param_list.syntax().clone_for_update()); new_param.syntax().clone().into(),
make::token(T![>]).into(),
];
editor.insert_all(position, elements);
} }
} }
} }