Update cursor correctly when inserting newlines
This commit is contained in:
parent
995f1657cd
commit
5d82c84239
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue