1 //! A map of all publicly exported items in a crate.
3 use std::{fmt, hash::BuildHasherDefault, sync::Arc};
6 use fst::{self, Streamer};
7 use hir_expand::name::Name;
8 use indexmap::{map::Entry, IndexMap};
9 use itertools::Itertools;
10 use rustc_hash::{FxHashSet, FxHasher};
13 db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
17 type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
19 /// Item import details stored in the `ImportMap`.
20 #[derive(Debug, Clone, Eq, PartialEq)]
21 pub struct ImportInfo {
22 /// A path that can be used to import the item, relative to the crate's root.
24 /// The module containing this item.
25 pub container: ModuleId,
26 /// Whether the import is a trait associated item or not.
27 pub is_trait_assoc_item: bool,
30 #[derive(Debug, Clone, Eq, PartialEq)]
31 pub struct ImportPath {
32 pub segments: Vec<Name>,
35 impl fmt::Display for ImportPath {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 fmt::Display::fmt(&self.segments.iter().format("::"), f)
42 fn len(&self) -> usize {
47 /// A map from publicly exported items to the path needed to import/name them from a downstream
50 /// Reexports of items are taken into account, ie. if something is exported under multiple
51 /// names, the one with the shortest import path will be used.
53 /// Note that all paths are relative to the containing crate's root, so the crate name still needs
54 /// to be prepended to the `ModPath` before the path is valid.
56 pub struct ImportMap {
57 map: FxIndexMap<ItemInNs, ImportInfo>,
59 /// List of keys stored in `map`, sorted lexicographically by their `ModPath`. Indexed by the
60 /// values returned by running `fst`.
62 /// Since a path can refer to multiple items due to namespacing, we store all items with the
63 /// same path right after each other. This allows us to find all items after the FST gives us
64 /// the index of the first one.
65 importables: Vec<ItemInNs>,
66 fst: fst::Map<Vec<u8>>,
70 pub fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
71 let _p = profile::span("import_map_query");
73 let mut import_map = collect_import_map(db, krate);
75 let mut importables = import_map
78 .map(|(item, info)| (item, fst_path(&info.path)))
80 importables.sort_by(|(_, fst_path), (_, fst_path2)| fst_path.cmp(fst_path2));
82 // Build the FST, taking care not to insert duplicate values.
84 let mut builder = fst::MapBuilder::memory();
85 let mut last_batch_start = 0;
87 for idx in 0..importables.len() {
88 let key = &importables[last_batch_start].1;
89 if let Some((_, fst_path)) = importables.get(idx + 1) {
95 let _ = builder.insert(key, last_batch_start as u64);
97 last_batch_start = idx + 1;
100 import_map.fst = builder.into_map();
101 import_map.importables = importables.iter().map(|&(&item, _)| item).collect();
106 /// Returns the `ModPath` needed to import/mention `item`, relative to this crate's root.
107 pub fn path_of(&self, item: ItemInNs) -> Option<&ImportPath> {
108 self.import_info_for(item).map(|it| &it.path)
111 pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
115 fn collect_trait_assoc_items(
117 db: &dyn DefDatabase,
120 original_import_info: &ImportInfo,
122 let _p = profile::span("collect_trait_assoc_items");
123 for (assoc_item_name, item) in &db.trait_data(tr).items {
124 let module_def_id = match item {
125 AssocItemId::FunctionId(f) => ModuleDefId::from(*f),
126 AssocItemId::ConstId(c) => ModuleDefId::from(*c),
127 // cannot use associated type aliases directly: need a `<Struct as Trait>::TypeAlias`
128 // qualifier, ergo no need to store it for imports in import_map
129 AssocItemId::TypeAliasId(_) => {
130 cov_mark::hit!(type_aliases_ignored);
134 let assoc_item = if is_type_in_ns {
135 ItemInNs::Types(module_def_id)
137 ItemInNs::Values(module_def_id)
140 let mut assoc_item_info = original_import_info.clone();
141 assoc_item_info.path.segments.push(assoc_item_name.to_owned());
142 assoc_item_info.is_trait_assoc_item = true;
143 self.map.insert(assoc_item, assoc_item_info);
148 fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
149 let _p = profile::span("collect_import_map");
151 let def_map = db.crate_def_map(krate);
152 let mut import_map = ImportMap::default();
154 // We look only into modules that are public(ly reexported), starting with the crate root.
155 let empty = ImportPath { segments: vec![] };
156 let root = def_map.module_id(def_map.root());
157 let mut worklist = vec![(root, empty)];
158 while let Some((module, mod_path)) = worklist.pop() {
160 let mod_data = if module.krate == krate {
161 &def_map[module.local_id]
163 // The crate might reexport a module defined in another crate.
164 ext_def_map = module.def_map(db);
165 &ext_def_map[module.local_id]
168 let visible_items = mod_data.scope.entries().filter_map(|(name, per_ns)| {
169 let per_ns = per_ns.filter_visibility(|vis| vis == Visibility::Public);
170 if per_ns.is_none() {
177 for (name, per_ns) in visible_items {
179 let mut path = mod_path.clone();
180 path.segments.push(name.clone());
184 for item in per_ns.iter_items() {
185 let path = mk_path();
186 let path_len = path.len();
188 ImportInfo { path, container: module, is_trait_assoc_item: false };
190 if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
191 import_map.collect_trait_assoc_items(
194 matches!(item, ItemInNs::Types(_)),
199 match import_map.map.entry(item) {
200 Entry::Vacant(entry) => {
201 entry.insert(import_info);
203 Entry::Occupied(mut entry) => {
204 // If the new path is shorter, prefer that one.
205 if path_len < entry.get().path.len() {
206 *entry.get_mut() = import_info;
213 // If we've just added a path to a module, descend into it. We might traverse
214 // modules multiple times, but only if the new path to it is shorter than the
215 // first (else we `continue` above).
216 if let Some(ModuleDefId::ModuleId(mod_id)) = item.as_module_def_id() {
217 worklist.push((mod_id, mk_path()));
226 impl PartialEq for ImportMap {
227 fn eq(&self, other: &Self) -> bool {
228 // `fst` and `importables` are built from `map`, so we don't need to compare them.
229 self.map == other.map
233 impl Eq for ImportMap {}
235 impl fmt::Debug for ImportMap {
236 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237 let mut importable_paths: Vec<_> = self
240 .map(|(item, info)| {
241 let ns = match item {
242 ItemInNs::Types(_) => "t",
243 ItemInNs::Values(_) => "v",
244 ItemInNs::Macros(_) => "m",
246 format!("- {} ({})", info.path, ns)
250 importable_paths.sort();
251 f.write_str(&importable_paths.join("\n"))
255 fn fst_path(path: &ImportPath) -> String {
256 let _p = profile::span("fst_path");
257 let mut s = path.to_string();
258 s.make_ascii_lowercase();
262 #[derive(Debug, Eq, PartialEq, Hash)]
263 pub enum ImportKind {
276 /// A way to match import map contents against the search query.
278 pub enum SearchMode {
279 /// Import map entry should strictly match the query string.
281 /// Import map entry should contain the query string.
283 /// Import map entry should contain all letters from the query string,
284 /// in the same order, but not necessary adjacent.
293 assoc_items_only: bool,
294 search_mode: SearchMode,
295 case_sensitive: bool,
297 exclude_import_kinds: FxHashSet<ImportKind>,
301 pub fn new(query: String) -> Self {
302 let lowercased = query.to_lowercase();
307 assoc_items_only: false,
308 search_mode: SearchMode::Contains,
309 case_sensitive: false,
310 limit: usize::max_value(),
311 exclude_import_kinds: FxHashSet::default(),
315 /// Matches entries' names only, ignoring the rest of
317 /// Example: for `std::marker::PhantomData`, the name is `PhantomData`.
318 pub fn name_only(self) -> Self {
319 Self { name_only: true, ..self }
322 /// Matches only the entries that are associated items, ignoring the rest.
323 pub fn assoc_items_only(self) -> Self {
324 Self { assoc_items_only: true, ..self }
327 /// Specifies the way to search for the entries using the query.
328 pub fn search_mode(self, search_mode: SearchMode) -> Self {
329 Self { search_mode, ..self }
332 /// Limits the returned number of items to `limit`.
333 pub fn limit(self, limit: usize) -> Self {
334 Self { limit, ..self }
337 /// Respect casing of the query string when matching.
338 pub fn case_sensitive(self) -> Self {
339 Self { case_sensitive: true, ..self }
342 /// Do not include imports of the specified kind in the search results.
343 pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
344 self.exclude_import_kinds.insert(import_kind);
348 fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
349 let _p = profile::span("import_map::Query::import_matches");
350 if import.is_trait_assoc_item {
351 if self.exclude_import_kinds.contains(&ImportKind::AssociatedItem) {
354 } else if self.assoc_items_only {
358 let mut input = if import.is_trait_assoc_item || self.name_only {
359 import.path.segments.last().unwrap().to_string()
361 import.path.to_string()
363 if enforce_lowercase || !self.case_sensitive {
364 input.make_ascii_lowercase();
368 if !enforce_lowercase && self.case_sensitive { &self.query } else { &self.lowercased };
370 match self.search_mode {
371 SearchMode::Equals => &input == query_string,
372 SearchMode::Contains => input.contains(query_string),
373 SearchMode::Fuzzy => {
374 let mut unchecked_query_chars = query_string.chars();
375 let mut mismatching_query_char = unchecked_query_chars.next();
377 for input_char in input.chars() {
378 match mismatching_query_char {
380 Some(matching_query_char) if matching_query_char == input_char => {
381 mismatching_query_char = unchecked_query_chars.next();
386 mismatching_query_char.is_none()
392 /// Searches dependencies of `krate` for an importable path matching `query`.
394 /// This returns a list of items that could be imported from dependencies of `krate`.
395 pub fn search_dependencies<'a>(
396 db: &'a dyn DefDatabase,
399 ) -> FxHashSet<ItemInNs> {
400 let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query));
402 let graph = db.crate_graph();
403 let import_maps: Vec<_> =
404 graph[krate].dependencies.iter().map(|dep| db.import_map(dep.crate_id)).collect();
406 let automaton = fst::automaton::Subsequence::new(&query.lowercased);
408 let mut op = fst::map::OpBuilder::new();
409 for map in &import_maps {
410 op = op.add(map.fst.search(&automaton));
413 let mut stream = op.union();
415 let mut all_indexed_values = FxHashSet::default();
416 while let Some((_, indexed_values)) = stream.next() {
417 all_indexed_values.extend(indexed_values.iter().copied());
420 let mut res = FxHashSet::default();
421 for indexed_value in all_indexed_values {
422 let import_map = &import_maps[indexed_value.index];
423 let importables = &import_map.importables[indexed_value.value as usize..];
425 let common_importable_data = &import_map.map[&importables[0]];
426 if !query.import_matches(common_importable_data, true) {
430 // Path shared by the importable items in this group.
431 let common_importables_path_fst = fst_path(&common_importable_data.path);
432 // Add the items from this `ModPath` group. Those are all subsequent items in
433 // `importables` whose paths match `path`.
434 let iter = importables
437 .take_while(|item| common_importables_path_fst == fst_path(&import_map.map[item].path))
438 .filter(|&item| match item_import_kind(item) {
439 Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
443 !query.case_sensitive // we've already checked the common importables path case-insensitively
444 || query.import_matches(&import_map.map[item], false)
448 if res.len() >= query.limit {
456 fn item_import_kind(item: ItemInNs) -> Option<ImportKind> {
457 Some(match item.as_module_def_id()? {
458 ModuleDefId::ModuleId(_) => ImportKind::Module,
459 ModuleDefId::FunctionId(_) => ImportKind::Function,
460 ModuleDefId::AdtId(_) => ImportKind::Adt,
461 ModuleDefId::EnumVariantId(_) => ImportKind::EnumVariant,
462 ModuleDefId::ConstId(_) => ImportKind::Const,
463 ModuleDefId::StaticId(_) => ImportKind::Static,
464 ModuleDefId::TraitId(_) => ImportKind::Trait,
465 ModuleDefId::TypeAliasId(_) => ImportKind::TypeAlias,
466 ModuleDefId::BuiltinType(_) => ImportKind::BuiltinType,
472 use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
473 use expect_test::{expect, Expect};
475 use crate::{test_db::TestDB, ItemContainerId, Lookup};
479 fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) {
480 let db = TestDB::with_files(ra_fixture);
481 let crate_graph = db.crate_graph();
482 let krate = crate_graph
485 crate_graph[*krate].display_name.as_ref().map(|n| n.to_string())
486 == Some(crate_name.to_string())
490 let actual = search_dependencies(db.upcast(), krate, query)
492 .filter_map(|dependency| {
493 let dependency_krate = dependency.krate(db.upcast())?;
494 let dependency_imports = db.import_map(dependency_krate);
496 let (path, mark) = match assoc_item_path(&db, &dependency_imports, dependency) {
497 Some(assoc_item_path) => (assoc_item_path, "a"),
499 dependency_imports.path_of(dependency)?.to_string(),
501 ItemInNs::Types(ModuleDefId::FunctionId(_))
502 | ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
503 ItemInNs::Types(_) => "t",
504 ItemInNs::Values(_) => "v",
505 ItemInNs::Macros(_) => "m",
512 crate_graph[dependency_krate].display_name.as_ref()?,
517 .collect::<String>();
518 expect.assert_eq(&actual)
522 db: &dyn DefDatabase,
523 dependency_imports: &ImportMap,
524 dependency: ItemInNs,
525 ) -> Option<String> {
526 let dependency_assoc_item_id = match dependency {
527 ItemInNs::Types(ModuleDefId::FunctionId(id))
528 | ItemInNs::Values(ModuleDefId::FunctionId(id)) => AssocItemId::from(id),
529 ItemInNs::Types(ModuleDefId::ConstId(id))
530 | ItemInNs::Values(ModuleDefId::ConstId(id)) => AssocItemId::from(id),
531 ItemInNs::Types(ModuleDefId::TypeAliasId(id))
532 | ItemInNs::Values(ModuleDefId::TypeAliasId(id)) => AssocItemId::from(id),
536 let trait_ = assoc_to_trait(db, dependency)?;
537 if let ModuleDefId::TraitId(tr) = trait_.as_module_def_id()? {
538 let trait_data = db.trait_data(tr);
539 let assoc_item_name =
540 trait_data.items.iter().find_map(|(assoc_item_name, assoc_item_id)| {
541 if &dependency_assoc_item_id == assoc_item_id {
542 Some(assoc_item_name)
547 return Some(format!("{}::{}", dependency_imports.path_of(trait_)?, assoc_item_name));
552 fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> Option<ItemInNs> {
553 let assoc: AssocItemId = match item {
554 ItemInNs::Types(it) | ItemInNs::Values(it) => match it {
555 ModuleDefId::TypeAliasId(it) => it.into(),
556 ModuleDefId::FunctionId(it) => it.into(),
557 ModuleDefId::ConstId(it) => it.into(),
563 let container = match assoc {
564 AssocItemId::FunctionId(it) => it.lookup(db).container,
565 AssocItemId::ConstId(it) => it.lookup(db).container,
566 AssocItemId::TypeAliasId(it) => it.lookup(db).container,
570 ItemContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
575 fn check(ra_fixture: &str, expect: Expect) {
576 let db = TestDB::with_files(ra_fixture);
577 let crate_graph = db.crate_graph();
579 let actual = crate_graph
581 .filter_map(|krate| {
582 let cdata = &crate_graph[krate];
583 let name = cdata.display_name.as_ref()?;
585 let map = db.import_map(krate);
587 Some(format!("{}:\n{:?}\n", name, map))
589 .collect::<String>();
591 expect.assert_eq(&actual)
598 //- /main.rs crate:main deps:lib
602 pub struct InPrivateModule;
612 pub mod real_pu2 { // same path length as above
616 //- /lib.rs crate:lib
618 pub struct Pub2; // t + v
636 fn prefers_shortest_path() {
639 //- /main.rs crate:main
646 pub use super::sub::subsub::Def;
659 fn type_reexport_cross_crate() {
660 // Reexports need to be visible from a crate, even if the original crate exports the item
661 // at a shorter path.
664 //- /main.rs crate:main deps:lib
668 //- /lib.rs crate:lib
684 fn macro_reexport() {
687 //- /main.rs crate:main deps:lib
689 pub use lib::pub_macro;
691 //- /lib.rs crate:lib
693 macro_rules! pub_macro {
708 fn module_reexport() {
709 // Reexporting modules from a dependency adds all contents to the import map.
712 //- /main.rs crate:main deps:lib
713 pub use lib::module as reexported_module;
714 //- /lib.rs crate:lib
721 - reexported_module (t)
722 - reexported_module::S (t)
723 - reexported_module::S (v)
733 fn cyclic_module_reexport() {
734 // A cyclic reexport does not hang.
737 //- /lib.rs crate:lib
740 pub use super::sub::*;
744 pub use super::module;
761 //- /lib.rs crate:lib
762 macro_rules! private_macro {
777 //- /lib.rs crate:lib
778 pub struct Thing; // t + v
780 macro_rules! Thing { // m
794 //- /lib.rs crate:lib
795 pub mod Thing {} // t
797 macro_rules! Thing { // m
810 fn fuzzy_import_trait_and_assoc_items() {
811 cov_mark::check!(type_aliases_ignored);
813 //- /main.rs crate:main deps:dep
814 //- /dep.rs crate:dep
818 const FMT_CONST: bool;
820 fn format_function();
821 fn format_method(&self);
829 Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
832 dep::fmt::Display::format_method (a)
833 dep::fmt::Display (t)
834 dep::fmt::Display::FMT_CONST (a)
835 dep::fmt::Display::format_function (a)
841 fn assoc_items_filtering() {
843 //- /main.rs crate:main deps:dep
844 //- /dep.rs crate:dep
848 const FMT_CONST: bool;
850 fn format_function();
851 fn format_method(&self);
859 Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy).assoc_items_only(),
861 dep::fmt::Display::format_method (a)
862 dep::fmt::Display::FMT_CONST (a)
863 dep::fmt::Display::format_function (a)
870 Query::new("fmt".to_string())
871 .search_mode(SearchMode::Fuzzy)
872 .exclude_import_kind(ImportKind::AssociatedItem),
875 dep::fmt::Display (t)
882 Query::new("fmt".to_string())
883 .search_mode(SearchMode::Fuzzy)
885 .exclude_import_kind(ImportKind::AssociatedItem),
893 //- /main.rs crate:main deps:dep
894 //- /dep.rs crate:dep deps:tdep
895 use tdep::fmt as fmt_dep;
910 //- /tdep.rs crate:tdep
912 pub struct NotImportableFromMain;
919 Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
926 dep::fmt::Display::fmt (a)
927 dep::fmt::Display (t)
934 Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
940 dep::fmt::Display::fmt (a)
947 Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
953 dep::fmt::Display::fmt (a)
954 dep::fmt::Display (t)
962 //- /main.rs crate:main deps:dep
963 //- /dep.rs crate:dep deps:tdep
964 use tdep::fmt as fmt_dep;
979 //- /tdep.rs crate:tdep
981 pub struct NotImportableFromMain;
988 Query::new("fmt".to_string()),
994 dep::fmt::Display::fmt (a)
995 dep::fmt::Display (t)
1002 Query::new("fmt".to_string()).name_only(),
1008 dep::fmt::Display::fmt (a)
1014 fn search_casing() {
1015 let ra_fixture = r#"
1016 //- /main.rs crate:main deps:dep
1017 //- /dep.rs crate:dep
1026 Query::new("FMT".to_string()),
1038 Query::new("FMT".to_string()).case_sensitive(),
1050 //- /main.rs crate:main deps:dep
1051 //- /dep.rs crate:dep
1067 Query::new("".to_string()).limit(2),
1078 fn search_exclusions() {
1079 let ra_fixture = r#"
1080 //- /main.rs crate:main deps:dep
1081 //- /dep.rs crate:dep
1090 Query::new("FMT".to_string()),
1102 Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),