ImportsLocator: use ImportMap for non-local crates

This commit is contained in:
Jonas Schievink 2020-06-09 18:48:44 +02:00
parent b01fb22494
commit a70a0ca73c
2 changed files with 70 additions and 23 deletions

View File

@ -130,7 +130,7 @@ impl AutoImportAssets {
fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> {
let _p = profile("auto_import::search_for_imports");
let current_crate = self.module_with_name_to_import.krate();
ImportsLocator::new(db)
ImportsLocator::new(db, current_crate)
.find_imports(&self.get_search_query())
.into_iter()
.filter_map(|candidate| match &self.import_candidate {
@ -841,4 +841,49 @@ fn main() {
",
)
}
#[test]
fn dep_import() {
check_assist(
auto_import,
r"
//- /lib.rs crate:dep
pub struct Struct;
//- /main.rs crate:main deps:dep
fn main() {
Struct<|>
}",
r"use dep::Struct;
fn main() {
Struct
}
",
);
}
#[test]
fn whole_segment() {
check_assist(
auto_import,
r"
//- /lib.rs crate:dep
pub mod fmt {
pub trait Display {}
}
pub fn panic_fmt() {}
//- /main.rs crate:main deps:dep
struct S;
impl f<|>mt::Display for S {}",
r"use dep::fmt;
struct S;
impl fmt::Display for S {}
",
);
}
}

View File

@ -1,7 +1,7 @@
//! This module contains an import search funcionality that is provided to the ra_assists module.
//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module.
use hir::{MacroDef, ModuleDef, Semantics};
use hir::{Crate, MacroDef, ModuleDef, Semantics};
use ra_prof::profile;
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
@ -11,44 +11,46 @@ use crate::{
RootDatabase,
};
use either::Either;
use rustc_hash::FxHashSet;
pub struct ImportsLocator<'a> {
sema: Semantics<'a, RootDatabase>,
krate: Crate,
}
impl<'a> ImportsLocator<'a> {
pub fn new(db: &'a RootDatabase) -> Self {
Self { sema: Semantics::new(db) }
pub fn new(db: &'a RootDatabase, krate: Crate) -> Self {
Self { sema: Semantics::new(db), krate }
}
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> {
let _p = profile("search_for_imports");
let db = self.sema.db;
let project_results = {
// Query dependencies first.
let mut candidates: FxHashSet<_> =
self.krate.query_external_importables(db, name_to_import).collect();
// Query the local crate using the symbol index.
let local_results = {
let mut query = Query::new(name_to_import.to_string());
query.exact();
query.limit(40);
symbol_index::world_symbols(db, query)
};
let lib_results = {
let mut query = Query::new(name_to_import.to_string());
query.libs();
query.exact();
query.limit(40);
symbol_index::world_symbols(db, query)
symbol_index::crate_symbols(db, self.krate.into(), query)
};
project_results
.into_iter()
.chain(lib_results.into_iter())
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
.filter_map(|name_definition_to_import| match name_definition_to_import {
Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
_ => None,
})
.collect()
candidates.extend(
local_results
.into_iter()
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
.filter_map(|name_definition_to_import| match name_definition_to_import {
Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
_ => None,
}),
);
candidates.into_iter().collect()
}
fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> {