use TextLines type
This commit is contained in:
1 parent
0520c23496
commit
7252c37092
2 files changed
+46
-29
No files matched your search
+27
-28
@@ -3,9 +3,10 @@ mod text;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crop::Rope;
|
||||
use tracing::info;
|
||||
|
||||
use self::text::TextLines;
|
||||
|
||||
use super::actions::CursorMovement;
|
||||
use super::{buffer_settings::BufferSettings, cursor::Cursor, error::BasicError};
|
||||
|
||||
@@ -16,7 +17,9 @@ pub(crate) struct BufferId(pub(crate) usize);
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TextBuffer {
|
||||
title: Option<String>,
|
||||
text: Rope,
|
||||
text: TextLines,
|
||||
//TODO cursor_pos needs to be way less complicated, all the logic for moving it needs to live
|
||||
//here in this buffer module
|
||||
pub(crate) cursor_pos: Cursor,
|
||||
pub(crate) buffer_settings: BufferSettings,
|
||||
pub(crate) backing_file: Option<PathBuf>,
|
||||
@@ -28,7 +31,7 @@ impl TextBuffer {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
title: None,
|
||||
text: Rope::new(),
|
||||
text: TextLines::empty(),
|
||||
cursor_pos: Cursor::new(),
|
||||
buffer_settings: BufferSettings::new(),
|
||||
backing_file: None,
|
||||
@@ -38,9 +41,7 @@ impl TextBuffer {
|
||||
|
||||
pub(crate) fn help() -> Self {
|
||||
let mut buf = Self::new();
|
||||
let mut t = Rope::new();
|
||||
t.insert(0, "Dummy help text");
|
||||
buf.text = t;
|
||||
buf.text = TextLines::from_text("Dummy help text");
|
||||
buf.writeable = false;
|
||||
buf.title = Some("Help".to_string());
|
||||
buf
|
||||
@@ -59,11 +60,7 @@ impl TextBuffer {
|
||||
|
||||
let text = std::fs::read_to_string(file)?;
|
||||
|
||||
let text = {
|
||||
let mut t = Rope::new();
|
||||
t.insert(0, &text);
|
||||
t
|
||||
};
|
||||
let text = TextLines::from_text(&text);
|
||||
let title = file
|
||||
.file_name()
|
||||
.map(|x| x.to_string_lossy().to_string())
|
||||
@@ -84,7 +81,7 @@ impl TextBuffer {
|
||||
//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() {
|
||||
for chunk in self.text.inner.chunks() {
|
||||
file.write_all(chunk.as_bytes())?;
|
||||
}
|
||||
|
||||
@@ -93,17 +90,18 @@ impl TextBuffer {
|
||||
|
||||
//#[tracing::instrument()]
|
||||
pub(crate) fn visible_lines(&self, start_line: usize, end_line: usize) -> Vec<String> {
|
||||
if self.text.line_len() == 0 {
|
||||
if self.text.inner.line_len() == 0 {
|
||||
return vec![];
|
||||
}
|
||||
let end_line = if end_line > self.text.line_len() {
|
||||
self.text.line_len()
|
||||
let end_line = if end_line > self.text.inner.line_len() {
|
||||
self.text.inner.line_len()
|
||||
} else {
|
||||
end_line
|
||||
};
|
||||
|
||||
let mut lines: Vec<String> = self
|
||||
.text
|
||||
.inner
|
||||
.line_slice(start_line..end_line)
|
||||
.lines()
|
||||
.map(|line| line.to_string())
|
||||
@@ -117,6 +115,7 @@ impl TextBuffer {
|
||||
fn final_newline(&self) -> bool {
|
||||
//TODO needs to handle multiple types of newline
|
||||
self.text
|
||||
.inner
|
||||
.chars()
|
||||
.next_back()
|
||||
.map(|ch| ch == '\n')
|
||||
@@ -128,18 +127,18 @@ impl TextBuffer {
|
||||
let y = self.cursor_pos.y() as usize;
|
||||
let x = self.cursor_pos.x() as usize;
|
||||
|
||||
if self.text.byte_len() == 0 {
|
||||
if self.text.inner.byte_len() == 0 {
|
||||
return 0;
|
||||
}
|
||||
let line_idx = self.text.byte_of_line(y);
|
||||
let line_len = self.text.line_len();
|
||||
let line_idx = self.text.inner.byte_of_line(y);
|
||||
let line_len = self.text.inner.line_len();
|
||||
info!(%line_idx, %line_len);
|
||||
|
||||
// We're at the current end of the Rope
|
||||
if y == line_len {
|
||||
line_idx
|
||||
} else {
|
||||
let line = self.text.line(y);
|
||||
let line = self.text.inner.line(y);
|
||||
//TODO this should happen at the grapheme level, controllably
|
||||
let char_idx = line
|
||||
.chars()
|
||||
@@ -154,7 +153,7 @@ impl TextBuffer {
|
||||
let char_idx = self.get_current_char_idx();
|
||||
let mut tmp = [0u8; 4];
|
||||
let s = ch.encode_utf8(&mut tmp);
|
||||
self.text.insert(char_idx, s);
|
||||
self.text.inner.insert(char_idx, s);
|
||||
self.cursor_pos.respond_to_new_char(ch);
|
||||
}
|
||||
|
||||
@@ -162,22 +161,22 @@ impl TextBuffer {
|
||||
// is zero-sized
|
||||
#[tracing::instrument(ret)]
|
||||
fn get_previous_char_idx(&self) -> Option<(usize, usize)> {
|
||||
if self.text.byte_len() == 0 {
|
||||
if self.text.inner.byte_len() == 0 {
|
||||
return None;
|
||||
}
|
||||
let y = self.cursor_pos.y() as usize;
|
||||
let x = self.cursor_pos.x() as usize;
|
||||
//TODO this should happen at the grapheme level, controllably
|
||||
let line_idx = self.text.byte_of_line(y);
|
||||
let line_idx = self.text.inner.byte_of_line(y);
|
||||
if x == 0 {
|
||||
let last_line_idx = self.text.byte_of_line(y - 1);
|
||||
let last_line_len = self.text.line(y - 1).byte_len();
|
||||
let last_line_idx = self.text.inner.byte_of_line(y - 1);
|
||||
let last_line_len = self.text.inner.line(y - 1).byte_len();
|
||||
|
||||
let char_idx = last_line_idx + last_line_len;
|
||||
let size = line_idx - char_idx;
|
||||
Some((char_idx, size))
|
||||
} else {
|
||||
let line = self.text.line(y);
|
||||
let line = self.text.inner.line(y);
|
||||
let char_idx = line
|
||||
.chars()
|
||||
.take(x - 1)
|
||||
@@ -192,12 +191,12 @@ impl TextBuffer {
|
||||
// Needs to handle conversion between utf-8 represenatation within rope, and char/grapheme representation within Cursor
|
||||
pub(super) fn delete_char(&mut self) {
|
||||
if let Some((char_idx, len_utf8)) = self.get_previous_char_idx() {
|
||||
self.text.delete(char_idx..(char_idx + len_utf8));
|
||||
self.cursor_pos.respond_to_delete(&self.text);
|
||||
self.text.inner.delete(char_idx..(char_idx + len_utf8));
|
||||
self.cursor_pos.respond_to_delete(&self.text.inner);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn move_cursor(&mut self, movement: CursorMovement) {
|
||||
self.cursor_pos.move_cursor(movement, &self.text);
|
||||
self.cursor_pos.move_cursor(movement, &self.text.inner);
|
||||
}
|
||||
}
|
||||
+19
-1
@@ -1,4 +1,22 @@
|
||||
use crop::Rope;
|
||||
|
||||
/// 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 {}
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TextLines {
|
||||
pub(super) inner: Rope,
|
||||
}
|
||||
|
||||
impl TextLines {
|
||||
/// Generates a new, empty `TextLines`
|
||||
pub(crate) fn empty() -> Self {
|
||||
Self { inner: Rope::new() }
|
||||
}
|
||||
|
||||
pub(crate) fn from_text(text: &str) -> Self {
|
||||
let mut inner = Rope::new();
|
||||
inner.insert(0, &text);
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user