Add EditorBuffer and Command; stubs for a few commands

This commit is contained in:
Matthew Gordon 2025-11-10 15:40:35 -04:00
parent 1de9569beb
commit 36ab1b1769
4 changed files with 126 additions and 0 deletions

View File

@ -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),
}

View File

@ -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<PathBuf>,
}
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<PathBuf>) -> 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!()
}
}

View File

@ -1,2 +1,4 @@
mod text_buffer;
pub use text_buffer::{Point, TextBuffer, TextBufferReader, TextBufferWriter};
mod editor_buffer;
pub use editor_buffer::{Command,EditorBuffer};

View File

@ -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,
}