pub struct ChangeSet<T, F>{ /* private fields */ }Expand description
A recorded set of tree modifications.
You can use ChangeSet::modify to construct a new modified tree from an
existing one.
Node ids and token ids are separate index spaces, so changes are recorded
against an Element with ChangeSet::remove, or against an id with
ChangeSet::remove_node or ChangeSet::remove_token.
§Examples
use syntree::edit::ChangeSet;
let tree = syntree::tree! {
"root" => {
"child" => {
("lit", 1),
("lit", 2),
},
("whitespace", 3),
}
};
let child = tree.first().and_then(|n| n.first()).ok_or("missing child")?;
let mut change_set = ChangeSet::new();
change_set.remove(child);
assert_eq!(
change_set.modify(&tree)?,
syntree::tree! {
"root" => {
("whitespace", 3)
}
}
);
let lit = child.first().ok_or("missing lit")?;
let mut change_set = ChangeSet::new();
change_set.remove(lit);
assert_eq!(
change_set.modify(&tree)?,
syntree::tree! {
"root" => {
"child" => {
("lit", 2),
},
("whitespace", 3)
}
}
);Implementations§
Source§impl<T, F> ChangeSet<T, F>
impl<T, F> ChangeSet<T, F>
Sourcepub fn remove(&mut self, element: Element<'_, T, F>)
pub fn remove(&mut self, element: Element<'_, T, F>)
Register the removal of a node or token in the changeset. Removing a node removes everything inside of it. Only one kind of modification for a given element will be preserved.
§Examples
use syntree::edit::ChangeSet;
let tree = syntree::tree! {
"root" => {
"child" => {
("lit", 1),
("lit", 2),
},
("whitespace", 3),
}
};
let child = tree.first().and_then(|n| n.first()).ok_or("missing child")?;
let mut change_set = ChangeSet::new();
change_set.remove(child);
assert_eq!(
change_set.modify(&tree)?,
syntree::tree! {
"root" => {
("whitespace", 3)
}
}
);Sourcepub fn remove_node(&mut self, id: F::Pointer)
pub fn remove_node(&mut self, id: F::Pointer)
Register the removal of the interior node with the given id, and everything inside of it.
§Examples
use syntree::edit::ChangeSet;
let mut tree = syntree::Builder::new();
tree.open("root")?;
let child = tree.open("child")?;
tree.token("lit", 1)?;
tree.close()?;
tree.token("whitespace", 3)?;
tree.close()?;
let tree = tree.build()?;
let mut change_set = ChangeSet::new();
change_set.remove_node(child);
assert_eq!(
change_set.modify(&tree)?,
syntree::tree! {
"root" => {
("whitespace", 3)
}
}
);Sourcepub fn remove_token(&mut self, id: F::Pointer)
pub fn remove_token(&mut self, id: F::Pointer)
Register the removal of the token with the given id.
§Examples
use syntree::edit::ChangeSet;
let mut tree = syntree::Builder::new();
tree.open("root")?;
tree.token("lit", 1)?;
let ws = tree.token("whitespace", 3)?;
tree.close()?;
let tree = tree.build()?;
let mut change_set = ChangeSet::new();
change_set.remove_token(ws);
assert_eq!(
change_set.modify(&tree)?,
syntree::tree! {
"root" => {
("lit", 1)
}
}
);Sourcepub fn modify(
&mut self,
tree: &Tree<T, F>,
) -> Result<Tree<T, F>, Error<F::Error>>
pub fn modify( &mut self, tree: &Tree<T, F>, ) -> Result<Tree<T, F>, Error<F::Error>>
Construct a modified tree where the recorded modifications have been applied.
Spans are compacted: the tokens which remain are laid out back-to-back from offset zero, so removed tokens and gaps left by explicit spans do not leave holes. Interior nodes which are left without children are kept as empty nodes.
§Errors
Errors with Error::Overflow in case we run out of node
identifiers.
§Examples
use syntree::edit::ChangeSet;
let tree = syntree::tree! {
"root" => {
"child" => {
("lit", 1),
("lit", 2),
},
("whitespace", 3),
}
};
let child = tree.first().and_then(|n| n.first()).ok_or("missing child")?;
let mut change_set = ChangeSet::new();
change_set.remove(child);
assert_eq!(
change_set.modify(&tree)?,
syntree::tree! {
"root" => {
("whitespace", 3)
}
}
);