From c61427cb4398dbae6f4fc23aa447d2f2dba8621c Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 2 Nov 2022 15:03:52 -0500 Subject: [PATCH] Ensure cursor is in view on window change If two windows are editing the same document, one may delete enough of the document so that the other window is pointing at a blank page (past the document end). In this change we ensure that the cursor is within view whenever we switch to a new window (for example with `w`). --- helix-view/src/editor.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 47edf30392cab..bb9616e82f092 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1223,9 +1223,11 @@ 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 + // if leaving the view: mode should reset and the cursor should be + // within view if prev_id != view_id { self.mode = Mode::Normal; + self.ensure_cursor_in_view(view_id); } } @@ -1234,9 +1236,11 @@ impl Editor { self.tree.focus_next(); let id = self.tree.focus; - // if leaving the view: mode should reset + // if leaving the view: mode should reset and the cursor should be + // within view if prev_id != id { self.mode = Mode::Normal; + self.ensure_cursor_in_view(id); } }