Fix Cursor::Caret position on lines that wrap on whitespace

This commit is contained in:
Héctor Ramón Jiménez 2023-09-14 00:47:04 +02:00
parent ab020383b9
commit e6c2db8a93
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -157,7 +157,7 @@ impl editor::Editor for Editor {
.map(|glyph| glyph.end)
.unwrap_or(0);
let is_cursor_after_start = start <= cursor.index;
let is_cursor_before_start = start > cursor.index;
let is_cursor_before_end = match cursor.affinity {
cosmic_text::Affinity::Before => {
@ -166,7 +166,17 @@ impl editor::Editor for Editor {
cosmic_text::Affinity::After => cursor.index < end,
};
if is_cursor_after_start && is_cursor_before_end {
if is_cursor_before_start {
// Sometimes, the glyph we are looking for is right
// between lines. This can happen when a line wraps
// on a space.
// In that case, we can assume the cursor is at the
// end of the previous line.
// i is guaranteed to be > 0 because `start` is always
// 0 for the first line, so there is no way for the
// cursor to be before it.
Some((i - 1, layout[i - 1].w))
} else if is_cursor_before_end {
let offset = line
.glyphs
.iter()