From a173e31890c1eb03d9d4c123986baae4154cd4fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 16:40:28 +0100 Subject: Make assists use ImportsLocator directly --- crates/ra_assists/Cargo.toml | 1 + crates/ra_assists/src/lib.rs | 18 ++++++------- crates/ra_ide/src/assists.rs | 4 +-- crates/ra_ide_db/Cargo.toml | 1 - crates/ra_ide_db/src/imports_locator.rs | 45 +++++++++++++++------------------ 5 files changed, 33 insertions(+), 36 deletions(-) (limited to 'crates') 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" } ra_fmt = { path = "../ra_fmt" } ra_prof = { path = "../ra_prof" } ra_db = { path = "../ra_db" } +ra_ide_db = { path = "../ra_ide_db" } hir = { path = "../ra_hir", package = "ra_hir" } 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; use either::Either; use hir::{db::HirDatabase, ModuleDef}; use ra_db::FileRange; +use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; use ra_syntax::{TextRange, TextUnit}; use ra_text_edit::TextEdit; @@ -88,20 +89,19 @@ pub trait ImportsLocator { fn find_imports(&mut self, name_to_import: &str) -> Vec; } +impl ImportsLocator for ImportsLocatorIde<'_> { + fn find_imports(&mut self, name_to_import: &str) -> Vec { + self.find_imports(name_to_import) + } +} + /// Return all the assists applicable at the given position /// and additional assists that need the imports locator functionality to work. /// /// Assists are returned in the "resolved" state, that is with edit fully /// computed. -pub fn assists_with_imports_locator( - db: &H, - range: FileRange, - mut imports_locator: F, -) -> Vec -where - H: HirDatabase + 'static, - F: ImportsLocator, -{ +pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec { + let mut imports_locator = ImportsLocatorIde::new(db); AssistCtx::with_ctx(db, range, true, |ctx| { let mut assists = assists::all() .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 @@ use either::Either; use ra_assists::{AssistAction, AssistLabel}; use ra_db::{FilePosition, FileRange}; -use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; +use ra_ide_db::RootDatabase; use crate::{FileId, SourceChange, SourceFileEdit}; @@ -17,7 +17,7 @@ pub struct Assist { } pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec { - ra_assists::assists_with_imports_locator(db, frange, ImportsLocatorIde::new(db)) + ra_assists::assists_with_imports_locator(db, frange) .into_iter() .map(|assist| { 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" } ra_fmt = { path = "../ra_fmt" } ra_prof = { path = "../ra_prof" } test_utils = { path = "../test_utils" } -ra_assists = { path = "../ra_assists" } # ra_ide should depend only on the top-level `hir` package. if you need # 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 @@ //! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. use hir::{db::HirDatabase, ModuleDef, SourceBinder}; -use ra_assists::ImportsLocator; use ra_prof::profile; use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; @@ -22,29 +21,7 @@ impl<'a> ImportsLocatorIde<'a> { Self { source_binder: SourceBinder::new(db) } } - fn get_name_definition( - &mut self, - db: &impl HirDatabase, - import_candidate: &FileSymbol, - ) -> Option { - let _p = profile("get_name_definition"); - let file_id = import_candidate.file_id.into(); - let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); - let candidate_name_node = if candidate_node.kind() != NAME { - candidate_node.children().find(|it| it.kind() == NAME)? - } else { - candidate_node - }; - classify_name( - &mut self.source_binder, - hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, - ) - .map(|it| it.kind) - } -} - -impl ImportsLocator for ImportsLocatorIde<'_> { - fn find_imports(&mut self, name_to_import: &str) -> Vec { + pub fn find_imports(&mut self, name_to_import: &str) -> Vec { let _p = profile("search_for_imports"); let db = self.source_binder.db; @@ -72,4 +49,24 @@ impl ImportsLocator for ImportsLocatorIde<'_> { }) .collect() } + + fn get_name_definition( + &mut self, + db: &impl HirDatabase, + import_candidate: &FileSymbol, + ) -> Option { + let _p = profile("get_name_definition"); + let file_id = import_candidate.file_id.into(); + let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); + let candidate_name_node = if candidate_node.kind() != NAME { + candidate_node.children().find(|it| it.kind() == NAME)? + } else { + candidate_node + }; + classify_name( + &mut self.source_binder, + hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, + ) + .map(|it| it.kind) + } } -- cgit v1.2.3