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.
This commit is contained in:
kerty
2025-01-24 13:16:35 +03:00
committed by Johannes Altmanninger
parent 6bb19cf1f4
commit 1339da330e

View File

@@ -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,