Compare commits
No commits in common. "28ce80211681c7a9c4ae8301523ebe6e6b76455a" and "41e9c197cdaefaa16d3b75ec5cf9cf858b506a6d" have entirely different histories.
28ce802116
...
41e9c197cd
|
|
@ -27,8 +27,8 @@ pub enum CommandResponse {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct CommandResult {
|
pub struct CommandResult {
|
||||||
success: bool,
|
success: bool,
|
||||||
pub response: CommandResponse,
|
response: CommandResponse,
|
||||||
pub buffer: EditorBuffer,
|
buffer: EditorBuffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandResult {
|
impl CommandResult {
|
||||||
|
|
@ -93,31 +93,14 @@ impl EditorBuffer {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_cursor_to_line_number(&self, line_num: usize) -> CommandResult {
|
fn move_cursor_to_line_number(&self, _line_num: usize) -> CommandResult {
|
||||||
self.move_cursor_to_point(Point::LineColumn(line_num, 0))
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
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: match self.cursor {
|
cursor: self.cursor.advance(),
|
||||||
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()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -321,57 +304,4 @@ 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,6 +26,16 @@ 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