Remove text_edit_builder api from AssistBuilder

This commit is contained in:
Lukas Wirth 2020-11-08 23:22:11 +01:00
parent c365329911
commit 4c03d98db4
3 changed files with 51 additions and 35 deletions

View file

@ -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);

View file

@ -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<ast::UseTree, ast::UseTreeList>,
path: ast::Path,
names_to_import: Vec<Name>,
@ -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
}
",
);
}
}

View file

@ -47,9 +47,11 @@ fn reorder<R: AstNode>(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);
},
)
}