From f193fbcbae0783953cfaa88aaec6a8d4e1255007 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 21:02:12 +0300 Subject: actually produce missing def kinds --- crates/ra_hir/src/code_model_api.rs | 48 +++++++++++++++++++++++++++++++++++++ crates/ra_hir/src/ids.rs | 32 ++++++++++++++++++++----- crates/ra_hir/src/lib.rs | 2 ++ crates/ra_hir/src/ty.rs | 8 +++++-- 4 files changed, 82 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 5f61bb589..d4244f70c 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -41,12 +41,17 @@ impl Crate { } } +#[derive(Debug)] pub enum Def { Module(Module), Struct(Struct), Enum(Enum), EnumVariant(EnumVariant), Function(Function), + Const(Const), + Static(Static), + Trait(Trait), + Type(Type), Item, } @@ -317,17 +322,60 @@ pub struct Const { pub(crate) def_id: DefId, } +impl Const { + pub(crate) fn new(def_id: DefId) -> Const { + Const { def_id } + } + + pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc)> { + Ok(def_id_to_ast(db, self.def_id)) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Static { pub(crate) def_id: DefId, } +impl Static { + pub(crate) fn new(def_id: DefId) -> Static { + Static { def_id } + } + + pub fn source( + &self, + db: &impl HirDatabase, + ) -> Cancelable<(HirFileId, TreeArc)> { + Ok(def_id_to_ast(db, self.def_id)) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Trait { pub(crate) def_id: DefId, } +impl Trait { + pub(crate) fn new(def_id: DefId) -> Trait { + Trait { def_id } + } + + pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc)> { + Ok(def_id_to_ast(db, self.def_id)) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Type { pub(crate) def_id: DefId, } + +impl Type { + pub(crate) fn new(def_id: DefId) -> Type { + Type { def_id } + } + + pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc)> { + Ok(def_id_to_ast(db, self.def_id)) + } +} diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 0805fd3db..316896dce 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -4,11 +4,10 @@ use ra_arena::{Arena, RawId, impl_arena_id}; use crate::{ HirDatabase, PerNs, Def, Function, Struct, Enum, EnumVariant, ImplBlock, Crate, + Module, Trait, Type, Static, Const, module_tree::ModuleId, }; -use crate::code_model_api::Module; - /// hir makes heavy use of ids: integer (u32) handlers to various things. You /// can think of id as a pointer (but without a lifetime) or a file descriptor /// (but for hir objects). @@ -146,6 +145,10 @@ pub(crate) enum DefKind { Struct, Enum, EnumVariant, + Const, + Static, + Trait, + Type, Item, StructCtor, @@ -173,6 +176,23 @@ impl DefId { } DefKind::Enum => Def::Enum(Enum::new(self)), DefKind::EnumVariant => Def::EnumVariant(EnumVariant::new(self)), + DefKind::Const => { + let def = Const::new(self); + Def::Const(def) + } + DefKind::Static => { + let def = Static::new(self); + Def::Static(def) + } + DefKind::Trait => { + let def = Trait::new(self); + Def::Trait(def) + } + DefKind::Type => { + let def = Type::new(self); + Def::Type(def) + } + DefKind::StructCtor => Def::Item, DefKind::Item => Def::Item, }; @@ -218,10 +238,10 @@ impl DefKind { SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor), SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum), // These define items, but don't have their own DefKinds yet: - SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Item), - SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Item), - SyntaxKind::CONST_DEF => PerNs::values(DefKind::Item), - SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Item), + SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), + SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), + SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const), + SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static), _ => PerNs::none(), } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index fe8be5700..45dda4f7f 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -60,4 +60,6 @@ pub use self::code_model_api::{ Module, ModuleSource, Problem, Struct, Enum, EnumVariant, Function, FnSignature, ScopeEntryWithSyntax, + Static, Const, + Trait, Type, }; diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 18c41a015..0c24a0652 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -470,8 +470,12 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable type_for_struct(db, s), Def::Enum(e) => type_for_enum(db, e), Def::EnumVariant(ev) => type_for_enum_variant(db, ev), - Def::Item => { - log::debug!("trying to get type for item of unknown type {:?}", def_id); + _ => { + log::debug!( + "trying to get type for item of unknown type {:?} {:?}", + def_id, + def + ); Ok(Ty::Unknown) } } -- cgit v1.2.3