From 1339da330ea61811ed4c73b55eba230ab038b8d0 Mon Sep 17 00:00:00 2001 From: kerty Date: Fri, 24 Jan 2025 13:16:35 +0300 Subject: [PATCH] Fix crash when clicking on empty lines This fixes a crash that occurred in Kitty when clicking on lines without characters, such as soft wrapped lines or first line without a prompt. --- src/screen.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/screen.rs b/src/screen.rs index 2bb666dfc..bd9f5ecee 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -611,8 +611,18 @@ pub fn offset_in_cmdline_given_cursor( let x = viewport_position.x - viewport_prompt_x; let line = self.actual.line(y); let x = x.max(line.indentation); - let char = line.text.get(x).or(line.text.last()).unwrap(); - match char.offset_in_cmdline { + let offset = line + .text + .get(x) + .or(line.text.last()) + .or(if y > 0 { + self.actual.line(y - 1).text.last() + } else { + None + }) + .map(|char| char.offset_in_cmdline) + .unwrap_or(CharOffset::Pointer(0)); + match offset { CharOffset::Cmd(value) if x >= line.len() => CharOffset::Cmd(value + 1), CharOffset::Pager(_) if x >= line.len() => CharOffset::None, offset => offset,