aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorvsrs <[email protected]>2020-06-11 12:41:42 +0100
committervsrs <[email protected]>2020-06-18 08:15:43 +0100
commit7ec0064409f90334f6b0dd61e572a65702702985 (patch)
tree4a97c35ea8f6defd4718fdfdc801f3646105de7e /crates
parent7e986d1504e6fd6dc1fc9b64f5fb9eac2bef952a (diff)
Remove AdtOrTrait
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs51
-rw-r--r--crates/ra_hir/src/lib.rs8
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs9
-rw-r--r--crates/ra_ide/src/hover.rs21
4 files changed, 39 insertions, 50 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}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index eded039e4..3364a822f 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -51,10 +51,10 @@ mod has_source;
51 51
52pub use crate::{ 52pub use crate::{
53 code_model::{ 53 code_model::{
54 Adt, AdtOrTrait, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, 54 Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency,
55 CrateDependency, DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function, 55 DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function, GenericDef, HasAttrs,
56 GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, 56 HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
57 Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, 57 Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
58 }, 58 },
59 has_source::HasSource, 59 has_source::HasSource,
60 semantics::{original_range, PathResolution, Semantics, SemanticsScope}, 60 semantics::{original_range, PathResolution, Semantics, SemanticsScope},
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs
index 325b247bb..c7bb1e69f 100644
--- a/crates/ra_ide/src/display/navigation_target.rs
+++ b/crates/ra_ide/src/display/navigation_target.rs
@@ -321,15 +321,6 @@ impl ToNav for hir::Adt {
321 } 321 }
322} 322}
323 323
324impl ToNav for hir::AdtOrTrait {
325 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
326 match self {
327 hir::AdtOrTrait::Adt(adt) => adt.to_nav(db),
328 hir::AdtOrTrait::Trait(trait_) => trait_.to_nav(db),
329 }
330 }
331}
332
333impl ToNav for hir::AssocItem { 324impl ToNav for hir::AssocItem {
334 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 325 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
335 match self { 326 match self {
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index 2a06006e1..045713519 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -1,8 +1,8 @@
1use std::iter::once; 1use std::iter::once;
2 2
3use hir::{ 3use hir::{
4 Adt, AdtOrTrait, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, 4 Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay,
5 HirDisplay, Module, ModuleDef, ModuleSource, Semantics, 5 Module, ModuleDef, ModuleSource, Semantics,
6}; 6};
7use itertools::Itertools; 7use itertools::Itertools;
8use ra_db::SourceDatabase; 8use ra_db::SourceDatabase;
@@ -13,7 +13,9 @@ use ra_ide_db::{
13use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset}; 13use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
14 14
15use crate::{ 15use crate::{
16 display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel, ToNav}, 16 display::{
17 macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel, ToNav, TryToNav,
18 },
17 runnables::runnable, 19 runnables::runnable,
18 FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, 20 FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
19}; 21};
@@ -238,9 +240,11 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
238 .ty(db) 240 .ty(db)
239 .flattened_type_items(db) 241 .flattened_type_items(db)
240 .into_iter() 242 .into_iter()
241 .map(|it| HoverGotoTypeData { 243 .filter_map(|it| {
242 mod_path: adt_or_trait_mod_path(db, &it), 244 Some(HoverGotoTypeData {
243 nav: it.to_nav(db), 245 mod_path: mod_path(db, &it)?,
246 nav: it.try_to_nav(db)?,
247 })
244 }) 248 })
245 .collect_vec(); 249 .collect_vec();
246 250
@@ -294,8 +298,9 @@ fn determine_mod_path(db: &RootDatabase, module: Module, name: Option<String>) -
294 .join("::") 298 .join("::")
295} 299}
296 300
297fn adt_or_trait_mod_path(db: &RootDatabase, item: &AdtOrTrait) -> String { 301// returns None only for ModuleDef::BuiltinType
298 determine_mod_path(db, item.module(db), Some(item.name(db).to_string())) 302fn mod_path(db: &RootDatabase, item: &ModuleDef) -> Option<String> {
303 Some(determine_mod_path(db, item.module(db)?, item.name(db).map(|name| name.to_string())))
299} 304}
300 305
301fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> { 306fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {