diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-11-09 11:40:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-11-09 11:40:08 +0000 |
commit | 2f247140817c9cbd9009085c9f9ccedb4f6a718f (patch) | |
tree | 9866d241ed3345fd89ac511eba4bd3d85bd41171 | |
parent | a12088456072459c7df2d9d8f077a69ab518710a (diff) | |
parent | 4c03d98db4638729e8b47449ba76bf51e33cd444 (diff) |
Merge #6501
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 <[email protected]>
-rw-r--r-- | crates/assists/src/assist_context.rs | 6 | ||||
-rw-r--r-- | crates/assists/src/handlers/expand_glob_import.rs | 76 | ||||
-rw-r--r-- | crates/assists/src/handlers/reorder_fields.rs | 4 |
3 files changed, 51 insertions, 35 deletions
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 { | |||
275 | algo::diff(&node, &new).into_text_edit(&mut self.edit); | 275 | algo::diff(&node, &new).into_text_edit(&mut self.edit); |
276 | } | 276 | } |
277 | 277 | ||
278 | // FIXME: kill this API | ||
279 | /// Get access to the raw `TextEditBuilder`. | ||
280 | pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder { | ||
281 | &mut self.edit | ||
282 | } | ||
283 | |||
284 | fn finish(mut self) -> SourceChange { | 278 | fn finish(mut self) -> SourceChange { |
285 | self.commit(); | 279 | self.commit(); |
286 | let mut change = mem::take(&mut self.change); | 280 | 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::{ | |||
5 | search::SearchScope, | 5 | search::SearchScope, |
6 | }; | 6 | }; |
7 | use syntax::{ | 7 | use syntax::{ |
8 | algo, | 8 | algo::SyntaxRewriter, |
9 | ast::{self, make}, | 9 | ast::{self, make}, |
10 | AstNode, Direction, SyntaxNode, SyntaxToken, T, | 10 | AstNode, Direction, SyntaxNode, SyntaxToken, T, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | assist_context::{AssistBuilder, AssistContext, Assists}, | 14 | assist_context::{AssistContext, Assists}, |
15 | AssistId, AssistKind, | 15 | AssistId, AssistKind, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -61,7 +61,9 @@ pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Opti | |||
61 | "Expand glob import", | 61 | "Expand glob import", |
62 | target.text_range(), | 62 | target.text_range(), |
63 | |builder| { | 63 | |builder| { |
64 | replace_ast(builder, parent, mod_path, names_to_import); | 64 | let mut rewriter = SyntaxRewriter::default(); |
65 | replace_ast(&mut rewriter, parent, mod_path, names_to_import); | ||
66 | builder.rewrite(rewriter); | ||
65 | }, | 67 | }, |
66 | ) | 68 | ) |
67 | } | 69 | } |
@@ -236,7 +238,7 @@ fn find_names_to_import( | |||
236 | } | 238 | } |
237 | 239 | ||
238 | fn replace_ast( | 240 | fn replace_ast( |
239 | builder: &mut AssistBuilder, | 241 | rewriter: &mut SyntaxRewriter, |
240 | parent: Either<ast::UseTree, ast::UseTreeList>, | 242 | parent: Either<ast::UseTree, ast::UseTreeList>, |
241 | path: ast::Path, | 243 | path: ast::Path, |
242 | names_to_import: Vec<Name>, | 244 | names_to_import: Vec<Name>, |
@@ -264,32 +266,21 @@ fn replace_ast( | |||
264 | match use_trees.as_slice() { | 266 | match use_trees.as_slice() { |
265 | [name] => { | 267 | [name] => { |
266 | if let Some(end_path) = name.path() { | 268 | if let Some(end_path) = name.path() { |
267 | let replacement = | 269 | rewriter.replace_ast( |
268 | make::use_tree(make::path_concat(path, end_path), None, None, false); | 270 | &parent.left_or_else(|tl| tl.parent_use_tree()), |
269 | 271 | &make::use_tree(make::path_concat(path, end_path), None, None, false), | |
270 | algo::diff( | 272 | ); |
271 | &parent.either(|n| n.syntax().clone(), |n| n.syntax().clone()), | ||
272 | replacement.syntax(), | ||
273 | ) | ||
274 | .into_text_edit(builder.text_edit_builder()); | ||
275 | } | 273 | } |
276 | } | 274 | } |
277 | names => { | 275 | names => match &parent { |
278 | let replacement = match parent { | 276 | Either::Left(parent) => rewriter.replace_ast( |
279 | Either::Left(_) => { | 277 | parent, |
280 | make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false) | 278 | &make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false), |
281 | .syntax() | 279 | ), |
282 | .clone() | 280 | Either::Right(parent) => { |
283 | } | 281 | rewriter.replace_ast(parent, &make::use_tree_list(names.to_owned())) |
284 | Either::Right(_) => make::use_tree_list(names.to_owned()).syntax().clone(), | 282 | } |
285 | }; | 283 | }, |
286 | |||
287 | algo::diff( | ||
288 | &parent.either(|n| n.syntax().clone(), |n| n.syntax().clone()), | ||
289 | &replacement, | ||
290 | ) | ||
291 | .into_text_edit(builder.text_edit_builder()); | ||
292 | } | ||
293 | }; | 284 | }; |
294 | } | 285 | } |
295 | 286 | ||
@@ -884,4 +875,33 @@ fn qux(baz: Baz) {} | |||
884 | ", | 875 | ", |
885 | ) | 876 | ) |
886 | } | 877 | } |
878 | |||
879 | #[test] | ||
880 | fn expanding_glob_import_single_nested_glob_only() { | ||
881 | check_assist( | ||
882 | expand_glob_import, | ||
883 | r" | ||
884 | mod foo { | ||
885 | pub struct Bar; | ||
886 | } | ||
887 | |||
888 | use foo::{*<|>}; | ||
889 | |||
890 | struct Baz { | ||
891 | bar: Bar | ||
892 | } | ||
893 | ", | ||
894 | r" | ||
895 | mod foo { | ||
896 | pub struct Bar; | ||
897 | } | ||
898 | |||
899 | use foo::Bar; | ||
900 | |||
901 | struct Baz { | ||
902 | bar: Bar | ||
903 | } | ||
904 | ", | ||
905 | ); | ||
906 | } | ||
887 | } | 907 | } |
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<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
47 | "Reorder record fields", | 47 | "Reorder record fields", |
48 | target, | 48 | target, |
49 | |edit| { | 49 | |edit| { |
50 | let mut rewriter = algo::SyntaxRewriter::default(); | ||
50 | for (old, new) in fields.iter().zip(&sorted_fields) { | 51 | for (old, new) in fields.iter().zip(&sorted_fields) { |
51 | algo::diff(old, new).into_text_edit(edit.text_edit_builder()); | 52 | rewriter.replace(old, new); |
52 | } | 53 | } |
54 | edit.rewrite(rewriter); | ||
53 | }, | 55 | }, |
54 | ) | 56 | ) |
55 | } | 57 | } |