aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEkaterina Babshukova <[email protected]>2019-07-10 19:53:44 +0100
committerEkaterina Babshukova <[email protected]>2019-07-10 20:03:00 +0100
commit895597d56739c4f181f22e56d8a13a5376d2974a (patch)
tree0d35c0ddd0c970b39bc0b3bd94b52ea00fe97e52 /crates
parent82c3fe7d13b67bdec67b53e9adb6f3ec404bf5f0 (diff)
move whitespace manipulation inside AstEditor
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/add_missing_impl_members.rs24
-rw-r--r--crates/ra_assists/src/ast_editor.rs12
2 files changed, 17 insertions, 19 deletions
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(
78 78
79 ctx.add_action(AssistId(assist_id), label, |edit| { 79 ctx.add_action(AssistId(assist_id), label, |edit| {
80 let n_existing_items = impl_item_list.impl_items().count(); 80 let n_existing_items = impl_item_list.impl_items().count();
81 let mut ast_editor = AstEditor::new(impl_item_list); 81 let items: Vec<_> = missing_items
82 if n_existing_items == 0 { 82 .into_iter()
83 ast_editor.make_multiline(); 83 .map(|it| match it.kind() {
84 }
85
86 for item in missing_items {
87 let it = match item.kind() {
88 ImplItemKind::FnDef(def) => { 84 ImplItemKind::FnDef(def) => {
89 strip_docstring(ImplItem::cast(add_body(def).syntax()).unwrap()) 85 strip_docstring(ImplItem::cast(add_body(def).syntax()).unwrap())
90 } 86 }
91 _ => strip_docstring(item), 87 _ => strip_docstring(it),
92 }; 88 })
93 ast_editor.append_item(&it) 89 .collect();
94 } 90 let mut ast_editor = AstEditor::new(impl_item_list);
91
92 ast_editor.append_items(items.iter().map(|it| &**it));
95 93
96 let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); 94 let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap();
97 let cursor_poisition = first_new_item.syntax().range().start(); 95 let cursor_position = first_new_item.syntax().range().start();
98 ast_editor.into_text_edit(edit.text_edit_builder()); 96 ast_editor.into_text_edit(edit.text_edit_builder());
99 97
100 edit.set_cursor(cursor_poisition); 98 edit.set_cursor(cursor_position);
101 }); 99 });
102 100
103 ctx.build() 101 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<ast::NamedFieldList> {
85 self.insert_field(InsertPosition::Last, field) 85 self.insert_field(InsertPosition::Last, field)
86 } 86 }
87 87
88 pub fn make_multiline(&mut self) {
89 self.do_make_multiline()
90 }
91
92 pub fn insert_field( 88 pub fn insert_field(
93 &mut self, 89 &mut self,
94 position: InsertPosition<&'_ ast::NamedField>, 90 position: InsertPosition<&'_ ast::NamedField>,
@@ -161,8 +157,12 @@ impl AstEditor<ast::NamedFieldList> {
161} 157}
162 158
163impl AstEditor<ast::ItemList> { 159impl AstEditor<ast::ItemList> {
164 pub fn make_multiline(&mut self) { 160 pub fn append_items<'a>(&mut self, items: impl Iterator<Item = &'a ast::ImplItem>) {
165 self.do_make_multiline() 161 let n_existing_items = self.ast().impl_items().count();
162 if n_existing_items == 0 {
163 self.do_make_multiline();
164 }
165 items.for_each(|it| self.append_item(it));
166 } 166 }
167 167
168 pub fn append_item(&mut self, item: &ast::ImplItem) { 168 pub fn append_item(&mut self, item: &ast::ImplItem) {