Allow iterating fields in the ItemTree

This commit is contained in:
Jonas Schievink 2020-06-25 13:50:27 +02:00
parent e2465ee2e9
commit 9ba7726579
3 changed files with 28 additions and 10 deletions

View file

@ -681,10 +681,28 @@ pub struct Variant {
pub fields: Fields,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldIds {
range: Range<u32>,
}
impl FieldIds {
fn new(range: Range<Idx<Field>>) -> Self {
Self { range: range.start.into_raw().into()..range.end.into_raw().into() }
}
}
impl Iterator for FieldIds {
type Item = Idx<Field>;
fn next(&mut self) -> Option<Self::Item> {
self.range.next().map(|raw| Idx::from_raw(raw.into()))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Fields {
Record(Range<Idx<Field>>),
Tuple(Range<Idx<Field>>),
Record(FieldIds),
Tuple(FieldIds),
Unit,
}

View file

@ -196,7 +196,7 @@ impl Ctx {
}
}
fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> Range<Idx<Field>> {
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<Field> {
@ -215,7 +215,7 @@ impl Ctx {
Some(res)
}
fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> Range<Idx<Field>> {
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<Field> {
@ -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 };

View file

@ -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::<ra_syntax::ast::generated::nodes::StructDef>(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::<Field>(0)..Idx::<Field>(1)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(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::<ra_syntax::ast::generated::nodes::StructDef>(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::<Field>(1)..Idx::<Field>(2)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(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::<ra_syntax::ast::generated::nodes::StructDef>(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::<Variant>(0)..Idx::<Variant>(1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(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::<Field>(3)..Idx::<Field>(4)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) }
Union { 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) }
"###);
}