From 9ba772657950cb8353f37bc2576b78c4f0c8996f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 25 Jun 2020 13:50:27 +0200 Subject: Allow iterating fields in the ItemTree --- crates/ra_hir_def/src/item_tree.rs | 22 ++++++++++++++++++++-- crates/ra_hir_def/src/item_tree/lower.rs | 10 +++++----- crates/ra_hir_def/src/item_tree/tests.rs | 6 +++--- 3 files changed, 28 insertions(+), 10 deletions(-) (limited to 'crates') 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 @@ -681,10 +681,28 @@ pub struct Variant { pub fields: Fields, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FieldIds { + range: Range, +} + +impl FieldIds { + fn new(range: Range>) -> Self { + Self { range: range.start.into_raw().into()..range.end.into_raw().into() } + } +} + +impl Iterator for FieldIds { + type Item = Idx; + fn next(&mut self) -> Option { + self.range.next().map(|raw| Idx::from_raw(raw.into())) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Fields { - Record(Range>), - Tuple(Range>), + Record(FieldIds), + Tuple(FieldIds), Unit, } 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 { } } - fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> Range> { + fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> FieldIds { let start = self.next_field_idx(); for field in fields.fields() { if let Some(data) = self.lower_record_field(&field) { @@ -204,7 +204,7 @@ impl Ctx { } } let end = self.next_field_idx(); - start..end + FieldIds::new(start..end) } fn lower_record_field(&mut self, field: &ast::RecordFieldDef) -> Option { @@ -215,7 +215,7 @@ impl Ctx { Some(res) } - fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> Range> { + fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> FieldIds { let start = self.next_field_idx(); for (i, field) in fields.fields().enumerate() { if let Some(data) = self.lower_tuple_field(i, &field) { @@ -223,7 +223,7 @@ impl Ctx { } } let end = self.next_field_idx(); - start..end + FieldIds::new(start..end) } fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleFieldDef) -> Option { @@ -242,7 +242,7 @@ impl Ctx { Some(record_field_def_list) => { self.lower_fields(&StructKind::Record(record_field_def_list)) } - None => Fields::Record(self.next_field_idx()..self.next_field_idx()), + None => Fields::Record(FieldIds::new(self.next_field_idx()..self.next_field_idx())), }; let ast_id = self.source_ast_id_map.ast_id(union); 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 #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }] Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::(3), kind: Unit } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }] -Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(Idx::(0)..Idx::(1)), ast_id: FileAstId::(4), kind: Tuple } +Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(FieldIds { range: 0..1 }), ast_id: FileAstId::(4), kind: Tuple } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }] -Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(Idx::(1)..Idx::(2)), ast_id: FileAstId::(5), kind: Record } +Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(FieldIds { range: 1..2 }), ast_id: FileAstId::(5), kind: Record } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }] Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: Idx::(0)..Idx::(1), ast_id: FileAstId::(6) } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }] -Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(Idx::(3)..Idx::(4)), ast_id: FileAstId::(7) } +Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(FieldIds { range: 3..4 }), ast_id: FileAstId::(7) } "###); } -- cgit v1.2.3