Skip to content

Commit 1816d22

Browse files
committed
Use SyntaxEditor
1 parent 6b8e927 commit 1816d22

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
utils::{self, DefaultMethods, IgnoreAssocItems},
99
};
1010

11-
fn insert_impl(editor: &mut SyntaxEditor, impl_: &ast::Impl, nominal: &ast::Adt) {
11+
fn insert_impl(editor: &mut SyntaxEditor, impl_: &ast::Impl, nominal: &impl Indent) {
1212
let indent = nominal.indent_level();
1313

1414
impl_.indent(indent);
@@ -158,6 +158,8 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
158158
format!("Generate `{name}` impl for type"),
159159
target,
160160
|edit| {
161+
let mut editor = edit.make_editor(trait_.syntax());
162+
161163
let holder_arg = ast::GenericArg::TypeArg(make::type_arg(make::ty_placeholder()));
162164
let missing_items = utils::filter_assoc_items(
163165
&ctx.sema,
@@ -182,8 +184,6 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
182184
)
183185
.clone_for_update();
184186

185-
let trait_ = edit.make_mut(trait_);
186-
187187
if !missing_items.is_empty() {
188188
utils::add_trait_assoc_items_to_impl(
189189
&ctx.sema,
@@ -198,26 +198,31 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
198198
if let Some(cap) = ctx.config.snippet_cap {
199199
if let Some(generics) = impl_.trait_().and_then(|it| it.generic_arg_list()) {
200200
for generic in generics.generic_args() {
201-
edit.add_placeholder_snippet(cap, generic);
201+
let placeholder = edit.make_placeholder_snippet(cap);
202+
editor.add_annotation(generic.syntax(), placeholder);
202203
}
203204
}
204205

205206
if let Some(ty) = impl_.self_ty() {
206-
edit.add_placeholder_snippet(cap, ty);
207+
let placeholder = edit.make_placeholder_snippet(cap);
208+
editor.add_annotation(ty.syntax(), placeholder);
207209
}
208210

209211
if let Some(expr) =
210212
impl_.assoc_item_list().and_then(|it| it.assoc_items().find_map(extract_expr))
211213
{
212-
edit.add_tabstop_before(cap, expr);
214+
let tabstop = edit.make_tabstop_before(cap);
215+
editor.add_annotation(expr.syntax(), tabstop);
213216
} else if let Some(l_curly) =
214217
impl_.assoc_item_list().and_then(|it| it.l_curly_token())
215218
{
216-
edit.add_tabstop_after_token(cap, l_curly);
219+
let tabstop = edit.make_tabstop_after(cap);
220+
editor.add_annotation(l_curly, tabstop);
217221
}
218222
}
219223

220-
insert_impl(impl_, &trait_);
224+
insert_impl(&mut editor, &impl_, &trait_);
225+
edit.add_file_edits(ctx.vfs_file_id(), editor);
221226
},
222227
)
223228
}
@@ -755,6 +760,43 @@ mod tests {
755760
);
756761
}
757762

763+
#[test]
764+
fn test_add_impl_trait_indent() {
765+
check_assist(
766+
generate_impl_trait,
767+
r#"
768+
mod foo {
769+
mod bar {
770+
trait $0Foo {
771+
type Output;
772+
773+
fn foo(&self) -> Self::Output;
774+
}
775+
}
776+
}
777+
"#,
778+
r#"
779+
mod foo {
780+
mod bar {
781+
trait Foo {
782+
type Output;
783+
784+
fn foo(&self) -> Self::Output;
785+
}
786+
787+
impl Foo for ${1:_} {
788+
type Output;
789+
790+
fn foo(&self) -> Self::Output {
791+
$0todo!()
792+
}
793+
}
794+
}
795+
}
796+
"#,
797+
);
798+
}
799+
758800
#[test]
759801
fn test_add_impl_trait_empty() {
760802
check_assist(

0 commit comments

Comments
 (0)