use std::collections::VecDeque;
use ropey::Rope;
use crate::change::{ChangeSet, Edit, EditEvent};
use crate::decorations::Decorations;
use crate::history::History;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LineEnding {
#[default]
Lf,
CrLf,
Cr,
}
impl LineEnding {
pub fn as_str(self) -> &'static str {
match self {
LineEnding::Lf => "\n",
LineEnding::CrLf => "\r\n",
LineEnding::Cr => "\r",
}
}
pub fn detect(rope: &Rope) -> Self {
let mut chars = rope.chars();
while let Some(c) = chars.next() {
match c {
'\n' => return LineEnding::Lf,
'\r' => return if chars.next() == Some('\n') { LineEnding::CrLf } else { LineEnding::Cr },
_ => {}
}
}
LineEnding::Lf
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Options {
pub tab_size: usize,
pub insert_spaces: bool,
}
impl Default for Options {
fn default() -> Self {
Self { tab_size: 4, insert_spaces: true }
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LanguageEditing {
pub line_comment: Option<String>,
pub block_comment: Option<(String, String)>,
pub auto_closing: Vec<(String, String)>,
pub brackets: Vec<(String, String)>,
pub indent_after: Vec<String>,
}
const LOG_CAPACITY: usize = 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Stale;
#[derive(Clone, Debug)]
struct LogEntry {
changes: ChangeSet,
events: Vec<EditEvent>,
}
#[derive(Clone, Debug)]
pub struct TextModel {
pub(crate) rope: Rope,
pub(crate) history: History,
pub(crate) line_ending: LineEnding,
pub options: Options,
pub editing: LanguageEditing,
pub decorations: Decorations,
version: u64,
log: VecDeque<LogEntry>,
log_base: u64,
}
impl TextModel {
pub fn new(text: &str) -> Self {
let rope = Rope::from_str(text);
Self {
line_ending: LineEnding::detect(&rope),
rope,
history: History::default(),
options: Options::default(),
editing: LanguageEditing::default(),
decorations: Decorations::default(),
version: 0,
log: VecDeque::new(),
log_base: 0,
}
}
pub fn rope(&self) -> &Rope {
&self.rope
}
pub fn text(&self) -> String {
self.rope.to_string()
}
pub fn len_chars(&self) -> usize {
self.rope.len_chars()
}
pub fn version(&self) -> u64 {
self.version
}
pub fn line_ending(&self) -> LineEnding {
self.line_ending
}
pub fn can_undo(&self) -> bool {
self.history.can_undo()
}
pub fn can_redo(&self) -> bool {
self.history.can_redo()
}
pub fn checkpoint(&mut self) {
self.history.break_group();
}
pub fn set_text(&mut self, text: &str) {
let all = ChangeSet::new(vec![Edit::new(0, self.rope.len_chars(), text)]);
self.apply(&all);
self.line_ending = LineEnding::detect(&self.rope);
self.history = History::default();
}
pub fn apply_changes(&mut self, changes: &ChangeSet) {
self.apply(changes);
}
pub fn changes_since(&self, version: u64) -> Result<impl Iterator<Item = &ChangeSet>, Stale> {
Ok(self.log_since(version)?.map(|e| &e.changes))
}
pub fn events_since(&self, version: u64) -> Result<impl Iterator<Item = &EditEvent>, Stale> {
Ok(self.log_since(version)?.flat_map(|e| e.events.iter()))
}
fn log_since(&self, version: u64) -> Result<std::collections::vec_deque::Iter<'_, LogEntry>, Stale> {
if version < self.log_base || version > self.version {
return Err(Stale);
}
Ok(self.log.range((version - self.log_base) as usize..))
}
pub(crate) fn apply(&mut self, changes: &ChangeSet) -> ChangeSet {
if changes.needs_crlf_snap(&self.rope) {
let snapped = changes.clone().snap_crlf(&self.rope);
return self.apply(&snapped);
}
let events = changes.describe(&self.rope);
let inverse = changes.apply(&mut self.rope);
self.decorations.apply(changes);
self.version += 1;
self.log.push_back(LogEntry { changes: changes.clone(), events });
if self.log.len() > LOG_CAPACITY {
self.log.pop_front();
self.log_base += 1;
}
inverse
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn log_serves_consumers_by_version() {
let mut m = TextModel::new("abc");
m.apply(&ChangeSet::new(vec![Edit::insert(0, "x")]));
m.apply(&ChangeSet::new(vec![Edit::insert(4, "y")]));
assert_eq!(m.text(), "xabcy");
assert_eq!(m.changes_since(0).unwrap().count(), 2);
assert_eq!(m.events_since(1).unwrap().map(|e| e.text.as_str()).collect::<Vec<_>>(), ["y"]);
assert_eq!(m.changes_since(2).unwrap().count(), 0);
assert!(m.changes_since(3).is_err());
}
#[test]
fn log_drops_old_entries() {
let mut m = TextModel::new("");
for _ in 0..LOG_CAPACITY + 5 {
m.apply(&ChangeSet::new(vec![Edit::insert(0, "a")]));
}
assert!(m.changes_since(0).is_err());
assert_eq!(m.changes_since(5).unwrap().count(), LOG_CAPACITY);
}
}