|
@@ -1,40 +1,39 @@
|
|
|
use crate::core::document::position::Path;
|
|
|
-use crate::core::{DocumentOperation, Node, NodeAttributes, NodeData, OperationTransform, TextDelta, Transaction};
|
|
|
+use crate::core::{Node, NodeAttributes, NodeData, NodeOperation, OperationTransform, TextDelta, Transaction};
|
|
|
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
|
|
use indextree::{Arena, Children, FollowingSiblings, NodeId};
|
|
|
|
|
|
-pub struct DocumentTree {
|
|
|
+pub struct NodeTree {
|
|
|
arena: Arena<NodeData>,
|
|
|
root: NodeId,
|
|
|
}
|
|
|
|
|
|
-impl Default for DocumentTree {
|
|
|
+impl Default for NodeTree {
|
|
|
fn default() -> Self {
|
|
|
Self::new()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl DocumentTree {
|
|
|
- pub fn new() -> DocumentTree {
|
|
|
+impl NodeTree {
|
|
|
+ pub fn new() -> NodeTree {
|
|
|
let mut arena = Arena::new();
|
|
|
-
|
|
|
let root = arena.new_node(NodeData::new("root"));
|
|
|
- DocumentTree { arena, root }
|
|
|
+ NodeTree { arena, root }
|
|
|
}
|
|
|
|
|
|
///
|
|
|
/// # Examples
|
|
|
///
|
|
|
/// ```
|
|
|
- /// use lib_ot::core::{DocumentOperation, DocumentTree, Node, Path};
|
|
|
+ /// use lib_ot::core::{NodeOperation, NodeTree, Node, Path};
|
|
|
/// let nodes = vec![Node::new("text")];
|
|
|
/// let root_path: Path = vec![0].into();
|
|
|
- /// let op = DocumentOperation::Insert {path: root_path.clone(),nodes };
|
|
|
+ /// let op = NodeOperation::Insert {path: root_path.clone(),nodes };
|
|
|
///
|
|
|
- /// let mut document = DocumentTree::new();
|
|
|
- /// document.apply_op(&op).unwrap();
|
|
|
- /// let node_id = document.node_at_path(&root_path).unwrap();
|
|
|
- /// let node_path = document.path_of_node(node_id);
|
|
|
+ /// let mut node_tree = NodeTree::new();
|
|
|
+ /// node_tree.apply_op(&op).unwrap();
|
|
|
+ /// let node_id = node_tree.node_at_path(&root_path).unwrap();
|
|
|
+ /// let node_path = node_tree.path_of_node(node_id);
|
|
|
/// debug_assert_eq!(node_path, root_path);
|
|
|
/// ```
|
|
|
pub fn node_at_path<T: Into<Path>>(&self, path: T) -> Option<NodeId> {
|
|
@@ -51,9 +50,10 @@ impl DocumentTree {
|
|
|
}
|
|
|
|
|
|
pub fn path_of_node(&self, node_id: NodeId) -> Path {
|
|
|
- let mut path: Vec<usize> = Vec::new();
|
|
|
- let mut ancestors = node_id.ancestors(&self.arena).skip(1);
|
|
|
+ let mut path = vec![];
|
|
|
let mut current_node = node_id;
|
|
|
+ // Use .skip(1) on the ancestors iterator to skip the root node.
|
|
|
+ let mut ancestors = node_id.ancestors(&self.arena).skip(1);
|
|
|
let mut parent = ancestors.next();
|
|
|
|
|
|
while parent.is_some() {
|
|
@@ -94,15 +94,15 @@ impl DocumentTree {
|
|
|
/// # Examples
|
|
|
///
|
|
|
/// ```
|
|
|
- /// use lib_ot::core::{DocumentOperation, DocumentTree, Node, Path};
|
|
|
+ /// use lib_ot::core::{NodeOperation, NodeTree, Node, Path};
|
|
|
/// let node = Node::new("text");
|
|
|
/// let inserted_path: Path = vec![0].into();
|
|
|
///
|
|
|
- /// let mut document = DocumentTree::new();
|
|
|
- /// document.apply_op(&DocumentOperation::Insert {path: inserted_path.clone(),nodes: vec![node.clone()] }).unwrap();
|
|
|
+ /// let mut node_tree = NodeTree::new();
|
|
|
+ /// node_tree.apply_op(&NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node.clone()] }).unwrap();
|
|
|
///
|
|
|
- /// let inserted_note = document.node_at_path(&inserted_path).unwrap();
|
|
|
- /// let inserted_data = document.get_node_data(inserted_note).unwrap();
|
|
|
+ /// let inserted_note = node_tree.node_at_path(&inserted_path).unwrap();
|
|
|
+ /// let inserted_data = node_tree.get_node_data(inserted_note).unwrap();
|
|
|
/// assert_eq!(inserted_data.node_type, node.note_type);
|
|
|
/// ```
|
|
|
pub fn child_from_node_with_index(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
|
|
@@ -149,12 +149,12 @@ impl DocumentTree {
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- pub fn apply_op(&mut self, op: &DocumentOperation) -> Result<(), OTError> {
|
|
|
+ pub fn apply_op(&mut self, op: &NodeOperation) -> Result<(), OTError> {
|
|
|
match op {
|
|
|
- DocumentOperation::Insert { path, nodes } => self.apply_insert(path, nodes),
|
|
|
- DocumentOperation::Update { path, attributes, .. } => self.apply_update(path, attributes),
|
|
|
- DocumentOperation::Delete { path, nodes } => self.apply_delete(path, nodes.len()),
|
|
|
- DocumentOperation::TextEdit { path, delta, .. } => self.apply_text_edit(path, delta),
|
|
|
+ NodeOperation::Insert { path, nodes } => self.apply_insert(path, nodes),
|
|
|
+ NodeOperation::Update { path, attributes, .. } => self.apply_update(path, attributes),
|
|
|
+ NodeOperation::Delete { path, nodes } => self.apply_delete(path, nodes.len()),
|
|
|
+ NodeOperation::TextEdit { path, delta, .. } => self.apply_text_edit(path, delta),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -195,7 +195,7 @@ impl DocumentTree {
|
|
|
// recursive append the subtrees to the node
|
|
|
fn append_subtree(&mut self, parent: &NodeId, insert_children: &[Node]) {
|
|
|
for child in insert_children {
|
|
|
- let child_id = self.arena.new_node(child.to_node_data());
|
|
|
+ let child_id = self.arena.new_node(child.into());
|
|
|
parent.append(child_id, &mut self.arena);
|
|
|
|
|
|
self.append_subtree(&child_id, &child.children);
|
|
@@ -204,7 +204,7 @@ impl DocumentTree {
|
|
|
|
|
|
fn insert_subtree_before(&mut self, before: &NodeId, insert_children: &[Node]) {
|
|
|
for child in insert_children {
|
|
|
- let child_id = self.arena.new_node(child.to_node_data());
|
|
|
+ let child_id = self.arena.new_node(child.into());
|
|
|
before.insert_before(child_id, &mut self.arena);
|
|
|
|
|
|
self.append_subtree(&child_id, &child.children);
|