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