From 41de475125a3ef108e8c51ae59c4c5583e0c386a Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 2 Nov 2022 15:00:21 -0500 Subject: [PATCH 1/3] Clamp highlighting range to be within document This fixes a panic possible when two vsplits of the same document exist and enough lines are deleted from the document so that one of the windows focuses past the end of the document. --- helix-term/src/ui/editor.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 72c9d15e6d0a..012944644c91 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -227,16 +227,16 @@ impl EditorView { _theme: &Theme, ) -> Box + 'doc> { let text = doc.text().slice(..); - let last_line = std::cmp::min( - // Saturating subs to make it inclusive zero indexing. - (offset.row + height as usize).saturating_sub(1), - doc.text().len_lines().saturating_sub(1), - ); let range = { - // calculate viewport byte ranges - let start = text.line_to_byte(offset.row); - let end = text.line_to_byte(last_line + 1); + // Calculate viewport byte ranges: + // Saturating subs to make it inclusive zero indexing. + let last_line = doc.text().len_lines().saturating_sub(1); + let last_visible_line = (offset.row + height as usize) + .saturating_sub(1) + .min(last_line); + let start = text.line_to_byte(offset.row.min(last_line)); + let end = text.line_to_byte(last_visible_line.min(last_line) + 1); start..end }; From e60266d7d070c31d141945d6017729b5875c45aa Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 2 Nov 2022 15:03:52 -0500 Subject: [PATCH 2/3] 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 47edf30392ca..bb9616e82f09 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); } } From ebb29bdb899f7b0e4b624bbc72029a3f0d783817 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 3 Nov 2022 00:56:02 -0500 Subject: [PATCH 3/3] Update helix-term/src/ui/editor.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Blaž Hrastnik --- helix-term/src/ui/editor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 012944644c91..2cd2ad056b53 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -236,7 +236,7 @@ impl EditorView { .saturating_sub(1) .min(last_line); let start = text.line_to_byte(offset.row.min(last_line)); - let end = text.line_to_byte(last_visible_line.min(last_line) + 1); + let end = text.line_to_byte(last_visible_line + 1); start..end };