Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(memory): add Cursor::resume for sequence continuation #211

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading