aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/lib.rs')
-rw-r--r--crates/ra_hir/src/lib.rs47
1 files changed, 36 insertions, 11 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index a0d99a84d..f1cc0ccd0 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -25,6 +25,8 @@ pub mod source_binder;
25mod krate; 25mod krate;
26mod module; 26mod module;
27mod function; 27mod function;
28mod adt;
29mod type_ref;
28mod ty; 30mod ty;
29 31
30use std::ops::Index; 32use std::ops::Index;
@@ -40,8 +42,10 @@ use crate::{
40pub use self::{ 42pub use self::{
41 path::{Path, PathKind}, 43 path::{Path, PathKind},
42 krate::Crate, 44 krate::Crate,
43 module::{Module, ModuleId, Problem, nameres::ItemMap, ModuleScope, Resolution}, 45 module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution},
44 function::{Function, FnScopes}, 46 function::{Function, FnScopes},
47 adt::{Struct, Enum},
48 ty::Ty,
45}; 49};
46 50
47pub use self::function::FnSignatureInfo; 51pub use self::function::FnSignatureInfo;
@@ -56,7 +60,11 @@ ra_db::impl_numeric_id!(DefId);
56pub(crate) enum DefKind { 60pub(crate) enum DefKind {
57 Module, 61 Module,
58 Function, 62 Function,
63 Struct,
64 Enum,
59 Item, 65 Item,
66
67 StructCtor,
60} 68}
61 69
62#[derive(Clone, Debug, PartialEq, Eq, Hash)] 70#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -68,18 +76,18 @@ pub struct DefLoc {
68} 76}
69 77
70impl DefKind { 78impl DefKind {
71 pub(crate) fn for_syntax_kind(kind: SyntaxKind) -> Option<DefKind> { 79 pub(crate) fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> {
72 match kind { 80 match kind {
73 SyntaxKind::FN_DEF => Some(DefKind::Function), 81 SyntaxKind::FN_DEF => PerNs::values(DefKind::Function),
74 SyntaxKind::MODULE => Some(DefKind::Module), 82 SyntaxKind::MODULE => PerNs::types(DefKind::Module),
83 SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor),
84 SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum),
75 // These define items, but don't have their own DefKinds yet: 85 // These define items, but don't have their own DefKinds yet:
76 SyntaxKind::STRUCT_DEF => Some(DefKind::Item), 86 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Item),
77 SyntaxKind::ENUM_DEF => Some(DefKind::Item), 87 SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Item),
78 SyntaxKind::TRAIT_DEF => Some(DefKind::Item), 88 SyntaxKind::CONST_DEF => PerNs::values(DefKind::Item),
79 SyntaxKind::TYPE_DEF => Some(DefKind::Item), 89 SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Item),
80 SyntaxKind::CONST_DEF => Some(DefKind::Item), 90 _ => PerNs::none(),
81 SyntaxKind::STATIC_DEF => Some(DefKind::Item),
82 _ => None,
83 } 91 }
84 } 92 }
85} 93}
@@ -99,6 +107,8 @@ impl DefLoc {
99pub enum Def { 107pub enum Def {
100 Module(Module), 108 Module(Module),
101 Function(Function), 109 Function(Function),
110 Struct(Struct),
111 Enum(Enum),
102 Item, 112 Item,
103} 113}
104 114
@@ -114,10 +124,25 @@ impl DefId {
114 let function = Function::new(self); 124 let function = Function::new(self);
115 Def::Function(function) 125 Def::Function(function)
116 } 126 }
127 DefKind::Struct => {
128 let struct_def = Struct::new(self);
129 Def::Struct(struct_def)
130 }
131 DefKind::Enum => {
132 let enum_def = Enum::new(self);
133 Def::Enum(enum_def)
134 }
135 DefKind::StructCtor => Def::Item,
117 DefKind::Item => Def::Item, 136 DefKind::Item => Def::Item,
118 }; 137 };
119 Ok(res) 138 Ok(res)
120 } 139 }
140
141 /// For a module, returns that module; for any other def, returns the containing module.
142 pub fn module(self, db: &impl HirDatabase) -> Cancelable<Module> {
143 let loc = self.loc(db);
144 Module::new(db, loc.source_root_id, loc.module_id)
145 }
121} 146}
122 147
123/// Identifier of item within a specific file. This is stable over reparses, so 148/// Identifier of item within a specific file. This is stable over reparses, so