ide_db/
famous_defs.rs

1//! See [`FamousDefs`].
2
3use base_db::{CrateOrigin, LangCrateOrigin};
4use hir::{Crate, Enum, Function, Macro, Module, ScopeDef, Semantics, Trait};
5
6use crate::RootDatabase;
7
8/// Helps with finding well-know things inside the standard library. This is
9/// somewhat similar to the known paths infra inside hir, but it different; We
10/// want to make sure that IDE specific paths don't become interesting inside
11/// the compiler itself as well.
12///
13/// Note that, by default, rust-analyzer tests **do not** include core or std
14/// libraries. If you are writing tests for functionality using [`FamousDefs`],
15/// you'd want to include minicore (see `test_utils::MiniCore`) declaration at
16/// the start of your tests:
17///
18/// ```text
19/// //- minicore: iterator, ord, derive
20/// ```
21pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Crate);
22
23#[allow(non_snake_case)]
24impl FamousDefs<'_, '_> {
25    pub fn std(&self) -> Option<Crate> {
26        self.find_lang_crate(LangCrateOrigin::Std)
27    }
28
29    pub fn core(&self) -> Option<Crate> {
30        self.find_lang_crate(LangCrateOrigin::Core)
31    }
32
33    pub fn alloc(&self) -> Option<Crate> {
34        self.find_lang_crate(LangCrateOrigin::Alloc)
35    }
36
37    pub fn test(&self) -> Option<Crate> {
38        self.find_lang_crate(LangCrateOrigin::Test)
39    }
40
41    pub fn proc_macro(&self) -> Option<Crate> {
42        self.find_lang_crate(LangCrateOrigin::ProcMacro)
43    }
44
45    pub fn core_cmp_Ord(&self) -> Option<Trait> {
46        self.find_trait("core:cmp:Ord")
47    }
48
49    pub fn core_convert_FromStr(&self) -> Option<Trait> {
50        self.find_trait("core:str:FromStr")
51    }
52
53    pub fn core_convert_From(&self) -> Option<Trait> {
54        self.find_trait("core:convert:From")
55    }
56
57    pub fn core_convert_Into(&self) -> Option<Trait> {
58        self.find_trait("core:convert:Into")
59    }
60
61    pub fn core_convert_TryFrom(&self) -> Option<Trait> {
62        self.find_trait("core:convert:TryFrom")
63    }
64
65    pub fn core_convert_TryInto(&self) -> Option<Trait> {
66        self.find_trait("core:convert:TryInto")
67    }
68
69    pub fn core_convert_Index(&self) -> Option<Trait> {
70        self.find_trait("core:ops:Index")
71    }
72
73    pub fn core_option_Option(&self) -> Option<Enum> {
74        self.find_enum("core:option:Option")
75    }
76
77    pub fn core_result_Result(&self) -> Option<Enum> {
78        self.find_enum("core:result:Result")
79    }
80
81    pub fn core_default_Default(&self) -> Option<Trait> {
82        self.find_trait("core:default:Default")
83    }
84
85    pub fn core_iter_Iterator(&self) -> Option<Trait> {
86        self.find_trait("core:iter:traits:iterator:Iterator")
87    }
88
89    pub fn core_iter_IntoIterator(&self) -> Option<Trait> {
90        self.find_trait("core:iter:traits:collect:IntoIterator")
91    }
92
93    pub fn core_iter(&self) -> Option<Module> {
94        self.find_module("core:iter")
95    }
96
97    pub fn core_ops_Deref(&self) -> Option<Trait> {
98        self.find_trait("core:ops:Deref")
99    }
100
101    pub fn core_ops_DerefMut(&self) -> Option<Trait> {
102        self.find_trait("core:ops:DerefMut")
103    }
104
105    pub fn core_convert_AsRef(&self) -> Option<Trait> {
106        self.find_trait("core:convert:AsRef")
107    }
108
109    pub fn core_convert_AsMut(&self) -> Option<Trait> {
110        self.find_trait("core:convert:AsMut")
111    }
112
113    pub fn core_borrow_Borrow(&self) -> Option<Trait> {
114        self.find_trait("core:borrow:Borrow")
115    }
116
117    pub fn core_borrow_BorrowMut(&self) -> Option<Trait> {
118        self.find_trait("core:borrow:BorrowMut")
119    }
120
121    pub fn core_ops_ControlFlow(&self) -> Option<Enum> {
122        self.find_enum("core:ops:ControlFlow")
123    }
124
125    pub fn core_ops_Drop(&self) -> Option<Trait> {
126        self.find_trait("core:ops:Drop")
127    }
128
129    pub fn core_marker_Copy(&self) -> Option<Trait> {
130        self.find_trait("core:marker:Copy")
131    }
132
133    pub fn core_marker_Sized(&self) -> Option<Trait> {
134        self.find_trait("core:marker:Sized")
135    }
136
137    pub fn core_future_Future(&self) -> Option<Trait> {
138        self.find_trait("core:future:Future")
139    }
140
141    pub fn core_macros_builtin_derive(&self) -> Option<Macro> {
142        self.find_macro("core:macros:builtin:derive")
143    }
144
145    pub fn core_mem_drop(&self) -> Option<Function> {
146        self.find_function("core:mem:drop")
147    }
148
149    pub fn core_macros_todo(&self) -> Option<Macro> {
150        self.find_macro("core:todo")
151    }
152
153    pub fn core_macros_unimplemented(&self) -> Option<Macro> {
154        self.find_macro("core:unimplemented")
155    }
156
157    pub fn core_fmt_Display(&self) -> Option<Trait> {
158        self.find_trait("core:fmt:Display")
159    }
160
161    pub fn alloc_string_ToString(&self) -> Option<Trait> {
162        self.find_trait("alloc:string:ToString")
163    }
164    pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
165        IntoIterator::into_iter([
166            self.std(),
167            self.core(),
168            self.alloc(),
169            self.test(),
170            self.proc_macro(),
171        ])
172        .flatten()
173    }
174
175    fn find_trait(&self, path: &str) -> Option<Trait> {
176        match self.find_def(path)? {
177            hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
178            _ => None,
179        }
180    }
181
182    fn find_macro(&self, path: &str) -> Option<Macro> {
183        match self.find_def(path)? {
184            hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(it)) => Some(it),
185            _ => None,
186        }
187    }
188
189    fn find_enum(&self, path: &str) -> Option<Enum> {
190        match self.find_def(path)? {
191            hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
192            _ => None,
193        }
194    }
195
196    fn find_module(&self, path: &str) -> Option<Module> {
197        match self.find_def(path)? {
198            hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
199            _ => None,
200        }
201    }
202
203    fn find_function(&self, path: &str) -> Option<Function> {
204        match self.find_def(path)? {
205            hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(it)) => Some(it),
206            _ => None,
207        }
208    }
209
210    fn find_lang_crate(&self, origin: LangCrateOrigin) -> Option<Crate> {
211        let krate = self.1;
212        let db = self.0.db;
213        let res = krate
214            .dependencies(db)
215            .into_iter()
216            .find(|dep| dep.krate.origin(db) == CrateOrigin::Lang(origin))?
217            .krate;
218        Some(res)
219    }
220
221    fn find_def(&self, path: &str) -> Option<ScopeDef> {
222        let db = self.0.db;
223        let mut path = path.split(':');
224        let trait_ = path.next_back()?;
225        let lang_crate = path.next()?;
226        let lang_crate = match LangCrateOrigin::from(lang_crate) {
227            LangCrateOrigin::Other => return None,
228            lang_crate => lang_crate,
229        };
230        let std_crate = self.find_lang_crate(lang_crate)?;
231        let mut module = std_crate.root_module();
232        for segment in path {
233            module = module.children(db).find_map(|child| {
234                let name = child.name(db)?;
235                if name.as_str() == segment { Some(child) } else { None }
236            })?;
237        }
238        let def =
239            module.scope(db, None).into_iter().find(|(name, _def)| name.as_str() == trait_)?.1;
240        Some(def)
241    }
242}