From 5d82c84239f3329293adbc17813d250a4d8391ca Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Thu, 27 Nov 2025 21:26:12 -0400 Subject: [PATCH] Update cursor correctly when inserting newlines --- core/src/editor_buffer/mod.rs | 19 ++++++++++++++++++- core/src/text_buffer/mod.rs | 10 ---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/core/src/editor_buffer/mod.rs b/core/src/editor_buffer/mod.rs index c6a0616..1e4249f 100644 --- a/core/src/editor_buffer/mod.rs +++ b/core/src/editor_buffer/mod.rs @@ -98,9 +98,26 @@ impl EditorBuffer { } fn insert_char(&self, c: char) -> CommandResult { + let newline = c == '\n'; CommandResult::ok(Self { buffer: self.buffer.insert_char(c, self.cursor), - cursor: self.cursor.advance(), + cursor: match self.cursor { + Point::Start => { + if newline { + Point::LineColumn(1, 0) + } else { + Point::LineColumn(0, 1) + } + } + Point::LineColumn(line, column) => { + if newline { + Point::LineColumn(line + 1, 0) + } else { + Point::LineColumn(line, column + 1) + } + } + Point::End => Point::End, + }, ..self.clone() }) } diff --git a/core/src/text_buffer/mod.rs b/core/src/text_buffer/mod.rs index c208bbf..f2a0b63 100644 --- a/core/src/text_buffer/mod.rs +++ b/core/src/text_buffer/mod.rs @@ -26,16 +26,6 @@ pub enum Point { End, } -impl Point { - pub fn advance(self) -> Self { - match self { - Self::Start => Self::LineColumn(0, 1), - Self::LineColumn(l, c) => Self::LineColumn(l, c + 1), - Self::End => Self::End, - } - } -} - impl TextBuffer { /// Create a new empty [TextBuffer] pub fn new() -> Self {