move whitespace manipulation inside AstEditor

This commit is contained in:
Ekaterina Babshukova 2019-07-10 21:53:44 +03:00
parent 82c3fe7d13
commit 895597d567
2 changed files with 17 additions and 19 deletions

View File

@ -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()

View File

@ -85,10 +85,6 @@ impl AstEditor<ast::NamedFieldList> {
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<ast::NamedFieldList> {
}
impl AstEditor<ast::ItemList> {
pub fn make_multiline(&mut self) {
self.do_make_multiline()
pub fn append_items<'a>(&mut self, items: impl Iterator<Item = &'a ast::ImplItem>) {
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) {