on master help sign in
fix: Reset document mode when losing focus

Fixes #3090

commite4c9d4082a139aac3aea4506918171b96e81f5b9 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 23 Aug 2022, 14:07 (4 years ago) parent7b8e4ac
3 files changed+28−15
helix-term/src/ui/editor.rs+6−8 helix-view/src/editor.rs+22−1 helix-view/src/tree.rs−6
helix-term/src/ui/editor.rs+6−8
@@ -976,16 +976,15 @@ impl EditorView {
976976 doc.set_selection(view_id, Selection::point(pos));
977977 }
978978
979− editor.tree.focus = view_id;
979+ editor.focus(view_id);
980980
981981 return EventResult::Consumed(None);
982982 }
983983
984984 if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
985− editor.tree.focus = view_id;
985+ editor.focus(view_id);
986986
987− let view = editor.tree.get(view_id);
988− let doc = editor.documents.get_mut(&view.doc).unwrap();
987+ let (view, doc) = current!(cxt.editor);
989988
990989 let path = match doc.path() {
991990 Some(path) => path.clone(),
@@ -1064,10 +1063,9 @@ impl EditorView {
10641063
10651064 MouseEventKind::Up(MouseButton::Right) => {
10661065 if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
1067− cxt.editor.tree.focus = view_id;
1066+ cxt.editor.focus(view_id);
10681067
1069− let view = cxt.editor.tree.get(view_id);
1070− let doc = cxt.editor.documents.get_mut(&view.doc).unwrap();
1068+ let (view, doc) = current!(cxt.editor);
10711069 let line = coords.row + view.offset.row;
10721070 if let Ok(pos) = doc.text().try_line_to_char(line) {
10731071 doc.set_selection(view_id, Selection::point(pos));
@@ -1100,7 +1098,7 @@ impl EditorView {
11001098 if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
11011099 let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
11021100 doc.set_selection(view_id, Selection::point(pos));
1103− editor.tree.focus = view_id;
1101+ cxt.editor.focus(view_id);
11041102 commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
11051103
11061104 return EventResult::Consumed(None);
helix-view/src/editor.rs+22−1
@@ -1091,12 +1091,33 @@ impl Editor {
10911091 };
10921092 }
10931093
1094+ pub fn focus(&mut self, view_id: ViewId) {
1095+ let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
1096+
1097+ // if leaving the view: mode should reset
1098+ if prev_id != view_id {
1099+ let doc_id = self.tree.get(prev_id).doc;
1100+ self.documents.get_mut(&doc_id).unwrap().mode = Mode::Normal;
1101+ }
1102+ }
1103+
10941104 pub fn focus_next(&mut self) {
1105+ let prev_id = self.tree.focus;
10951106 self.tree.focus_next();
1107+ let id = self.tree.focus;
1108+
1109+ // if leaving the view: mode should reset
1110+ if prev_id != id {
1111+ let doc_id = self.tree.get(prev_id).doc;
1112+ self.documents.get_mut(&doc_id).unwrap().mode = Mode::Normal;
1113+ }
10961114 }
10971115
10981116 pub fn focus_direction(&mut self, direction: tree::Direction) {
1099− self.tree.focus_direction(direction);
1117+ let current_view = self.tree.focus;
1118+ if let Some(id) = self.tree.find_split_in_direction(current_view, direction) {
1119+ self.focus(id)
1120+ }
11001121 }
11011122
11021123 pub fn swap_split_in_direction(&mut self, direction: tree::Direction) {
helix-view/src/tree.rs−6
@@ -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)