Skip to content

Commit f320c38

Browse files
bors[bot]Jonas Schievink
andauthored
Merge #4819
4819: Add an FST index to `ImportMap` and use it to speed up auto import r=matklad a=jonas-schievink For the importing crate, we still use the symbol index, but I've modified it to only look at files that comprise that crate (instead of the whole workspace). Oh, and since now the symbol query limit is respected correctly, it's possible that some results from the local crate now disappear if there are many matches. Fixes #4763 Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
2 parents dfbd81e + 6766a6b commit f320c38

File tree

7 files changed

+537
-44
lines changed

7 files changed

+537
-44
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_assists/src/handlers/auto_import.rs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl AutoImportAssets {
130130
fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> {
131131
let _p = profile("auto_import::search_for_imports");
132132
let current_crate = self.module_with_name_to_import.krate();
133-
ImportsLocator::new(db)
133+
ImportsLocator::new(db, current_crate)
134134
.find_imports(&self.get_search_query())
135135
.into_iter()
136136
.filter_map(|candidate| match &self.import_candidate {
@@ -841,4 +841,105 @@ fn main() {
841841
",
842842
)
843843
}
844+
845+
#[test]
846+
fn dep_import() {
847+
check_assist(
848+
auto_import,
849+
r"
850+
//- /lib.rs crate:dep
851+
pub struct Struct;
852+
853+
//- /main.rs crate:main deps:dep
854+
fn main() {
855+
Struct<|>
856+
}",
857+
r"use dep::Struct;
858+
859+
fn main() {
860+
Struct
861+
}
862+
",
863+
);
864+
}
865+
866+
#[test]
867+
fn whole_segment() {
868+
// Tests that only imports whose last segment matches the identifier get suggested.
869+
check_assist(
870+
auto_import,
871+
r"
872+
//- /lib.rs crate:dep
873+
pub mod fmt {
874+
pub trait Display {}
875+
}
876+
877+
pub fn panic_fmt() {}
878+
879+
//- /main.rs crate:main deps:dep
880+
struct S;
881+
882+
impl f<|>mt::Display for S {}",
883+
r"use dep::fmt;
884+
885+
struct S;
886+
impl fmt::Display for S {}
887+
",
888+
);
889+
}
890+
891+
#[test]
892+
fn macro_generated() {
893+
// Tests that macro-generated items are suggested from external crates.
894+
check_assist(
895+
auto_import,
896+
r"
897+
//- /lib.rs crate:dep
898+
899+
macro_rules! mac {
900+
() => {
901+
pub struct Cheese;
902+
};
903+
}
904+
905+
mac!();
906+
907+
//- /main.rs crate:main deps:dep
908+
909+
fn main() {
910+
Cheese<|>;
911+
}",
912+
r"use dep::Cheese;
913+
914+
fn main() {
915+
Cheese;
916+
}
917+
",
918+
);
919+
}
920+
921+
#[test]
922+
fn casing() {
923+
// Tests that differently cased names don't interfere and we only suggest the matching one.
924+
check_assist(
925+
auto_import,
926+
r"
927+
//- /lib.rs crate:dep
928+
929+
pub struct FMT;
930+
pub struct fmt;
931+
932+
//- /main.rs crate:main deps:dep
933+
934+
fn main() {
935+
FMT<|>;
936+
}",
937+
r"use dep::FMT;
938+
939+
fn main() {
940+
FMT;
941+
}
942+
",
943+
);
944+
}
844945
}

crates/ra_hir/src/code_model.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use hir_def::{
99
builtin_type::BuiltinType,
1010
docs::Documentation,
1111
expr::{BindingAnnotation, Pat, PatId},
12+
import_map,
1213
per_ns::PerNs,
1314
resolver::{HasResolver, Resolver},
1415
type_ref::{Mutability, TypeRef},
@@ -98,6 +99,23 @@ impl Crate {
9899
db.crate_graph()[self.id].display_name.as_ref().cloned()
99100
}
100101

102+
pub fn query_external_importables(
103+
self,
104+
db: &dyn DefDatabase,
105+
query: &str,
106+
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
107+
import_map::search_dependencies(
108+
db,
109+
self.into(),
110+
import_map::Query::new(query).anchor_end().case_sensitive().limit(40),
111+
)
112+
.into_iter()
113+
.map(|item| match item {
114+
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()),
115+
ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()),
116+
})
117+
}
118+
101119
pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
102120
db.crate_graph().iter().map(|id| Crate { id }).collect()
103121
}

crates/ra_hir_def/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ rustc-hash = "1.1.0"
1414
either = "1.5.3"
1515
anymap = "0.12.1"
1616
drop_bomb = "0.1.4"
17+
fst = { version = "0.4", default-features = false }
18+
itertools = "0.9.0"
19+
indexmap = "1.4.0"
1720

1821
stdx = { path = "../stdx" }
1922

0 commit comments

Comments
 (0)