Some refactoring

This commit is contained in:
Greg Shuflin committed 2024-03-12 10:14:53 -07:00
1 parent 9e3a99f435
commit f9d1c70843
2 files changed
+11 -30

No files matched your search

+1 -7
View File
@@ -107,14 +107,8 @@ impl EditorCore {
}
fn load_file(&mut self, file: &Path) -> Result<(), BasicError> {
info!(file=%file.display(), "Opening file");
//TODO needs to be an async operation
let s = std::fs::read_to_string(file)?;
let new_buffer = {
let mut b = TextBuffer::new_with_text(s);
b.backing_file = Some(file.to_owned());
b
};
let new_buffer = TextBuffer::load_file(file)?;
self.buffers.insert(self.active_buffer, new_buffer);
Ok(())
}
+10 -23
View File
@@ -1,30 +1,13 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use crop::Rope;
use tracing::info;
use super::{buffer_settings::BufferSettings, cursor::Cursor};
use super::{buffer_settings::BufferSettings, cursor::Cursor, error::BasicError};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct BufferId(pub(crate) usize);
#[derive(Debug)]
pub(crate) enum Buffer {
Text(TextBuffer),
Internal(InternalBuffer),
}
#[derive(Debug)]
pub(crate) struct InternalBuffer {}
// this should be a trait
impl InternalBuffer {
//TODO maybe this can't be str
fn text(&self) -> &str {
todo!()
}
}
#[derive(Debug)]
pub(crate) struct TextBuffer {
pub(crate) text: Rope,
@@ -46,19 +29,23 @@ impl TextBuffer {
}
}
pub(crate) fn new_with_text(text: String) -> Self {
pub(crate) fn load_file(file: &Path) -> Result<Self, BasicError> {
info!(file=%file.display(), "Opening file");
let text = std::fs::read_to_string(file)?;
let text = {
let mut t = Rope::new();
t.insert(0, &text);
t
};
Self {
Ok(Self {
text,
cursor_pos: Cursor::new(),
buffer_settings: BufferSettings::new(),
backing_file: None,
backing_file: Some(file.to_owned()),
writeable: true,
}
})
}
//#[tracing::instrument()]