diff options
author | Aleksey Kladov <[email protected]> | 2020-02-06 15:40:28 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-06 15:40:28 +0000 |
commit | a173e31890c1eb03d9d4c123986baae4154cd4fa (patch) | |
tree | a2b4de01cd6128b70f0fc0cf4156b6537304ae56 /crates | |
parent | ff0f0fc31e84deb7aebf8ab6ec510051d531c9f2 (diff) |
Make assists use ImportsLocator directly
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 18 | ||||
-rw-r--r-- | crates/ra_ide/src/assists.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide_db/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_ide_db/src/imports_locator.rs | 45 |
5 files changed, 33 insertions, 36 deletions
diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 9d3091b91..6973038d4 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml | |||
@@ -18,5 +18,6 @@ ra_text_edit = { path = "../ra_text_edit" } | |||
18 | ra_fmt = { path = "../ra_fmt" } | 18 | ra_fmt = { path = "../ra_fmt" } |
19 | ra_prof = { path = "../ra_prof" } | 19 | ra_prof = { path = "../ra_prof" } |
20 | ra_db = { path = "../ra_db" } | 20 | ra_db = { path = "../ra_db" } |
21 | ra_ide_db = { path = "../ra_ide_db" } | ||
21 | hir = { path = "../ra_hir", package = "ra_hir" } | 22 | hir = { path = "../ra_hir", package = "ra_hir" } |
22 | test_utils = { path = "../test_utils" } | 23 | test_utils = { path = "../test_utils" } |
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 625ebc4a2..0ebb8e8b0 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -16,6 +16,7 @@ pub mod ast_transform; | |||
16 | use either::Either; | 16 | use either::Either; |
17 | use hir::{db::HirDatabase, ModuleDef}; | 17 | use hir::{db::HirDatabase, ModuleDef}; |
18 | use ra_db::FileRange; | 18 | use ra_db::FileRange; |
19 | use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; | ||
19 | use ra_syntax::{TextRange, TextUnit}; | 20 | use ra_syntax::{TextRange, TextUnit}; |
20 | use ra_text_edit::TextEdit; | 21 | use ra_text_edit::TextEdit; |
21 | 22 | ||
@@ -88,20 +89,19 @@ pub trait ImportsLocator { | |||
88 | fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>; | 89 | fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>; |
89 | } | 90 | } |
90 | 91 | ||
92 | impl ImportsLocator for ImportsLocatorIde<'_> { | ||
93 | fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { | ||
94 | self.find_imports(name_to_import) | ||
95 | } | ||
96 | } | ||
97 | |||
91 | /// Return all the assists applicable at the given position | 98 | /// Return all the assists applicable at the given position |
92 | /// and additional assists that need the imports locator functionality to work. | 99 | /// and additional assists that need the imports locator functionality to work. |
93 | /// | 100 | /// |
94 | /// Assists are returned in the "resolved" state, that is with edit fully | 101 | /// Assists are returned in the "resolved" state, that is with edit fully |
95 | /// computed. | 102 | /// computed. |
96 | pub fn assists_with_imports_locator<H, F>( | 103 | pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { |
97 | db: &H, | 104 | let mut imports_locator = ImportsLocatorIde::new(db); |
98 | range: FileRange, | ||
99 | mut imports_locator: F, | ||
100 | ) -> Vec<ResolvedAssist> | ||
101 | where | ||
102 | H: HirDatabase + 'static, | ||
103 | F: ImportsLocator, | ||
104 | { | ||
105 | AssistCtx::with_ctx(db, range, true, |ctx| { | 105 | AssistCtx::with_ctx(db, range, true, |ctx| { |
106 | let mut assists = assists::all() | 106 | let mut assists = assists::all() |
107 | .iter() | 107 | .iter() |
diff --git a/crates/ra_ide/src/assists.rs b/crates/ra_ide/src/assists.rs index 4a7d8cfa9..6ee617e79 100644 --- a/crates/ra_ide/src/assists.rs +++ b/crates/ra_ide/src/assists.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | use either::Either; | 3 | use either::Either; |
4 | use ra_assists::{AssistAction, AssistLabel}; | 4 | use ra_assists::{AssistAction, AssistLabel}; |
5 | use ra_db::{FilePosition, FileRange}; | 5 | use ra_db::{FilePosition, FileRange}; |
6 | use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; | 6 | use ra_ide_db::RootDatabase; |
7 | 7 | ||
8 | use crate::{FileId, SourceChange, SourceFileEdit}; | 8 | use crate::{FileId, SourceChange, SourceFileEdit}; |
9 | 9 | ||
@@ -17,7 +17,7 @@ pub struct Assist { | |||
17 | } | 17 | } |
18 | 18 | ||
19 | pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> { | 19 | pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> { |
20 | ra_assists::assists_with_imports_locator(db, frange, ImportsLocatorIde::new(db)) | 20 | ra_assists::assists_with_imports_locator(db, frange) |
21 | .into_iter() | 21 | .into_iter() |
22 | .map(|assist| { | 22 | .map(|assist| { |
23 | let file_id = frange.file_id; | 23 | let file_id = frange.file_id; |
diff --git a/crates/ra_ide_db/Cargo.toml b/crates/ra_ide_db/Cargo.toml index 1b7905eb3..716e88bc1 100644 --- a/crates/ra_ide_db/Cargo.toml +++ b/crates/ra_ide_db/Cargo.toml | |||
@@ -32,7 +32,6 @@ ra_cfg = { path = "../ra_cfg" } | |||
32 | ra_fmt = { path = "../ra_fmt" } | 32 | ra_fmt = { path = "../ra_fmt" } |
33 | ra_prof = { path = "../ra_prof" } | 33 | ra_prof = { path = "../ra_prof" } |
34 | test_utils = { path = "../test_utils" } | 34 | test_utils = { path = "../test_utils" } |
35 | ra_assists = { path = "../ra_assists" } | ||
36 | 35 | ||
37 | # ra_ide should depend only on the top-level `hir` package. if you need | 36 | # ra_ide should depend only on the top-level `hir` package. if you need |
38 | # something from some `hir_xxx` subpackage, reexport the API via `hir`. | 37 | # something from some `hir_xxx` subpackage, reexport the API via `hir`. |
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs index 21e637608..d77fc53f3 100644 --- a/crates/ra_ide_db/src/imports_locator.rs +++ b/crates/ra_ide_db/src/imports_locator.rs | |||
@@ -2,7 +2,6 @@ | |||
2 | //! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. | 2 | //! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. |
3 | 3 | ||
4 | use hir::{db::HirDatabase, ModuleDef, SourceBinder}; | 4 | use hir::{db::HirDatabase, ModuleDef, SourceBinder}; |
5 | use ra_assists::ImportsLocator; | ||
6 | use ra_prof::profile; | 5 | use ra_prof::profile; |
7 | use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; | 6 | use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; |
8 | 7 | ||
@@ -22,29 +21,7 @@ impl<'a> ImportsLocatorIde<'a> { | |||
22 | Self { source_binder: SourceBinder::new(db) } | 21 | Self { source_binder: SourceBinder::new(db) } |
23 | } | 22 | } |
24 | 23 | ||
25 | fn get_name_definition( | 24 | pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { |
26 | &mut self, | ||
27 | db: &impl HirDatabase, | ||
28 | import_candidate: &FileSymbol, | ||
29 | ) -> Option<NameKind> { | ||
30 | let _p = profile("get_name_definition"); | ||
31 | let file_id = import_candidate.file_id.into(); | ||
32 | let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); | ||
33 | let candidate_name_node = if candidate_node.kind() != NAME { | ||
34 | candidate_node.children().find(|it| it.kind() == NAME)? | ||
35 | } else { | ||
36 | candidate_node | ||
37 | }; | ||
38 | classify_name( | ||
39 | &mut self.source_binder, | ||
40 | hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, | ||
41 | ) | ||
42 | .map(|it| it.kind) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | impl ImportsLocator for ImportsLocatorIde<'_> { | ||
47 | fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { | ||
48 | let _p = profile("search_for_imports"); | 25 | let _p = profile("search_for_imports"); |
49 | let db = self.source_binder.db; | 26 | let db = self.source_binder.db; |
50 | 27 | ||
@@ -72,4 +49,24 @@ impl ImportsLocator for ImportsLocatorIde<'_> { | |||
72 | }) | 49 | }) |
73 | .collect() | 50 | .collect() |
74 | } | 51 | } |
52 | |||
53 | fn get_name_definition( | ||
54 | &mut self, | ||
55 | db: &impl HirDatabase, | ||
56 | import_candidate: &FileSymbol, | ||
57 | ) -> Option<NameKind> { | ||
58 | let _p = profile("get_name_definition"); | ||
59 | let file_id = import_candidate.file_id.into(); | ||
60 | let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); | ||
61 | let candidate_name_node = if candidate_node.kind() != NAME { | ||
62 | candidate_node.children().find(|it| it.kind() == NAME)? | ||
63 | } else { | ||
64 | candidate_node | ||
65 | }; | ||
66 | classify_name( | ||
67 | &mut self.source_binder, | ||
68 | hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, | ||
69 | ) | ||
70 | .map(|it| it.kind) | ||
71 | } | ||
75 | } | 72 | } |