use reovim_kernel::api::v1::{BufferId, Position};
pub trait BufferApi: Send {
fn active_buffer(&self) -> Option<BufferId>;
fn set_active_buffer(&mut self, id: Option<BufferId>);
fn buffer_line(&self, buffer: BufferId, line: usize) -> Option<String>;
fn buffer_line_count(&self, buffer: BufferId) -> Option<usize>;
fn buffer_line_len(&self, buffer: BufferId, line: usize) -> Option<usize>;
fn buffer_text_range(&self, buffer: BufferId, start: Position, end: Position)
-> Option<String>;
fn buffer_content(&self, buffer: BufferId) -> Option<String>;
fn buffer_file_path(&self, buffer: BufferId) -> Option<String>;
fn is_buffer_modified(&self, buffer: BufferId) -> Option<bool>;
fn set_buffer_modified(&mut self, buffer: BufferId, modified: bool);
fn insert_text(&mut self, buffer: BufferId, pos: Position, text: &str);
fn delete_range(&mut self, buffer: BufferId, start: Position, end: Position);
fn replace_content(&mut self, buffer: BufferId, content: &str);
fn create_buffer(&mut self, name: Option<&str>, content: &str) -> BufferId;
fn delete_buffer(&mut self, buffer: BufferId) -> Result<(), BufferError>;
fn rename_buffer(&mut self, buffer: BufferId, new_name: &str);
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Selection {
pub start: Position,
pub end: Position,
pub mode: SelectionMode,
}
impl Selection {
#[must_use]
pub const fn new(start: Position, end: Position, mode: SelectionMode) -> Self {
Self { start, end, mode }
}
#[must_use]
pub const fn character(start: Position, end: Position) -> Self {
Self::new(start, end, SelectionMode::Character)
}
#[must_use]
pub const fn line(start: Position, end: Position) -> Self {
Self::new(start, end, SelectionMode::Line)
}
#[must_use]
pub const fn block(start: Position, end: Position) -> Self {
Self::new(start, end, SelectionMode::Block)
}
#[must_use]
pub const fn is_linewise(&self) -> bool {
matches!(self.mode, SelectionMode::Line)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SelectionMode {
#[default]
Character,
Line,
Block,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BufferError {
NotFound(BufferId),
CannotDeleteLastBuffer,
}
impl std::fmt::Display for BufferError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotFound(id) => write!(f, "buffer not found: {id:?}"),
Self::CannotDeleteLastBuffer => write!(f, "cannot delete last buffer"),
}
}
}
impl std::error::Error for BufferError {}
#[cfg(test)]
#[path = "tests/buffer.rs"]
mod tests;