aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs83
1 files changed, 78 insertions, 5 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index fa3e4baa7..e69f546ff 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -2,10 +2,10 @@ use std::sync::Arc;
2 2
3use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
4use ra_db::{CrateId, Cancelable, FileId}; 4use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreePtr, SyntaxNode}; 5use ra_syntax::{ast, TreePtr, SyntaxNode, AstNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::ModuleScope, 10 nameres::ModuleScope,
11 db::HirDatabase, 11 db::HirDatabase,
@@ -44,6 +44,7 @@ pub enum Def {
44 Module(Module), 44 Module(Module),
45 Struct(Struct), 45 Struct(Struct),
46 Enum(Enum), 46 Enum(Enum),
47 EnumVariant(EnumVariant),
47 Function(Function), 48 Function(Function),
48 Item, 49 Item,
49} 50}
@@ -180,6 +181,19 @@ impl Struct {
180 .collect(); 181 .collect();
181 Ok(res) 182 Ok(res)
182 } 183 }
184
185 pub fn source(
186 &self,
187 db: &impl HirDatabase,
188 ) -> Cancelable<(HirFileId, TreePtr<ast::StructDef>)> {
189 let (file_id, syntax) = self.def_id.source(db);
190 Ok((
191 file_id,
192 ast::StructDef::cast(&syntax)
193 .expect("struct def should point to StructDef node")
194 .to_owned(),
195 ))
196 }
183} 197}
184 198
185#[derive(Debug, Clone, PartialEq, Eq, Hash)] 199#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -188,6 +202,10 @@ pub struct Enum {
188} 202}
189 203
190impl Enum { 204impl Enum {
205 pub(crate) fn new(def_id: DefId) -> Self {
206 Enum { def_id }
207 }
208
191 pub fn def_id(&self) -> DefId { 209 pub fn def_id(&self) -> DefId {
192 self.def_id 210 self.def_id
193 } 211 }
@@ -196,9 +214,59 @@ impl Enum {
196 Ok(db.enum_data(self.def_id)?.name.clone()) 214 Ok(db.enum_data(self.def_id)?.name.clone())
197 } 215 }
198 216
199 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> { 217 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> {
200 Ok(db.enum_data(self.def_id)?.variants.clone()) 218 Ok(db.enum_data(self.def_id)?.variants.clone())
201 } 219 }
220
221 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreePtr<ast::EnumDef>)> {
222 let (file_id, syntax) = self.def_id.source(db);
223 Ok((
224 file_id,
225 ast::EnumDef::cast(&syntax)
226 .expect("enum def should point to EnumDef node")
227 .to_owned(),
228 ))
229 }
230}
231
232#[derive(Debug, Clone, PartialEq, Eq, Hash)]
233pub struct EnumVariant {
234 pub(crate) def_id: DefId,
235}
236
237impl EnumVariant {
238 pub(crate) fn new(def_id: DefId) -> Self {
239 EnumVariant { def_id }
240 }
241
242 pub fn def_id(&self) -> DefId {
243 self.def_id
244 }
245
246 pub fn parent_enum(&self, db: &impl HirDatabase) -> Cancelable<Enum> {
247 Ok(db.enum_variant_data(self.def_id)?.parent_enum.clone())
248 }
249
250 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
251 Ok(db.enum_variant_data(self.def_id)?.name.clone())
252 }
253
254 pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
255 Ok(db.enum_variant_data(self.def_id)?.variant_data.clone())
256 }
257
258 pub fn source(
259 &self,
260 db: &impl HirDatabase,
261 ) -> Cancelable<(HirFileId, TreePtr<ast::EnumVariant>)> {
262 let (file_id, syntax) = self.def_id.source(db);
263 Ok((
264 file_id,
265 ast::EnumVariant::cast(&syntax)
266 .expect("variant def should point to EnumVariant node")
267 .to_owned(),
268 ))
269 }
202} 270}
203 271
204#[derive(Debug, Clone, PartialEq, Eq, Hash)] 272#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -209,11 +277,16 @@ pub struct Function {
209/// The declared signature of a function. 277/// The declared signature of a function.
210#[derive(Debug, Clone, PartialEq, Eq)] 278#[derive(Debug, Clone, PartialEq, Eq)]
211pub struct FnSignature { 279pub struct FnSignature {
280 pub(crate) name: Name,
212 pub(crate) args: Vec<TypeRef>, 281 pub(crate) args: Vec<TypeRef>,
213 pub(crate) ret_type: TypeRef, 282 pub(crate) ret_type: TypeRef,
214} 283}
215 284
216impl FnSignature { 285impl FnSignature {
286 pub fn name(&self) -> &Name {
287 &self.name
288 }
289
217 pub fn args(&self) -> &[TypeRef] { 290 pub fn args(&self) -> &[TypeRef] {
218 &self.args 291 &self.args
219 } 292 }
@@ -228,8 +301,8 @@ impl Function {
228 self.def_id 301 self.def_id
229 } 302 }
230 303
231 pub fn source(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> { 304 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreePtr<ast::FnDef>)> {
232 self.source_impl(db) 305 Ok(self.source_impl(db))
233 } 306 }
234 307
235 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 308 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {