Skip to content

Commit

Permalink
patch(memory): add Cursor::resume for sequence continuation
Browse files Browse the repository at this point in the history
Now, it's possible to resume a ended sesquence.
  • Loading branch information
pythonbrad committed Mar 25, 2024
1 parent 5192a5f commit 99b4be3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion engine/preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ impl Preprocessor {

true
} else {
self.cursor.resume();

false
}
}
Expand Down Expand Up @@ -229,7 +231,6 @@ impl Preprocessor {
///
/// // Verification.
/// while let Some(command) = preprocessor.pop_queue() {
/// dbg!(command.clone());
/// assert_eq!(command, expecteds.pop_front().unwrap());
/// }
/// ```
Expand Down
40 changes: 40 additions & 0 deletions memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,38 @@ impl Cursor {
})
}

/// Resumes at the current sequence.
///
/// By removing the end marker of the current sequence, this method allows the cursor
/// to continue using it as an ongoing sequence.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// text_buffer.insert(vec!['c', '_'], "ç".to_owned());
/// let memory = Rc::new(text_buffer);
///
/// let mut cursor = Cursor::new(memory, 8);
/// cursor.hit('c');
/// cursor.hit('c');
/// cursor.undo();
/// assert_eq!(cursor.to_sequence(), vec!['\0', 'c', '\0']);
///
/// // We want the next hit to reuse this sequence.
/// cursor.resume();
/// assert_eq!(cursor.to_sequence(), vec!['\0', 'c']);
/// assert_eq!(cursor.hit('_'), Some("ç".to_owned()).to_owned());
/// ```
pub fn resume(&mut self) {
if let Some(true) = self.buffer.iter().last().map(|node| node.is_root()) {
self.buffer.pop_back();
}
}

/// Returns the current state of the cursor.
///
/// Permits to know the current position in the memory and also the last hit.
Expand Down Expand Up @@ -610,7 +642,15 @@ mod tests {
undo!(cursor 1);
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'a']);

// Cursor::resume
hit!(cursor 'x');
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'a', '\0', 'x']);
undo!(cursor 1);
cursor.resume();
hit!(cursor 'f');
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'a', 'f']);

undo!(cursor 2);
cursor.hit('e');
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'e']);

Expand Down

0 comments on commit 99b4be3

Please sign in to comment.