Remove AdtOrTrait

This commit is contained in:
vsrs 2020-06-11 14:41:42 +03:00
parent 7e986d1504
commit 7ec0064409
4 changed files with 39 additions and 50 deletions

View File

@ -186,6 +186,22 @@ pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility>
module.visibility_of(db, self)
}
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
match self {
ModuleDef::Adt(it) => Some(it.name(db)),
ModuleDef::Trait(it) => Some(it.name(db)),
ModuleDef::Function(it) => Some(it.name(db)),
ModuleDef::EnumVariant(it) => Some(it.name(db)),
ModuleDef::TypeAlias(it) => Some(it.name(db)),
ModuleDef::Module(it) => it.name(db),
ModuleDef::Const(it) => it.name(db),
ModuleDef::Static(it) => it.name(db),
ModuleDef::BuiltinType(it) => Some(it.as_name()),
}
}
}
pub use hir_def::{
@ -1382,8 +1398,8 @@ fn derived(&self, ty: Ty) -> Type {
}
/// Returns a flattened list of all ADTs and Traits mentioned in the type
pub fn flattened_type_items(&self, db: &dyn HirDatabase) -> Vec<AdtOrTrait> {
fn push_new_item(item: AdtOrTrait, acc: &mut Vec<AdtOrTrait>) {
pub fn flattened_type_items(&self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
fn push_new_item(item: ModuleDef, acc: &mut Vec<ModuleDef>) {
if !acc.contains(&item) {
acc.push(item);
}
@ -1392,7 +1408,7 @@ fn push_new_item(item: AdtOrTrait, acc: &mut Vec<AdtOrTrait>) {
fn push_bounds(
db: &dyn HirDatabase,
predicates: &[GenericPredicate],
acc: &mut Vec<AdtOrTrait>,
acc: &mut Vec<ModuleDef>,
) {
for p in predicates.iter() {
match p {
@ -1407,13 +1423,13 @@ fn push_bounds(
}
// TypeWalk::walk does not preserve items order!
fn walk_substs(db: &dyn HirDatabase, substs: &Substs, acc: &mut Vec<AdtOrTrait>) {
fn walk_substs(db: &dyn HirDatabase, substs: &Substs, acc: &mut Vec<ModuleDef>) {
for ty in substs.iter() {
walk_type(db, ty, acc);
}
}
fn walk_type(db: &dyn HirDatabase, ty: &Ty, acc: &mut Vec<AdtOrTrait>) {
fn walk_type(db: &dyn HirDatabase, ty: &Ty, acc: &mut Vec<ModuleDef>) {
match ty.strip_references() {
Ty::Apply(ApplicationTy { ctor, parameters, .. }) => {
match ctor {
@ -1468,7 +1484,7 @@ fn walk_type(db: &dyn HirDatabase, ty: &Ty, acc: &mut Vec<AdtOrTrait>) {
}
}
let mut res: Vec<AdtOrTrait> = Vec::new(); // not a Set to preserve the order
let mut res: Vec<ModuleDef> = Vec::new(); // not a Set to preserve the order
walk_type(db, &self.ty.value, &mut res);
res
}
@ -1580,26 +1596,3 @@ fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
vis.is_visible_from(db.upcast(), module.id)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AdtOrTrait {
Adt(Adt),
Trait(Trait),
}
impl_froms!(AdtOrTrait: Adt, Trait);
impl AdtOrTrait {
pub fn module(self, db: &dyn HirDatabase) -> Module {
match self {
AdtOrTrait::Adt(adt) => adt.module(db),
AdtOrTrait::Trait(trait_) => trait_.module(db),
}
}
pub fn name(self, db: &dyn HirDatabase) -> Name {
match self {
AdtOrTrait::Adt(adt) => adt.name(db),
AdtOrTrait::Trait(trait_) => trait_.name(db),
}
}
}

View File

@ -51,10 +51,10 @@ fn from(it: $sv) -> $e {
pub use crate::{
code_model::{
Adt, AdtOrTrait, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate,
CrateDependency, DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function,
GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef,
Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency,
DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function, GenericDef, HasAttrs,
HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
},
has_source::HasSource,
semantics::{original_range, PathResolution, Semantics, SemanticsScope},

View File

@ -321,15 +321,6 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
}
}
impl ToNav for hir::AdtOrTrait {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
match self {
hir::AdtOrTrait::Adt(adt) => adt.to_nav(db),
hir::AdtOrTrait::Trait(trait_) => trait_.to_nav(db),
}
}
}
impl ToNav for hir::AssocItem {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
match self {

View File

@ -1,8 +1,8 @@
use std::iter::once;
use hir::{
Adt, AdtOrTrait, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource,
HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay,
Module, ModuleDef, ModuleSource, Semantics,
};
use itertools::Itertools;
use ra_db::SourceDatabase;
@ -13,7 +13,9 @@
use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
use crate::{
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel, ToNav},
display::{
macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel, ToNav, TryToNav,
},
runnables::runnable,
FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
};
@ -238,9 +240,11 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
.ty(db)
.flattened_type_items(db)
.into_iter()
.map(|it| HoverGotoTypeData {
mod_path: adt_or_trait_mod_path(db, &it),
nav: it.to_nav(db),
.filter_map(|it| {
Some(HoverGotoTypeData {
mod_path: mod_path(db, &it)?,
nav: it.try_to_nav(db)?,
})
})
.collect_vec();
@ -294,8 +298,9 @@ fn determine_mod_path(db: &RootDatabase, module: Module, name: Option<String>) -
.join("::")
}
fn adt_or_trait_mod_path(db: &RootDatabase, item: &AdtOrTrait) -> String {
determine_mod_path(db, item.module(db), Some(item.name(db).to_string()))
// returns None only for ModuleDef::BuiltinType
fn mod_path(db: &RootDatabase, item: &ModuleDef) -> Option<String> {
Some(determine_mod_path(db, item.module(db)?, item.name(db).map(|name| name.to_string())))
}
fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {