mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
ImportsLocator: use ImportMap for non-local crates
This commit is contained in:
parent
b01fb22494
commit
a70a0ca73c
@ -130,7 +130,7 @@ impl AutoImportAssets {
|
|||||||
fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> {
|
fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> {
|
||||||
let _p = profile("auto_import::search_for_imports");
|
let _p = profile("auto_import::search_for_imports");
|
||||||
let current_crate = self.module_with_name_to_import.krate();
|
let current_crate = self.module_with_name_to_import.krate();
|
||||||
ImportsLocator::new(db)
|
ImportsLocator::new(db, current_crate)
|
||||||
.find_imports(&self.get_search_query())
|
.find_imports(&self.get_search_query())
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|candidate| match &self.import_candidate {
|
.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 {}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! This module contains an import search funcionality that is provided to the ra_assists module.
|
//! 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.
|
//! 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_prof::profile;
|
||||||
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
|
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
|
||||||
|
|
||||||
@ -11,44 +11,46 @@ use crate::{
|
|||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use either::Either;
|
use either::Either;
|
||||||
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
pub struct ImportsLocator<'a> {
|
pub struct ImportsLocator<'a> {
|
||||||
sema: Semantics<'a, RootDatabase>,
|
sema: Semantics<'a, RootDatabase>,
|
||||||
|
krate: Crate,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ImportsLocator<'a> {
|
impl<'a> ImportsLocator<'a> {
|
||||||
pub fn new(db: &'a RootDatabase) -> Self {
|
pub fn new(db: &'a RootDatabase, krate: Crate) -> Self {
|
||||||
Self { sema: Semantics::new(db) }
|
Self { sema: Semantics::new(db), krate }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> {
|
pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> {
|
||||||
let _p = profile("search_for_imports");
|
let _p = profile("search_for_imports");
|
||||||
let db = self.sema.db;
|
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());
|
let mut query = Query::new(name_to_import.to_string());
|
||||||
query.exact();
|
query.exact();
|
||||||
query.limit(40);
|
query.limit(40);
|
||||||
symbol_index::world_symbols(db, query)
|
symbol_index::crate_symbols(db, self.krate.into(), 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)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
project_results
|
candidates.extend(
|
||||||
.into_iter()
|
local_results
|
||||||
.chain(lib_results.into_iter())
|
.into_iter()
|
||||||
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
|
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
|
||||||
.filter_map(|name_definition_to_import| match name_definition_to_import {
|
.filter_map(|name_definition_to_import| match name_definition_to_import {
|
||||||
Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
|
Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
|
||||||
Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
|
Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
}),
|
||||||
.collect()
|
);
|
||||||
|
|
||||||
|
candidates.into_iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> {
|
fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> {
|
||||||
|
Loading…
Reference in New Issue
Block a user