aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-06-25 12:50:27 +0100
committerJonas Schievink <[email protected]>2020-06-25 12:50:27 +0100
commit9ba772657950cb8353f37bc2576b78c4f0c8996f (patch)
treecc63a79f90006ad761202ca7b2a38c3c16be394f /crates/ra_hir_def/src
parente2465ee2e949d81f98270c57afa17666dac6afa4 (diff)
Allow iterating fields in the ItemTree
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/item_tree.rs22
-rw-r--r--crates/ra_hir_def/src/item_tree/lower.rs10
-rw-r--r--crates/ra_hir_def/src/item_tree/tests.rs6
3 files changed, 28 insertions, 10 deletions
diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs
index d7bc64e6c..f99e05432 100644
--- a/crates/ra_hir_def/src/item_tree.rs
+++ b/crates/ra_hir_def/src/item_tree.rs
@@ -682,9 +682,27 @@ pub struct Variant {
682} 682}
683 683
684#[derive(Debug, Clone, PartialEq, Eq)] 684#[derive(Debug, Clone, PartialEq, Eq)]
685pub struct FieldIds {
686 range: Range<u32>,
687}
688
689impl FieldIds {
690 fn new(range: Range<Idx<Field>>) -> Self {
691 Self { range: range.start.into_raw().into()..range.end.into_raw().into() }
692 }
693}
694
695impl Iterator for FieldIds {
696 type Item = Idx<Field>;
697 fn next(&mut self) -> Option<Self::Item> {
698 self.range.next().map(|raw| Idx::from_raw(raw.into()))
699 }
700}
701
702#[derive(Debug, Clone, PartialEq, Eq)]
685pub enum Fields { 703pub enum Fields {
686 Record(Range<Idx<Field>>), 704 Record(FieldIds),
687 Tuple(Range<Idx<Field>>), 705 Tuple(FieldIds),
688 Unit, 706 Unit,
689} 707}
690 708
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index f10ad25f7..e2e00323c 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -196,7 +196,7 @@ impl Ctx {
196 } 196 }
197 } 197 }
198 198
199 fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> Range<Idx<Field>> { 199 fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> FieldIds {
200 let start = self.next_field_idx(); 200 let start = self.next_field_idx();
201 for field in fields.fields() { 201 for field in fields.fields() {
202 if let Some(data) = self.lower_record_field(&field) { 202 if let Some(data) = self.lower_record_field(&field) {
@@ -204,7 +204,7 @@ impl Ctx {
204 } 204 }
205 } 205 }
206 let end = self.next_field_idx(); 206 let end = self.next_field_idx();
207 start..end 207 FieldIds::new(start..end)
208 } 208 }
209 209
210 fn lower_record_field(&mut self, field: &ast::RecordFieldDef) -> Option<Field> { 210 fn lower_record_field(&mut self, field: &ast::RecordFieldDef) -> Option<Field> {
@@ -215,7 +215,7 @@ impl Ctx {
215 Some(res) 215 Some(res)
216 } 216 }
217 217
218 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> Range<Idx<Field>> { 218 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> FieldIds {
219 let start = self.next_field_idx(); 219 let start = self.next_field_idx();
220 for (i, field) in fields.fields().enumerate() { 220 for (i, field) in fields.fields().enumerate() {
221 if let Some(data) = self.lower_tuple_field(i, &field) { 221 if let Some(data) = self.lower_tuple_field(i, &field) {
@@ -223,7 +223,7 @@ impl Ctx {
223 } 223 }
224 } 224 }
225 let end = self.next_field_idx(); 225 let end = self.next_field_idx();
226 start..end 226 FieldIds::new(start..end)
227 } 227 }
228 228
229 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleFieldDef) -> Option<Field> { 229 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleFieldDef) -> Option<Field> {
@@ -242,7 +242,7 @@ impl Ctx {
242 Some(record_field_def_list) => { 242 Some(record_field_def_list) => {
243 self.lower_fields(&StructKind::Record(record_field_def_list)) 243 self.lower_fields(&StructKind::Record(record_field_def_list))
244 } 244 }
245 None => Fields::Record(self.next_field_idx()..self.next_field_idx()), 245 None => Fields::Record(FieldIds::new(self.next_field_idx()..self.next_field_idx())),
246 }; 246 };
247 let ast_id = self.source_ast_id_map.ast_id(union); 247 let ast_id = self.source_ast_id_map.ast_id(union);
248 let res = Union { name, visibility, generic_params, fields, ast_id }; 248 let res = Union { name, visibility, generic_params, fields, ast_id };
diff --git a/crates/ra_hir_def/src/item_tree/tests.rs b/crates/ra_hir_def/src/item_tree/tests.rs
index dc035d809..fd7ffee24 100644
--- a/crates/ra_hir_def/src/item_tree/tests.rs
+++ b/crates/ra_hir_def/src/item_tree/tests.rs
@@ -237,13 +237,13 @@ Trait { name: Name(Text("Tr")), visibility: RawVisibilityId("pub(self)"), generi
237#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }] 237#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }]
238Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(3), kind: Unit } 238Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(3), kind: Unit }
239#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }] 239#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }]
240Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(Idx::<Field>(0)..Idx::<Field>(1)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(4), kind: Tuple } 240Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(FieldIds { range: 0..1 }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(4), kind: Tuple }
241#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }] 241#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }]
242Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(Idx::<Field>(1)..Idx::<Field>(2)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(5), kind: Record } 242Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(FieldIds { range: 1..2 }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(5), kind: Record }
243#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }] 243#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }]
244Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: Idx::<Variant>(0)..Idx::<Variant>(1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(6) } 244Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: Idx::<Variant>(0)..Idx::<Variant>(1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(6) }
245#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }] 245#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }]
246Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(Idx::<Field>(3)..Idx::<Field>(4)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) } 246Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(FieldIds { range: 3..4 }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) }
247 "###); 247 "###);
248} 248}
249 249