diff --git a/crates/ide-assists/src/handlers/introduce_named_generic.rs b/crates/ide-assists/src/handlers/introduce_named_generic.rs index ae30dacfd0..c6945d6245 100644 --- a/crates/ide-assists/src/handlers/introduce_named_generic.rs +++ b/crates/ide-assists/src/handlers/introduce_named_generic.rs @@ -1,10 +1,7 @@ use ide_db::syntax_helpers::suggest_name; use itertools::Itertools; use syntax::{ - ast::{ - self, edit_in_place::GenericParamsOwnerEdit, syntax_factory::SyntaxFactory, AstNode, - HasGenericParams, HasName, - }, + ast::{self, syntax_factory::SyntaxFactory, AstNode, HasGenericParams, HasName}, SyntaxElement, }; @@ -42,7 +39,8 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_> target, |edit| { let mut editor = edit.make_editor(&parent_node); - let fn_generic_param_list = fn_.get_or_create_generic_param_list(); + let fn_generic_param_list = + fn_.syntax_editor_get_or_create_generic_param_list(&mut editor); let existing_names = fn_generic_param_list .generic_params() diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 2b152a45c9..61580a5cae 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -55,6 +55,29 @@ impl GenericParamsOwnerEdit for ast::Fn { } } +impl ast::Fn { + pub fn syntax_editor_get_or_create_generic_param_list( + &self, + editor: &mut SyntaxEditor, + ) -> ast::GenericParamList { + match self.generic_param_list() { + Some(it) => it, + None => { + let position = if let Some(name) = self.name() { + crate::syntax_editor::Position::after(name.syntax) + } else if let Some(fn_token) = self.fn_token() { + crate::syntax_editor::Position::after(fn_token) + } else if let Some(param_list) = self.param_list() { + crate::syntax_editor::Position::before(param_list.syntax) + } else { + crate::syntax_editor::Position::last_child_of(self.syntax()) + }; + syntax_editor_create_generic_param_list(editor, position) + } + } + } +} + impl GenericParamsOwnerEdit for ast::Impl { fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { match self.generic_param_list() { @@ -191,6 +214,15 @@ fn create_generic_param_list(position: Position) -> ast::GenericParamList { gpl } +fn syntax_editor_create_generic_param_list( + editor: &mut SyntaxEditor, + position: crate::syntax_editor::Position, +) -> ast::GenericParamList { + let gpl = make::generic_param_list(empty()).clone_for_update(); + editor.insert(position, gpl.syntax()); + gpl +} + pub trait AttrsOwnerEdit: ast::HasAttrs { fn remove_attrs_and_docs(&self) { remove_attrs_and_docs(self.syntax());