use std::ops::Range;
use std::sync::Arc;
use canvist_model::InlineText;
use canvist_model::Point;
#[cfg(feature = "provisional")]
use super::DocumentLayout;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Provisional {
pub at: Point,
pub text: Arc<str>,
}
impl Provisional {
#[must_use]
pub fn new(at: Point, text: &str) -> Self {
Self {
at,
text: Arc::from(text),
}
}
pub(crate) fn insert_into(&self, inline: &InlineText) -> Option<(InlineText, Range<u32>)> {
let offset = self.at.offset;
let marks = inline
.marks_before(offset)
.or_else(|| inline.marks_after(offset))
.cloned()
.unwrap_or_default();
let inserted = InlineText::plain(&self.text, marks);
let range = offset..offset + inserted.len();
let mut combined = inline.clone();
combined.insert(offset, inserted).ok()?;
Some((combined, range))
}
}
#[cfg(feature = "provisional")]
impl DocumentLayout {
#[must_use]
pub fn provisional(&self) -> Option<&Provisional> {
self.provisional.as_ref()
}
pub fn set_provisional(&mut self, provisional: Option<Provisional>) -> bool {
if provisional == self.provisional {
return false;
}
let blocks = [&self.provisional, &provisional]
.map(|shown| shown.as_ref().map(|shown| shown.at.block));
for block in blocks.into_iter().flatten() {
let Some(index) = self.row_index(block) else {
continue;
};
let row = &mut self.flat.rows[index];
row.layout = None;
row.previous = None;
}
self.provisional = provisional;
true
}
}
#[cfg(test)]
mod tests {
use canvist_model::BlockId;
use canvist_model::MarkSet;
use super::*;
#[test]
fn inserting_reports_the_bytes_the_text_takes() {
let inline = InlineText::plain("Hello", MarkSet::default());
let provisional = Provisional::new(Point::new(BlockId::new(1, 1), 5), " world");
let (combined, range) = provisional.insert_into(&inline).expect("inserts");
assert_eq!(combined.as_str(), "Hello world");
assert_eq!(range, 5..11);
}
#[test]
fn a_point_past_the_end_inserts_nothing() {
let inline = InlineText::plain("Hi", MarkSet::default());
let provisional = Provisional::new(Point::new(BlockId::new(1, 1), 9), "there");
assert_eq!(provisional.insert_into(&inline), None);
}
}