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

View File

@ -58,3 +58,16 @@ fn test_undo() {
line.undo(); line.undo();
assert_eq!(line.text(), L!("abc").to_owned()); 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");
}