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.rs67
1 files changed, 19 insertions, 48 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 66c016180..fa3e4baa7 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -5,12 +5,13 @@ use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreePtr, SyntaxNode}; 5use ra_syntax::{ast, TreePtr, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::ModuleScope, 10 nameres::ModuleScope,
11 db::HirDatabase, 11 db::HirDatabase,
12 expr::BodySyntaxMapping, 12 expr::BodySyntaxMapping,
13 ty::InferenceResult, 13 ty::InferenceResult,
14 adt::VariantData,
14}; 15};
15 16
16/// hir::Crate describes a single crate. It's the main interface with which 17/// hir::Crate describes a single crate. It's the main interface with which
@@ -137,58 +138,18 @@ impl Module {
137 } 138 }
138} 139}
139 140
140/// A single field of an enum variant or struct 141#[derive(Debug, Clone, PartialEq, Eq, Hash)]
141#[derive(Debug, Clone, PartialEq, Eq)]
142pub struct StructField { 142pub struct StructField {
143 pub(crate) name: Name, 143 struct_: Struct,
144 pub(crate) type_ref: TypeRef, 144 name: Name,
145} 145}
146 146
147impl StructField { 147impl StructField {
148 pub fn name(&self) -> &Name { 148 pub fn name(&self) -> &Name {
149 &self.name 149 &self.name
150 } 150 }
151 151 pub fn ty(&self, db: &impl HirDatabase) -> Cancelable<Option<Ty>> {
152 pub fn type_ref(&self) -> &TypeRef { 152 db.type_for_field(self.struct_.def_id, self.name.clone())
153 &self.type_ref
154 }
155}
156
157/// Fields of an enum variant or struct
158#[derive(Debug, Clone, PartialEq, Eq)]
159pub enum VariantData {
160 Struct(Vec<StructField>),
161 Tuple(Vec<StructField>),
162 Unit,
163}
164
165impl VariantData {
166 pub fn fields(&self) -> &[StructField] {
167 match self {
168 VariantData::Struct(fields) | VariantData::Tuple(fields) => fields,
169 _ => &[],
170 }
171 }
172
173 pub fn is_struct(&self) -> bool {
174 match self {
175 VariantData::Struct(..) => true,
176 _ => false,
177 }
178 }
179
180 pub fn is_tuple(&self) -> bool {
181 match self {
182 VariantData::Tuple(..) => true,
183 _ => false,
184 }
185 }
186
187 pub fn is_unit(&self) -> bool {
188 match self {
189 VariantData::Unit => true,
190 _ => false,
191 }
192 } 153 }
193} 154}
194 155
@@ -206,8 +167,18 @@ impl Struct {
206 Ok(db.struct_data(self.def_id)?.name.clone()) 167 Ok(db.struct_data(self.def_id)?.name.clone())
207 } 168 }
208 169
209 pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> { 170 pub fn fields(&self, db: &impl HirDatabase) -> Cancelable<Vec<StructField>> {
210 Ok(db.struct_data(self.def_id)?.variant_data.clone()) 171 let res = db
172 .struct_data(self.def_id)?
173 .variant_data
174 .fields()
175 .iter()
176 .map(|it| StructField {
177 struct_: self.clone(),
178 name: it.name.clone(),
179 })
180 .collect();
181 Ok(res)
211 } 182 }
212} 183}
213 184