Coalesce IME compositions into a single edit

This commit is contained in:
Antonio Scandurra 2022-07-25 09:53:51 +02:00
parent 555e705ccb
commit d3567e381c
5 changed files with 168 additions and 15 deletions

View file

@ -525,7 +525,7 @@ fn test_history() {
let mut now = Instant::now();
let mut buffer = Buffer::new(0, 0, "123456".into());
buffer.start_transaction_at(now);
let transaction_1 = buffer.start_transaction_at(now).unwrap();
buffer.edit([(2..4, "cd")]);
buffer.end_transaction_at(now);
assert_eq!(buffer.text(), "12cd56");
@ -574,6 +574,16 @@ fn test_history() {
assert_eq!(buffer.text(), "12cde6");
buffer.undo();
assert_eq!(buffer.text(), "123456");
// Transactions can be grouped manually.
buffer.redo();
buffer.redo();
assert_eq!(buffer.text(), "X12cde6");
buffer.group_until_transaction(transaction_1);
buffer.undo();
assert_eq!(buffer.text(), "123456");
buffer.redo();
assert_eq!(buffer.text(), "X12cde6");
}
#[test]

View file

@ -191,22 +191,39 @@ impl History {
}
fn group(&mut self) -> Option<TransactionId> {
let mut new_len = self.undo_stack.len();
let mut entries = self.undo_stack.iter_mut();
let mut count = 0;
let mut entries = self.undo_stack.iter();
if let Some(mut entry) = entries.next_back() {
while let Some(prev_entry) = entries.next_back() {
if !prev_entry.suppress_grouping
&& entry.first_edit_at - prev_entry.last_edit_at <= self.group_interval
{
entry = prev_entry;
new_len -= 1;
count += 1;
} else {
break;
}
}
}
self.group_trailing(count)
}
fn group_until(&mut self, transaction_id: TransactionId) {
let mut count = 0;
for entry in self.undo_stack.iter().rev() {
if entry.transaction_id() == transaction_id {
self.group_trailing(count);
break;
} else if entry.suppress_grouping {
break;
} else {
count += 1;
}
}
}
fn group_trailing(&mut self, n: usize) -> Option<TransactionId> {
let new_len = self.undo_stack.len() - n;
let (entries_to_keep, entries_to_merge) = self.undo_stack.split_at_mut(new_len);
if let Some(last_entry) = entries_to_keep.last_mut() {
for entry in &*entries_to_merge {
@ -1195,6 +1212,10 @@ impl Buffer {
self.history.finalize_last_transaction()
}
pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
self.history.group_until(transaction_id);
}
pub fn base_text(&self) -> &Arc<str> {
&self.history.base_text
}