From e9853120ce3f9aca0d4b1ec00a1723d2ce4dfc42 Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Thu, 11 Sep 2025 17:12:11 -0400 Subject: [PATCH] sort of fix text editing (better but still bad) --- src/core/text.rs | 16 +++++++++++----- src/layout/text.rs | 2 +- src/lib.rs | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/core/text.rs b/src/core/text.rs index a6b9be5..af5f417 100644 --- a/src/core/text.rs +++ b/src/core/text.rs @@ -63,18 +63,23 @@ impl Text { match &mut self.cursor { Cursor::None => (), - Cursor::Select { line, col } => { + Cursor::Select { col, .. } => { *col += 1; } } + self.update_cursor(); } pub fn backspace(&mut self) { - if let Some(i) = self.update_cursor().checked_sub(1) { + if let Some(i) = self + .update_cursor() + .checked_sub(1) + .map(|i| self.content.floor_char_boundary(i)) + { self.content.remove(i); match &mut self.cursor { Cursor::None => (), - Cursor::Select { line, col } => { + Cursor::Select { col, .. } => { *col -= 1; } } @@ -89,6 +94,7 @@ impl Text { } pub fn move_cursor(&mut self, dir: Dir) { + self.update_cursor(); if let Cursor::Select { line, col } = &mut self.cursor { match dir { Dir::LEFT => *col -= 1, @@ -114,13 +120,13 @@ impl Text { let mut l = 0; let mut c = 0; let mut cur_len = 0; - for (i, ch) in self.content.chars().enumerate() { + for (i, ch) in self.content.char_indices() { if ch == '\n' { l += 1; c = 0; } else { if l == *line { - cur_len = i as isize + 1; + cur_len = c + 1; if c == *col { idx = i; } diff --git a/src/layout/text.rs b/src/layout/text.rs index c27a45a..300ac98 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -28,7 +28,7 @@ pub struct TextAttrs { pub family: Family<'static>, } -#[derive(Default)] +#[derive(Default, Debug, Copy, Clone)] pub enum Cursor { #[default] None, diff --git a/src/lib.rs b/src/lib.rs index 9457dd9..39f8826 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![feature(const_from)] #![feature(map_try_insert)] #![feature(trait_alias)] +#![feature(round_char_boundary)] pub mod core; pub mod layout;