Update cursor correctly when inserting newlines

This commit is contained in:
Matthew Gordon 2025-11-27 21:26:12 -04:00
parent 995f1657cd
commit 5d82c84239
2 changed files with 18 additions and 11 deletions

View File

@ -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()
})
}

View File

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