diff options
author | Aleksey Kladov <[email protected]> | 2019-01-24 21:02:18 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-24 21:02:18 +0000 |
commit | 90215eb5a026d446ae7e5d4e62c43d6d8c82edf1 (patch) | |
tree | 75b35f218e07a8c6feba4e6204a9da762c2a9089 | |
parent | 1a5f32fcb516c1647c7581f21be9b11c8f1f94ba (diff) |
generalize boilerplate
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 37 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/function.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/ids.rs | 49 |
3 files changed, 51 insertions, 39 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index e16013956..1fa591ea4 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -16,7 +16,7 @@ use crate::{ | |||
16 | code_model_impl::def_id_to_ast, | 16 | code_model_impl::def_id_to_ast, |
17 | docs::{Documentation, Docs, docs_from_ast}, | 17 | docs::{Documentation, Docs, docs_from_ast}, |
18 | module_tree::ModuleId, | 18 | module_tree::ModuleId, |
19 | ids::{FunctionId, StructId, EnumId, EnumVariantId}, | 19 | ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef}, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | /// hir::Crate describes a single crate. It's the main interface with which | 22 | /// hir::Crate describes a single crate. It's the main interface with which |
@@ -197,8 +197,12 @@ pub struct Struct { | |||
197 | } | 197 | } |
198 | 198 | ||
199 | impl Struct { | 199 | impl Struct { |
200 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { | ||
201 | self.id.source(db) | ||
202 | } | ||
203 | |||
200 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 204 | pub fn module(&self, db: &impl HirDatabase) -> Module { |
201 | self.id.loc(db).module | 205 | self.id.module(db) |
202 | } | 206 | } |
203 | 207 | ||
204 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 208 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { |
@@ -217,10 +221,6 @@ impl Struct { | |||
217 | .collect() | 221 | .collect() |
218 | } | 222 | } |
219 | 223 | ||
220 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { | ||
221 | self.id.loc(db).source(db) | ||
222 | } | ||
223 | |||
224 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { | 224 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { |
225 | db.generic_params((*self).into()) | 225 | db.generic_params((*self).into()) |
226 | } | 226 | } |
@@ -238,8 +238,12 @@ pub struct Enum { | |||
238 | } | 238 | } |
239 | 239 | ||
240 | impl Enum { | 240 | impl Enum { |
241 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { | ||
242 | self.id.source(db) | ||
243 | } | ||
244 | |||
241 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 245 | pub fn module(&self, db: &impl HirDatabase) -> Module { |
242 | self.id.loc(db).module | 246 | self.id.module(db) |
243 | } | 247 | } |
244 | 248 | ||
245 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 249 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { |
@@ -250,10 +254,6 @@ impl Enum { | |||
250 | db.enum_data(*self).variants.clone() | 254 | db.enum_data(*self).variants.clone() |
251 | } | 255 | } |
252 | 256 | ||
253 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { | ||
254 | self.id.loc(db).source(db) | ||
255 | } | ||
256 | |||
257 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { | 257 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { |
258 | db.generic_params((*self).into()) | 258 | db.generic_params((*self).into()) |
259 | } | 259 | } |
@@ -271,8 +271,11 @@ pub struct EnumVariant { | |||
271 | } | 271 | } |
272 | 272 | ||
273 | impl EnumVariant { | 273 | impl EnumVariant { |
274 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { | ||
275 | self.id.source(db) | ||
276 | } | ||
274 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 277 | pub fn module(&self, db: &impl HirDatabase) -> Module { |
275 | self.id.loc(db).module | 278 | self.id.module(db) |
276 | } | 279 | } |
277 | pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum { | 280 | pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum { |
278 | db.enum_variant_data(*self).parent_enum.clone() | 281 | db.enum_variant_data(*self).parent_enum.clone() |
@@ -296,10 +299,6 @@ impl EnumVariant { | |||
296 | }) | 299 | }) |
297 | .collect() | 300 | .collect() |
298 | } | 301 | } |
299 | |||
300 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { | ||
301 | self.id.loc(db).source(db) | ||
302 | } | ||
303 | } | 302 | } |
304 | 303 | ||
305 | impl Docs for EnumVariant { | 304 | impl Docs for EnumVariant { |
@@ -348,7 +347,11 @@ impl FnSignature { | |||
348 | 347 | ||
349 | impl Function { | 348 | impl Function { |
350 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { | 349 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { |
351 | self.id.loc(db).source(db) | 350 | self.id.source(db) |
351 | } | ||
352 | |||
353 | pub fn module(&self, db: &impl HirDatabase) -> Module { | ||
354 | self.id.module(db) | ||
352 | } | 355 | } |
353 | 356 | ||
354 | pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> { | 357 | pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> { |
diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs index 8a2ab5714..6ce5c77b1 100644 --- a/crates/ra_hir/src/code_model_impl/function.rs +++ b/crates/ra_hir/src/code_model_impl/function.rs | |||
@@ -30,10 +30,6 @@ impl Function { | |||
30 | db.body_hir(*self) | 30 | db.body_hir(*self) |
31 | } | 31 | } |
32 | 32 | ||
33 | pub(crate) fn module(&self, db: &impl HirDatabase) -> Module { | ||
34 | self.id.loc(db).module | ||
35 | } | ||
36 | |||
37 | /// The containing impl block, if this is a method. | 33 | /// The containing impl block, if this is a method. |
38 | pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Option<ImplBlock> { | 34 | pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Option<ImplBlock> { |
39 | let module_impls = db.impls_in_module(self.module(db)); | 35 | let module_impls = db.impls_in_module(self.module(db)); |
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 7ed5dbec7..262047849 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
@@ -1,8 +1,11 @@ | |||
1 | use std::marker::PhantomData; | 1 | use std::{ |
2 | marker::PhantomData, | ||
3 | hash::Hash, | ||
4 | }; | ||
2 | 5 | ||
3 | use ra_db::{LocationIntener, FileId}; | 6 | use ra_db::{LocationIntener, FileId}; |
4 | use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, ast}; | 7 | use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, ast}; |
5 | use ra_arena::{Arena, RawId, impl_arena_id}; | 8 | use ra_arena::{Arena, RawId, ArenaId, impl_arena_id}; |
6 | 9 | ||
7 | use crate::{ | 10 | use crate::{ |
8 | HirDatabase, Def, | 11 | HirDatabase, Def, |
@@ -179,43 +182,53 @@ impl<N: AstNode> Clone for ItemLoc<N> { | |||
179 | } | 182 | } |
180 | } | 183 | } |
181 | 184 | ||
185 | pub(crate) trait AstItemDef<N: AstNode + Eq + Hash>: ArenaId + Clone { | ||
186 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<N>, Self>; | ||
187 | fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<N>) { | ||
188 | let int = Self::interner(db.as_ref()); | ||
189 | let loc = int.id2loc(self); | ||
190 | loc.source(db) | ||
191 | } | ||
192 | fn module(self, db: &impl HirDatabase) -> Module { | ||
193 | let int = Self::interner(db.as_ref()); | ||
194 | let loc = int.id2loc(self); | ||
195 | loc.module | ||
196 | } | ||
197 | } | ||
198 | |||
182 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 199 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
183 | pub struct FunctionId(RawId); | 200 | pub struct FunctionId(RawId); |
184 | impl_arena_id!(FunctionId); | 201 | impl_arena_id!(FunctionId); |
185 | 202 | impl AstItemDef<ast::FnDef> for FunctionId { | |
186 | impl FunctionId { | 203 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::FnDef>, Self> { |
187 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::FnDef> { | 204 | &interner.fns |
188 | db.as_ref().fns.id2loc(self) | ||
189 | } | 205 | } |
190 | } | 206 | } |
191 | 207 | ||
192 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 208 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
193 | pub struct StructId(RawId); | 209 | pub struct StructId(RawId); |
194 | impl_arena_id!(StructId); | 210 | impl_arena_id!(StructId); |
195 | 211 | impl AstItemDef<ast::StructDef> for StructId { | |
196 | impl StructId { | 212 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StructDef>, Self> { |
197 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::StructDef> { | 213 | &interner.structs |
198 | db.as_ref().structs.id2loc(self) | ||
199 | } | 214 | } |
200 | } | 215 | } |
201 | 216 | ||
202 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 217 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
203 | pub struct EnumId(RawId); | 218 | pub struct EnumId(RawId); |
204 | impl_arena_id!(EnumId); | 219 | impl_arena_id!(EnumId); |
205 | 220 | impl AstItemDef<ast::EnumDef> for EnumId { | |
206 | impl EnumId { | 221 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::EnumDef>, Self> { |
207 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::EnumDef> { | 222 | &interner.enums |
208 | db.as_ref().enums.id2loc(self) | ||
209 | } | 223 | } |
210 | } | 224 | } |
211 | 225 | ||
212 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 226 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
213 | pub struct EnumVariantId(RawId); | 227 | pub struct EnumVariantId(RawId); |
214 | impl_arena_id!(EnumVariantId); | 228 | impl_arena_id!(EnumVariantId); |
215 | 229 | impl AstItemDef<ast::EnumVariant> for EnumVariantId { | |
216 | impl EnumVariantId { | 230 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::EnumVariant>, Self> { |
217 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::EnumVariant> { | 231 | &interner.enum_variants |
218 | db.as_ref().enum_variants.id2loc(self) | ||
219 | } | 232 | } |
220 | } | 233 | } |
221 | 234 | ||