Skip to content

Commit

Permalink
Merge pull request #230 from gwenn/changes
Browse files Browse the repository at this point in the history
Make possible to detect changes
  • Loading branch information
gwenn authored May 19, 2019
2 parents bbc7dcf + 4875602 commit ff6c383
Showing 1 changed file with 15 additions and 1 deletion.
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());
}
}

0 comments on commit ff6c383

Please sign in to comment.