Skip to content

Commit b309d72

Browse files
Fix bugs in Editor::focus (#14262)
1 parent d546a79 commit b309d72

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

helix-term/src/ui/editor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,8 @@ impl EditorView {
11591159
let editor = &mut cxt.editor;
11601160

11611161
if let Some((pos, view_id)) = pos_and_view(editor, row, column, true) {
1162+
editor.focus(view_id);
1163+
11621164
let prev_view_id = view!(editor).id;
11631165
let doc = doc_mut!(editor, &view!(editor, view_id).doc);
11641166

@@ -1182,7 +1184,6 @@ impl EditorView {
11821184
self.clear_completion(editor);
11831185
}
11841186

1185-
editor.focus(view_id);
11861187
editor.ensure_cursor_in_view(view_id);
11871188

11881189
return EventResult::Consumed(None);

helix-view/src/editor.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,28 +1987,29 @@ impl Editor {
19871987
}
19881988

19891989
pub fn focus(&mut self, view_id: ViewId) {
1990-
let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
1991-
1992-
// if leaving the view: mode should reset and the cursor should be
1993-
// within view
1994-
if prev_id != view_id {
1995-
self.enter_normal_mode();
1996-
self.ensure_cursor_in_view(view_id);
1990+
if self.tree.focus == view_id {
1991+
return;
1992+
}
19971993

1998-
// Update jumplist selections with new document changes.
1999-
for (view, _focused) in self.tree.views_mut() {
2000-
let doc = doc_mut!(self, &view.doc);
2001-
view.sync_changes(doc);
2002-
}
2003-
let view = view!(self, view_id);
1994+
// Reset mode to normal and ensure any pending changes are committed in the old document.
1995+
self.enter_normal_mode();
1996+
let (view, doc) = current!(self);
1997+
doc.append_changes_to_history(view);
1998+
self.ensure_cursor_in_view(view_id);
1999+
// Update jumplist selections with new document changes.
2000+
for (view, _focused) in self.tree.views_mut() {
20042001
let doc = doc_mut!(self, &view.doc);
2005-
doc.mark_as_focused();
2006-
let focus_lost = self.tree.get(prev_id).doc;
2007-
dispatch(DocumentFocusLost {
2008-
editor: self,
2009-
doc: focus_lost,
2010-
});
2002+
view.sync_changes(doc);
20112003
}
2004+
2005+
let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
2006+
doc_mut!(self).mark_as_focused();
2007+
2008+
let focus_lost = self.tree.get(prev_id).doc;
2009+
dispatch(DocumentFocusLost {
2010+
editor: self,
2011+
doc: focus_lost,
2012+
});
20122013
}
20132014

20142015
pub fn focus_next(&mut self) {

0 commit comments

Comments
 (0)