Skip to content

Commit d715316

Browse files
archseerthomasskk
authored andcommitted
fix: Reset document mode when losing focus
Fixes helix-editor#3090
1 parent 18fd83c commit d715316

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

helix-term/src/ui/editor.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -978,16 +978,15 @@ impl EditorView {
978978
doc.set_selection(view_id, Selection::point(pos));
979979
}
980980

981-
editor.tree.focus = view_id;
981+
editor.focus(view_id);
982982

983983
return EventResult::Consumed(None);
984984
}
985985

986986
if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
987-
editor.tree.focus = view_id;
987+
editor.focus(view_id);
988988

989-
let view = editor.tree.get(view_id);
990-
let doc = editor.documents.get_mut(&view.doc).unwrap();
989+
let (view, doc) = current!(cxt.editor);
991990

992991
let path = match doc.path() {
993992
Some(path) => path.clone(),
@@ -1066,10 +1065,9 @@ impl EditorView {
10661065

10671066
MouseEventKind::Up(MouseButton::Right) => {
10681067
if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
1069-
cxt.editor.tree.focus = view_id;
1068+
cxt.editor.focus(view_id);
10701069

1071-
let view = cxt.editor.tree.get(view_id);
1072-
let doc = cxt.editor.documents.get_mut(&view.doc).unwrap();
1070+
let (view, doc) = current!(cxt.editor);
10731071
let line = coords.row + view.offset.row;
10741072
if let Ok(pos) = doc.text().try_line_to_char(line) {
10751073
doc.set_selection(view_id, Selection::point(pos));
@@ -1102,7 +1100,7 @@ impl EditorView {
11021100
if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
11031101
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
11041102
doc.set_selection(view_id, Selection::point(pos));
1105-
editor.tree.focus = view_id;
1103+
cxt.editor.focus(view_id);
11061104
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
11071105

11081106
return EventResult::Consumed(None);

helix-view/src/editor.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,12 +1157,33 @@ impl Editor {
11571157
};
11581158
}
11591159

1160+
pub fn focus(&mut self, view_id: ViewId) {
1161+
let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
1162+
1163+
// if leaving the view: mode should reset
1164+
if prev_id != view_id {
1165+
let doc_id = self.tree.get(prev_id).doc;
1166+
self.documents.get_mut(&doc_id).unwrap().mode = Mode::Normal;
1167+
}
1168+
}
1169+
11601170
pub fn focus_next(&mut self) {
1171+
let prev_id = self.tree.focus;
11611172
self.tree.focus_next();
1173+
let id = self.tree.focus;
1174+
1175+
// if leaving the view: mode should reset
1176+
if prev_id != id {
1177+
let doc_id = self.tree.get(prev_id).doc;
1178+
self.documents.get_mut(&doc_id).unwrap().mode = Mode::Normal;
1179+
}
11621180
}
11631181

11641182
pub fn focus_direction(&mut self, direction: tree::Direction) {
1165-
self.tree.focus_direction(direction);
1183+
let current_view = self.tree.focus;
1184+
if let Some(id) = self.tree.find_split_in_direction(current_view, direction) {
1185+
self.focus(id)
1186+
}
11661187
}
11671188

11681189
pub fn swap_split_in_direction(&mut self, direction: tree::Direction) {

helix-view/src/tree.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,6 @@ impl Tree {
504504
Some(child_id)
505505
}
506506

507-
pub fn focus_direction(&mut self, direction: Direction) {
508-
if let Some(id) = self.find_split_in_direction(self.focus, direction) {
509-
self.focus = id;
510-
}
511-
}
512-
513507
pub fn focus_next(&mut self) {
514508
// This function is very dumb, but that's because we don't store any parent links.
515509
// (we'd be able to go parent.next_sibling() recursively until we find something)

0 commit comments

Comments
 (0)