diff options
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 725bc7d80..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 | ||
3 | use relative_path::RelativePathBuf; | 3 | use relative_path::RelativePathBuf; |
4 | use ra_db::{CrateId, Cancelable, FileId}; | 4 | use ra_db::{CrateId, Cancelable, FileId}; |
5 | use ra_syntax::{ast, TreePtr, SyntaxNode}; | 5 | use ra_syntax::{ast, TreePtr, SyntaxNode, AstNode}; |
6 | 6 | ||
7 | use crate::{ | 7 | use 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, |
@@ -181,6 +181,19 @@ impl Struct { | |||
181 | .collect(); | 181 | .collect(); |
182 | Ok(res) | 182 | Ok(res) |
183 | } | 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 | } | ||
184 | } | 197 | } |
185 | 198 | ||
186 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 199 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -204,6 +217,16 @@ impl Enum { | |||
204 | pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> { | 217 | pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> { |
205 | Ok(db.enum_data(self.def_id)?.variants.clone()) | 218 | Ok(db.enum_data(self.def_id)?.variants.clone()) |
206 | } | 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 | } | ||
207 | } | 230 | } |
208 | 231 | ||
209 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 232 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -231,6 +254,19 @@ impl EnumVariant { | |||
231 | pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> { | 254 | pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> { |
232 | Ok(db.enum_variant_data(self.def_id)?.variant_data.clone()) | 255 | Ok(db.enum_variant_data(self.def_id)?.variant_data.clone()) |
233 | } | 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 | } | ||
234 | } | 270 | } |
235 | 271 | ||
236 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 272 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -241,11 +277,16 @@ pub struct Function { | |||
241 | /// The declared signature of a function. | 277 | /// The declared signature of a function. |
242 | #[derive(Debug, Clone, PartialEq, Eq)] | 278 | #[derive(Debug, Clone, PartialEq, Eq)] |
243 | pub struct FnSignature { | 279 | pub struct FnSignature { |
280 | pub(crate) name: Name, | ||
244 | pub(crate) args: Vec<TypeRef>, | 281 | pub(crate) args: Vec<TypeRef>, |
245 | pub(crate) ret_type: TypeRef, | 282 | pub(crate) ret_type: TypeRef, |
246 | } | 283 | } |
247 | 284 | ||
248 | impl FnSignature { | 285 | impl FnSignature { |
286 | pub fn name(&self) -> &Name { | ||
287 | &self.name | ||
288 | } | ||
289 | |||
249 | pub fn args(&self) -> &[TypeRef] { | 290 | pub fn args(&self) -> &[TypeRef] { |
250 | &self.args | 291 | &self.args |
251 | } | 292 | } |
@@ -260,8 +301,8 @@ impl Function { | |||
260 | self.def_id | 301 | self.def_id |
261 | } | 302 | } |
262 | 303 | ||
263 | pub fn source(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> { | 304 | pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreePtr<ast::FnDef>)> { |
264 | self.source_impl(db) | 305 | Ok(self.source_impl(db)) |
265 | } | 306 | } |
266 | 307 | ||
267 | 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>> { |