Add very basic cursor control
This commit is contained in:
parent
f33d51f931
commit
a8ce6e8e59
|
|
@ -13,12 +13,23 @@ pub struct EditorBuffer {
|
|||
filepath: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum CommandResponse {
|
||||
Ok,
|
||||
Success(String),
|
||||
Failure(String),
|
||||
}
|
||||
|
||||
impl CommandResponse {
|
||||
pub fn is_ok(&self) -> bool {
|
||||
match self {
|
||||
Self::Ok => true,
|
||||
Self::Success(_) => true,
|
||||
Self::Failure(_) => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EditorBuffer {
|
||||
/// Create new empty [EditorBuffer]
|
||||
pub fn new() -> Self {
|
||||
|
|
@ -40,8 +51,13 @@ impl EditorBuffer {
|
|||
}
|
||||
}
|
||||
|
||||
fn move_cursor_to_point(&mut self, _point: Point) -> CommandResponse {
|
||||
todo!()
|
||||
pub fn get_cursor_position(&self) -> Point {
|
||||
self.cursor
|
||||
}
|
||||
|
||||
fn move_cursor_to_point(&mut self, point: Point) -> CommandResponse {
|
||||
self.cursor = point;
|
||||
CommandResponse::Ok
|
||||
}
|
||||
|
||||
fn move_cursor_to_line_number(&mut self, _line_num: usize) -> CommandResponse {
|
||||
|
|
@ -107,4 +123,25 @@ mod tests {
|
|||
let found_contents: String = target.buffer.iter_chars().collect();
|
||||
assert_eq!(expected_contents, found_contents);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cursor_at_beginning_after_file_opened() {
|
||||
let test_file = create_simple_test_file();
|
||||
let mut target = EditorBuffer::new();
|
||||
target.execute(Command::OpenFile(test_file.path().into()));
|
||||
assert_eq!(Point::Start, target.get_cursor_position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_cursor_to_point_in_file() {
|
||||
let test_file = create_simple_test_file();
|
||||
let mut target = EditorBuffer::new();
|
||||
target.execute(Command::OpenFile(test_file.path().into()));
|
||||
assert!(target.execute(Command::MoveCursorTo(Point::LineColumn(0, 5))).is_ok());
|
||||
assert_eq!(Point::LineColumn(0,5), target.get_cursor_position());
|
||||
assert!(target.execute(Command::MoveCursorTo(Point::LineColumn(3, 11))).is_ok());
|
||||
assert_eq!(Point::LineColumn(3,11), target.get_cursor_position());
|
||||
assert!(target.execute(Command::MoveCursorTo(Point::LineColumn(3, 0))).is_ok());
|
||||
assert_eq!(Point::LineColumn(3,0), target.get_cursor_position());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,12 @@ pub struct TextBuffer {
|
|||
}
|
||||
|
||||
/// A location in a [TextBuffer]
|
||||
#[derive(Default, Clone, Copy)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub enum Point {
|
||||
/// The end of the buffer
|
||||
#[default]
|
||||
Start,
|
||||
LineColumn(usize, usize),
|
||||
End,
|
||||
}
|
||||
|
||||
|
|
@ -63,6 +65,14 @@ impl TextBuffer {
|
|||
/// Insert `text` at `point`.
|
||||
pub fn insert_text(&mut self, text: impl Into<String>, point: Point) {
|
||||
match point {
|
||||
Point::Start => {
|
||||
self.contents = self
|
||||
.contents
|
||||
.insert_at_char_index(0, text);
|
||||
},
|
||||
Point::LineColumn(_, _) => {
|
||||
todo!()
|
||||
}
|
||||
Point::End => {
|
||||
self.contents = self
|
||||
.contents
|
||||
|
|
@ -73,6 +83,14 @@ impl TextBuffer {
|
|||
|
||||
pub fn insert_char(&mut self, c: char, point: Point) {
|
||||
match point {
|
||||
Point::Start => {
|
||||
self.contents = self
|
||||
.contents
|
||||
.insert_at_char_index(0, c)
|
||||
},
|
||||
Point::LineColumn(_, _) => {
|
||||
todo!()
|
||||
}
|
||||
Point::End => {
|
||||
self.contents = self
|
||||
.contents
|
||||
|
|
|
|||
Loading…
Reference in New Issue