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