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 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'crates/ra_assists/src/assists/move_bounds.rs') 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) -- cgit v1.2.3