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

Make possible to detect changes #230

Merged
merged 1 commit into from
May 19, 2019
Merged
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
16 changes: 15 additions & 1 deletion src/undo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,22 @@ impl Changeset {
mark
}

pub fn end(&mut self) {
/// Returns `true` when changes happen between the last call to `begin` and this `end`.
pub fn end(&mut self) -> bool {
debug!(target: "rustyline", "Changeset::end");
self.redos.clear();
let mut touched = false;
while self.undo_group_level > 0 {
self.undo_group_level -= 1;
if let Some(&Change::Begin) = self.undos.last() {
// empty Begin..End
self.undos.pop();
} else {
self.undos.push(Change::End);
touched = true;
}
}
touched
}

fn insert_char(idx: usize, c: char) -> Change {
Expand Down Expand Up @@ -469,4 +473,14 @@ mod tests {
let insert = cs.last_insert();
assert_eq!(Some("Bye".to_owned()), insert);
}

#[test]
fn test_end() {
let mut cs = Changeset::new();
cs.begin();
assert!(!cs.end());
cs.begin();
cs.insert_str(0, "Hi");
assert!(cs.end());
}
}