#[cfg(test)]
use std::ops::Range;
use std::sync::Arc;
use canvist_model::InlineText;
use canvist_model::Point;
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 insertion(&self, inline: &InlineText) -> Insertion {
let offset = self.at.offset;
let marks = inline
.marks_before(offset)
.or_else(|| inline.marks_after(offset))
.cloned()
.unwrap_or_default();
Insertion {
at: offset,
text: InlineText::plain(&self.text, marks),
adornment: false,
}
}
#[cfg(test)]
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))
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Adornment {
pub at: Point,
pub text: Arc<str>,
}
impl Adornment {
#[must_use]
pub fn new(at: Point, text: &str) -> Self {
Self {
at,
text: Arc::from(text),
}
}
}
pub(crate) struct Insertion {
pub(crate) at: u32,
pub(crate) text: InlineText,
pub(crate) adornment: bool,
}
pub(crate) fn insert_all(
inline: &InlineText,
mut insertions: Vec<Insertion>,
) -> Option<(InlineText, Vec<crate::text::Inserted>)> {
insertions.sort_by_key(|insertion| (insertion.at, !insertion.adornment));
let mut combined = inline.clone();
let mut inserted = Vec::with_capacity(insertions.len());
let mut shift = 0;
for insertion in insertions {
let start = insertion.at.checked_add(shift)?;
let len = insertion.text.len();
combined.insert(start, insertion.text).ok()?;
inserted.push(crate::text::Inserted {
at: insertion.at,
range: start..start + len,
adornment: insertion.adornment,
});
shift += len;
}
Some((combined, inserted))
}
impl DocumentLayout {
#[must_use]
pub fn adornments(&self) -> &[Adornment] {
&self.adornments
}
pub fn set_adornments(&mut self, adornments: Vec<Adornment>) -> bool {
if adornments == self.adornments {
return false;
}
let mut blocks: Vec<_> = self
.adornments
.iter()
.chain(&adornments)
.map(|adornment| adornment.at.block)
.collect();
blocks.sort_unstable();
blocks.dedup();
for block in blocks {
self.relayout(block);
}
self.adornments = adornments;
true
}
fn relayout(&mut self, block: canvist_model::BlockId) {
let Some(index) = self.row_index(block) else {
return;
};
let row = &mut self.flat.rows[index];
row.layout = None;
row.previous = None;
}
}
#[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() {
self.relayout(block);
}
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);
}
}