Skip to content

Commit 8c978e3

Browse files
authored
Merge pull request #21109 from Young-Flash/migrate_to_SyntaxEditor_api
internal: migrate to SyntaxEditor api for `replace_qualified_name_with_use`
2 parents e540a44 + d2e251c commit 8c978e3

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use ide_db::{
66
use syntax::{
77
AstNode, Edition, SyntaxNode,
88
ast::{self, HasGenericArgs, make},
9-
match_ast, ted,
9+
match_ast,
10+
syntax_editor::SyntaxEditor,
1011
};
1112

1213
use crate::{AssistContext, AssistId, Assists};
@@ -71,8 +72,10 @@ pub(crate) fn replace_qualified_name_with_use(
7172
|builder| {
7273
// Now that we've brought the name into scope, re-qualify all paths that could be
7374
// affected (that is, all paths inside the node we added the `use` to).
74-
let scope = builder.make_import_scope_mut(scope);
75-
shorten_paths(scope.as_syntax_node(), &original_path);
75+
let scope_node = scope.as_syntax_node();
76+
let mut editor = builder.make_editor(scope_node);
77+
shorten_paths(&mut editor, scope_node, &original_path);
78+
builder.add_file_edits(ctx.vfs_file_id(), editor);
7679
let path = drop_generic_args(&original_path);
7780
let edition = ctx
7881
.sema
@@ -85,6 +88,7 @@ pub(crate) fn replace_qualified_name_with_use(
8588
Some(qualifier) => make::path_concat(qualifier, path),
8689
None => path,
8790
};
91+
let scope = builder.make_import_scope_mut(scope);
8892
insert_use(&scope, path, &ctx.config.insert_use);
8993
},
9094
)
@@ -107,17 +111,19 @@ fn target_path(ctx: &AssistContext<'_>, mut original_path: ast::Path) -> Option<
107111
}
108112

109113
fn drop_generic_args(path: &ast::Path) -> ast::Path {
110-
let path = path.clone_for_update();
114+
let path = path.clone_subtree();
115+
let mut editor = SyntaxEditor::new(path.syntax().clone());
111116
if let Some(segment) = path.segment()
112117
&& let Some(generic_args) = segment.generic_arg_list()
113118
{
114-
ted::remove(generic_args.syntax());
119+
editor.delete(generic_args.syntax());
115120
}
116-
path
121+
122+
ast::Path::cast(editor.finish().new_root().clone()).unwrap()
117123
}
118124

119125
/// Mutates `node` to shorten `path` in all descendants of `node`.
120-
fn shorten_paths(node: &SyntaxNode, path: &ast::Path) {
126+
fn shorten_paths(editor: &mut SyntaxEditor, node: &SyntaxNode, path: &ast::Path) {
121127
for child in node.children() {
122128
match_ast! {
123129
match child {
@@ -127,26 +133,26 @@ fn shorten_paths(node: &SyntaxNode, path: &ast::Path) {
127133
// Don't descend into submodules, they don't have the same `use` items in scope.
128134
// FIXME: This isn't true due to `super::*` imports?
129135
ast::Module(_) => continue,
130-
ast::Path(p) => if maybe_replace_path(p.clone(), path.clone()).is_none() {
131-
shorten_paths(p.syntax(), path);
136+
ast::Path(p) => if maybe_replace_path(editor, p.clone(), path.clone()).is_none() {
137+
shorten_paths(editor, p.syntax(), path);
132138
},
133-
_ => shorten_paths(&child, path),
139+
_ => shorten_paths(editor, &child, path),
134140
}
135141
}
136142
}
137143
}
138144

139-
fn maybe_replace_path(path: ast::Path, target: ast::Path) -> Option<()> {
145+
fn maybe_replace_path(editor: &mut SyntaxEditor, path: ast::Path, target: ast::Path) -> Option<()> {
140146
if !path_eq_no_generics(path.clone(), target) {
141147
return None;
142148
}
143149

144150
// Shorten `path`, leaving only its last segment.
145151
if let Some(parent) = path.qualifier() {
146-
ted::remove(parent.syntax());
152+
editor.delete(parent.syntax());
147153
}
148154
if let Some(double_colon) = path.coloncolon_token() {
149-
ted::remove(&double_colon);
155+
editor.delete(double_colon);
150156
}
151157

152158
Some(())

0 commit comments

Comments
 (0)