diff options
author | Kirill Bulatov <[email protected]> | 2020-01-24 07:33:18 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-01-26 22:17:10 +0000 |
commit | d0a782ef1c53ef5c5d3b49b02c498f7a688c3a4d (patch) | |
tree | d0fe872ed30d97994efd3ceac708719ce71ae98a /crates/ra_assists/src/assists | |
parent | bef5cf0b9929539e8d9fece006bfd3db1b68bec4 (diff) |
Have a better trait interface
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r-- | crates/ra_assists/src/assists/auto_import.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index ae216944b..d196f6c5c 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::db::HirDatabase; | 1 | use hir::{db::HirDatabase, AsName}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode}, | 3 | ast::{self, AstNode}, |
4 | SmolStr, SyntaxElement, | 4 | SmolStr, SyntaxElement, |
@@ -41,15 +41,21 @@ pub(crate) fn auto_import<F: ImportsLocator>( | |||
41 | current_file.syntax().clone() | 41 | current_file.syntax().clone() |
42 | } | 42 | } |
43 | }; | 43 | }; |
44 | let source_analyzer = ctx.source_analyzer(&position, None); | ||
45 | let module_with_name_to_import = source_analyzer.module()?; | ||
46 | let path_to_import = ctx.covering_element().ancestors().find_map(ast::Path::cast)?; | ||
47 | if source_analyzer.resolve_path(ctx.db, &path_to_import).is_some() { | ||
48 | return None; | ||
49 | } | ||
44 | 50 | ||
45 | let module_with_name_to_import = ctx.source_analyzer(&position, None).module()?; | 51 | let name_to_import = &find_applicable_name_ref(ctx.covering_element())?.as_name(); |
46 | let name_to_import = hir::InFile { | 52 | let proposed_imports = imports_locator |
47 | file_id: ctx.frange.file_id.into(), | 53 | .find_imports(&name_to_import.to_string()) |
48 | value: &find_applicable_name_ref(ctx.covering_element())?, | 54 | .into_iter() |
49 | }; | 55 | .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) |
50 | 56 | .filter(|use_path| !use_path.segments.is_empty()) | |
51 | let proposed_imports = | 57 | .take(20) |
52 | imports_locator.find_imports(name_to_import, module_with_name_to_import)?; | 58 | .collect::<std::collections::HashSet<_>>(); |
53 | if proposed_imports.is_empty() { | 59 | if proposed_imports.is_empty() { |
54 | return None; | 60 | return None; |
55 | } | 61 | } |
@@ -57,7 +63,7 @@ pub(crate) fn auto_import<F: ImportsLocator>( | |||
57 | ctx.add_assist_group(AssistId("auto_import"), "auto import", || { | 63 | ctx.add_assist_group(AssistId("auto_import"), "auto import", || { |
58 | proposed_imports | 64 | proposed_imports |
59 | .into_iter() | 65 | .into_iter() |
60 | .map(|import| import_to_action(import.to_string(), &position, &path)) | 66 | .map(|import| import_to_action(import.to_string(), &position, &path_to_import)) |
61 | .collect() | 67 | .collect() |
62 | }) | 68 | }) |
63 | } | 69 | } |