Skip to content

Commit

Permalink
make it clear that span tree holds parent offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizi committed Apr 24, 2023
1 parent 070e467 commit d790c74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
8 changes: 4 additions & 4 deletions app/gui/language/span-tree/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ pub trait Builder<T: Payload>: Sized {
let kind = kind.into();
let node = Node::<T>::new().with_kind(kind).with_size(len.into());
let prev_child = self.node_being_built().children.last();
let prev_child_end = prev_child.map_or(0, |c| (c.offset + c.node.size).as_usize());
let sibling_offset = offset.saturating_sub(prev_child_end);
let prev_child_end = prev_child.map_or(0, |c| (c.parent_offset + c.node.size).as_usize());
let parent_offset = prev_child_end + offset;
let child = node::Child {
node,
offset: offset.into(),
sibling_offset: sibling_offset.into(),
parent_offset: parent_offset.into(),
sibling_offset: offset.into(),
ast_crumbs: crumbs.into_crumbs(),
};
ChildBuilder { built: child, parent: self }
Expand Down
33 changes: 14 additions & 19 deletions app/gui/language/span-tree/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,26 @@ impl<T: Payload> ChildGenerator<T> {
}

fn add_node(&mut self, ast_crumbs: ast::Crumbs, node: Node<T>) -> &mut node::Child<T> {
let offset = self.current_offset;
let parent_offset = self.current_offset;
let sibling_offset = self.sibling_offset;
let child = node::Child { node, offset, sibling_offset, ast_crumbs };
let child = node::Child { node, parent_offset, sibling_offset, ast_crumbs };
self.current_offset += child.node.size;
self.sibling_offset = 0.byte_diff();
self.children.push(child);
self.children.last_mut().unwrap()
}

fn generate_empty_node(&mut self, insert_type: InsertionPointType) -> &mut node::Child<T> {
let child = node::Child {
node: Node::<T>::new().with_kind(insert_type),
offset: self.current_offset,
sibling_offset: self.sibling_offset,
ast_crumbs: vec![],
};
self.sibling_offset = 0.byte_diff();
self.children.push(child);
self.children.last_mut().unwrap()
self.add_node(vec![], Node::<T>::new().with_kind(insert_type))
}

fn reverse_children(&mut self) {
self.children.reverse();
let mut last_parent_offset = 0.byte_diff();
for child in &mut self.children {
child.offset = self.current_offset - child.offset - child.node.size;
child.parent_offset = self.current_offset - child.parent_offset - child.node.size;
child.sibling_offset = child.parent_offset - last_parent_offset;
last_parent_offset = child.parent_offset;
}
}

Expand Down Expand Up @@ -822,35 +817,35 @@ fn tree_generate_node<T: Payload>(
if let Some(leaf_info) = &tree.leaf_info {
size = ByteDiff::from(leaf_info.len());
} else {
let mut offset = ByteDiff::from(0);
let mut parent_offset = ByteDiff::from(0);
let mut sibling_offset = ByteDiff::from(0);
for (index, raw_span_info) in tree.span_info.iter().enumerate() {
match raw_span_info {
SpanSeed::Space(ast::SpanSeedSpace { space }) => {
offset += ByteDiff::from(space);
parent_offset += ByteDiff::from(space);
sibling_offset += ByteDiff::from(space);
}
SpanSeed::Token(ast::SpanSeedToken { token }) => {
let kind = node::Kind::Token;
let size = ByteDiff::from(token.len());
let ast_crumbs = vec![TreeCrumb { index }.into()];
let node = Node { kind, size, ..default() };
children.push(node::Child { node, offset, sibling_offset, ast_crumbs });
offset += size;
children.push(node::Child { node, parent_offset, sibling_offset, ast_crumbs });
parent_offset += size;
sibling_offset = 0.byte_diff();
}
SpanSeed::Child(ast::SpanSeedChild { node }) => {
let kind = node::Kind::argument();
let node = node.generate_node(kind, context)?;
let child_size = node.size;
let ast_crumbs = vec![TreeCrumb { index }.into()];
children.push(node::Child { node, offset, sibling_offset, ast_crumbs });
offset += child_size;
children.push(node::Child { node, parent_offset, sibling_offset, ast_crumbs });
parent_offset += child_size;
sibling_offset = 0.byte_diff();
}
}
}
size = offset;
size = parent_offset;
}
let payload = default();
Ok(Node { kind, parenthesized, size, children, ast_id, payload })
Expand Down
12 changes: 6 additions & 6 deletions app/gui/language/span-tree/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub struct Child<T = ()> {
/// A child node.
pub node: Node<T>,
/// An offset counted from the parent node starting index to the start of this node's span.
pub offset: ByteDiff,
pub parent_offset: ByteDiff,
/// The offset counted from the end of previous sibling node.
pub sibling_offset: ByteDiff,
/// AST crumbs which lead from parent to child associated AST node.
Expand All @@ -194,10 +194,10 @@ impl<T> Child<T> {
/// Payload mapping utility.
pub fn map<S>(self, f: impl Copy + Fn(T) -> S) -> Child<S> {
let node = self.node.map(f);
let offset = self.offset;
let parent_offset = self.parent_offset;
let ast_crumbs = self.ast_crumbs;
let sibling_offset = self.sibling_offset;
Child { node, offset, sibling_offset, ast_crumbs }
Child { node, parent_offset, sibling_offset, ast_crumbs }
}
}

Expand Down Expand Up @@ -370,7 +370,7 @@ impl<'a, T> Ref<'a, T> {
None => Err(InvalidCrumb::new(node.children.len(), index, &crumbs).into()),
Some(child) => {
let node = &child.node;
span_offset += child.offset;
span_offset += child.parent_offset;
let sibling_offset = child.sibling_offset;
let crumbs = crumbs.into_sub(index);
ast_crumbs.extend_from_slice(&child.ast_crumbs);
Expand Down Expand Up @@ -617,9 +617,9 @@ impl<'a, T> RefMut<'a, T> {
crumbs: Crumbs,
mut ast_crumbs: ast::Crumbs,
) -> RefMut<'a, T> {
let offset = child.offset;
let offset = child.parent_offset;
let node = &mut child.node;
span_begin += child.offset;
span_begin += child.parent_offset;
let crumbs = crumbs.into_sub(index);
ast_crumbs.extend(child.ast_crumbs.iter().cloned());
Self { node, offset, span_offset: span_begin, crumbs, ast_crumbs }
Expand Down

0 comments on commit d790c74

Please sign in to comment.