Skip to content

Commit 0990d59

Browse files
committed
fix: implement syntax_editor_create_generic_param_list
Signed-off-by: Tarek <tareknaser360@gmail.com>
1 parent 642d4f3 commit 0990d59

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use ide_db::syntax_helpers::suggest_name;
22
use itertools::Itertools;
33
use syntax::{
4-
ast::{
5-
self, edit_in_place::GenericParamsOwnerEdit, syntax_factory::SyntaxFactory, AstNode,
6-
HasGenericParams, HasName,
7-
},
4+
ast::{self, syntax_factory::SyntaxFactory, AstNode, HasGenericParams, HasName},
85
SyntaxElement,
96
};
107

@@ -42,7 +39,8 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
4239
target,
4340
|edit| {
4441
let mut editor = edit.make_editor(&parent_node);
45-
let fn_generic_param_list = fn_.get_or_create_generic_param_list();
42+
let fn_generic_param_list =
43+
fn_.syntax_editor_get_or_create_generic_param_list(&mut editor);
4644

4745
let existing_names = fn_generic_param_list
4846
.generic_params()

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ impl GenericParamsOwnerEdit for ast::Fn {
5555
}
5656
}
5757

58+
impl ast::Fn {
59+
pub fn syntax_editor_get_or_create_generic_param_list(
60+
&self,
61+
editor: &mut SyntaxEditor,
62+
) -> ast::GenericParamList {
63+
match self.generic_param_list() {
64+
Some(it) => it,
65+
None => {
66+
let position = if let Some(name) = self.name() {
67+
crate::syntax_editor::Position::after(name.syntax)
68+
} else if let Some(fn_token) = self.fn_token() {
69+
crate::syntax_editor::Position::after(fn_token)
70+
} else if let Some(param_list) = self.param_list() {
71+
crate::syntax_editor::Position::before(param_list.syntax)
72+
} else {
73+
crate::syntax_editor::Position::last_child_of(self.syntax())
74+
};
75+
syntax_editor_create_generic_param_list(editor, position)
76+
}
77+
}
78+
}
79+
}
80+
5881
impl GenericParamsOwnerEdit for ast::Impl {
5982
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
6083
match self.generic_param_list() {
@@ -191,6 +214,15 @@ fn create_generic_param_list(position: Position) -> ast::GenericParamList {
191214
gpl
192215
}
193216

217+
fn syntax_editor_create_generic_param_list(
218+
editor: &mut SyntaxEditor,
219+
position: crate::syntax_editor::Position,
220+
) -> ast::GenericParamList {
221+
let gpl = make::generic_param_list(empty()).clone_for_update();
222+
editor.insert(position, gpl.syntax());
223+
gpl
224+
}
225+
194226
pub trait AttrsOwnerEdit: ast::HasAttrs {
195227
fn remove_attrs_and_docs(&self) {
196228
remove_attrs_and_docs(self.syntax());

0 commit comments

Comments
 (0)