diff --git a/core/src/editor_buffer/command.rs b/core/src/editor_buffer/command.rs new file mode 100644 index 0000000..55ae872 --- /dev/null +++ b/core/src/editor_buffer/command.rs @@ -0,0 +1,53 @@ +use std::path::PathBuf; + +#[allow(unused_imports)] +use crate::{EditorBuffer, Point}; + +pub enum Unit { + Char, + Word, + Line, + Buffer, +} +pub enum Movement { + Next(Unit), + Prev(Unit), + Start(Unit), + End(Unit), +} + +/// Editing commands accepted by [EditorBuffer] +pub enum Command { + /// Open a file with the specified path + OpenFile(PathBuf), + + /// Save the file + /// + /// Saves the contents of the [EditorBuffer] to it's associated file + /// path. (Normally either the same location it was last saved to, or the + /// location it was opened from if it hasn't been saved yet.) + Save, + + /// Save the file + /// + /// Saves the contents of the [EditorBuffer] to the specified path. + SaveAs(PathBuf), + + /// Move cursor to given [Point] + MoveCursorTo(Point), + + /// Move cursor + MoveCursor(Movement), + + /// Move cursor to start of nth line + MoveCursorToLine(usize), + + /// Insert the `char` at the current cursor location + InsertChar(char), + + /// Insert string at the current cursor location + InsertString(String), + + /// Delete everything between cursor and destinationm of [Movement] + Delete(Movement), +} diff --git a/core/src/editor_buffer/mod.rs b/core/src/editor_buffer/mod.rs new file mode 100644 index 0000000..cb2c75a --- /dev/null +++ b/core/src/editor_buffer/mod.rs @@ -0,0 +1,69 @@ +use std::path::PathBuf; + +use crate::{Point, TextBuffer}; + +mod command; +pub use command::{Command, Movement, Unit}; + +#[derive(Default)] +pub struct EditorBuffer { + buffer: TextBuffer, + cursor: Point, + filepath: Option, +} + +pub enum CommandResponse {} + +impl EditorBuffer { + /// Create new empty [EditorBuffer] + pub fn new() -> Self { + Self::default() + } + + /// Execute a command on the [EditorBuffer] + pub fn execute(&mut self, command: Command) -> CommandResponse { + match command { + Command::OpenFile(filepath) => self.open_file(filepath), + Command::Save => self.save_file(None), + Command::SaveAs(filepath) => self.save_file(Some(filepath)), + Command::MoveCursorTo(point) => self.move_cursor_to_point(point), + Command::MoveCursorToLine(line_num) => self.move_cursor_to_line_number(line_num), + Command::MoveCursor(movement) => self.move_cursor(movement), + Command::InsertChar(c) => self.insert_char(c), + Command::InsertString(s) => self.insert_string(s), + Command::Delete(movement) => self.delete(movement), + } + } + + fn open_file(&mut self, _filepath: PathBuf) -> CommandResponse { + todo!() + } + + fn save_file(&mut self, _filepath: Option) -> CommandResponse { + todo!() + } + + fn move_cursor_to_point(&mut self, _point: Point) -> CommandResponse { + todo!() + } + + fn move_cursor_to_line_number(&mut self, _line_num: usize) -> CommandResponse { + todo!() + } + + fn insert_char(&mut self, _c: char) -> CommandResponse { + todo!() + } + + fn insert_string(&mut self, _s: String) -> CommandResponse { + todo!() + } + + fn move_cursor(&mut self, _movement: Movement) -> CommandResponse { + todo!() + } + + fn delete(&mut self, _movement: Movement) -> CommandResponse { + todo!() + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index fb8cae4..54a4f9e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,2 +1,4 @@ mod text_buffer; pub use text_buffer::{Point, TextBuffer, TextBufferReader, TextBufferWriter}; +mod editor_buffer; +pub use editor_buffer::{Command,EditorBuffer}; diff --git a/core/src/text_buffer/mod.rs b/core/src/text_buffer/mod.rs index a8e3b69..03f6d2c 100644 --- a/core/src/text_buffer/mod.rs +++ b/core/src/text_buffer/mod.rs @@ -16,8 +16,10 @@ pub struct TextBuffer { } /// A location in a [TextBuffer] +#[derive(Default, Clone, Copy)] pub enum Point { /// The end of the buffer + #[default] End, }