Refactor
This commit is contained in:
1 parent
f0e9859c75
commit
0520c23496
3 files changed
+27
-11
No files matched your search
+2
-10
@@ -8,7 +8,6 @@ mod pane_layout;
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
@@ -137,14 +136,7 @@ impl EditorCore {
|
||||
}
|
||||
};
|
||||
|
||||
//TODO write file and then atomically move
|
||||
//TODO cf. https://docs.rs/crop/latest/crop/index.html example, do this write
|
||||
//asynchronously
|
||||
let mut file = std::fs::File::create(&filename)?;
|
||||
for chunk in active_buffer.text.chunks() {
|
||||
file.write_all(chunk.as_bytes())?;
|
||||
}
|
||||
Ok(())
|
||||
active_buffer.write_to_file(filename)
|
||||
}
|
||||
|
||||
//TODO this needs to only emit EditorAction's , it can't directly modify anything
|
||||
@@ -210,7 +202,7 @@ impl EditorCore {
|
||||
}
|
||||
MoveCursor(movement) => {
|
||||
let buf = self.active_buffer_mut();
|
||||
buf.cursor_pos.move_cursor(movement, &buf.text);
|
||||
buf.move_cursor(movement);
|
||||
}
|
||||
PopupHelp(toggle) => {
|
||||
info!(%toggle, "invoke help");
|
||||
|
||||
+21
-1
@@ -1,8 +1,12 @@
|
||||
mod text;
|
||||
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crop::Rope;
|
||||
use tracing::info;
|
||||
|
||||
use super::actions::CursorMovement;
|
||||
use super::{buffer_settings::BufferSettings, cursor::Cursor, error::BasicError};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
@@ -12,7 +16,7 @@ pub(crate) struct BufferId(pub(crate) usize);
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TextBuffer {
|
||||
title: Option<String>,
|
||||
pub(crate) text: Rope,
|
||||
text: Rope,
|
||||
pub(crate) cursor_pos: Cursor,
|
||||
pub(crate) buffer_settings: BufferSettings,
|
||||
pub(crate) backing_file: Option<PathBuf>,
|
||||
@@ -75,6 +79,18 @@ impl TextBuffer {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn write_to_file(&self, filename: &Path) -> Result<(), BasicError> {
|
||||
//TODO write file and then atomically move
|
||||
//TODO cf. https://docs.rs/crop/latest/crop/index.html example, do this write
|
||||
//asynchronously
|
||||
let mut file = std::fs::File::create(&filename)?;
|
||||
for chunk in self.text.chunks() {
|
||||
file.write_all(chunk.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//#[tracing::instrument()]
|
||||
pub(crate) fn visible_lines(&self, start_line: usize, end_line: usize) -> Vec<String> {
|
||||
if self.text.line_len() == 0 {
|
||||
@@ -180,4 +196,8 @@ impl TextBuffer {
|
||||
self.cursor_pos.respond_to_delete(&self.text);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn move_cursor(&mut self, movement: CursorMovement) {
|
||||
self.cursor_pos.move_cursor(movement, &self.text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/// Represents the lines of text in a buffer.
|
||||
///
|
||||
/// Currently this wraps a crop::Rope, but this will probably change at some point
|
||||
pub(crate) struct TextLines {}
|
||||
Reference in new issue
Block a user