diff options
Diffstat (limited to 'crates/ra_assists/src/assists/add_missing_impl_members.rs')
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 9edf1b21a..565b96fb5 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -2,11 +2,11 @@ | |||
2 | 2 | ||
3 | use hir::{db::HirDatabase, HasSource}; | 3 | use hir::{db::HirDatabase, HasSource}; |
4 | use ra_syntax::{ | 4 | use ra_syntax::{ |
5 | ast::{self, make, AstNode, NameOwner}, | 5 | ast::{self, edit, make, AstNode, NameOwner}, |
6 | SmolStr, | 6 | SmolStr, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; | 9 | use crate::{Assist, AssistCtx, AssistId}; |
10 | 10 | ||
11 | #[derive(PartialEq)] | 11 | #[derive(PartialEq)] |
12 | enum AddMissingImplMembersMode { | 12 | enum AddMissingImplMembersMode { |
@@ -77,30 +77,26 @@ fn add_missing_impl_members_inner( | |||
77 | 77 | ||
78 | ctx.add_action(AssistId(assist_id), label, |edit| { | 78 | ctx.add_action(AssistId(assist_id), label, |edit| { |
79 | let n_existing_items = impl_item_list.impl_items().count(); | 79 | let n_existing_items = impl_item_list.impl_items().count(); |
80 | let items = missing_items.into_iter().map(|it| match it { | 80 | let items = missing_items |
81 | ast::ImplItem::FnDef(def) => strip_docstring(add_body(def).into()), | 81 | .into_iter() |
82 | _ => strip_docstring(it), | 82 | .map(|it| match it { |
83 | }); | 83 | ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), |
84 | let mut ast_editor = AstEditor::new(impl_item_list); | 84 | _ => it, |
85 | 85 | }) | |
86 | ast_editor.append_items(items); | 86 | .map(|it| edit::strip_attrs_and_docs(&it)); |
87 | 87 | let new_impl_item_list = impl_item_list.append_items(items); | |
88 | let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); | 88 | let cursor_position = { |
89 | let cursor_position = first_new_item.syntax().text_range().start(); | 89 | let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap(); |
90 | ast_editor.into_text_edit(edit.text_edit_builder()); | 90 | first_new_item.syntax().text_range().start() |
91 | 91 | }; | |
92 | |||
93 | edit.replace_ast(impl_item_list, new_impl_item_list); | ||
92 | edit.set_cursor(cursor_position); | 94 | edit.set_cursor(cursor_position); |
93 | }); | 95 | }); |
94 | 96 | ||
95 | ctx.build() | 97 | ctx.build() |
96 | } | 98 | } |
97 | 99 | ||
98 | fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { | ||
99 | let mut ast_editor = AstEditor::new(item); | ||
100 | ast_editor.strip_attrs_and_docs(); | ||
101 | ast_editor.ast().to_owned() | ||
102 | } | ||
103 | |||
104 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 100 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
105 | if fn_def.body().is_none() { | 101 | if fn_def.body().is_none() { |
106 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) | 102 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) |