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.rs51
1 files changed, 22 insertions, 29 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 64b9a4cc3..5137a16e6 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -186,6 +186,22 @@ impl ModuleDef {
186 186
187 module.visibility_of(db, self) 187 module.visibility_of(db, self)
188 } 188 }
189
190 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
191 match self {
192 ModuleDef::Adt(it) => Some(it.name(db)),
193 ModuleDef::Trait(it) => Some(it.name(db)),
194 ModuleDef::Function(it) => Some(it.name(db)),
195 ModuleDef::EnumVariant(it) => Some(it.name(db)),
196 ModuleDef::TypeAlias(it) => Some(it.name(db)),
197
198 ModuleDef::Module(it) => it.name(db),
199 ModuleDef::Const(it) => it.name(db),
200 ModuleDef::Static(it) => it.name(db),
201
202 ModuleDef::BuiltinType(it) => Some(it.as_name()),
203 }
204 }
189} 205}
190 206
191pub use hir_def::{ 207pub use hir_def::{
@@ -1382,8 +1398,8 @@ impl Type {
1382 } 1398 }
1383 1399
1384 /// Returns a flattened list of all ADTs and Traits mentioned in the type 1400 /// Returns a flattened list of all ADTs and Traits mentioned in the type
1385 pub fn flattened_type_items(&self, db: &dyn HirDatabase) -> Vec<AdtOrTrait> { 1401 pub fn flattened_type_items(&self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
1386 fn push_new_item(item: AdtOrTrait, acc: &mut Vec<AdtOrTrait>) { 1402 fn push_new_item(item: ModuleDef, acc: &mut Vec<ModuleDef>) {
1387 if !acc.contains(&item) { 1403 if !acc.contains(&item) {
1388 acc.push(item); 1404 acc.push(item);
1389 } 1405 }
@@ -1392,7 +1408,7 @@ impl Type {
1392 fn push_bounds( 1408 fn push_bounds(
1393 db: &dyn HirDatabase, 1409 db: &dyn HirDatabase,
1394 predicates: &[GenericPredicate], 1410 predicates: &[GenericPredicate],
1395 acc: &mut Vec<AdtOrTrait>, 1411 acc: &mut Vec<ModuleDef>,
1396 ) { 1412 ) {
1397 for p in predicates.iter() { 1413 for p in predicates.iter() {
1398 match p { 1414 match p {
@@ -1407,13 +1423,13 @@ impl Type {
1407 } 1423 }
1408 1424
1409 // TypeWalk::walk does not preserve items order! 1425 // TypeWalk::walk does not preserve items order!
1410 fn walk_substs(db: &dyn HirDatabase, substs: &Substs, acc: &mut Vec<AdtOrTrait>) { 1426 fn walk_substs(db: &dyn HirDatabase, substs: &Substs, acc: &mut Vec<ModuleDef>) {
1411 for ty in substs.iter() { 1427 for ty in substs.iter() {
1412 walk_type(db, ty, acc); 1428 walk_type(db, ty, acc);
1413 } 1429 }
1414 } 1430 }
1415 1431
1416 fn walk_type(db: &dyn HirDatabase, ty: &Ty, acc: &mut Vec<AdtOrTrait>) { 1432 fn walk_type(db: &dyn HirDatabase, ty: &Ty, acc: &mut Vec<ModuleDef>) {
1417 match ty.strip_references() { 1433 match ty.strip_references() {
1418 Ty::Apply(ApplicationTy { ctor, parameters, .. }) => { 1434 Ty::Apply(ApplicationTy { ctor, parameters, .. }) => {
1419 match ctor { 1435 match ctor {
@@ -1468,7 +1484,7 @@ impl Type {
1468 } 1484 }
1469 } 1485 }
1470 1486
1471 let mut res: Vec<AdtOrTrait> = Vec::new(); // not a Set to preserve the order 1487 let mut res: Vec<ModuleDef> = Vec::new(); // not a Set to preserve the order
1472 walk_type(db, &self.ty.value, &mut res); 1488 walk_type(db, &self.ty.value, &mut res);
1473 res 1489 res
1474 } 1490 }
@@ -1580,26 +1596,3 @@ pub trait HasVisibility {
1580 vis.is_visible_from(db.upcast(), module.id) 1596 vis.is_visible_from(db.upcast(), module.id)
1581 } 1597 }
1582} 1598}
1583
1584#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1585pub enum AdtOrTrait {
1586 Adt(Adt),
1587 Trait(Trait),
1588}
1589impl_froms!(AdtOrTrait: Adt, Trait);
1590
1591impl AdtOrTrait {
1592 pub fn module(self, db: &dyn HirDatabase) -> Module {
1593 match self {
1594 AdtOrTrait::Adt(adt) => adt.module(db),
1595 AdtOrTrait::Trait(trait_) => trait_.module(db),
1596 }
1597 }
1598
1599 pub fn name(self, db: &dyn HirDatabase) -> Name {
1600 match self {
1601 AdtOrTrait::Adt(adt) => adt.name(db),
1602 AdtOrTrait::Trait(trait_) => trait_.name(db),
1603 }
1604 }
1605}