]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/helpers/famous_defs.rs
Merge #10423
[rust.git] / crates / ide_db / src / helpers / famous_defs.rs
1 //! See [`FamousDefs`].
2 use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
3
4 use crate::RootDatabase;
5
6 /// Helps with finding well-know things inside the standard library. This is
7 /// somewhat similar to the known paths infra inside hir, but it different; We
8 /// want to make sure that IDE specific paths don't become interesting inside
9 /// the compiler itself as well.
10 ///
11 /// Note that, by default, rust-analyzer tests **do not** include core or std
12 /// libraries. If you are writing tests for functionality using [`FamousDefs`],
13 /// you'd want to include minicore (see `test_utils::MiniCore`) declaration at
14 /// the start of your tests:
15 ///
16 /// ```
17 /// //- minicore: iterator, ord, derive
18 /// ```
19 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
20
21 #[allow(non_snake_case)]
22 impl FamousDefs<'_, '_> {
23     pub fn std(&self) -> Option<Crate> {
24         self.find_crate("std")
25     }
26
27     pub fn core(&self) -> Option<Crate> {
28         self.find_crate("core")
29     }
30
31     pub fn core_cmp_Ord(&self) -> Option<Trait> {
32         self.find_trait("core:cmp:Ord")
33     }
34
35     pub fn core_convert_From(&self) -> Option<Trait> {
36         self.find_trait("core:convert:From")
37     }
38
39     pub fn core_convert_Into(&self) -> Option<Trait> {
40         self.find_trait("core:convert:Into")
41     }
42
43     pub fn core_option_Option(&self) -> Option<Enum> {
44         self.find_enum("core:option:Option")
45     }
46
47     pub fn core_result_Result(&self) -> Option<Enum> {
48         self.find_enum("core:result:Result")
49     }
50
51     pub fn core_default_Default(&self) -> Option<Trait> {
52         self.find_trait("core:default:Default")
53     }
54
55     pub fn core_iter_Iterator(&self) -> Option<Trait> {
56         self.find_trait("core:iter:traits:iterator:Iterator")
57     }
58
59     pub fn core_iter_IntoIterator(&self) -> Option<Trait> {
60         self.find_trait("core:iter:traits:collect:IntoIterator")
61     }
62
63     pub fn core_iter(&self) -> Option<Module> {
64         self.find_module("core:iter")
65     }
66
67     pub fn core_ops_Deref(&self) -> Option<Trait> {
68         self.find_trait("core:ops:Deref")
69     }
70
71     pub fn alloc(&self) -> Option<Crate> {
72         self.find_crate("alloc")
73     }
74
75     pub fn test(&self) -> Option<Crate> {
76         self.find_crate("test")
77     }
78
79     pub fn proc_macro(&self) -> Option<Crate> {
80         self.find_crate("proc_macro")
81     }
82
83     pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
84         IntoIterator::into_iter([
85             self.std(),
86             self.core(),
87             self.alloc(),
88             self.test(),
89             self.proc_macro(),
90         ])
91         .filter_map(|it| it)
92     }
93
94     fn find_trait(&self, path: &str) -> Option<Trait> {
95         match self.find_def(path)? {
96             hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
97             _ => None,
98         }
99     }
100
101     fn find_enum(&self, path: &str) -> Option<Enum> {
102         match self.find_def(path)? {
103             hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
104             _ => None,
105         }
106     }
107
108     fn find_module(&self, path: &str) -> Option<Module> {
109         match self.find_def(path)? {
110             hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
111             _ => None,
112         }
113     }
114
115     fn find_crate(&self, name: &str) -> Option<Crate> {
116         let krate = self.1?;
117         let db = self.0.db;
118         let res =
119             krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
120         Some(res)
121     }
122
123     fn find_def(&self, path: &str) -> Option<ScopeDef> {
124         let db = self.0.db;
125         let mut path = path.split(':');
126         let trait_ = path.next_back()?;
127         let std_crate = path.next()?;
128         let std_crate = self.find_crate(std_crate)?;
129         let mut module = std_crate.root_module(db);
130         for segment in path {
131             module = module.children(db).find_map(|child| {
132                 let name = child.name(db)?;
133                 if name.to_string() == segment {
134                     Some(child)
135                 } else {
136                     None
137                 }
138             })?;
139         }
140         let def =
141             module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
142         Some(def)
143     }
144 }