Skip to content

Commit 61e8403

Browse files
committed
feat: migrate introduce_named_generic assist to use SyntaxFactory
Signed-off-by: Tarek <tareknaser360@gmail.com>
1 parent 609621d commit 61e8403

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/introduce_named_generic.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use ide_db::syntax_helpers::suggest_name;
22
use itertools::Itertools;
33
use syntax::{
4-
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasGenericParams, HasName},
5-
ted,
4+
ast::{
5+
self, edit_in_place::GenericParamsOwnerEdit, syntax_factory::SyntaxFactory, AstNode,
6+
HasGenericParams, HasName,
7+
},
8+
SyntaxElement,
69
};
710

811
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -25,12 +28,20 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
2528

2629
let type_bound_list = impl_trait_type.type_bound_list()?;
2730

31+
// FIXME: Is this node appropriate to use for SyntaxEditor in this case?
32+
let parent_node = match ctx.covering_element() {
33+
SyntaxElement::Node(n) => n,
34+
SyntaxElement::Token(t) => t.parent()?,
35+
};
36+
let make = SyntaxFactory::new();
37+
2838
let target = fn_.syntax().text_range();
2939
acc.add(
3040
AssistId("introduce_named_generic", AssistKind::RefactorRewrite),
3141
"Replace impl trait with generic",
3242
target,
3343
|edit| {
44+
let mut editor = edit.make_editor(&parent_node);
3445
let impl_trait_type = edit.make_mut(impl_trait_type);
3546
let fn_ = edit.make_mut(fn_);
3647
let fn_generic_param_list = fn_.get_or_create_generic_param_list();
@@ -47,11 +58,12 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
4758
)
4859
.for_impl_trait_as_generic(&impl_trait_type);
4960

50-
let type_param = make::type_param(make::name(&type_param_name), Some(type_bound_list))
61+
let type_param = make
62+
.type_param(make.name(&type_param_name), Some(type_bound_list))
5163
.clone_for_update();
52-
let new_ty = make::ty(&type_param_name).clone_for_update();
64+
let new_ty = make.ty(&type_param_name).clone_for_update();
5365

54-
ted::replace(impl_trait_type.syntax(), new_ty.syntax());
66+
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
5567
fn_generic_param_list.add_generic_param(type_param.into());
5668

5769
if let Some(cap) = ctx.config.snippet_cap {
@@ -61,6 +73,9 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
6173
edit.add_tabstop_before(cap, generic_param);
6274
}
6375
}
76+
77+
editor.add_mappings(make.finish_with_mappings());
78+
edit.add_file_edits(ctx.file_id(), editor);
6479
},
6580
)
6681
}

src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ impl SyntaxFactory {
1414
make::name(name).clone_for_update()
1515
}
1616

17+
pub fn ty(&self, text: &str) -> ast::Type {
18+
// FIXME: Is there anything to map here?
19+
make::ty(text).clone_for_update()
20+
}
21+
22+
pub fn type_param(
23+
&self,
24+
name: ast::Name,
25+
bounds: Option<ast::TypeBoundList>,
26+
) -> ast::TypeParam {
27+
let ast = make::type_param(name.clone(), bounds.clone()).clone_for_update();
28+
29+
if let Some(mut mapping) = self.mappings() {
30+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
31+
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
32+
builder.finish(&mut mapping);
33+
}
34+
35+
ast
36+
}
37+
1738
pub fn ident_pat(&self, ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat {
1839
let ast = make::ident_pat(ref_, mut_, name.clone()).clone_for_update();
1940

0 commit comments

Comments
 (0)