diff options
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 65c461148..e839a5a90 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -1,10 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::{SmolStr, ast::{self, NameOwner, StructFlavor}}; | 3 | use ra_syntax::ast::{self, NameOwner, StructFlavor}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | DefId, Cancelable, | 6 | DefId, Cancelable, Name, AsName, |
7 | db::{HirDatabase}, | 7 | db::HirDatabase, |
8 | type_ref::TypeRef, | 8 | type_ref::TypeRef, |
9 | }; | 9 | }; |
10 | 10 | ||
@@ -29,26 +29,26 @@ impl Struct { | |||
29 | Ok(db.struct_data(self.def_id)?) | 29 | Ok(db.struct_data(self.def_id)?) |
30 | } | 30 | } |
31 | 31 | ||
32 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> { | 32 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { |
33 | Ok(db.struct_data(self.def_id)?.name.clone()) | 33 | Ok(db.struct_data(self.def_id)?.name.clone()) |
34 | } | 34 | } |
35 | } | 35 | } |
36 | 36 | ||
37 | #[derive(Debug, Clone, PartialEq, Eq)] | 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
38 | pub struct StructData { | 38 | pub struct StructData { |
39 | name: Option<SmolStr>, | 39 | name: Option<Name>, |
40 | variant_data: Arc<VariantData>, | 40 | variant_data: Arc<VariantData>, |
41 | } | 41 | } |
42 | 42 | ||
43 | impl StructData { | 43 | impl StructData { |
44 | pub(crate) fn new(struct_def: ast::StructDef) -> StructData { | 44 | pub(crate) fn new(struct_def: ast::StructDef) -> StructData { |
45 | let name = struct_def.name().map(|n| n.text()); | 45 | let name = struct_def.name().map(|n| n.as_name()); |
46 | let variant_data = VariantData::new(struct_def.flavor()); | 46 | let variant_data = VariantData::new(struct_def.flavor()); |
47 | let variant_data = Arc::new(variant_data); | 47 | let variant_data = Arc::new(variant_data); |
48 | StructData { name, variant_data } | 48 | StructData { name, variant_data } |
49 | } | 49 | } |
50 | 50 | ||
51 | pub fn name(&self) -> Option<&SmolStr> { | 51 | pub fn name(&self) -> Option<&Name> { |
52 | self.name.as_ref() | 52 | self.name.as_ref() |
53 | } | 53 | } |
54 | 54 | ||
@@ -70,27 +70,29 @@ impl Enum { | |||
70 | self.def_id | 70 | self.def_id |
71 | } | 71 | } |
72 | 72 | ||
73 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> { | 73 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { |
74 | Ok(db.enum_data(self.def_id)?.name.clone()) | 74 | Ok(db.enum_data(self.def_id)?.name.clone()) |
75 | } | 75 | } |
76 | |||
77 | pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> { | ||
78 | Ok(db.enum_data(self.def_id)?.variants.clone()) | ||
79 | } | ||
76 | } | 80 | } |
77 | 81 | ||
78 | #[derive(Debug, Clone, PartialEq, Eq)] | 82 | #[derive(Debug, Clone, PartialEq, Eq)] |
79 | pub struct EnumData { | 83 | pub struct EnumData { |
80 | name: Option<SmolStr>, | 84 | name: Option<Name>, |
81 | variants: Vec<(SmolStr, Arc<VariantData>)>, | 85 | variants: Vec<(Name, Arc<VariantData>)>, |
82 | } | 86 | } |
83 | 87 | ||
84 | impl EnumData { | 88 | impl EnumData { |
85 | pub(crate) fn new(enum_def: ast::EnumDef) -> Self { | 89 | pub(crate) fn new(enum_def: ast::EnumDef) -> Self { |
86 | let name = enum_def.name().map(|n| n.text()); | 90 | let name = enum_def.name().map(|n| n.as_name()); |
87 | let variants = if let Some(evl) = enum_def.variant_list() { | 91 | let variants = if let Some(evl) = enum_def.variant_list() { |
88 | evl.variants() | 92 | evl.variants() |
89 | .map(|v| { | 93 | .map(|v| { |
90 | ( | 94 | ( |
91 | v.name() | 95 | v.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), |
92 | .map(|n| n.text()) | ||
93 | .unwrap_or_else(|| SmolStr::new("[error]")), | ||
94 | Arc::new(VariantData::new(v.flavor())), | 96 | Arc::new(VariantData::new(v.flavor())), |
95 | ) | 97 | ) |
96 | }) | 98 | }) |
@@ -105,12 +107,12 @@ impl EnumData { | |||
105 | /// A single field of an enum variant or struct | 107 | /// A single field of an enum variant or struct |
106 | #[derive(Debug, Clone, PartialEq, Eq)] | 108 | #[derive(Debug, Clone, PartialEq, Eq)] |
107 | pub struct StructField { | 109 | pub struct StructField { |
108 | name: SmolStr, | 110 | name: Name, |
109 | type_ref: TypeRef, | 111 | type_ref: TypeRef, |
110 | } | 112 | } |
111 | 113 | ||
112 | impl StructField { | 114 | impl StructField { |
113 | pub fn name(&self) -> SmolStr { | 115 | pub fn name(&self) -> Name { |
114 | self.name.clone() | 116 | self.name.clone() |
115 | } | 117 | } |
116 | pub fn type_ref(&self) -> &TypeRef { | 118 | pub fn type_ref(&self) -> &TypeRef { |
@@ -134,7 +136,7 @@ impl VariantData { | |||
134 | .fields() | 136 | .fields() |
135 | .enumerate() | 137 | .enumerate() |
136 | .map(|(i, fd)| StructField { | 138 | .map(|(i, fd)| StructField { |
137 | name: SmolStr::new(i.to_string()), | 139 | name: Name::tuple_field_name(i), |
138 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), | 140 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), |
139 | }) | 141 | }) |
140 | .collect(); | 142 | .collect(); |
@@ -144,10 +146,7 @@ impl VariantData { | |||
144 | let fields = fl | 146 | let fields = fl |
145 | .fields() | 147 | .fields() |
146 | .map(|fd| StructField { | 148 | .map(|fd| StructField { |
147 | name: fd | 149 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), |
148 | .name() | ||
149 | .map(|n| n.text()) | ||
150 | .unwrap_or_else(|| SmolStr::new("[error]")), | ||
151 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), | 150 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), |
152 | }) | 151 | }) |
153 | .collect(); | 152 | .collect(); |
@@ -157,10 +156,10 @@ impl VariantData { | |||
157 | } | 156 | } |
158 | } | 157 | } |
159 | 158 | ||
160 | pub(crate) fn get_field_type_ref(&self, field_name: &str) -> Option<&TypeRef> { | 159 | pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> { |
161 | self.fields() | 160 | self.fields() |
162 | .iter() | 161 | .iter() |
163 | .find(|f| f.name == field_name) | 162 | .find(|f| f.name == *field_name) |
164 | .map(|f| &f.type_ref) | 163 | .map(|f| &f.type_ref) |
165 | } | 164 | } |
166 | 165 | ||