editor: Restore selections to positions after last edit (#28527)

Closes #22692

Makes it so when undoing a format operation, the selections are set to
where they were at the last edit.

Release Notes:

- Made it so the cursor position is reset to where it was after the last
edit when undoing a format operation. This will only result in different
behavior when you make an edit, scroll away, initiate formatting (either
by saving or manually) and then undo the format.

---------

Co-authored-by: Zed AI <ai@zed.dev>
This commit is contained in:
Ben Kunkle 2025-04-10 14:33:49 -04:00 committed by GitHub
parent 26f4705198
commit fbbc23bec3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 117 additions and 4 deletions

View file

@ -1118,9 +1118,16 @@ impl MultiBuffer {
self.history.start_transaction(now)
}
pub fn last_transaction_id(&self) -> Option<TransactionId> {
let last_transaction = self.history.undo_stack.last()?;
return Some(last_transaction.id);
pub fn last_transaction_id(&self, cx: &App) -> Option<TransactionId> {
if let Some(buffer) = self.as_singleton() {
return buffer.read_with(cx, |b, _| {
b.peek_undo_stack()
.map(|history_entry| history_entry.transaction_id())
});
} else {
let last_transaction = self.history.undo_stack.last()?;
return Some(last_transaction.id);
}
}
pub fn end_transaction(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {