From 3ce8fac6c8b586fb14c8484c2d56436d4d68f673 Mon Sep 17 00:00:00 2001 From: "Ben Fekih, Hichem" Date: Fri, 19 Apr 2024 22:26:09 +0200 Subject: [PATCH] popup: bugfix: reset the position to the cursor when outside the viewport while resizing, if the position is outside the view, then the program panics. check the position and reset it to the cursor position Signed-off-by: Ben Fekih, Hichem --- helix-term/src/ui/popup.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 124b4402fdb1f..f953ae98cbc76 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -98,9 +98,20 @@ impl Popup { /// Calculate the position where the popup should be rendered and return the coordinates of the /// top left corner. pub fn get_rel_position(&mut self, viewport: Rect, editor: &Editor) -> (u16, u16) { - let position = self - .position - .get_or_insert_with(|| editor.cursor().0.unwrap_or_default()); + let position = if let Some(position) = self.position { + // check if the position is still inside the viewport + if position.row as u16 >= viewport.y + && (position.row as u16) < (viewport.y + viewport.height) + && position.col as u16 >= viewport.x + && (position.col as u16) < (viewport.x + viewport.width) + { + position + } else { + editor.cursor().0.unwrap_or_default() + } + } else { + editor.cursor().0.unwrap_or_default() + }; let (width, height) = self.size;