diff options
author | Aleksey Kladov <[email protected]> | 2020-02-28 21:43:13 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-28 21:43:13 +0000 |
commit | 067cb928f09482c63ec2bfbdafb04fbd53b754e1 (patch) | |
tree | 4a39fe02cab714a84eca5742ecaa2592cfe5d57b /crates | |
parent | 61ba56d433a4d3d2651999efee43be8917ada747 (diff) |
Simplify
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 1 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs | 16 | ||||
-rw-r--r-- | crates/ra_assists/src/utils/insert_use.rs | 9 |
3 files changed, 3 insertions, 23 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 46fbdb525..c8bf181f9 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -53,7 +53,6 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { | |||
53 | edit.target(auto_import_assets.syntax_under_caret.text_range()); | 53 | edit.target(auto_import_assets.syntax_under_caret.text_range()); |
54 | insert_use_statement( | 54 | insert_use_statement( |
55 | &auto_import_assets.syntax_under_caret, | 55 | &auto_import_assets.syntax_under_caret, |
56 | &auto_import_assets.syntax_under_caret, | ||
57 | &import, | 56 | &import, |
58 | edit.text_edit_builder(), | 57 | edit.text_edit_builder(), |
59 | ); | 58 | ); |
diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index 44f3f5e7f..94f5d6c50 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -33,26 +33,12 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist> | |||
33 | return None; | 33 | return None; |
34 | } | 34 | } |
35 | 35 | ||
36 | let module = path.syntax().ancestors().find_map(ast::Module::cast); | ||
37 | let position = match module.and_then(|it| it.item_list()) { | ||
38 | Some(item_list) => item_list.syntax().clone(), | ||
39 | None => { | ||
40 | let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; | ||
41 | current_file.syntax().clone() | ||
42 | } | ||
43 | }; | ||
44 | |||
45 | ctx.add_assist( | 36 | ctx.add_assist( |
46 | AssistId("replace_qualified_name_with_use"), | 37 | AssistId("replace_qualified_name_with_use"), |
47 | "Replace qualified path with use", | 38 | "Replace qualified path with use", |
48 | |edit| { | 39 | |edit| { |
49 | let path_to_import = hir_path.mod_path().clone(); | 40 | let path_to_import = hir_path.mod_path().clone(); |
50 | insert_use_statement( | 41 | insert_use_statement(path.syntax(), &path_to_import, edit.text_edit_builder()); |
51 | &position, | ||
52 | &path.syntax(), | ||
53 | &path_to_import, | ||
54 | edit.text_edit_builder(), | ||
55 | ); | ||
56 | 42 | ||
57 | if let Some(last) = path.segment() { | 43 | if let Some(last) = path.segment() { |
58 | // Here we are assuming the assist will provide a correct use statement | 44 | // Here we are assuming the assist will provide a correct use statement |
diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs index 7ae3440ca..36fd2fc0b 100644 --- a/crates/ra_assists/src/utils/insert_use.rs +++ b/crates/ra_assists/src/utils/insert_use.rs | |||
@@ -15,8 +15,6 @@ use ra_text_edit::TextEditBuilder; | |||
15 | pub fn insert_use_statement( | 15 | pub fn insert_use_statement( |
16 | // Ideally the position of the cursor, used to | 16 | // Ideally the position of the cursor, used to |
17 | position: &SyntaxNode, | 17 | position: &SyntaxNode, |
18 | // The statement to use as anchor (last resort) | ||
19 | anchor: &SyntaxNode, | ||
20 | path_to_import: &ModPath, | 18 | path_to_import: &ModPath, |
21 | edit: &mut TextEditBuilder, | 19 | edit: &mut TextEditBuilder, |
22 | ) { | 20 | ) { |
@@ -29,7 +27,7 @@ pub fn insert_use_statement( | |||
29 | }); | 27 | }); |
30 | 28 | ||
31 | if let Some(container) = container { | 29 | if let Some(container) = container { |
32 | let action = best_action_for_target(container, anchor.clone(), &target); | 30 | let action = best_action_for_target(container, position.clone(), &target); |
33 | make_assist(&action, &target, edit); | 31 | make_assist(&action, &target, edit); |
34 | } | 32 | } |
35 | } | 33 | } |
@@ -379,10 +377,7 @@ fn best_action_for_target( | |||
379 | // another item and we use it as anchor. | 377 | // another item and we use it as anchor. |
380 | // If there are no items above, we choose the target path itself as anchor. | 378 | // If there are no items above, we choose the target path itself as anchor. |
381 | // todo: we should include even whitespace blocks as anchor candidates | 379 | // todo: we should include even whitespace blocks as anchor candidates |
382 | let anchor = container | 380 | let anchor = container.children().next().or_else(|| Some(anchor)); |
383 | .children() | ||
384 | .find(|n| n.text_range().start() < anchor.text_range().start()) | ||
385 | .or_else(|| Some(anchor)); | ||
386 | 381 | ||
387 | let add_after_anchor = anchor | 382 | let add_after_anchor = anchor |
388 | .clone() | 383 | .clone() |