From b83c7eedccea4c9cb35b1d1cc58231f07a5e3ba2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 23 Mar 2021 17:31:19 +0300 Subject: Tweak assits API to fit mutable syntax trees changelog: skip --- crates/ide_assists/src/handlers/merge_imports.rs | 24 ++++++++---------------- crates/ide_assists/src/handlers/move_bounds.rs | 8 ++++---- 2 files changed, 12 insertions(+), 20 deletions(-) (limited to 'crates/ide_assists/src/handlers') diff --git a/crates/ide_assists/src/handlers/merge_imports.rs b/crates/ide_assists/src/handlers/merge_imports.rs index cfc472a32..8e0794218 100644 --- a/crates/ide_assists/src/handlers/merge_imports.rs +++ b/crates/ide_assists/src/handlers/merge_imports.rs @@ -21,12 +21,6 @@ use crate::{ // ``` pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let tree: ast::UseTree = ctx.find_node_at_offset()?; - let original_parent = tree.syntax().ancestors().last()?; - - let tree = tree.clone_for_update(); - let new_parent = tree.syntax().ancestors().last()?; - - let mut offset = ctx.offset(); let mut imports = None; let mut uses = None; @@ -53,22 +47,20 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<() target, |builder| { if let Some((to_replace, replacement, to_remove)) = imports { - if to_remove.syntax().text_range().end() < offset { - offset -= to_remove.syntax().text_range().len(); - } - ted::replace(to_replace.syntax().clone(), replacement.syntax().clone()); + let to_replace = builder.make_ast_mut(to_replace); + let to_remove = builder.make_ast_mut(to_remove); + + ted::replace(to_replace.syntax(), replacement.syntax()); to_remove.remove(); } if let Some((to_replace, replacement, to_remove)) = uses { - if to_remove.syntax().text_range().end() < offset { - offset -= to_remove.syntax().text_range().len(); - } - ted::replace(to_replace.syntax().clone(), replacement.syntax().clone()); + let to_replace = builder.make_ast_mut(to_replace); + let to_remove = builder.make_ast_mut(to_remove); + + ted::replace(to_replace.syntax(), replacement.syntax()); to_remove.remove() } - - builder.replace(original_parent.text_range(), new_parent.to_string()) }, ) } diff --git a/crates/ide_assists/src/handlers/move_bounds.rs b/crates/ide_assists/src/handlers/move_bounds.rs index b5dec8014..011a28d44 100644 --- a/crates/ide_assists/src/handlers/move_bounds.rs +++ b/crates/ide_assists/src/handlers/move_bounds.rs @@ -21,7 +21,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // } // ``` pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - let type_param_list = ctx.find_node_at_offset::()?.clone_for_update(); + let type_param_list = ctx.find_node_at_offset::()?; let mut type_params = type_param_list.type_params(); if type_params.all(|p| p.type_bound_list().is_none()) { @@ -29,7 +29,6 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext } let parent = type_param_list.syntax().parent()?; - let original_parent_range = parent.text_range(); let target = type_param_list.syntax().text_range(); acc.add( @@ -37,6 +36,9 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext "Move to where clause", target, |edit| { + let type_param_list = edit.make_ast_mut(type_param_list); + let parent = edit.make_mut(parent); + let where_clause: ast::WhereClause = match_ast! { match parent { ast::Fn(it) => it.get_or_create_where_clause(), @@ -56,8 +58,6 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext tbl.remove() } } - - edit.replace(original_parent_range, parent.to_string()) }, ) } -- cgit v1.2.3