From a525e830a62272d21fbb0fb1c20bfa865791512d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Sep 2019 17:57:12 +0300 Subject: add new editing API, suitable for modifying several nodes at once --- crates/ra_assists/src/assists/move_bounds.rs | 39 ++++++++-------- crates/ra_assists/src/ast_editor.rs | 68 +++++++++++++++++++++++----- 2 files changed, 76 insertions(+), 31 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index 6fd2fb72b..671826013 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs @@ -3,10 +3,9 @@ use ra_syntax::{ ast::{self, AstNode, NameOwner, TypeBoundsOwner}, SyntaxElement, SyntaxKind::*, - TextRange, }; -use crate::{ast_builder::Make, Assist, AssistCtx, AssistId}; +use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) -> Option { let type_param_list = ctx.node_at_offset::()?; @@ -36,23 +35,23 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) AssistId("move_bounds_to_where_clause"), "move_bounds_to_where_clause", |edit| { - let type_params = type_param_list.type_params().collect::>(); - - for param in &type_params { - if let Some(bounds) = param.type_bound_list() { - let colon = param - .syntax() - .children_with_tokens() - .find(|it| it.kind() == COLON) - .unwrap(); - let start = colon.text_range().start(); - let end = bounds.syntax().text_range().end(); - edit.delete(TextRange::from_to(start, end)); - } - } - - let predicates = type_params.iter().filter_map(build_predicate); - let where_clause = Make::::from_predicates(predicates); + let new_params = type_param_list + .type_params() + .filter(|it| it.type_bound_list().is_some()) + .map(|type_param| { + let without_bounds = + AstEditor::new(type_param.clone()).remove_bounds().ast().clone(); + (type_param, without_bounds) + }); + + let mut ast_editor = AstEditor::new(type_param_list.clone()); + ast_editor.replace_descendants(new_params); + ast_editor.into_text_edit(edit.text_edit_builder()); + + let where_clause = { + let predicates = type_param_list.type_params().filter_map(build_predicate); + Make::::from_predicates(predicates) + }; let to_insert = match anchor.prev_sibling_or_token() { Some(ref elem) if elem.kind() == WHITESPACE => { @@ -68,7 +67,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) ctx.build() } -fn build_predicate(param: &ast::TypeParam) -> Option { +fn build_predicate(param: ast::TypeParam) -> Option { let path = Make::::from_name(param.name()?); let predicate = Make::::from(path, param.type_bound_list()?.bounds()); Some(predicate) diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 55c0aa59f..4e253f0a4 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -1,11 +1,13 @@ use std::{iter, ops::RangeInclusive}; use arrayvec::ArrayVec; +use rustc_hash::FxHashMap; use ra_fmt::leading_indent; use ra_syntax::{ - algo::{insert_children, replace_children}, - ast, AstNode, Direction, InsertPosition, SyntaxElement, + algo, + ast::{self, TypeBoundsOwner}, + AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind::*, T, }; @@ -27,26 +29,55 @@ impl AstEditor { } pub fn into_text_edit(self, builder: &mut TextEditBuilder) { - // FIXME: compute a more fine-grained diff here. - // If *you* know a nice algorithm to compute diff between two syntax - // tree, tell me about it! - builder.replace( - self.original_ast.syntax().text_range(), - self.ast().syntax().text().to_string(), - ); + // FIXME: this is both horrible inefficient and gives larger than + // necessary diff. I bet there's a cool algorithm to diff trees properly. + go(builder, self.original_ast.syntax().clone().into(), self.ast().syntax().clone().into()); + + fn go(buf: &mut TextEditBuilder, lhs: SyntaxElement, rhs: SyntaxElement) { + if lhs.kind() == rhs.kind() && lhs.text_range().len() == rhs.text_range().len() { + if match (&lhs, &rhs) { + (NodeOrToken::Node(lhs), NodeOrToken::Node(rhs)) => lhs.text() == rhs.text(), + (NodeOrToken::Token(lhs), NodeOrToken::Token(rhs)) => lhs.text() == rhs.text(), + _ => false, + } { + return; + } + } + if let (Some(lhs), Some(rhs)) = (lhs.as_node(), rhs.as_node()) { + if lhs.children_with_tokens().count() == rhs.children_with_tokens().count() { + for (lhs, rhs) in lhs.children_with_tokens().zip(rhs.children_with_tokens()) { + go(buf, lhs, rhs) + } + return; + } + } + buf.replace(lhs.text_range(), rhs.to_string()) + } } pub fn ast(&self) -> &N { &self.ast } + pub fn replace_descendants( + &mut self, + replacement_map: impl Iterator, + ) -> &mut Self { + let map = replacement_map + .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) + .collect::>(); + let new_syntax = algo::replace_descendants(self.ast.syntax(), &map); + self.ast = N::cast(new_syntax).unwrap(); + self + } + #[must_use] fn insert_children( &self, position: InsertPosition, mut to_insert: impl Iterator, ) -> N { - let new_syntax = insert_children(self.ast().syntax(), position, &mut to_insert); + let new_syntax = algo::insert_children(self.ast().syntax(), position, &mut to_insert); N::cast(new_syntax).unwrap() } @@ -56,7 +87,7 @@ impl AstEditor { to_delete: RangeInclusive, mut to_insert: impl Iterator, ) -> N { - let new_syntax = replace_children(self.ast().syntax(), to_delete, &mut to_insert); + let new_syntax = algo::replace_children(self.ast().syntax(), to_delete, &mut to_insert); N::cast(new_syntax).unwrap() } @@ -240,3 +271,18 @@ impl AstEditor { self.ast = self.replace_children(replace_range, to_insert.into_iter()) } } + +impl AstEditor { + pub fn remove_bounds(&mut self) -> &mut Self { + let colon = match self.ast.colon_token() { + Some(it) => it, + None => return self, + }; + let end = match self.ast.type_bound_list() { + Some(it) => it.syntax().clone().into(), + None => colon.clone().into(), + }; + self.ast = self.replace_children(RangeInclusive::new(colon.into(), end), iter::empty()); + self + } +} -- cgit v1.2.3