Fix begin-undo-group regression

Fixes #10145
This commit is contained in:
Johannes Altmanninger 2023-12-09 16:08:31 +01:00
parent 8fe82fcfcf
commit 0942ace6c9
2 changed files with 29 additions and 12 deletions

View File

@ -270,7 +270,7 @@ impl EditableLine {
if self.edit_group_level.is_some() {
return;
}
self.edit_group_level = Some(55 + 1);
self.edit_group_level = Some(1);
// Indicate that the next change must trigger the creation of a new history item
self.undo_history.may_coalesce = false;
// Indicate that future changes should be coalesced into the same edit if possible.
@ -281,18 +281,22 @@ impl EditableLine {
/// End a logical grouping of command line edits that should be undone/redone together.
pub fn end_edit_group(&mut self) {
let Some(edit_group_level) = self.edit_group_level.as_mut() else {
// Clamp the minimum value to -1 to prevent unbalanced end_edit_group() calls from breaking
// everything.
return;
};
*edit_group_level -= 1;
if *edit_group_level == 55 {
self.undo_history.try_coalesce = false;
self.undo_history.may_coalesce = false;
match self.edit_group_level.as_mut() {
Some(edit_group_level) => {
*edit_group_level -= 1;
if *edit_group_level > 0 {
return;
}
}
None => {
// Prevent unbalanced end_edit_group() calls from breaking everything.
return;
}
}
self.edit_group_level = None;
self.undo_history.try_coalesce = false;
self.undo_history.may_coalesce = false;
}
/// Whether we want to append this string to the previous edit.

View File

@ -58,3 +58,16 @@ fn test_undo() {
line.undo();
assert_eq!(line.text(), L!("abc").to_owned());
}
#[test]
fn test_undo_group() {
let mut line = EditableLine::default();
line.begin_edit_group();
line.push_edit(Edit::new(0..0, L!("a").to_owned()), true);
line.end_edit_group();
line.begin_edit_group();
line.push_edit(Edit::new(1..1, L!("b").to_owned()), true);
line.end_edit_group();
line.undo();
assert_eq!(line.text(), "a");
}