Compare commits

...

3 Commits

2 changed files with 75 additions and 15 deletions

View File

@ -27,8 +27,8 @@ pub enum CommandResponse {
#[must_use]
pub struct CommandResult {
success: bool,
response: CommandResponse,
buffer: EditorBuffer,
pub response: CommandResponse,
pub buffer: EditorBuffer,
}
impl CommandResult {
@ -93,14 +93,31 @@ impl EditorBuffer {
})
}
fn move_cursor_to_line_number(&self, _line_num: usize) -> CommandResult {
todo!()
fn move_cursor_to_line_number(&self, line_num: usize) -> CommandResult {
self.move_cursor_to_point(Point::LineColumn(line_num, 0))
}
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()
})
}
@ -304,4 +321,57 @@ mod tests {
.collect();
assert_eq!(expected_lines, found_lines);
}
#[test]
fn insert_line() {
let mut test_file = create_simple_test_file();
let mut file_contents = String::new();
test_file
.read_to_string(&mut file_contents)
.expect("Reading text file");
let test_file = test_file;
let mut expected_lines: Vec<_> = file_contents.lines().collect();
expected_lines.insert(6, "abc 123".into());
let target = EditorBuffer::new();
let result = target.execute(Command::OpenFile(test_file.path().into()));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::MoveCursorToLine(7));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('a'));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('b'));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('c'));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar(' '));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('1'));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('2'));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('3'));
assert!(result.is_ok());
let target = result.buffer;
let result = target.execute(Command::InsertChar('\n'));
assert!(result.is_ok());
let target = result.buffer;
let found_lines: Vec<String> = target
.buffer
.iter_chars()
.collect::<String>()
.lines()
.map(|l| l.into())
.collect();
assert_eq!(expected_lines, found_lines);
assert_eq!(Point::LineColumn(8, 0), target.get_cursor_position());
}
}

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 {