Starting to generalize buffers

This commit is contained in:
Greg Shuflin committed 2024-04-04 02:37:36 -07:00
1 parent ac5ea7f95d
commit c83c91c335
2 files changed
+26 -18

No files matched your search

+22 -18
View File
@@ -24,7 +24,7 @@ use crate::{
};
use self::{
buffer::{BufferId, TextBuffer},
buffer::{Buffer, BufferId, TextBuffer},
commands::{CommandOutput, Commands},
error::{log_only, BasicError},
pane_layout::PaneLayout,
@@ -49,7 +49,7 @@ pub(crate) enum Mode {
pub(crate) struct EditorCore {
// buffer-mgmt-related stuff - TODO move this into a substructure
buffers: HashMap<BufferId, TextBuffer>,
buffers: HashMap<BufferId, Buffer>,
pane_layout: PaneLayout,
active_buffer: BufferId,
@@ -64,7 +64,7 @@ impl EditorCore {
pub(crate) fn new() -> Self {
let mut buffers = HashMap::new();
let active_buffer = BufferId(0);
buffers.insert(active_buffer, TextBuffer::new());
buffers.insert(active_buffer, Buffer::Text(TextBuffer::new()));
let pane_layout = PaneLayout::new(active_buffer);
@@ -94,15 +94,15 @@ impl EditorCore {
self.commands.current_command.as_ref().map(|x| x.as_str())
}
pub(crate) fn get_buffer_by_id(&self, id: BufferId) -> Option<&TextBuffer> {
pub(crate) fn get_buffer_by_id(&self, id: BufferId) -> Option<&Buffer> {
self.buffers.get(&id)
}
pub(crate) fn active_buffer(&self) -> &TextBuffer {
pub(crate) fn active_buffer(&self) -> &Buffer {
let active = self.active_buffer;
self.buffers.get(&active).unwrap()
}
fn active_buffer_mut(&mut self) -> &mut TextBuffer {
fn active_buffer_mut(&mut self) -> &mut Buffer {
let active = self.active_buffer;
self.buffers.get_mut(&active).unwrap()
}
@@ -117,23 +117,26 @@ impl EditorCore {
fn load_file(&mut self, file: &Path) -> Result<(), BasicError> {
//TODO needs to be an async operation
let new_buffer = TextBuffer::load_file(file)?;
let new_buffer = Buffer::Text(new_buffer);
self.buffers.insert(self.active_buffer, new_buffer);
Ok(())
}
fn write_current_buffer(&mut self, path: Option<PathBuf>) -> Result<(), BasicError> {
let active_buffer = self.active_buffer();
if let Buffer::Text(active_buffer) = self.active_buffer() {
let filename = match (&active_buffer.backing_file, &path) {
(_, Some(ref path)) => path,
(Some(ref path), None) => path,
(None, None) => {
error!("No path to write buffer to");
return Err(BasicErrorKind::InvalidArgument.to_error());
}
};
let filename = match (&active_buffer.backing_file, &path) {
(_, Some(ref path)) => path,
(Some(ref path), None) => path,
(None, None) => {
error!("No path to write buffer to");
return Err(BasicErrorKind::InvalidArgument.to_error());
}
};
active_buffer.write_to_file(filename)
active_buffer.write_to_file(filename)
} else {
Err(BasicErrorKind::InvalidArgument.to_error())
}
}
//TODO this needs to only emit EditorAction's , it can't directly modify anything
@@ -209,7 +212,8 @@ impl EditorCore {
//TODO change this hardcoding of the buffer id
let buffer_id = BufferId(1);
if toggle {
self.buffers.insert(buffer_id, TextBuffer::help());
self.buffers
.insert(buffer_id, Buffer::Text(TextBuffer::help()));
let descriptor = PaneDescriptor::Floating;
self.pane_layout.add(buffer_id, descriptor);
} else {
+4
View File
@@ -13,6 +13,10 @@ use super::{buffer_settings::BufferSettings, cursor::Cursor, error::BasicError};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct BufferId(pub(crate) usize);
pub(crate) enum Buffer {
Text(TextBuffer),
}
//TODO make these settings nonpublic, except for some setting that indicates style
#[derive(Debug)]
pub(crate) struct TextBuffer {