Fix bugs in Editor::focus (#14262)

This commit is contained in:
Michael Davis
2025-08-31 08:52:40 -04:00
committed by GitHub
parent d546a799e5
commit b309d72688
2 changed files with 24 additions and 22 deletions

View File

@@ -1987,28 +1987,29 @@ impl Editor {
}
pub fn focus(&mut self, view_id: ViewId) {
let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
// if leaving the view: mode should reset and the cursor should be
// within view
if prev_id != view_id {
self.enter_normal_mode();
self.ensure_cursor_in_view(view_id);
// Update jumplist selections with new document changes.
for (view, _focused) in self.tree.views_mut() {
let doc = doc_mut!(self, &view.doc);
view.sync_changes(doc);
}
let view = view!(self, view_id);
let doc = doc_mut!(self, &view.doc);
doc.mark_as_focused();
let focus_lost = self.tree.get(prev_id).doc;
dispatch(DocumentFocusLost {
editor: self,
doc: focus_lost,
});
if self.tree.focus == view_id {
return;
}
// Reset mode to normal and ensure any pending changes are committed in the old document.
self.enter_normal_mode();
let (view, doc) = current!(self);
doc.append_changes_to_history(view);
self.ensure_cursor_in_view(view_id);
// Update jumplist selections with new document changes.
for (view, _focused) in self.tree.views_mut() {
let doc = doc_mut!(self, &view.doc);
view.sync_changes(doc);
}
let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
doc_mut!(self).mark_as_focused();
let focus_lost = self.tree.get(prev_id).doc;
dispatch(DocumentFocusLost {
editor: self,
doc: focus_lost,
});
}
pub fn focus_next(&mut self) {