fix: refactor syntax_editor_add_generic_param to handle new generic parameters

Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
Tarek 2024-11-22 16:26:14 +02:00
parent 0990d5956d
commit 98e10d76ca

View file

@ -293,22 +293,21 @@ impl ast::GenericParamList {
pub fn syntax_editor_add_generic_param( pub fn syntax_editor_add_generic_param(
&self, &self,
editor: &mut SyntaxEditor, editor: &mut SyntaxEditor,
generic_param: ast::GenericParam, new_param: ast::GenericParam,
) { ) {
match self.generic_params().last() { match self.generic_params().last() {
Some(last_param) => { Some(_) => {
let position = crate::syntax_editor::Position::after(last_param.syntax()); let mut params =
let elements = vec![ self.generic_params().map(|param| param.clone()).collect::<Vec<_>>();
make::token(T![,]).into(), params.push(new_param.into());
make::tokens::single_space().into(), let new_param_list = make::generic_param_list(params);
generic_param.syntax().clone().into(),
]; editor.replace(self.syntax(), new_param_list.syntax());
editor.insert_all(position, elements);
} }
None => { None => {
let after_l_angle = let position = crate::syntax_editor::Position::after(self.l_angle_token().unwrap());
crate::syntax_editor::Position::after(self.l_angle_token().unwrap()); let new_param_list = make::generic_param_list(once(new_param.clone()));
editor.insert(after_l_angle, generic_param.syntax()); editor.insert(position, new_param_list.syntax());
} }
} }
} }