1 //! A map of all publicly exported items in a crate.
3 use std::{cmp::Ordering, 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};
14 db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
18 type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
20 /// Item import details stored in the `ImportMap`.
21 #[derive(Debug, Clone, Eq, PartialEq)]
22 pub struct ImportInfo {
23 /// A path that can be used to import the item, relative to the crate's root.
25 /// The module containing this item.
26 pub container: ModuleId,
27 /// Whether the import is a trait associated item or not.
28 pub is_trait_assoc_item: bool,
31 #[derive(Debug, Clone, Eq, PartialEq)]
32 pub struct ImportPath {
33 pub segments: Vec<Name>,
36 impl fmt::Display for ImportPath {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 fmt::Display::fmt(&self.segments.iter().format("::"), f)
43 fn len(&self) -> usize {
48 /// A map from publicly exported items to the path needed to import/name them from a downstream
51 /// Reexports of items are taken into account, ie. if something is exported under multiple
52 /// names, the one with the shortest import path will be used.
54 /// Note that all paths are relative to the containing crate's root, so the crate name still needs
55 /// to be prepended to the `ModPath` before the path is valid.
57 pub struct ImportMap {
58 map: FxIndexMap<ItemInNs, ImportInfo>,
60 /// List of keys stored in `map`, sorted lexicographically by their `ModPath`. Indexed by the
61 /// values returned by running `fst`.
63 /// Since a path can refer to multiple items due to namespacing, we store all items with the
64 /// same path right after each other. This allows us to find all items after the FST gives us
65 /// the index of the first one.
66 importables: Vec<ItemInNs>,
67 fst: fst::Map<Vec<u8>>,
71 pub fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
72 let _p = profile::span("import_map_query");
73 let def_map = db.crate_def_map(krate);
74 let mut import_map = Self::default();
76 // We look only into modules that are public(ly reexported), starting with the crate root.
77 let empty = ImportPath { segments: vec![] };
78 let root = ModuleId { krate, local_id: def_map.root };
79 let mut worklist = vec![(root, empty)];
80 while let Some((module, mod_path)) = worklist.pop() {
82 let mod_data = if module.krate == krate {
83 &def_map[module.local_id]
85 // The crate might reexport a module defined in another crate.
86 ext_def_map = db.crate_def_map(module.krate);
87 &ext_def_map[module.local_id]
90 let visible_items = mod_data.scope.entries().filter_map(|(name, per_ns)| {
91 let per_ns = per_ns.filter_visibility(|vis| vis == Visibility::Public);
99 for (name, per_ns) in visible_items {
101 let mut path = mod_path.clone();
102 path.segments.push(name.clone());
106 for item in per_ns.iter_items() {
107 let path = mk_path();
108 let path_len = path.len();
110 ImportInfo { path, container: module, is_trait_assoc_item: false };
112 if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
113 import_map.collect_trait_assoc_items(
116 matches!(item, ItemInNs::Types(_)),
121 match import_map.map.entry(item) {
122 Entry::Vacant(entry) => {
123 entry.insert(import_info);
125 Entry::Occupied(mut entry) => {
126 // If the new path is shorter, prefer that one.
127 if path_len < entry.get().path.len() {
128 *entry.get_mut() = import_info;
135 // If we've just added a path to a module, descend into it. We might traverse
136 // modules multiple times, but only if the new path to it is shorter than the
137 // first (else we `continue` above).
138 if let Some(ModuleDefId::ModuleId(mod_id)) = item.as_module_def_id() {
139 worklist.push((mod_id, mk_path()));
145 let mut importables = import_map.map.iter().collect::<Vec<_>>();
147 importables.sort_by(cmp);
149 // Build the FST, taking care not to insert duplicate values.
151 let mut builder = fst::MapBuilder::memory();
152 let mut last_batch_start = 0;
154 for idx in 0..importables.len() {
155 if let Some(next_item) = importables.get(idx + 1) {
156 if cmp(&importables[last_batch_start], next_item) == Ordering::Equal {
161 let key = fst_path(&importables[last_batch_start].1.path);
162 builder.insert(key, last_batch_start as u64).unwrap();
164 last_batch_start = idx + 1;
167 import_map.fst = fst::Map::new(builder.into_inner().unwrap()).unwrap();
168 import_map.importables = importables.iter().map(|(item, _)| **item).collect();
173 /// Returns the `ModPath` needed to import/mention `item`, relative to this crate's root.
174 pub fn path_of(&self, item: ItemInNs) -> Option<&ImportPath> {
175 self.import_info_for(item).map(|it| &it.path)
178 pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
182 fn collect_trait_assoc_items(
184 db: &dyn DefDatabase,
187 original_import_info: &ImportInfo,
189 mark::hit!(type_aliases_ignored);
190 for (assoc_item_name, item) in &db.trait_data(tr).items {
191 let module_def_id = match item {
192 AssocItemId::FunctionId(f) => ModuleDefId::from(*f),
193 AssocItemId::ConstId(c) => ModuleDefId::from(*c),
194 // cannot use associated type aliases directly: need a `<Struct as Trait>::TypeAlias`
195 // qualifier, ergo no need to store it for imports in import_map
196 AssocItemId::TypeAliasId(_) => continue,
198 let assoc_item = if is_type_in_ns {
199 ItemInNs::Types(module_def_id)
201 ItemInNs::Values(module_def_id)
204 let mut assoc_item_info = original_import_info.clone();
205 assoc_item_info.path.segments.push(assoc_item_name.to_owned());
206 assoc_item_info.is_trait_assoc_item = true;
207 self.map.insert(assoc_item, assoc_item_info);
212 impl PartialEq for ImportMap {
213 fn eq(&self, other: &Self) -> bool {
214 // `fst` and `importables` are built from `map`, so we don't need to compare them.
215 self.map == other.map
219 impl Eq for ImportMap {}
221 impl fmt::Debug for ImportMap {
222 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
223 let mut importable_paths: Vec<_> = self
226 .map(|(item, info)| {
227 let ns = match item {
228 ItemInNs::Types(_) => "t",
229 ItemInNs::Values(_) => "v",
230 ItemInNs::Macros(_) => "m",
232 format!("- {} ({})", info.path, ns)
236 importable_paths.sort();
237 f.write_str(&importable_paths.join("\n"))
241 fn fst_path(path: &ImportPath) -> String {
242 let mut s = path.to_string();
243 s.make_ascii_lowercase();
247 fn cmp((_, lhs): &(&ItemInNs, &ImportInfo), (_, rhs): &(&ItemInNs, &ImportInfo)) -> Ordering {
248 let lhs_str = fst_path(&lhs.path);
249 let rhs_str = fst_path(&rhs.path);
250 lhs_str.cmp(&rhs_str)
253 #[derive(Debug, Eq, PartialEq, Hash)]
254 pub enum ImportKind {
266 /// A way to match import map contents against the search query.
268 pub enum SearchMode {
269 /// Import map entry should strictly match the query string.
271 /// Import map entry should contain the query string.
273 /// Import map entry should contain all letters from the query string,
274 /// in the same order, but not necessary adjacent.
283 search_mode: SearchMode,
284 case_sensitive: bool,
286 exclude_import_kinds: FxHashSet<ImportKind>,
290 pub fn new(query: String) -> Self {
291 let lowercased = query.to_lowercase();
296 search_mode: SearchMode::Contains,
297 case_sensitive: false,
298 limit: usize::max_value(),
299 exclude_import_kinds: FxHashSet::default(),
303 /// Matches entries' names only, ignoring the rest of
305 /// Example: for `std::marker::PhantomData`, the name is `PhantomData`.
306 pub fn name_only(self) -> Self {
307 Self { name_only: true, ..self }
310 /// Specifies the way to search for the entries using the query.
311 pub fn search_mode(self, search_mode: SearchMode) -> Self {
312 Self { search_mode, ..self }
315 /// Limits the returned number of items to `limit`.
316 pub fn limit(self, limit: usize) -> Self {
317 Self { limit, ..self }
320 /// Respect casing of the query string when matching.
321 pub fn case_sensitive(self) -> Self {
322 Self { case_sensitive: true, ..self }
325 /// Do not include imports of the specified kind in the search results.
326 pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
327 self.exclude_import_kinds.insert(import_kind);
331 fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
332 let mut input = if import.is_trait_assoc_item || self.name_only {
333 import.path.segments.last().unwrap().to_string()
335 import.path.to_string()
337 if enforce_lowercase || !self.case_sensitive {
338 input.make_ascii_lowercase();
342 if !enforce_lowercase && self.case_sensitive { &self.query } else { &self.lowercased };
344 match self.search_mode {
345 SearchMode::Equals => &input == query_string,
346 SearchMode::Contains => input.contains(query_string),
347 SearchMode::Fuzzy => {
348 let mut unchecked_query_chars = query_string.chars();
349 let mut mismatching_query_char = unchecked_query_chars.next();
351 for input_char in input.chars() {
352 match mismatching_query_char {
354 Some(matching_query_char) if matching_query_char == input_char => {
355 mismatching_query_char = unchecked_query_chars.next();
360 mismatching_query_char.is_none()
366 /// Searches dependencies of `krate` for an importable path matching `query`.
368 /// This returns a list of items that could be imported from dependencies of `krate`.
369 pub fn search_dependencies<'a>(
370 db: &'a dyn DefDatabase,
374 let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query));
376 let graph = db.crate_graph();
377 let import_maps: Vec<_> =
378 graph[krate].dependencies.iter().map(|dep| db.import_map(dep.crate_id)).collect();
380 let automaton = fst::automaton::Subsequence::new(&query.lowercased);
382 let mut op = fst::map::OpBuilder::new();
383 for map in &import_maps {
384 op = op.add(map.fst.search(&automaton));
387 let mut stream = op.union();
388 let mut res = Vec::new();
389 while let Some((_, indexed_values)) = stream.next() {
390 for indexed_value in indexed_values {
391 let import_map = &import_maps[indexed_value.index];
392 let importables = &import_map.importables[indexed_value.value as usize..];
394 let common_importable_data = &import_map.map[&importables[0]];
395 if !query.import_matches(common_importable_data, true) {
399 // Path shared by the importable items in this group.
400 let common_importables_path_fst = fst_path(&common_importable_data.path);
401 // Add the items from this `ModPath` group. Those are all subsequent items in
402 // `importables` whose paths match `path`.
403 let iter = importables
407 common_importables_path_fst == fst_path(&import_map.map[item].path)
409 .filter(|&item| match item_import_kind(item) {
410 Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
414 !query.case_sensitive // we've already checked the common importables path case-insensitively
415 || query.import_matches(&import_map.map[item], false)
419 if res.len() >= query.limit {
420 res.truncate(query.limit);
429 fn item_import_kind(item: ItemInNs) -> Option<ImportKind> {
430 Some(match item.as_module_def_id()? {
431 ModuleDefId::ModuleId(_) => ImportKind::Module,
432 ModuleDefId::FunctionId(_) => ImportKind::Function,
433 ModuleDefId::AdtId(_) => ImportKind::Adt,
434 ModuleDefId::EnumVariantId(_) => ImportKind::EnumVariant,
435 ModuleDefId::ConstId(_) => ImportKind::Const,
436 ModuleDefId::StaticId(_) => ImportKind::Static,
437 ModuleDefId::TraitId(_) => ImportKind::Trait,
438 ModuleDefId::TypeAliasId(_) => ImportKind::TypeAlias,
439 ModuleDefId::BuiltinType(_) => ImportKind::BuiltinType,
445 use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
446 use expect_test::{expect, Expect};
447 use test_utils::mark;
449 use crate::{test_db::TestDB, AssocContainerId, Lookup};
453 fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) {
454 let db = TestDB::with_files(ra_fixture);
455 let crate_graph = db.crate_graph();
456 let krate = crate_graph
459 crate_graph[*krate].display_name.as_ref().map(|n| n.to_string())
460 == Some(crate_name.to_string())
464 let actual = search_dependencies(db.upcast(), krate, query)
466 .filter_map(|dependency| {
467 let dependency_krate = dependency.krate(db.upcast())?;
468 let dependency_imports = db.import_map(dependency_krate);
470 let (path, mark) = match assoc_item_path(&db, &dependency_imports, dependency) {
471 Some(assoc_item_path) => (assoc_item_path, "a"),
473 dependency_imports.path_of(dependency)?.to_string(),
475 ItemInNs::Types(ModuleDefId::FunctionId(_))
476 | ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
477 ItemInNs::Types(_) => "t",
478 ItemInNs::Values(_) => "v",
479 ItemInNs::Macros(_) => "m",
486 crate_graph[dependency_krate].display_name.as_ref()?,
491 .collect::<String>();
492 expect.assert_eq(&actual)
496 db: &dyn DefDatabase,
497 dependency_imports: &ImportMap,
498 dependency: ItemInNs,
499 ) -> Option<String> {
500 let dependency_assoc_item_id = match dependency {
501 ItemInNs::Types(ModuleDefId::FunctionId(id))
502 | ItemInNs::Values(ModuleDefId::FunctionId(id)) => AssocItemId::from(id),
503 ItemInNs::Types(ModuleDefId::ConstId(id))
504 | ItemInNs::Values(ModuleDefId::ConstId(id)) => AssocItemId::from(id),
505 ItemInNs::Types(ModuleDefId::TypeAliasId(id))
506 | ItemInNs::Values(ModuleDefId::TypeAliasId(id)) => AssocItemId::from(id),
510 let trait_ = assoc_to_trait(db, dependency)?;
511 if let ModuleDefId::TraitId(tr) = trait_.as_module_def_id()? {
512 let trait_data = db.trait_data(tr);
513 let assoc_item_name =
514 trait_data.items.iter().find_map(|(assoc_item_name, assoc_item_id)| {
515 if &dependency_assoc_item_id == assoc_item_id {
516 Some(assoc_item_name)
521 return Some(format!("{}::{}", dependency_imports.path_of(trait_)?, assoc_item_name));
526 fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> Option<ItemInNs> {
527 let assoc: AssocItemId = match item {
528 ItemInNs::Types(it) | ItemInNs::Values(it) => match it {
529 ModuleDefId::TypeAliasId(it) => it.into(),
530 ModuleDefId::FunctionId(it) => it.into(),
531 ModuleDefId::ConstId(it) => it.into(),
537 let container = match assoc {
538 AssocItemId::FunctionId(it) => it.lookup(db).container,
539 AssocItemId::ConstId(it) => it.lookup(db).container,
540 AssocItemId::TypeAliasId(it) => it.lookup(db).container,
544 AssocContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
549 fn check(ra_fixture: &str, expect: Expect) {
550 let db = TestDB::with_files(ra_fixture);
551 let crate_graph = db.crate_graph();
553 let actual = crate_graph
555 .filter_map(|krate| {
556 let cdata = &crate_graph[krate];
557 let name = cdata.display_name.as_ref()?;
559 let map = db.import_map(krate);
561 Some(format!("{}:\n{:?}\n", name, map))
563 .collect::<String>();
565 expect.assert_eq(&actual)
572 //- /main.rs crate:main deps:lib
576 pub struct InPrivateModule;
586 pub mod real_pu2 { // same path length as above
590 //- /lib.rs crate:lib
592 pub struct Pub2; // t + v
610 fn prefers_shortest_path() {
613 //- /main.rs crate:main
620 pub use super::sub::subsub::Def;
633 fn type_reexport_cross_crate() {
634 // Reexports need to be visible from a crate, even if the original crate exports the item
635 // at a shorter path.
638 //- /main.rs crate:main deps:lib
642 //- /lib.rs crate:lib
658 fn macro_reexport() {
661 //- /main.rs crate:main deps:lib
663 pub use lib::pub_macro;
665 //- /lib.rs crate:lib
667 macro_rules! pub_macro {
682 fn module_reexport() {
683 // Reexporting modules from a dependency adds all contents to the import map.
686 //- /main.rs crate:main deps:lib
687 pub use lib::module as reexported_module;
688 //- /lib.rs crate:lib
695 - reexported_module (t)
696 - reexported_module::S (t)
697 - reexported_module::S (v)
707 fn cyclic_module_reexport() {
708 // A cyclic reexport does not hang.
711 //- /lib.rs crate:lib
714 pub use super::sub::*;
718 pub use super::module;
735 //- /lib.rs crate:lib
736 macro_rules! private_macro {
751 //- /lib.rs crate:lib
752 pub struct Thing; // t + v
754 macro_rules! Thing { // m
768 //- /lib.rs crate:lib
769 pub mod Thing {} // t
771 macro_rules! Thing { // m
784 fn fuzzy_import_trait_and_assoc_items() {
785 mark::check!(type_aliases_ignored);
787 //- /main.rs crate:main deps:dep
788 //- /dep.rs crate:dep
792 const FMT_CONST: bool;
794 fn format_function();
795 fn format_method(&self);
803 Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
806 dep::fmt::Display (t)
807 dep::fmt::Display::FMT_CONST (a)
808 dep::fmt::Display::format_function (a)
809 dep::fmt::Display::format_method (a)
817 //- /main.rs crate:main deps:dep
818 //- /dep.rs crate:dep deps:tdep
819 use tdep::fmt as fmt_dep;
834 //- /tdep.rs crate:tdep
836 pub struct NotImportableFromMain;
843 Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
849 dep::fmt::Display (t)
850 dep::fmt::Display::fmt (a)
858 Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
864 dep::fmt::Display::fmt (a)
871 Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
877 dep::fmt::Display (t)
878 dep::fmt::Display::fmt (a)
886 //- /main.rs crate:main deps:dep
887 //- /dep.rs crate:dep deps:tdep
888 use tdep::fmt as fmt_dep;
903 //- /tdep.rs crate:tdep
905 pub struct NotImportableFromMain;
912 Query::new("fmt".to_string()),
918 dep::fmt::Display (t)
919 dep::fmt::Display::fmt (a)
926 Query::new("fmt".to_string()).name_only(),
932 dep::fmt::Display::fmt (a)
940 //- /main.rs crate:main deps:dep
941 //- /dep.rs crate:dep
950 Query::new("FMT".to_string()),
962 Query::new("FMT".to_string()).case_sensitive(),
974 //- /main.rs crate:main deps:dep
975 //- /dep.rs crate:dep
991 Query::new("".to_string()).limit(2),
1000 fn search_exclusions() {
1001 let ra_fixture = r#"
1002 //- /main.rs crate:main deps:dep
1003 //- /dep.rs crate:dep
1012 Query::new("FMT".to_string()),
1024 Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),