From e010b144d5abcbd0947d0490123ef693a6a17c78 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Sep 2019 09:27:26 +0300 Subject: move field list to ast/edit.rs --- crates/ra_assists/src/assist_ctx.rs | 4 +- crates/ra_assists/src/ast_editor.rs | 101 ++---------------------------------- 2 files changed, 5 insertions(+), 100 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index cbe12e908..5f564be0b 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -178,9 +178,7 @@ impl AssistBuilder { } pub(crate) fn replace_ast(&mut self, old: N, new: N) { - for (from, to) in algo::diff(old.syntax(), new.syntax()) { - self.edit.replace(from.text_range(), to.to_string()) - } + algo::diff(old.syntax(), new.syntax()).into_text_edit(&mut self.edit) } fn build(self) -> AssistAction { diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 262e2fcf4..54849b7b0 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -1,15 +1,12 @@ use std::{iter, ops::RangeInclusive}; -use arrayvec::ArrayVec; -use rustc_hash::FxHashMap; - -use ra_fmt::leading_indent; use ra_syntax::{ algo, - ast::{self, make::tokens, TypeBoundsOwner}, - AstNode, Direction, InsertPosition, SyntaxElement, T, + ast::{self, TypeBoundsOwner}, + AstNode, SyntaxElement, }; use ra_text_edit::TextEditBuilder; +use rustc_hash::FxHashMap; pub struct AstEditor { original_ast: N, @@ -25,9 +22,7 @@ impl AstEditor { } pub fn into_text_edit(self, builder: &mut TextEditBuilder) { - for (from, to) in algo::diff(&self.original_ast.syntax(), self.ast().syntax()) { - builder.replace(from.text_range(), to.to_string()) - } + algo::diff(&self.original_ast.syntax(), self.ast().syntax()).into_text_edit(builder) } pub fn ast(&self) -> &N { @@ -46,16 +41,6 @@ impl AstEditor { self } - #[must_use] - fn insert_children( - &self, - position: InsertPosition, - mut to_insert: impl Iterator, - ) -> N { - let new_syntax = algo::insert_children(self.ast().syntax(), position, &mut to_insert); - N::cast(new_syntax).unwrap() - } - #[must_use] fn replace_children( &self, @@ -67,84 +52,6 @@ impl AstEditor { } } -impl AstEditor { - pub fn append_field(&mut self, field: &ast::RecordField) { - self.insert_field(InsertPosition::Last, field) - } - - pub fn insert_field( - &mut self, - position: InsertPosition<&'_ ast::RecordField>, - field: &ast::RecordField, - ) { - let is_multiline = self.ast().syntax().text().contains_char('\n'); - let ws; - let space = if is_multiline { - ws = tokens::WsBuilder::new(&format!( - "\n{} ", - leading_indent(self.ast().syntax()).unwrap_or("".into()) - )); - ws.ws() - } else { - tokens::single_space() - }; - - let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new(); - to_insert.push(space.into()); - to_insert.push(field.syntax().clone().into()); - to_insert.push(tokens::comma().into()); - - macro_rules! after_l_curly { - () => {{ - let anchor = match self.l_curly() { - Some(it) => it, - None => return, - }; - InsertPosition::After(anchor) - }}; - } - - macro_rules! after_field { - ($anchor:expr) => { - if let Some(comma) = $anchor - .syntax() - .siblings_with_tokens(Direction::Next) - .find(|it| it.kind() == T![,]) - { - InsertPosition::After(comma) - } else { - to_insert.insert(0, tokens::comma().into()); - InsertPosition::After($anchor.syntax().clone().into()) - } - }; - }; - - let position = match position { - InsertPosition::First => after_l_curly!(), - InsertPosition::Last => { - if !is_multiline { - // don't insert comma before curly - to_insert.pop(); - } - match self.ast().fields().last() { - Some(it) => after_field!(it), - None => after_l_curly!(), - } - } - InsertPosition::Before(anchor) => { - InsertPosition::Before(anchor.syntax().clone().into()) - } - InsertPosition::After(anchor) => after_field!(anchor), - }; - - self.ast = self.insert_children(position, to_insert.iter().cloned()); - } - - fn l_curly(&self) -> Option { - self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{']) - } -} - impl AstEditor { pub fn remove_bounds(&mut self) -> &mut Self { let colon = match self.ast.colon_token() { -- cgit v1.2.3