aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-01-24 07:33:18 +0000
committerKirill Bulatov <[email protected]>2020-01-26 22:17:10 +0000
commitd0a782ef1c53ef5c5d3b49b02c498f7a688c3a4d (patch)
treed0fe872ed30d97994efd3ceac708719ce71ae98a /crates/ra_assists
parentbef5cf0b9929539e8d9fece006bfd3db1b68bec4 (diff)
Have a better trait interface
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/assists/auto_import.rs26
-rw-r--r--crates/ra_assists/src/lib.rs10
2 files changed, 19 insertions, 17 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 @@
1use hir::db::HirDatabase; 1use hir::{db::HirDatabase, AsName};
2use ra_syntax::{ 2use 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}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 381a51df6..9e4ebec47 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -14,9 +14,9 @@ mod test_db;
14pub mod ast_transform; 14pub mod ast_transform;
15 15
16use either::Either; 16use either::Either;
17use hir::{db::HirDatabase, InFile, ModPath, Module}; 17use hir::{db::HirDatabase, ModuleDef};
18use ra_db::FileRange; 18use ra_db::FileRange;
19use ra_syntax::{ast::NameRef, TextRange, TextUnit}; 19use ra_syntax::{TextRange, TextUnit};
20use ra_text_edit::TextEdit; 20use ra_text_edit::TextEdit;
21 21
22pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; 22pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
@@ -85,11 +85,7 @@ where
85/// accessible from the ra_assists crate. 85/// accessible from the ra_assists crate.
86pub trait ImportsLocator { 86pub trait ImportsLocator {
87 /// Finds all imports for the given name and the module that contains this name. 87 /// Finds all imports for the given name and the module that contains this name.
88 fn find_imports( 88 fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>;
89 &mut self,
90 name_to_import: InFile<&NameRef>,
91 module_with_name_to_import: Module,
92 ) -> Option<Vec<ModPath>>;
93} 89}
94 90
95/// Return all the assists applicable at the given position 91/// Return all the assists applicable at the given position