]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/import_map.rs
Merge #11866
[rust.git] / crates / hir_def / src / import_map.rs
1 //! A map of all publicly exported items in a crate.
2
3 use std::{fmt, hash::BuildHasherDefault, sync::Arc};
4
5 use base_db::CrateId;
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};
11
12 use crate::{
13     db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
14     ModuleId, TraitId,
15 };
16
17 type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
18
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.
23     pub path: ImportPath,
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,
28 }
29
30 #[derive(Debug, Clone, Eq, PartialEq)]
31 pub struct ImportPath {
32     pub segments: Vec<Name>,
33 }
34
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)
38     }
39 }
40
41 impl ImportPath {
42     fn len(&self) -> usize {
43         self.segments.len()
44     }
45 }
46
47 /// A map from publicly exported items to the path needed to import/name them from a downstream
48 /// crate.
49 ///
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.
52 ///
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.
55 #[derive(Default)]
56 pub struct ImportMap {
57     map: FxIndexMap<ItemInNs, ImportInfo>,
58
59     /// List of keys stored in `map`, sorted lexicographically by their `ModPath`. Indexed by the
60     /// values returned by running `fst`.
61     ///
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>>,
67 }
68
69 impl ImportMap {
70     pub fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
71         let _p = profile::span("import_map_query");
72
73         let mut import_map = collect_import_map(db, krate);
74
75         let mut importables = import_map
76             .map
77             .iter()
78             .map(|(item, info)| (item, fst_path(&info.path)))
79             .collect::<Vec<_>>();
80         importables.sort_by(|(_, fst_path), (_, fst_path2)| fst_path.cmp(fst_path2));
81
82         // Build the FST, taking care not to insert duplicate values.
83
84         let mut builder = fst::MapBuilder::memory();
85         let mut last_batch_start = 0;
86
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) {
90                 if key == fst_path {
91                     continue;
92                 }
93             }
94
95             let _ = builder.insert(key, last_batch_start as u64);
96
97             last_batch_start = idx + 1;
98         }
99
100         import_map.fst = builder.into_map();
101         import_map.importables = importables.iter().map(|&(&item, _)| item).collect();
102
103         Arc::new(import_map)
104     }
105
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)
109     }
110
111     pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
112         self.map.get(&item)
113     }
114
115     fn collect_trait_assoc_items(
116         &mut self,
117         db: &dyn DefDatabase,
118         tr: TraitId,
119         is_type_in_ns: bool,
120         original_import_info: &ImportInfo,
121     ) {
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);
131                     continue;
132                 }
133             };
134             let assoc_item = if is_type_in_ns {
135                 ItemInNs::Types(module_def_id)
136             } else {
137                 ItemInNs::Values(module_def_id)
138             };
139
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);
144         }
145     }
146 }
147
148 fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
149     let _p = profile::span("collect_import_map");
150
151     let def_map = db.crate_def_map(krate);
152     let mut import_map = ImportMap::default();
153
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() {
159         let ext_def_map;
160         let mod_data = if module.krate == krate {
161             &def_map[module.local_id]
162         } else {
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]
166         };
167
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() {
171                 None
172             } else {
173                 Some((name, per_ns))
174             }
175         });
176
177         for (name, per_ns) in visible_items {
178             let mk_path = || {
179                 let mut path = mod_path.clone();
180                 path.segments.push(name.clone());
181                 path
182             };
183
184             for item in per_ns.iter_items() {
185                 let path = mk_path();
186                 let path_len = path.len();
187                 let import_info =
188                     ImportInfo { path, container: module, is_trait_assoc_item: false };
189
190                 if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
191                     import_map.collect_trait_assoc_items(
192                         db,
193                         tr,
194                         matches!(item, ItemInNs::Types(_)),
195                         &import_info,
196                     );
197                 }
198
199                 match import_map.map.entry(item) {
200                     Entry::Vacant(entry) => {
201                         entry.insert(import_info);
202                     }
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;
207                         } else {
208                             continue;
209                         }
210                     }
211                 }
212
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()));
218                 }
219             }
220         }
221     }
222
223     import_map
224 }
225
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
230     }
231 }
232
233 impl Eq for ImportMap {}
234
235 impl fmt::Debug for ImportMap {
236     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237         let mut importable_paths: Vec<_> = self
238             .map
239             .iter()
240             .map(|(item, info)| {
241                 let ns = match item {
242                     ItemInNs::Types(_) => "t",
243                     ItemInNs::Values(_) => "v",
244                     ItemInNs::Macros(_) => "m",
245                 };
246                 format!("- {} ({})", info.path, ns)
247             })
248             .collect();
249
250         importable_paths.sort();
251         f.write_str(&importable_paths.join("\n"))
252     }
253 }
254
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();
259     s
260 }
261
262 #[derive(Debug, Eq, PartialEq, Hash)]
263 pub enum ImportKind {
264     Module,
265     Function,
266     Adt,
267     EnumVariant,
268     Const,
269     Static,
270     Trait,
271     TypeAlias,
272     BuiltinType,
273     AssociatedItem,
274     Macro,
275 }
276
277 /// A way to match import map contents against the search query.
278 #[derive(Debug)]
279 pub enum SearchMode {
280     /// Import map entry should strictly match the query string.
281     Equals,
282     /// Import map entry should contain the query string.
283     Contains,
284     /// Import map entry should contain all letters from the query string,
285     /// in the same order, but not necessary adjacent.
286     Fuzzy,
287 }
288
289 #[derive(Debug)]
290 pub struct Query {
291     query: String,
292     lowercased: String,
293     name_only: bool,
294     assoc_items_only: bool,
295     search_mode: SearchMode,
296     case_sensitive: bool,
297     limit: usize,
298     exclude_import_kinds: FxHashSet<ImportKind>,
299 }
300
301 impl Query {
302     pub fn new(query: String) -> Self {
303         let lowercased = query.to_lowercase();
304         Self {
305             query,
306             lowercased,
307             name_only: false,
308             assoc_items_only: false,
309             search_mode: SearchMode::Contains,
310             case_sensitive: false,
311             limit: usize::max_value(),
312             exclude_import_kinds: FxHashSet::default(),
313         }
314     }
315
316     /// Matches entries' names only, ignoring the rest of
317     /// the qualifier.
318     /// Example: for `std::marker::PhantomData`, the name is `PhantomData`.
319     pub fn name_only(self) -> Self {
320         Self { name_only: true, ..self }
321     }
322
323     /// Matches only the entries that are associated items, ignoring the rest.
324     pub fn assoc_items_only(self) -> Self {
325         Self { assoc_items_only: true, ..self }
326     }
327
328     /// Specifies the way to search for the entries using the query.
329     pub fn search_mode(self, search_mode: SearchMode) -> Self {
330         Self { search_mode, ..self }
331     }
332
333     /// Limits the returned number of items to `limit`.
334     pub fn limit(self, limit: usize) -> Self {
335         Self { limit, ..self }
336     }
337
338     /// Respect casing of the query string when matching.
339     pub fn case_sensitive(self) -> Self {
340         Self { case_sensitive: true, ..self }
341     }
342
343     /// Do not include imports of the specified kind in the search results.
344     pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
345         self.exclude_import_kinds.insert(import_kind);
346         self
347     }
348
349     fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
350         let _p = profile::span("import_map::Query::import_matches");
351         if import.is_trait_assoc_item {
352             if self.exclude_import_kinds.contains(&ImportKind::AssociatedItem) {
353                 return false;
354             }
355         } else if self.assoc_items_only {
356             return false;
357         }
358
359         let mut input = if import.is_trait_assoc_item || self.name_only {
360             import.path.segments.last().unwrap().to_string()
361         } else {
362             import.path.to_string()
363         };
364         if enforce_lowercase || !self.case_sensitive {
365             input.make_ascii_lowercase();
366         }
367
368         let query_string =
369             if !enforce_lowercase && self.case_sensitive { &self.query } else { &self.lowercased };
370
371         match self.search_mode {
372             SearchMode::Equals => &input == query_string,
373             SearchMode::Contains => input.contains(query_string),
374             SearchMode::Fuzzy => {
375                 let mut unchecked_query_chars = query_string.chars();
376                 let mut mismatching_query_char = unchecked_query_chars.next();
377
378                 for input_char in input.chars() {
379                     match mismatching_query_char {
380                         None => return true,
381                         Some(matching_query_char) if matching_query_char == input_char => {
382                             mismatching_query_char = unchecked_query_chars.next();
383                         }
384                         _ => (),
385                     }
386                 }
387                 mismatching_query_char.is_none()
388             }
389         }
390     }
391 }
392
393 /// Searches dependencies of `krate` for an importable path matching `query`.
394 ///
395 /// This returns a list of items that could be imported from dependencies of `krate`.
396 pub fn search_dependencies<'a>(
397     db: &'a dyn DefDatabase,
398     krate: CrateId,
399     query: Query,
400 ) -> FxHashSet<ItemInNs> {
401     let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query));
402
403     let graph = db.crate_graph();
404     let import_maps: Vec<_> =
405         graph[krate].dependencies.iter().map(|dep| db.import_map(dep.crate_id)).collect();
406
407     let automaton = fst::automaton::Subsequence::new(&query.lowercased);
408
409     let mut op = fst::map::OpBuilder::new();
410     for map in &import_maps {
411         op = op.add(map.fst.search(&automaton));
412     }
413
414     let mut stream = op.union();
415
416     let mut all_indexed_values = FxHashSet::default();
417     while let Some((_, indexed_values)) = stream.next() {
418         all_indexed_values.extend(indexed_values.iter().copied());
419     }
420
421     let mut res = FxHashSet::default();
422     for indexed_value in all_indexed_values {
423         let import_map = &import_maps[indexed_value.index];
424         let importables = &import_map.importables[indexed_value.value as usize..];
425
426         let common_importable_data = &import_map.map[&importables[0]];
427         if !query.import_matches(common_importable_data, true) {
428             continue;
429         }
430
431         // Path shared by the importable items in this group.
432         let common_importables_path_fst = fst_path(&common_importable_data.path);
433         // Add the items from this `ModPath` group. Those are all subsequent items in
434         // `importables` whose paths match `path`.
435         let iter = importables
436             .iter()
437             .copied()
438             .take_while(|item| common_importables_path_fst == fst_path(&import_map.map[item].path))
439             .filter(|&item| match item_import_kind(item) {
440                 Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
441                 None => true,
442             })
443             .filter(|item| {
444                 !query.case_sensitive // we've already checked the common importables path case-insensitively
445                         || query.import_matches(&import_map.map[item], false)
446             });
447         res.extend(iter);
448
449         if res.len() >= query.limit {
450             return res;
451         }
452     }
453
454     res
455 }
456
457 fn item_import_kind(item: ItemInNs) -> Option<ImportKind> {
458     Some(match item.as_module_def_id()? {
459         ModuleDefId::ModuleId(_) => ImportKind::Module,
460         ModuleDefId::FunctionId(_) => ImportKind::Function,
461         ModuleDefId::AdtId(_) => ImportKind::Adt,
462         ModuleDefId::EnumVariantId(_) => ImportKind::EnumVariant,
463         ModuleDefId::ConstId(_) => ImportKind::Const,
464         ModuleDefId::StaticId(_) => ImportKind::Static,
465         ModuleDefId::TraitId(_) => ImportKind::Trait,
466         ModuleDefId::TypeAliasId(_) => ImportKind::TypeAlias,
467         ModuleDefId::BuiltinType(_) => ImportKind::BuiltinType,
468         ModuleDefId::MacroId(_) => ImportKind::Macro,
469     })
470 }
471
472 #[cfg(test)]
473 mod tests {
474     use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
475     use expect_test::{expect, Expect};
476
477     use crate::{test_db::TestDB, ItemContainerId, Lookup};
478
479     use super::*;
480
481     fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) {
482         let db = TestDB::with_files(ra_fixture);
483         let crate_graph = db.crate_graph();
484         let krate = crate_graph
485             .iter()
486             .find(|krate| {
487                 crate_graph[*krate].display_name.as_ref().map(|n| n.to_string())
488                     == Some(crate_name.to_string())
489             })
490             .unwrap();
491
492         let actual = search_dependencies(db.upcast(), krate, query)
493             .into_iter()
494             .filter_map(|dependency| {
495                 let dependency_krate = dependency.krate(db.upcast())?;
496                 let dependency_imports = db.import_map(dependency_krate);
497
498                 let (path, mark) = match assoc_item_path(&db, &dependency_imports, dependency) {
499                     Some(assoc_item_path) => (assoc_item_path, "a"),
500                     None => (
501                         dependency_imports.path_of(dependency)?.to_string(),
502                         match dependency {
503                             ItemInNs::Types(ModuleDefId::FunctionId(_))
504                             | ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
505                             ItemInNs::Types(_) => "t",
506                             ItemInNs::Values(_) => "v",
507                             ItemInNs::Macros(_) => "m",
508                         },
509                     ),
510                 };
511
512                 Some(format!(
513                     "{}::{} ({})\n",
514                     crate_graph[dependency_krate].display_name.as_ref()?,
515                     path,
516                     mark
517                 ))
518             })
519             .collect::<String>();
520         expect.assert_eq(&actual)
521     }
522
523     fn assoc_item_path(
524         db: &dyn DefDatabase,
525         dependency_imports: &ImportMap,
526         dependency: ItemInNs,
527     ) -> Option<String> {
528         let dependency_assoc_item_id = match dependency {
529             ItemInNs::Types(ModuleDefId::FunctionId(id))
530             | ItemInNs::Values(ModuleDefId::FunctionId(id)) => AssocItemId::from(id),
531             ItemInNs::Types(ModuleDefId::ConstId(id))
532             | ItemInNs::Values(ModuleDefId::ConstId(id)) => AssocItemId::from(id),
533             ItemInNs::Types(ModuleDefId::TypeAliasId(id))
534             | ItemInNs::Values(ModuleDefId::TypeAliasId(id)) => AssocItemId::from(id),
535             _ => return None,
536         };
537
538         let trait_ = assoc_to_trait(db, dependency)?;
539         if let ModuleDefId::TraitId(tr) = trait_.as_module_def_id()? {
540             let trait_data = db.trait_data(tr);
541             let assoc_item_name =
542                 trait_data.items.iter().find_map(|(assoc_item_name, assoc_item_id)| {
543                     if &dependency_assoc_item_id == assoc_item_id {
544                         Some(assoc_item_name)
545                     } else {
546                         None
547                     }
548                 })?;
549             return Some(format!("{}::{}", dependency_imports.path_of(trait_)?, assoc_item_name));
550         }
551         None
552     }
553
554     fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> Option<ItemInNs> {
555         let assoc: AssocItemId = match item {
556             ItemInNs::Types(it) | ItemInNs::Values(it) => match it {
557                 ModuleDefId::TypeAliasId(it) => it.into(),
558                 ModuleDefId::FunctionId(it) => it.into(),
559                 ModuleDefId::ConstId(it) => it.into(),
560                 _ => return None,
561             },
562             _ => return None,
563         };
564
565         let container = match assoc {
566             AssocItemId::FunctionId(it) => it.lookup(db).container,
567             AssocItemId::ConstId(it) => it.lookup(db).container,
568             AssocItemId::TypeAliasId(it) => it.lookup(db).container,
569         };
570
571         match container {
572             ItemContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
573             _ => None,
574         }
575     }
576
577     fn check(ra_fixture: &str, expect: Expect) {
578         let db = TestDB::with_files(ra_fixture);
579         let crate_graph = db.crate_graph();
580
581         let actual = crate_graph
582             .iter()
583             .filter_map(|krate| {
584                 let cdata = &crate_graph[krate];
585                 let name = cdata.display_name.as_ref()?;
586
587                 let map = db.import_map(krate);
588
589                 Some(format!("{}:\n{:?}\n", name, map))
590             })
591             .collect::<String>();
592
593         expect.assert_eq(&actual)
594     }
595
596     #[test]
597     fn smoke() {
598         check(
599             r"
600             //- /main.rs crate:main deps:lib
601
602             mod private {
603                 pub use lib::Pub;
604                 pub struct InPrivateModule;
605             }
606
607             pub mod publ1 {
608                 use lib::Pub;
609             }
610
611             pub mod real_pub {
612                 pub use lib::Pub;
613             }
614             pub mod real_pu2 { // same path length as above
615                 pub use lib::Pub;
616             }
617
618             //- /lib.rs crate:lib
619             pub struct Pub {}
620             pub struct Pub2; // t + v
621             struct Priv;
622         ",
623             expect![[r#"
624                 main:
625                 - publ1 (t)
626                 - real_pu2 (t)
627                 - real_pub (t)
628                 - real_pub::Pub (t)
629                 lib:
630                 - Pub (t)
631                 - Pub2 (t)
632                 - Pub2 (v)
633             "#]],
634         );
635     }
636
637     #[test]
638     fn prefers_shortest_path() {
639         check(
640             r"
641             //- /main.rs crate:main
642
643             pub mod sub {
644                 pub mod subsub {
645                     pub struct Def {}
646                 }
647
648                 pub use super::sub::subsub::Def;
649             }
650         ",
651             expect![[r#"
652                 main:
653                 - sub (t)
654                 - sub::Def (t)
655                 - sub::subsub (t)
656             "#]],
657         );
658     }
659
660     #[test]
661     fn type_reexport_cross_crate() {
662         // Reexports need to be visible from a crate, even if the original crate exports the item
663         // at a shorter path.
664         check(
665             r"
666             //- /main.rs crate:main deps:lib
667             pub mod m {
668                 pub use lib::S;
669             }
670             //- /lib.rs crate:lib
671             pub struct S;
672         ",
673             expect![[r#"
674                 main:
675                 - m (t)
676                 - m::S (t)
677                 - m::S (v)
678                 lib:
679                 - S (t)
680                 - S (v)
681             "#]],
682         );
683     }
684
685     #[test]
686     fn macro_reexport() {
687         check(
688             r"
689             //- /main.rs crate:main deps:lib
690             pub mod m {
691                 pub use lib::pub_macro;
692             }
693             //- /lib.rs crate:lib
694             #[macro_export]
695             macro_rules! pub_macro {
696                 () => {};
697             }
698         ",
699             expect![[r#"
700                 main:
701                 - m (t)
702                 - m::pub_macro (m)
703                 lib:
704                 - pub_macro (m)
705             "#]],
706         );
707     }
708
709     #[test]
710     fn module_reexport() {
711         // Reexporting modules from a dependency adds all contents to the import map.
712         check(
713             r"
714             //- /main.rs crate:main deps:lib
715             pub use lib::module as reexported_module;
716             //- /lib.rs crate:lib
717             pub mod module {
718                 pub struct S;
719             }
720         ",
721             expect![[r#"
722                 main:
723                 - reexported_module (t)
724                 - reexported_module::S (t)
725                 - reexported_module::S (v)
726                 lib:
727                 - module (t)
728                 - module::S (t)
729                 - module::S (v)
730             "#]],
731         );
732     }
733
734     #[test]
735     fn cyclic_module_reexport() {
736         // A cyclic reexport does not hang.
737         check(
738             r"
739             //- /lib.rs crate:lib
740             pub mod module {
741                 pub struct S;
742                 pub use super::sub::*;
743             }
744
745             pub mod sub {
746                 pub use super::module;
747             }
748         ",
749             expect![[r#"
750                 lib:
751                 - module (t)
752                 - module::S (t)
753                 - module::S (v)
754                 - sub (t)
755             "#]],
756         );
757     }
758
759     #[test]
760     fn private_macro() {
761         check(
762             r"
763             //- /lib.rs crate:lib
764             macro_rules! private_macro {
765                 () => {};
766             }
767         ",
768             expect![[r#"
769                 lib:
770
771             "#]],
772         );
773     }
774
775     #[test]
776     fn namespacing() {
777         check(
778             r"
779             //- /lib.rs crate:lib
780             pub struct Thing;     // t + v
781             #[macro_export]
782             macro_rules! Thing {  // m
783                 () => {};
784             }
785         ",
786             expect![[r#"
787                 lib:
788                 - Thing (m)
789                 - Thing (t)
790                 - Thing (v)
791             "#]],
792         );
793
794         check(
795             r"
796             //- /lib.rs crate:lib
797             pub mod Thing {}      // t
798             #[macro_export]
799             macro_rules! Thing {  // m
800                 () => {};
801             }
802         ",
803             expect![[r#"
804                 lib:
805                 - Thing (m)
806                 - Thing (t)
807             "#]],
808         );
809     }
810
811     #[test]
812     fn fuzzy_import_trait_and_assoc_items() {
813         cov_mark::check!(type_aliases_ignored);
814         let ra_fixture = r#"
815         //- /main.rs crate:main deps:dep
816         //- /dep.rs crate:dep
817         pub mod fmt {
818             pub trait Display {
819                 type FmtTypeAlias;
820                 const FMT_CONST: bool;
821
822                 fn format_function();
823                 fn format_method(&self);
824             }
825         }
826     "#;
827
828         check_search(
829             ra_fixture,
830             "main",
831             Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
832             expect![[r#"
833                 dep::fmt (t)
834                 dep::fmt::Display::format_method (a)
835                 dep::fmt::Display (t)
836                 dep::fmt::Display::FMT_CONST (a)
837                 dep::fmt::Display::format_function (a)
838             "#]],
839         );
840     }
841
842     #[test]
843     fn assoc_items_filtering() {
844         let ra_fixture = r#"
845         //- /main.rs crate:main deps:dep
846         //- /dep.rs crate:dep
847         pub mod fmt {
848             pub trait Display {
849                 type FmtTypeAlias;
850                 const FMT_CONST: bool;
851
852                 fn format_function();
853                 fn format_method(&self);
854             }
855         }
856     "#;
857
858         check_search(
859             ra_fixture,
860             "main",
861             Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy).assoc_items_only(),
862             expect![[r#"
863             dep::fmt::Display::format_method (a)
864             dep::fmt::Display::FMT_CONST (a)
865             dep::fmt::Display::format_function (a)
866         "#]],
867         );
868
869         check_search(
870             ra_fixture,
871             "main",
872             Query::new("fmt".to_string())
873                 .search_mode(SearchMode::Fuzzy)
874                 .exclude_import_kind(ImportKind::AssociatedItem),
875             expect![[r#"
876             dep::fmt (t)
877             dep::fmt::Display (t)
878         "#]],
879         );
880
881         check_search(
882             ra_fixture,
883             "main",
884             Query::new("fmt".to_string())
885                 .search_mode(SearchMode::Fuzzy)
886                 .assoc_items_only()
887                 .exclude_import_kind(ImportKind::AssociatedItem),
888             expect![[r#""#]],
889         );
890     }
891
892     #[test]
893     fn search_mode() {
894         let ra_fixture = r#"
895             //- /main.rs crate:main deps:dep
896             //- /dep.rs crate:dep deps:tdep
897             use tdep::fmt as fmt_dep;
898             pub mod fmt {
899                 pub trait Display {
900                     fn fmt();
901                 }
902             }
903             #[macro_export]
904             macro_rules! Fmt {
905                 () => {};
906             }
907             pub struct Fmt;
908
909             pub fn format() {}
910             pub fn no() {}
911
912             //- /tdep.rs crate:tdep
913             pub mod fmt {
914                 pub struct NotImportableFromMain;
915             }
916         "#;
917
918         check_search(
919             ra_fixture,
920             "main",
921             Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
922             expect![[r#"
923                 dep::fmt (t)
924                 dep::format (f)
925                 dep::Fmt (v)
926                 dep::Fmt (m)
927                 dep::Fmt (t)
928                 dep::fmt::Display::fmt (a)
929                 dep::fmt::Display (t)
930             "#]],
931         );
932
933         check_search(
934             ra_fixture,
935             "main",
936             Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
937             expect![[r#"
938                 dep::fmt (t)
939                 dep::Fmt (v)
940                 dep::Fmt (m)
941                 dep::Fmt (t)
942                 dep::fmt::Display::fmt (a)
943             "#]],
944         );
945
946         check_search(
947             ra_fixture,
948             "main",
949             Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
950             expect![[r#"
951                 dep::fmt (t)
952                 dep::Fmt (v)
953                 dep::Fmt (m)
954                 dep::Fmt (t)
955                 dep::fmt::Display::fmt (a)
956                 dep::fmt::Display (t)
957             "#]],
958         );
959     }
960
961     #[test]
962     fn name_only() {
963         let ra_fixture = r#"
964             //- /main.rs crate:main deps:dep
965             //- /dep.rs crate:dep deps:tdep
966             use tdep::fmt as fmt_dep;
967             pub mod fmt {
968                 pub trait Display {
969                     fn fmt();
970                 }
971             }
972             #[macro_export]
973             macro_rules! Fmt {
974                 () => {};
975             }
976             pub struct Fmt;
977
978             pub fn format() {}
979             pub fn no() {}
980
981             //- /tdep.rs crate:tdep
982             pub mod fmt {
983                 pub struct NotImportableFromMain;
984             }
985         "#;
986
987         check_search(
988             ra_fixture,
989             "main",
990             Query::new("fmt".to_string()),
991             expect![[r#"
992                 dep::fmt (t)
993                 dep::Fmt (v)
994                 dep::Fmt (m)
995                 dep::Fmt (t)
996                 dep::fmt::Display::fmt (a)
997                 dep::fmt::Display (t)
998             "#]],
999         );
1000
1001         check_search(
1002             ra_fixture,
1003             "main",
1004             Query::new("fmt".to_string()).name_only(),
1005             expect![[r#"
1006                 dep::fmt (t)
1007                 dep::Fmt (v)
1008                 dep::Fmt (m)
1009                 dep::Fmt (t)
1010                 dep::fmt::Display::fmt (a)
1011             "#]],
1012         );
1013     }
1014
1015     #[test]
1016     fn search_casing() {
1017         let ra_fixture = r#"
1018             //- /main.rs crate:main deps:dep
1019             //- /dep.rs crate:dep
1020
1021             pub struct fmt;
1022             pub struct FMT;
1023         "#;
1024
1025         check_search(
1026             ra_fixture,
1027             "main",
1028             Query::new("FMT".to_string()),
1029             expect![[r#"
1030                 dep::fmt (t)
1031                 dep::FMT (v)
1032                 dep::fmt (v)
1033                 dep::FMT (t)
1034             "#]],
1035         );
1036
1037         check_search(
1038             ra_fixture,
1039             "main",
1040             Query::new("FMT".to_string()).case_sensitive(),
1041             expect![[r#"
1042                 dep::FMT (t)
1043                 dep::FMT (v)
1044             "#]],
1045         );
1046     }
1047
1048     #[test]
1049     fn search_limit() {
1050         check_search(
1051             r#"
1052         //- /main.rs crate:main deps:dep
1053         //- /dep.rs crate:dep
1054         pub mod fmt {
1055             pub trait Display {
1056                 fn fmt();
1057             }
1058         }
1059         #[macro_export]
1060         macro_rules! Fmt {
1061             () => {};
1062         }
1063         pub struct Fmt;
1064
1065         pub fn format() {}
1066         pub fn no() {}
1067     "#,
1068             "main",
1069             Query::new("".to_string()).limit(2),
1070             expect![[r#"
1071                 dep::fmt (t)
1072                 dep::Fmt (t)
1073                 dep::Fmt (v)
1074                 dep::Fmt (m)
1075             "#]],
1076         );
1077     }
1078
1079     #[test]
1080     fn search_exclusions() {
1081         let ra_fixture = r#"
1082             //- /main.rs crate:main deps:dep
1083             //- /dep.rs crate:dep
1084
1085             pub struct fmt;
1086             pub struct FMT;
1087         "#;
1088
1089         check_search(
1090             ra_fixture,
1091             "main",
1092             Query::new("FMT".to_string()),
1093             expect![[r#"
1094                 dep::fmt (t)
1095                 dep::FMT (v)
1096                 dep::fmt (v)
1097                 dep::FMT (t)
1098             "#]],
1099         );
1100
1101         check_search(
1102             ra_fixture,
1103             "main",
1104             Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),
1105             expect![[r#""#]],
1106         );
1107     }
1108 }