Skip to content

Commit

Permalink
Merge #6501
Browse files Browse the repository at this point in the history
6501: Remove text_edit_builder api from AssistBuilder r=matklad a=Veykril

Also fixes a small bug in `expand_glob_import` in regards to the very nice looking `something::{*}` import when only one item was used. Before it would duplicate the path and just append it, causing the following wrong import `something::something::UsedItem`.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
  • Loading branch information
bors[bot] and Veykril committed Nov 9, 2020
2 parents a120884 + 4c03d98 commit 2f24714
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
6 changes: 0 additions & 6 deletions crates/assists/src/assist_context.rs
Expand Up @@ -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);
Expand Down
76 changes: 48 additions & 28 deletions crates/assists/src/handlers/expand_glob_import.rs
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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);
},
)
}
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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()))
}
},
};
}

Expand Down Expand Up @@ -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
}
",
);
}
}
4 changes: 3 additions & 1 deletion crates/assists/src/handlers/reorder_fields.rs
Expand Up @@ -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);
},
)
}
Expand Down

0 comments on commit 2f24714

Please sign in to comment.