pub fn insert_next_to_cursor(
        &mut self,
        prefix_count: usize,
        insertable: Insertable,
        side: Side,
    ) -> EditResult<Node::Class> {
        let (second_count, class) = match insertable {
            Insertable::CountedNode(count, c) => (
                count,
                Node::Class::from_char(c).ok_or(EditErr::CharNotANode(c))?,
            ),
        };
        // For inserting next to the cursor, we can simply multiply the two counts together to form
        // one single count
        let count = prefix_count * second_count;
        self.perform_edit(
            |this: &mut Self,
             parent_and_index: Option<(&'arena Node, usize)>,
             _cursor: &'arena Node| {
                // Find (and cache) the parent of the cursor.  If the parent of the cursor doesn't
                // exist, the cursor must be the root and we can't insert a node next to the root.
                let (parent, cursor_index) = parent_and_index.ok_or(EditErr::AddSiblingToRoot)?;
                // Calculate the index of the first child
                let insert_start_index = cursor_index
                    + match side {
                        Side::Prev => 0,
                        Side::Next => 1,
                    };
                // Clone the parent, and insert the new nodes to it one by one
                let mut cloned_parent = parent.clone();
                for i in 0..count {
                    let insert_index = insert_start_index + i;
                    // Short circuit if not an insertable char
                    if !parent.is_valid_child(insert_index, class) {
                        return Err(EditErr::CannotBeChild {
                            class,
                            parent_name: parent.display_name(),
                        });
                    }
                    // Add the new child to the children of the cloned cursor
                    cloned_parent.insert_child(
                        this.arena.alloc(Node::from_class(class)),
                        this.arena,
                        insert_index,
                    )?;
                }

                // Move the cursor to the last inserted node.  We can unwrap it here, because
                // inserting siblings to the root would cause an error
                *this.current_cursor_path.last_mut().unwrap() = insert_start_index + count - 1;
                Ok((
                    cloned_parent,
                    EditLocation::Parent,
                    EditSuccess::InsertNextToCursor { side, class },
                ))
            },
        )
    }
/// Updates the internal state so that the tree now contains `new_node` inserted as the first
/// child of the selected node.  Also moves the cursor so that the new node is selected.
Updates the internal state so that the tree now contains `new_node` inserted as the first
child of the selected node.  Also moves the cursor so that the new node is selected.
Updates the internal state so that the tree now contains ` new_node ` inserted as the first child of the selected node . Also moves the cursor so that the new node is selected .
pub fn insert_next_to_cursor(
        &mut self,
        prefix_count: usize,
        insertable: Insertable,
        side: Side,
    ) -> EditResult<Node::Class> {
        let (second_count, class) = match insertable {
            Insertable::CountedNode(count, c) => (
                count,
                Node::Class::from_char(c).ok_or(EditErr::CharNotANode(c))?,
            ),
        };
        // For inserting next to the cursor, we can simply multiply the two counts together to form
        // one single count
        let count = prefix_count * second_count;
        self.perform_edit(
            |this: &mut Self,
             parent_and_index: Option<(&'arena Node, usize)>,
             _cursor: &'arena Node| {
                // Find (and cache) the parent of the cursor.  If the parent of the cursor doesn't
                // exist, the cursor must be the root and we can't insert a node next to the root.
                let (parent, cursor_index) = parent_and_index.ok_or(EditErr::AddSiblingToRoot)?;
                // Calculate the index of the first child
                let insert_start_index = cursor_index
                    + match side {
                        Side::Prev => 0,
                        Side::Next => 1,
                    };
                // Clone the parent, and insert the new nodes to it one by one
                let mut cloned_parent = parent.clone();
                for i in 0..count {
                    let insert_index = insert_start_index + i;
                    // Short circuit if not an insertable char
                    if !parent.is_valid_child(insert_index, class) {
                        return Err(EditErr::CannotBeChild {
                            class,
                            parent_name: parent.display_name(),
                        });
                    }
                    // Add the new child to the children of the cloned cursor
                    cloned_parent.insert_child(
                        this.arena.alloc(Node::from_class(class)),
                        this.arena,
                        insert_index,
                    )?;
                }
                // Move the cursor to the last inserted node.  We can unwrap it here, because
                // inserting siblings to the root would cause an error
                *this.current_cursor_path.last_mut().unwrap() = insert_start_index + count - 1;
                Ok((
                    cloned_parent,
                    EditLocation::Parent,
                    EditSuccess::InsertNextToCursor { side, class },
                ))
            },
        )
    }
pub fn insert_next_to_cursor ( & mut self , prefix_count : usize , insertable : Insertable , side : Side , ) -> EditResult < Node :: Class > { let ( second_count , class ) = match insertable { Insertable :: CountedNode ( count , c ) => ( count , Node :: Class :: from_char ( c ) . ok_or ( EditErr :: CharNotANode ( c ) ) ? , ) , } ; // For inserting next to the cursor, we can simply multiply the two counts together to form // one single count let count = prefix_count * second_count ; self . perform_edit ( | this : & mut Self , parent_and_index : Option < ( & ' arena Node , usize ) > , _cursor : & ' arena Node | { // Find (and cache) the parent of the cursor.  If the parent of the cursor doesn't // exist, the cursor must be the root and we can't insert a node next to the root. let ( parent , cursor_index ) = parent_and_index . ok_or ( EditErr :: AddSiblingToRoot ) ? ; // Calculate the index of the first child let insert_start_index = cursor_index + match side { Side :: Prev => 0 , Side :: Next => 1 , } ; // Clone the parent, and insert the new nodes to it one by one let mut cloned_parent = parent . clone ( ) ; for i in 0 .. count { let insert_index = insert_start_index + i ; // Short circuit if not an insertable char if ! parent . is_valid_child ( insert_index , class ) { return Err ( EditErr :: CannotBeChild { class , parent_name : parent . display_name ( ) , } ) ; } // Add the new child to the children of the cloned cursor cloned_parent . insert_child ( this . arena . alloc ( Node :: from_class ( class ) ) , this . arena , insert_index , ) ? ; } // Move the cursor to the last inserted node.  We can unwrap it here, because // inserting siblings to the root would cause an error * this . current_cursor_path . last_mut ( ) . unwrap ( ) = insert_start_index + count - 1 ; Ok ( ( cloned_parent , EditLocation :: Parent , EditSuccess :: InsertNextToCursor { side , class } , ) ) } , ) }
Updates the internal state so that the tree now contains `new_node` inserted as the first
child of the selected node.
Updates the internal state so that the tree now contains ` new_node ` inserted as the first child of the selected node .
&mut self
prefix_count
usize
insertable
Insertable
side
Side
&mut self
prefix_count
usize
insertable
Insertable
side
Side