sort of fix text editing (better but still bad)

This commit is contained in:
2025-09-11 17:12:11 -04:00
parent 242c3b992e
commit e9853120ce
3 changed files with 13 additions and 6 deletions

View File

@@ -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;
}

View File

@@ -28,7 +28,7 @@ pub struct TextAttrs {
pub family: Family<'static>,
}
#[derive(Default)]
#[derive(Default, Debug, Copy, Clone)]
pub enum Cursor {
#[default]
None,

View File

@@ -4,6 +4,7 @@
#![feature(const_from)]
#![feature(map_try_insert)]
#![feature(trait_alias)]
#![feature(round_char_boundary)]
pub mod core;
pub mod layout;