use super::{DocumentChange, IncrementalParser};
use crate::core::errors::EditorError;
use crate::core::Result;
use ass_core::parser::Script;
#[cfg(not(feature = "std"))]
use alloc::{string::ToString, vec::Vec};
impl IncrementalParser {
pub fn new() -> Self {
Self {
cached_script: None,
pending_changes: Vec::new(),
next_change_id: 1,
reparse_threshold: 10_000, bytes_changed: 0,
}
}
pub fn set_reparse_threshold(&mut self, threshold: usize) {
self.reparse_threshold = threshold;
}
pub fn initialize_cache(&mut self, content: &str) {
self.cached_script = Some(content.to_string());
self.pending_changes.clear();
self.bytes_changed = 0;
}
pub fn has_cached_script(&self) -> bool {
self.cached_script.is_some()
}
pub fn with_cached_script<F, R>(&self, f: F) -> Result<R>
where
F: FnOnce(&Script) -> Result<R>,
{
let cached = self
.cached_script
.as_ref()
.ok_or_else(|| EditorError::command_failed("No cached script available"))?;
let script = Script::parse(cached).map_err(EditorError::from)?;
f(&script)
}
pub fn clear_cache(&mut self) {
self.cached_script = None;
self.pending_changes.clear();
self.bytes_changed = 0;
self.next_change_id = 1;
}
pub fn pending_changes(&self) -> &[DocumentChange<'static>] {
&self.pending_changes
}
pub fn should_reparse(&self) -> bool {
self.bytes_changed >= self.reparse_threshold || self.pending_changes.len() > 50
}
}