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::{FxHashMap, FxHashSet, FxHasher};
11 use smallvec::SmallVec;
15 db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
19 type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
21 /// Item import details stored in the `ImportMap`.
22 #[derive(Debug, Clone, Eq, PartialEq)]
23 pub struct ImportInfo {
24 /// A path that can be used to import the item, relative to the crate's root.
26 /// The module containing this item.
27 pub container: ModuleId,
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>>,
68 /// Maps names of associated items to the item's ID. Only includes items whose defining trait is
70 assoc_map: FxHashMap<SmolStr, SmallVec<[AssocItemId; 1]>>,
74 pub fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
75 let _p = profile::span("import_map_query");
76 let def_map = db.crate_def_map(krate);
77 let mut import_map = Self::default();
79 // We look only into modules that are public(ly reexported), starting with the crate root.
80 let empty = ImportPath { segments: vec![] };
81 let root = ModuleId { krate, local_id: def_map.root };
82 let mut worklist = vec![(root, empty)];
83 while let Some((module, mod_path)) = worklist.pop() {
85 let mod_data = if module.krate == krate {
86 &def_map[module.local_id]
88 // The crate might reexport a module defined in another crate.
89 ext_def_map = db.crate_def_map(module.krate);
90 &ext_def_map[module.local_id]
93 let visible_items = mod_data.scope.entries().filter_map(|(name, per_ns)| {
94 let per_ns = per_ns.filter_visibility(|vis| vis == Visibility::Public);
102 for (name, per_ns) in visible_items {
104 let mut path = mod_path.clone();
105 path.segments.push(name.clone());
109 for item in per_ns.iter_items() {
110 let path = mk_path();
111 match import_map.map.entry(item) {
112 Entry::Vacant(entry) => {
113 entry.insert(ImportInfo { path, container: module });
115 Entry::Occupied(mut entry) => {
116 // If the new path is shorter, prefer that one.
117 if path.len() < entry.get().path.len() {
118 *entry.get_mut() = ImportInfo { path, container: module };
125 // If we've just added a path to a module, descend into it. We might traverse
126 // modules multiple times, but only if the new path to it is shorter than the
127 // first (else we `continue` above).
128 if let Some(ModuleDefId::ModuleId(mod_id)) = item.as_module_def_id() {
129 worklist.push((mod_id, mk_path()));
132 // If we've added a path to a trait, add the trait's methods to the method map.
133 if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
134 import_map.collect_trait_methods(db, tr);
140 let mut importables = import_map.map.iter().collect::<Vec<_>>();
142 importables.sort_by(cmp);
144 // Build the FST, taking care not to insert duplicate values.
146 let mut builder = fst::MapBuilder::memory();
147 let mut last_batch_start = 0;
149 for idx in 0..importables.len() {
150 if let Some(next_item) = importables.get(idx + 1) {
151 if cmp(&importables[last_batch_start], next_item) == Ordering::Equal {
156 let start = last_batch_start;
157 last_batch_start = idx + 1;
159 let key = fst_path(&importables[start].1.path);
161 builder.insert(key, start as u64).unwrap();
164 import_map.fst = fst::Map::new(builder.into_inner().unwrap()).unwrap();
165 import_map.importables = importables.iter().map(|(item, _)| **item).collect();
170 /// Returns the `ModPath` needed to import/mention `item`, relative to this crate's root.
171 pub fn path_of(&self, item: ItemInNs) -> Option<&ImportPath> {
172 self.import_info_for(item).map(|it| &it.path)
175 pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
179 fn collect_trait_methods(&mut self, db: &dyn DefDatabase, tr: TraitId) {
180 let data = db.trait_data(tr);
181 for (name, item) in data.items.iter() {
182 self.assoc_map.entry(name.to_string().into()).or_default().push(*item);
187 impl PartialEq for ImportMap {
188 fn eq(&self, other: &Self) -> bool {
189 // `fst` and `importables` are built from `map`, so we don't need to compare them.
190 self.map == other.map
194 impl Eq for ImportMap {}
196 impl fmt::Debug for ImportMap {
197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198 let mut importable_paths: Vec<_> = self
201 .map(|(item, info)| {
202 let ns = match item {
203 ItemInNs::Types(_) => "t",
204 ItemInNs::Values(_) => "v",
205 ItemInNs::Macros(_) => "m",
207 format!("- {} ({})", info.path, ns)
211 importable_paths.sort();
212 f.write_str(&importable_paths.join("\n"))
216 fn fst_path(path: &ImportPath) -> String {
217 let mut s = path.to_string();
218 s.make_ascii_lowercase();
222 fn cmp((_, lhs): &(&ItemInNs, &ImportInfo), (_, rhs): &(&ItemInNs, &ImportInfo)) -> Ordering {
223 let lhs_str = fst_path(&lhs.path);
224 let rhs_str = fst_path(&rhs.path);
225 lhs_str.cmp(&rhs_str)
228 #[derive(Debug, Eq, PartialEq, Hash)]
229 pub enum ImportKind {
241 /// A way to match import map contents against the search query.
243 pub enum SearchMode {
244 /// Import map entry should strictly match the query string.
246 /// Import map entry should contain the query string.
248 /// Import map entry should contain all letters from the query string,
249 /// in the same order, but not necessary adjacent.
258 search_mode: SearchMode,
259 case_sensitive: bool,
261 exclude_import_kinds: FxHashSet<ImportKind>,
265 pub fn new(query: String) -> Self {
266 let lowercased = query.to_lowercase();
271 search_mode: SearchMode::Contains,
272 case_sensitive: false,
273 limit: usize::max_value(),
274 exclude_import_kinds: FxHashSet::default(),
278 /// Matches entries' names only, ignoring the rest of
280 /// Example: for `std::marker::PhantomData`, the name is `PhantomData`.
281 pub fn name_only(self) -> Self {
282 Self { name_only: true, ..self }
285 /// Specifies the way to search for the entries using the query.
286 pub fn search_mode(self, search_mode: SearchMode) -> Self {
287 Self { search_mode, ..self }
290 /// Limits the returned number of items to `limit`.
291 pub fn limit(self, limit: usize) -> Self {
292 Self { limit, ..self }
295 /// Respect casing of the query string when matching.
296 pub fn case_sensitive(self) -> Self {
297 Self { case_sensitive: true, ..self }
300 /// Do not include imports of the specified kind in the search results.
301 pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
302 self.exclude_import_kinds.insert(import_kind);
307 fn contains_query(query: &Query, input_path: &ImportPath, enforce_lowercase: bool) -> bool {
308 let mut input = if query.name_only {
309 input_path.segments.last().unwrap().to_string()
311 input_path.to_string()
313 if enforce_lowercase || !query.case_sensitive {
314 input.make_ascii_lowercase();
318 if !enforce_lowercase && query.case_sensitive { &query.query } else { &query.lowercased };
320 match query.search_mode {
321 SearchMode::Equals => &input == query_string,
322 SearchMode::Contains => input.contains(query_string),
323 SearchMode::Fuzzy => {
324 let mut unchecked_query_chars = query_string.chars();
325 let mut mismatching_query_char = unchecked_query_chars.next();
327 for input_char in input.chars() {
328 match mismatching_query_char {
330 Some(matching_query_char) if matching_query_char == input_char => {
331 mismatching_query_char = unchecked_query_chars.next();
336 mismatching_query_char.is_none()
341 /// Searches dependencies of `krate` for an importable path matching `query`.
343 /// This returns a list of items that could be imported from dependencies of `krate`.
344 pub fn search_dependencies<'a>(
345 db: &'a dyn DefDatabase,
349 let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query));
351 let graph = db.crate_graph();
352 let import_maps: Vec<_> =
353 graph[krate].dependencies.iter().map(|dep| db.import_map(dep.crate_id)).collect();
355 let automaton = fst::automaton::Subsequence::new(&query.lowercased);
357 let mut op = fst::map::OpBuilder::new();
358 for map in &import_maps {
359 op = op.add(map.fst.search(&automaton));
362 let mut stream = op.union();
363 let mut res = Vec::new();
364 while let Some((_, indexed_values)) = stream.next() {
365 for indexed_value in indexed_values {
366 let import_map = &import_maps[indexed_value.index];
367 let importables = &import_map.importables[indexed_value.value as usize..];
369 // Path shared by the importable items in this group.
370 let common_importables_path = &import_map.map[&importables[0]].path;
371 if !contains_query(&query, common_importables_path, true) {
375 let common_importables_path_fst = fst_path(common_importables_path);
376 // Add the items from this `ModPath` group. Those are all subsequent items in
377 // `importables` whose paths match `path`.
378 let iter = importables
382 common_importables_path_fst == fst_path(&import_map.map[item].path)
384 .filter(|&item| match item_import_kind(item) {
385 Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
389 !query.case_sensitive // we've already checked the common importables path case-insensitively
390 || contains_query(&query, &import_map.map[item].path, false)
394 if res.len() >= query.limit {
395 res.truncate(query.limit);
401 // Add all exported associated items whose names match the query (exactly).
402 for map in &import_maps {
403 if let Some(v) = map.assoc_map.get(&*query.query) {
404 res.extend(v.iter().map(|&assoc| {
405 ItemInNs::Types(match assoc {
406 AssocItemId::FunctionId(it) => it.into(),
407 AssocItemId::ConstId(it) => it.into(),
408 AssocItemId::TypeAliasId(it) => it.into(),
417 fn item_import_kind(item: ItemInNs) -> Option<ImportKind> {
418 Some(match item.as_module_def_id()? {
419 ModuleDefId::ModuleId(_) => ImportKind::Module,
420 ModuleDefId::FunctionId(_) => ImportKind::Function,
421 ModuleDefId::AdtId(_) => ImportKind::Adt,
422 ModuleDefId::EnumVariantId(_) => ImportKind::EnumVariant,
423 ModuleDefId::ConstId(_) => ImportKind::Const,
424 ModuleDefId::StaticId(_) => ImportKind::Static,
425 ModuleDefId::TraitId(_) => ImportKind::Trait,
426 ModuleDefId::TypeAliasId(_) => ImportKind::TypeAlias,
427 ModuleDefId::BuiltinType(_) => ImportKind::BuiltinType,
433 use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
434 use expect_test::{expect, Expect};
437 use crate::{data::FunctionData, test_db::TestDB, AssocContainerId, Lookup};
441 fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) {
442 let db = TestDB::with_files(ra_fixture);
443 let crate_graph = db.crate_graph();
444 let krate = crate_graph
447 crate_graph[*krate].display_name.as_ref().map(|n| n.to_string())
448 == Some(crate_name.to_string())
452 let actual = search_dependencies(db.upcast(), krate, query)
455 let mark = match item {
456 ItemInNs::Types(ModuleDefId::FunctionId(_))
457 | ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
458 ItemInNs::Types(_) => "t",
459 ItemInNs::Values(_) => "v",
460 ItemInNs::Macros(_) => "m",
462 item.krate(db.upcast()).map(|krate| {
463 let map = db.import_map(krate);
465 let path = match assoc_to_trait(&db, item) {
467 let mut full_path = map.path_of(trait_).unwrap().to_string();
468 if let ItemInNs::Types(ModuleDefId::FunctionId(function_id))
469 | ItemInNs::Values(ModuleDefId::FunctionId(function_id)) = item
474 FunctionData::fn_data_query(&db, function_id).name,
479 None => map.path_of(item).unwrap().to_string(),
484 crate_graph[krate].display_name.as_ref().unwrap(),
490 .collect::<String>();
491 expect.assert_eq(&actual)
494 fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> Option<ItemInNs> {
495 let assoc: AssocItemId = match item {
496 ItemInNs::Types(it) | ItemInNs::Values(it) => match it {
497 ModuleDefId::TypeAliasId(it) => it.into(),
498 ModuleDefId::FunctionId(it) => it.into(),
499 ModuleDefId::ConstId(it) => it.into(),
505 let container = match assoc {
506 AssocItemId::FunctionId(it) => it.lookup(db).container,
507 AssocItemId::ConstId(it) => it.lookup(db).container,
508 AssocItemId::TypeAliasId(it) => it.lookup(db).container,
512 AssocContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
517 fn check(ra_fixture: &str, expect: Expect) {
518 let db = TestDB::with_files(ra_fixture);
519 let crate_graph = db.crate_graph();
521 let actual = crate_graph
523 .filter_map(|krate| {
524 let cdata = &crate_graph[krate];
525 let name = cdata.display_name.as_ref()?;
527 let map = db.import_map(krate);
529 Some(format!("{}:\n{:?}\n", name, map))
531 .collect::<String>();
533 expect.assert_eq(&actual)
540 //- /main.rs crate:main deps:lib
544 pub struct InPrivateModule;
554 pub mod real_pu2 { // same path length as above
558 //- /lib.rs crate:lib
560 pub struct Pub2; // t + v
578 fn prefers_shortest_path() {
581 //- /main.rs crate:main
588 pub use super::sub::subsub::Def;
601 fn type_reexport_cross_crate() {
602 // Reexports need to be visible from a crate, even if the original crate exports the item
603 // at a shorter path.
606 //- /main.rs crate:main deps:lib
610 //- /lib.rs crate:lib
626 fn macro_reexport() {
629 //- /main.rs crate:main deps:lib
631 pub use lib::pub_macro;
633 //- /lib.rs crate:lib
635 macro_rules! pub_macro {
650 fn module_reexport() {
651 // Reexporting modules from a dependency adds all contents to the import map.
654 //- /main.rs crate:main deps:lib
655 pub use lib::module as reexported_module;
656 //- /lib.rs crate:lib
663 - reexported_module (t)
664 - reexported_module::S (t)
665 - reexported_module::S (v)
675 fn cyclic_module_reexport() {
676 // A cyclic reexport does not hang.
679 //- /lib.rs crate:lib
682 pub use super::sub::*;
686 pub use super::module;
703 //- /lib.rs crate:lib
704 macro_rules! private_macro {
719 //- /lib.rs crate:lib
720 pub struct Thing; // t + v
722 macro_rules! Thing { // m
736 //- /lib.rs crate:lib
737 pub mod Thing {} // t
739 macro_rules! Thing { // m
754 //- /main.rs crate:main deps:dep
755 //- /dep.rs crate:dep deps:tdep
756 use tdep::fmt as fmt_dep;
771 //- /tdep.rs crate:tdep
773 pub struct NotImportableFromMain;
780 Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
786 dep::fmt::Display (t)
788 dep::fmt::Display::fmt (f)
795 Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
801 dep::fmt::Display::fmt (f)
808 Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
814 dep::fmt::Display (t)
815 dep::fmt::Display::fmt (f)
823 //- /main.rs crate:main deps:dep
824 //- /dep.rs crate:dep deps:tdep
825 use tdep::fmt as fmt_dep;
840 //- /tdep.rs crate:tdep
842 pub struct NotImportableFromMain;
849 Query::new("fmt".to_string()),
855 dep::fmt::Display (t)
856 dep::fmt::Display::fmt (f)
863 Query::new("fmt".to_string()).name_only(),
869 dep::fmt::Display::fmt (f)
877 //- /main.rs crate:main deps:dep
878 //- /dep.rs crate:dep
887 Query::new("FMT".to_string()),
899 Query::new("FMT".to_string()).case_sensitive(),
911 //- /main.rs crate:main deps:dep
912 //- /dep.rs crate:dep
928 Query::new("".to_string()).limit(2),
937 fn search_exclusions() {
939 //- /main.rs crate:main deps:dep
940 //- /dep.rs crate:dep
949 Query::new("FMT".to_string()),
961 Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),