fix: implement syntax_editor_create_generic_param_list

Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
Tarek 2024-11-12 19:18:26 +02:00
parent 642d4f3385
commit 0990d5956d
2 changed files with 35 additions and 5 deletions

View file

@ -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()

View file

@ -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());