Skip to content

Commit 642d4f3

Browse files
committed
define syntax_editor_add_generic_param
Signed-off-by: Tarek <tareknaser360@gmail.com>
1 parent 4af3d6f commit 642d4f3

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
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
@@ -56,13 +56,11 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
5656
)
5757
.for_impl_trait_as_generic(&impl_trait_type);
5858

59-
let type_param = make
60-
.type_param(make.name(&type_param_name), Some(type_bound_list))
61-
.clone_for_update();
62-
let new_ty = make.ty(&type_param_name).clone_for_update();
59+
let type_param = make.type_param(make.name(&type_param_name), Some(type_bound_list));
60+
let new_ty = make.ty(&type_param_name);
6361

6462
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
65-
fn_generic_param_list.add_generic_param(type_param.into());
63+
fn_generic_param_list.syntax_editor_add_generic_param(&mut editor, type_param.into());
6664

6765
if let Some(cap) = ctx.config.snippet_cap {
6866
if let Some(generic_param) =

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use parser::{SyntaxKind, T};
77
use crate::{
88
algo::{self, neighbor},
99
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
10+
syntax_editor::SyntaxEditor,
1011
ted::{self, Position},
1112
AstNode, AstToken, Direction, SyntaxElement,
1213
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
@@ -257,6 +258,29 @@ impl ast::GenericParamList {
257258
}
258259
}
259260

261+
pub fn syntax_editor_add_generic_param(
262+
&self,
263+
editor: &mut SyntaxEditor,
264+
generic_param: ast::GenericParam,
265+
) {
266+
match self.generic_params().last() {
267+
Some(last_param) => {
268+
let position = crate::syntax_editor::Position::after(last_param.syntax());
269+
let elements = vec![
270+
make::token(T![,]).into(),
271+
make::tokens::single_space().into(),
272+
generic_param.syntax().clone().into(),
273+
];
274+
editor.insert_all(position, elements);
275+
}
276+
None => {
277+
let after_l_angle =
278+
crate::syntax_editor::Position::after(self.l_angle_token().unwrap());
279+
editor.insert(after_l_angle, generic_param.syntax());
280+
}
281+
}
282+
}
283+
260284
/// Removes the existing generic param
261285
pub fn remove_generic_param(&self, generic_param: ast::GenericParam) {
262286
if let Some(previous) = generic_param.syntax().prev_sibling() {

crates/syntax/src/ast/syntax_factory/constructors.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use itertools::Itertools;
33

44
use crate::{
5-
ast::{self, make, HasName},
5+
ast::{self, make, HasName, HasTypeBounds},
66
syntax_editor::SyntaxMappingBuilder,
77
AstNode,
88
};
@@ -15,7 +15,6 @@ impl SyntaxFactory {
1515
}
1616

1717
pub fn ty(&self, text: &str) -> ast::Type {
18-
// FIXME: Is there anything to map here?
1918
make::ty(text).clone_for_update()
2019
}
2120

@@ -29,6 +28,12 @@ impl SyntaxFactory {
2928
if let Some(mut mapping) = self.mappings() {
3029
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
3130
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
31+
if let Some(input) = bounds {
32+
builder.map_node(
33+
input.syntax().clone(),
34+
ast.type_bound_list().unwrap().syntax().clone(),
35+
);
36+
}
3237
builder.finish(&mut mapping);
3338
}
3439

0 commit comments

Comments
 (0)