From 895597d56739c4f181f22e56d8a13a5376d2974a Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Wed, 10 Jul 2019 21:53:44 +0300 Subject: move whitespace manipulation inside AstEditor --- crates/ra_assists/src/add_missing_impl_members.rs | 24 +++++++++++------------ crates/ra_assists/src/ast_editor.rs | 12 ++++++------ 2 files changed, 17 insertions(+), 19 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 7fc8f63b3..6ffdad0b1 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -78,26 +78,24 @@ fn add_missing_impl_members_inner( ctx.add_action(AssistId(assist_id), label, |edit| { let n_existing_items = impl_item_list.impl_items().count(); - let mut ast_editor = AstEditor::new(impl_item_list); - if n_existing_items == 0 { - ast_editor.make_multiline(); - } - - for item in missing_items { - let it = match item.kind() { + let items: Vec<_> = missing_items + .into_iter() + .map(|it| match it.kind() { ImplItemKind::FnDef(def) => { strip_docstring(ImplItem::cast(add_body(def).syntax()).unwrap()) } - _ => strip_docstring(item), - }; - ast_editor.append_item(&it) - } + _ => strip_docstring(it), + }) + .collect(); + let mut ast_editor = AstEditor::new(impl_item_list); + + ast_editor.append_items(items.iter().map(|it| &**it)); let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); - let cursor_poisition = first_new_item.syntax().range().start(); + let cursor_position = first_new_item.syntax().range().start(); ast_editor.into_text_edit(edit.text_edit_builder()); - edit.set_cursor(cursor_poisition); + edit.set_cursor(cursor_position); }); ctx.build() diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index c9a5cf8d9..ba816eb65 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -85,10 +85,6 @@ impl AstEditor { self.insert_field(InsertPosition::Last, field) } - pub fn make_multiline(&mut self) { - self.do_make_multiline() - } - pub fn insert_field( &mut self, position: InsertPosition<&'_ ast::NamedField>, @@ -161,8 +157,12 @@ impl AstEditor { } impl AstEditor { - pub fn make_multiline(&mut self) { - self.do_make_multiline() + pub fn append_items<'a>(&mut self, items: impl Iterator) { + let n_existing_items = self.ast().impl_items().count(); + if n_existing_items == 0 { + self.do_make_multiline(); + } + items.for_each(|it| self.append_item(it)); } pub fn append_item(&mut self, item: &ast::ImplItem) { -- cgit v1.2.3