Compare commits
3 Commits
41e9c197cd
...
28ce802116
| Author | SHA1 | Date |
|---|---|---|
|
|
28ce802116 | |
|
|
5d82c84239 | |
|
|
995f1657cd |
|
|
@ -27,8 +27,8 @@ pub enum CommandResponse {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct CommandResult {
|
pub struct CommandResult {
|
||||||
success: bool,
|
success: bool,
|
||||||
response: CommandResponse,
|
pub response: CommandResponse,
|
||||||
buffer: EditorBuffer,
|
pub buffer: EditorBuffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandResult {
|
impl CommandResult {
|
||||||
|
|
@ -93,14 +93,31 @@ impl EditorBuffer {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_cursor_to_line_number(&self, _line_num: usize) -> CommandResult {
|
fn move_cursor_to_line_number(&self, line_num: usize) -> CommandResult {
|
||||||
todo!()
|
self.move_cursor_to_point(Point::LineColumn(line_num, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_char(&self, c: char) -> CommandResult {
|
fn insert_char(&self, c: char) -> CommandResult {
|
||||||
|
let newline = c == '\n';
|
||||||
CommandResult::ok(Self {
|
CommandResult::ok(Self {
|
||||||
buffer: self.buffer.insert_char(c, self.cursor),
|
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()
|
..self.clone()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -304,4 +321,57 @@ mod tests {
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(expected_lines, found_lines);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,16 +26,6 @@ pub enum Point {
|
||||||
End,
|
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 {
|
impl TextBuffer {
|
||||||
/// Create a new empty [TextBuffer]
|
/// Create a new empty [TextBuffer]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue