diff options
author | Kirill Bulatov <[email protected]> | 2020-02-03 11:56:03 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-02-03 11:56:03 +0000 |
commit | 4f27155d5cf6b12ed0df6f7dd430dcddda9e41d2 (patch) | |
tree | 393726f2a137e27a609a1859ab5b5094468ed6b1 | |
parent | fcf5bbbbeb1ab48e80d45483c810bae708f848a1 (diff) |
Simplify paths searches
-rw-r--r-- | crates/ra_assists/src/assists/auto_import.rs | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 7fd1f0e7d..e908ab1fc 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use hir::{db::HirDatabase, ModPath}; | 1 | use hir::{db::HirDatabase, ModPath}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode}, | 3 | ast::{self, AstNode}, |
4 | SyntaxKind::{NAME_REF, USE_ITEM}, | ||
5 | SyntaxNode, | 4 | SyntaxNode, |
6 | }; | 5 | }; |
7 | 6 | ||
@@ -33,11 +32,11 @@ pub(crate) fn auto_import<F: ImportsLocator>( | |||
33 | ) -> Option<Assist> { | 32 | ) -> Option<Assist> { |
34 | let path_to_import: ast::Path = ctx.find_node_at_offset()?; | 33 | let path_to_import: ast::Path = ctx.find_node_at_offset()?; |
35 | let path_to_import_syntax = path_to_import.syntax(); | 34 | let path_to_import_syntax = path_to_import.syntax(); |
36 | if path_to_import_syntax.ancestors().find(|ancestor| ancestor.kind() == USE_ITEM).is_some() { | 35 | if path_to_import_syntax.ancestors().find_map(ast::UseItem::cast).is_some() { |
37 | return None; | 36 | return None; |
38 | } | 37 | } |
39 | let name_to_import = | 38 | let name_to_import = |
40 | path_to_import_syntax.descendants().find(|child| child.kind() == NAME_REF)?; | 39 | path_to_import_syntax.descendants().find_map(ast::NameRef::cast)?.syntax().to_string(); |
41 | 40 | ||
42 | let module = path_to_import_syntax.ancestors().find_map(ast::Module::cast); | 41 | let module = path_to_import_syntax.ancestors().find_map(ast::Module::cast); |
43 | let position = match module.and_then(|it| it.item_list()) { | 42 | let position = match module.and_then(|it| it.item_list()) { |
@@ -54,7 +53,7 @@ pub(crate) fn auto_import<F: ImportsLocator>( | |||
54 | } | 53 | } |
55 | 54 | ||
56 | let proposed_imports = imports_locator | 55 | let proposed_imports = imports_locator |
57 | .find_imports(&name_to_import.to_string()) | 56 | .find_imports(&name_to_import) |
58 | .into_iter() | 57 | .into_iter() |
59 | .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) | 58 | .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) |
60 | .filter(|use_path| !use_path.segments.is_empty()) | 59 | .filter(|use_path| !use_path.segments.is_empty()) |