Make assists use ImportsLocator directly

This commit is contained in:
Aleksey Kladov 2020-02-06 16:40:28 +01:00
parent ff0f0fc31e
commit a173e31890
6 changed files with 34 additions and 37 deletions

2
Cargo.lock generated
View File

@ -963,6 +963,7 @@ dependencies = [
"ra_db",
"ra_fmt",
"ra_hir",
"ra_ide_db",
"ra_prof",
"ra_syntax",
"ra_text_edit",
@ -1165,7 +1166,6 @@ dependencies = [
"log",
"once_cell",
"proptest",
"ra_assists",
"ra_cfg",
"ra_db",
"ra_fmt",

View File

@ -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" }

View File

@ -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<ModuleDef>;
}
impl ImportsLocator for ImportsLocatorIde<'_> {
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
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<H, F>(
db: &H,
range: FileRange,
mut imports_locator: F,
) -> Vec<ResolvedAssist>
where
H: HirDatabase + 'static,
F: ImportsLocator,
{
pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
let mut imports_locator = ImportsLocatorIde::new(db);
AssistCtx::with_ctx(db, range, true, |ctx| {
let mut assists = assists::all()
.iter()

View File

@ -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<Assist> {
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;

View File

@ -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`.

View File

@ -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<NameKind> {
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<ModuleDef> {
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
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<NameKind> {
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)
}
}