aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs48
1 files changed, 38 insertions, 10 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 41d4e2ed3..9e2fa03f8 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -1,6 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use std::sync::Arc; 2use std::sync::Arc;
3 3
4use arrayvec::ArrayVec;
4use either::Either; 5use either::Either;
5use hir_def::{ 6use hir_def::{
6 adt::StructKind, 7 adt::StructKind,
@@ -226,7 +227,11 @@ impl Module {
226 Some((name, def)) 227 Some((name, def))
227 } 228 }
228 }) 229 })
229 .map(|(name, def)| (name.clone(), def.into())) 230 .flat_map(|(name, def)|
231 ScopeDef::all_items(def)
232 .into_iter()
233 .map(move |item| (name.clone(), item))
234 )
230 .collect() 235 .collect()
231 } 236 }
232 237
@@ -1288,15 +1293,38 @@ pub enum ScopeDef {
1288 Unknown, 1293 Unknown,
1289} 1294}
1290 1295
1291impl From<PerNs> for ScopeDef { 1296impl ScopeDef {
1292 fn from(def: PerNs) -> Self { 1297 pub fn all_items(def: PerNs) -> ArrayVec<[Self; 3]> {
1293 def.take_types() 1298 let mut items = ArrayVec::new();
1294 .or_else(|| def.take_values()) 1299
1295 .map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into())) 1300 match (def.take_types(), def.take_values()) {
1296 .or_else(|| { 1301 (Some(m1), None) =>
1297 def.take_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into())) 1302 items.push(ScopeDef::ModuleDef(m1.into())),
1298 }) 1303 (None, Some(m2)) =>
1299 .unwrap_or(ScopeDef::Unknown) 1304 items.push(ScopeDef::ModuleDef(m2.into())),
1305 (Some(m1), Some(m2)) => {
1306 // Some items, like unit structs and enum variants, are
1307 // returned as both a type and a value. Here we want
1308 // to de-duplicate them.
1309 if m1 != m2 {
1310 items.push(ScopeDef::ModuleDef(m1.into()));
1311 items.push(ScopeDef::ModuleDef(m2.into()));
1312 } else {
1313 items.push(ScopeDef::ModuleDef(m1.into()));
1314 }
1315 },
1316 (None, None) => {},
1317 };
1318
1319 if let Some(macro_def_id) = def.take_macros() {
1320 items.push(ScopeDef::MacroDef(macro_def_id.into()));
1321 }
1322
1323 if items.is_empty() {
1324 items.push(ScopeDef::Unknown);
1325 }
1326
1327 items
1300 } 1328 }
1301} 1329}
1302 1330