use crate::folds::Fold;
pub struct Buffer {
pub(crate) text: ropey::Rope,
pub(crate) dirty_gen: u64,
pub(crate) folds: Vec<Fold>,
pub(crate) fold_gen: u64,
pub(crate) cached_joined: Option<(u64, std::sync::Arc<String>)>,
pub(crate) cached_byte_len: Option<(u64, usize)>,
pub(crate) undo: crate::UndoTree,
pub(crate) undo_group_depth: u32,
pub(crate) undo_group_armed: bool,
pub(crate) undo_group_open_gen: u64,
pub(crate) content_dirty: bool,
pub(crate) cached_editor_content: Option<std::sync::Arc<String>>,
pub(crate) pending_fold_ops: Vec<crate::FoldOp>,
pub(crate) change_log: Vec<crate::EngineEdit>,
pub(crate) pending_content_edits: Vec<crate::ContentEdit>,
pub(crate) pending_content_reset: bool,
pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
pub(crate) last_cursor: (usize, usize),
}
impl Default for Buffer {
fn default() -> Self {
Self::new()
}
}
impl Buffer {
pub fn new() -> Self {
let text = ropey::Rope::new();
let undo = crate::UndoTree::new(text.clone());
Self {
text,
dirty_gen: 0,
folds: Vec::new(),
fold_gen: 0,
cached_joined: None,
cached_byte_len: None,
undo,
undo_group_depth: 0,
undo_group_armed: false,
undo_group_open_gen: 0,
content_dirty: false,
cached_editor_content: None,
pending_fold_ops: Vec::new(),
change_log: Vec::new(),
pending_content_edits: Vec::new(),
pending_content_reset: false,
marks: std::collections::BTreeMap::new(),
syntax_fold_ranges: Vec::new(),
last_cursor: (0, 0),
}
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(text: &str) -> Self {
let text = ropey::Rope::from_str(text);
let undo = crate::UndoTree::new(text.clone());
Self {
text,
dirty_gen: 0,
folds: Vec::new(),
fold_gen: 0,
cached_joined: None,
cached_byte_len: None,
undo,
undo_group_depth: 0,
undo_group_armed: false,
undo_group_open_gen: 0,
content_dirty: false,
cached_editor_content: None,
pending_fold_ops: Vec::new(),
change_log: Vec::new(),
pending_content_edits: Vec::new(),
pending_content_reset: false,
marks: std::collections::BTreeMap::new(),
syntax_fold_ranges: Vec::new(),
last_cursor: (0, 0),
}
}
pub fn undo_group_enter(&mut self) {
if self.undo_group_depth == 0 {
self.undo_group_armed = false;
self.undo_group_open_gen = self.dirty_gen;
}
self.undo_group_depth = self.undo_group_depth.saturating_add(1);
}
pub fn undo_group_exit(&mut self) {
if self.undo_group_depth == 0 {
return;
}
self.undo_group_depth -= 1;
if self.undo_group_depth == 0 {
if self.undo_group_armed && self.dirty_gen == self.undo_group_open_gen {
self.undo.pop_committed();
}
self.undo_group_armed = false;
}
}
pub fn undo_group_active(&self) -> bool {
self.undo_group_depth > 0
}
pub fn undo_group_arm(&mut self) -> bool {
if self.undo_group_armed {
false
} else {
self.undo_group_armed = true;
true
}
}
}