Skip to content

Commit 649ec27

Browse files
authored
Merge pull request #20223 from Hmikihiro/migrate_generate_impl
Migrate `generate_impl` assist to use `SyntaxEditor`
2 parents 7625ed9 + b692051 commit 649ec27

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use syntax::{
22
ast::{self, AstNode, HasName, edit_in_place::Indent, make},
3-
ted,
3+
syntax_editor::{Position, SyntaxEditor},
44
};
55

66
use crate::{AssistContext, AssistId, Assists, utils};
77

8-
fn insert_impl(impl_: ast::Impl, nominal: &ast::Adt) {
8+
fn insert_impl(editor: &mut SyntaxEditor, impl_: &ast::Impl, nominal: &ast::Adt) {
99
let indent = nominal.indent_level();
10-
ted::insert_all_raw(
11-
ted::Position::after(nominal.syntax()),
10+
editor.insert_all(
11+
Position::after(nominal.syntax()),
1212
vec![
1313
// Add a blank line after the ADT, and indentation for the impl to match the ADT
1414
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
@@ -51,14 +51,17 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
5151
// Generate the impl
5252
let impl_ = utils::generate_impl(&nominal);
5353

54+
let mut editor = edit.make_editor(nominal.syntax());
5455
// Add a tabstop after the left curly brace
5556
if let Some(cap) = ctx.config.snippet_cap {
5657
if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) {
57-
edit.add_tabstop_after_token(cap, l_curly);
58+
let tabstop = edit.make_tabstop_after(cap);
59+
editor.add_annotation(l_curly, tabstop);
5860
}
5961
}
6062

61-
insert_impl(impl_, &edit.make_mut(nominal));
63+
insert_impl(&mut editor, &impl_, &nominal);
64+
edit.add_file_edits(ctx.vfs_file_id(), editor);
6265
},
6366
)
6467
}
@@ -97,18 +100,22 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
97100
// Generate the impl
98101
let impl_ = utils::generate_trait_impl_intransitive(&nominal, make::ty_placeholder());
99102

103+
let mut editor = edit.make_editor(nominal.syntax());
100104
// Make the trait type a placeholder snippet
101105
if let Some(cap) = ctx.config.snippet_cap {
102106
if let Some(trait_) = impl_.trait_() {
103-
edit.add_placeholder_snippet(cap, trait_);
107+
let placeholder = edit.make_placeholder_snippet(cap);
108+
editor.add_annotation(trait_.syntax(), placeholder);
104109
}
105110

106111
if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) {
107-
edit.add_tabstop_after_token(cap, l_curly);
112+
let tabstop = edit.make_tabstop_after(cap);
113+
editor.add_annotation(l_curly, tabstop);
108114
}
109115
}
110116

111-
insert_impl(impl_, &edit.make_mut(nominal));
117+
insert_impl(&mut editor, &impl_, &nominal);
118+
edit.add_file_edits(ctx.vfs_file_id(), editor);
112119
},
113120
)
114121
}

0 commit comments

Comments
 (0)