diff options
author | Aleksey Kladov <[email protected]> | 2019-01-25 08:35:38 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-25 08:35:38 +0000 |
commit | 2ee2fba752c0f03ab8d1b7009bc56cc785738413 (patch) | |
tree | 656ed2e90603c06af1df84c6a9bc3a7c5046eff2 /crates/ra_hir/src | |
parent | 946aea3eb34d1b7f09900e017bb94708e6299d0a (diff) |
switch to positional enum variants
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 54 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/ids.rs | 15 |
3 files changed, 44 insertions, 38 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 6b13b464d..e0f515642 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -3,11 +3,14 @@ | |||
3 | 3 | ||
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | 5 | ||
6 | use ra_syntax::ast::{self, NameOwner, StructFlavor}; | 6 | use ra_syntax::{ |
7 | TreeArc, | ||
8 | ast::{self, NameOwner, StructFlavor} | ||
9 | }; | ||
7 | 10 | ||
8 | use crate::{ | 11 | use crate::{ |
9 | Name, AsName, Struct, Enum, EnumVariant, Crate, | 12 | Name, AsName, Struct, Enum, EnumVariant, Crate, |
10 | HirDatabase, | 13 | HirDatabase, HirFileId, |
11 | type_ref::TypeRef, | 14 | type_ref::TypeRef, |
12 | ids::LocationCtx, | 15 | ids::LocationCtx, |
13 | }; | 16 | }; |
@@ -55,6 +58,24 @@ impl StructData { | |||
55 | } | 58 | } |
56 | } | 59 | } |
57 | 60 | ||
61 | fn variants(enum_def: &ast::EnumDef) -> impl Iterator<Item = &ast::EnumVariant> { | ||
62 | enum_def | ||
63 | .variant_list() | ||
64 | .into_iter() | ||
65 | .flat_map(|it| it.variants()) | ||
66 | } | ||
67 | |||
68 | impl EnumVariant { | ||
69 | pub fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { | ||
70 | let (file_id, enum_def) = self.parent.source(db); | ||
71 | let var = variants(&*enum_def) | ||
72 | .nth(self.idx as usize) | ||
73 | .unwrap() | ||
74 | .to_owned(); | ||
75 | (file_id, var) | ||
76 | } | ||
77 | } | ||
78 | |||
58 | #[derive(Debug, Clone, PartialEq, Eq)] | 79 | #[derive(Debug, Clone, PartialEq, Eq)] |
59 | pub struct EnumData { | 80 | pub struct EnumData { |
60 | pub(crate) name: Option<Name>, | 81 | pub(crate) name: Option<Name>, |
@@ -68,22 +89,19 @@ impl EnumData { | |||
68 | } | 89 | } |
69 | 90 | ||
70 | pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> { | 91 | pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> { |
71 | let (file_id, enum_def) = e.source(db); | 92 | let (_file_id, enum_def) = e.source(db); |
72 | let module = e.module(db); | 93 | let variants = variants(&*enum_def) |
73 | let ctx = LocationCtx::new(db, module, file_id); | 94 | .enumerate() |
74 | let variants = if let Some(vl) = enum_def.variant_list() { | 95 | .filter_map(|(idx, variant_def)| { |
75 | vl.variants() | 96 | let name = variant_def.name()?.as_name(); |
76 | .filter_map(|variant_def| { | 97 | let var = EnumVariant { |
77 | let name = variant_def.name()?.as_name(); | 98 | parent: e, |
78 | let var = EnumVariant { | 99 | idx: idx as u32, |
79 | id: ctx.to_def(variant_def), | 100 | }; |
80 | }; | 101 | Some((name, var)) |
81 | Some((name, var)) | 102 | }) |
82 | }) | 103 | .collect(); |
83 | .collect() | 104 | |
84 | } else { | ||
85 | Vec::new() | ||
86 | }; | ||
87 | Arc::new(EnumData::new(&*enum_def, variants)) | 105 | Arc::new(EnumData::new(&*enum_def, variants)) |
88 | } | 106 | } |
89 | } | 107 | } |
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index a469ad477..4fc47aba5 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -15,7 +15,7 @@ use crate::{ | |||
15 | generics::GenericParams, | 15 | generics::GenericParams, |
16 | docs::{Documentation, Docs, docs_from_ast}, | 16 | docs::{Documentation, Docs, docs_from_ast}, |
17 | module_tree::ModuleId, | 17 | module_tree::ModuleId, |
18 | ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, | 18 | ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | /// hir::Crate describes a single crate. It's the main interface with which | 21 | /// hir::Crate describes a single crate. It's the main interface with which |
@@ -269,18 +269,19 @@ impl Docs for Enum { | |||
269 | 269 | ||
270 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 270 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
271 | pub struct EnumVariant { | 271 | pub struct EnumVariant { |
272 | pub(crate) id: EnumVariantId, | 272 | pub(crate) parent: Enum, |
273 | pub(crate) idx: u32, | ||
273 | } | 274 | } |
274 | 275 | ||
275 | impl EnumVariant { | 276 | impl EnumVariant { |
276 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { | 277 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { |
277 | self.id.source(db) | 278 | self.source_impl(db) |
278 | } | 279 | } |
279 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 280 | pub fn module(&self, db: &impl HirDatabase) -> Module { |
280 | self.id.module(db) | 281 | self.parent.module(db) |
281 | } | 282 | } |
282 | pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum { | 283 | pub fn parent_enum(&self, _db: &impl HirDatabase) -> Enum { |
283 | db.enum_variant_data(*self).parent_enum.clone() | 284 | self.parent |
284 | } | 285 | } |
285 | 286 | ||
286 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 287 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { |
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index b125a1518..5272656ec 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
@@ -18,7 +18,6 @@ pub struct HirInterner { | |||
18 | fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>, | 18 | fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>, |
19 | structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>, | 19 | structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>, |
20 | enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>, | 20 | enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>, |
21 | enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>, | ||
22 | consts: LocationIntener<ItemLoc<ast::ConstDef>, ConstId>, | 21 | consts: LocationIntener<ItemLoc<ast::ConstDef>, ConstId>, |
23 | statics: LocationIntener<ItemLoc<ast::StaticDef>, StaticId>, | 22 | statics: LocationIntener<ItemLoc<ast::StaticDef>, StaticId>, |
24 | traits: LocationIntener<ItemLoc<ast::TraitDef>, TraitId>, | 23 | traits: LocationIntener<ItemLoc<ast::TraitDef>, TraitId>, |
@@ -31,7 +30,6 @@ impl HirInterner { | |||
31 | + self.fns.len() | 30 | + self.fns.len() |
32 | + self.structs.len() | 31 | + self.structs.len() |
33 | + self.enums.len() | 32 | + self.enums.len() |
34 | + self.enum_variants.len() | ||
35 | + self.consts.len() | 33 | + self.consts.len() |
36 | + self.statics.len() | 34 | + self.statics.len() |
37 | + self.traits.len() | 35 | + self.traits.len() |
@@ -262,15 +260,6 @@ impl AstItemDef<ast::EnumDef> for EnumId { | |||
262 | } | 260 | } |
263 | 261 | ||
264 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 262 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
265 | pub struct EnumVariantId(RawId); | ||
266 | impl_arena_id!(EnumVariantId); | ||
267 | impl AstItemDef<ast::EnumVariant> for EnumVariantId { | ||
268 | fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::EnumVariant>, Self> { | ||
269 | &interner.enum_variants | ||
270 | } | ||
271 | } | ||
272 | |||
273 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
274 | pub struct ConstId(RawId); | 263 | pub struct ConstId(RawId); |
275 | impl_arena_id!(ConstId); | 264 | impl_arena_id!(ConstId); |
276 | impl AstItemDef<ast::ConstDef> for ConstId { | 265 | impl AstItemDef<ast::ConstDef> for ConstId { |
@@ -342,9 +331,7 @@ impl SourceFileItems { | |||
342 | // change parent's id. This means that, say, adding a new function to a | 331 | // change parent's id. This means that, say, adding a new function to a |
343 | // trait does not chage ids of top-level items, which helps caching. | 332 | // trait does not chage ids of top-level items, which helps caching. |
344 | bfs(source_file.syntax(), |it| { | 333 | bfs(source_file.syntax(), |it| { |
345 | if let Some(enum_variant) = ast::EnumVariant::cast(it) { | 334 | if let Some(module_item) = ast::ModuleItem::cast(it) { |
346 | self.alloc(enum_variant.syntax().to_owned()); | ||
347 | } else if let Some(module_item) = ast::ModuleItem::cast(it) { | ||
348 | self.alloc(module_item.syntax().to_owned()); | 335 | self.alloc(module_item.syntax().to_owned()); |
349 | } else if let Some(macro_call) = ast::MacroCall::cast(it) { | 336 | } else if let Some(macro_call) = ast::MacroCall::cast(it) { |
350 | self.alloc(macro_call.syntax().to_owned()); | 337 | self.alloc(macro_call.syntax().to_owned()); |