From 4c03d98db4638729e8b47449ba76bf51e33cd444 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 8 Nov 2020 23:22:11 +0100 Subject: Remove text_edit_builder api from AssistBuilder --- crates/assists/src/assist_context.rs | 6 -- crates/assists/src/handlers/expand_glob_import.rs | 76 ++++++++++++++--------- crates/assists/src/handlers/reorder_fields.rs | 4 +- 3 files changed, 51 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs index fcfe2d6ee..51a160f40 100644 --- a/crates/assists/src/assist_context.rs +++ b/crates/assists/src/assist_context.rs @@ -275,12 +275,6 @@ impl AssistBuilder { algo::diff(&node, &new).into_text_edit(&mut self.edit); } - // FIXME: kill this API - /// Get access to the raw `TextEditBuilder`. - pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder { - &mut self.edit - } - fn finish(mut self) -> SourceChange { self.commit(); let mut change = mem::take(&mut self.change); diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs index 853266395..f51a9a4ad 100644 --- a/crates/assists/src/handlers/expand_glob_import.rs +++ b/crates/assists/src/handlers/expand_glob_import.rs @@ -5,13 +5,13 @@ use ide_db::{ search::SearchScope, }; use syntax::{ - algo, + algo::SyntaxRewriter, ast::{self, make}, AstNode, Direction, SyntaxNode, SyntaxToken, T, }; use crate::{ - assist_context::{AssistBuilder, AssistContext, Assists}, + assist_context::{AssistContext, Assists}, AssistId, AssistKind, }; @@ -61,7 +61,9 @@ pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Opti "Expand glob import", target.text_range(), |builder| { - replace_ast(builder, parent, mod_path, names_to_import); + let mut rewriter = SyntaxRewriter::default(); + replace_ast(&mut rewriter, parent, mod_path, names_to_import); + builder.rewrite(rewriter); }, ) } @@ -236,7 +238,7 @@ fn find_names_to_import( } fn replace_ast( - builder: &mut AssistBuilder, + rewriter: &mut SyntaxRewriter, parent: Either, path: ast::Path, names_to_import: Vec, @@ -264,32 +266,21 @@ fn replace_ast( match use_trees.as_slice() { [name] => { if let Some(end_path) = name.path() { - let replacement = - make::use_tree(make::path_concat(path, end_path), None, None, false); - - algo::diff( - &parent.either(|n| n.syntax().clone(), |n| n.syntax().clone()), - replacement.syntax(), - ) - .into_text_edit(builder.text_edit_builder()); + rewriter.replace_ast( + &parent.left_or_else(|tl| tl.parent_use_tree()), + &make::use_tree(make::path_concat(path, end_path), None, None, false), + ); } } - names => { - let replacement = match parent { - Either::Left(_) => { - make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false) - .syntax() - .clone() - } - Either::Right(_) => make::use_tree_list(names.to_owned()).syntax().clone(), - }; - - algo::diff( - &parent.either(|n| n.syntax().clone(), |n| n.syntax().clone()), - &replacement, - ) - .into_text_edit(builder.text_edit_builder()); - } + names => match &parent { + Either::Left(parent) => rewriter.replace_ast( + parent, + &make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false), + ), + Either::Right(parent) => { + rewriter.replace_ast(parent, &make::use_tree_list(names.to_owned())) + } + }, }; } @@ -884,4 +875,33 @@ fn qux(baz: Baz) {} ", ) } + + #[test] + fn expanding_glob_import_single_nested_glob_only() { + check_assist( + expand_glob_import, + r" +mod foo { + pub struct Bar; +} + +use foo::{*<|>}; + +struct Baz { + bar: Bar +} +", + r" +mod foo { + pub struct Bar; +} + +use foo::Bar; + +struct Baz { + bar: Bar +} +", + ); + } } diff --git a/crates/assists/src/handlers/reorder_fields.rs b/crates/assists/src/handlers/reorder_fields.rs index 527f457a7..7c0f0f44e 100644 --- a/crates/assists/src/handlers/reorder_fields.rs +++ b/crates/assists/src/handlers/reorder_fields.rs @@ -47,9 +47,11 @@ fn reorder(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { "Reorder record fields", target, |edit| { + let mut rewriter = algo::SyntaxRewriter::default(); for (old, new) in fields.iter().zip(&sorted_fields) { - algo::diff(old, new).into_text_edit(edit.text_edit_builder()); + rewriter.replace(old, new); } + edit.rewrite(rewriter); }, ) } -- cgit v1.2.3