use canvist_model::BlockId;
use canvist_model::Document;
use canvist_model::Point;
use crate::HeadlessError;
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Position {
pub path: Vec<u32>,
pub offset: u32,
}
impl Position {
#[must_use]
pub fn new(path: impl Into<Vec<u32>>, offset: u32) -> Self {
Self {
path: path.into(),
offset,
}
}
}
pub(crate) fn resolve(doc: &Document, position: &Position) -> Result<Point, HeadlessError> {
let block = block_at(doc, &position.path)?;
let text = doc.inline(block).ok_or_else(|| {
HeadlessError::InvalidPosition(format!("{:?} is not a text block", position.path))
})?;
let offset = utf16_to_utf8(text.as_str(), position.offset).ok_or_else(|| {
HeadlessError::InvalidPosition(format!(
"offset {} does not fit the text of {:?}",
position.offset, position.path
))
})?;
Ok(Point::new(block, offset))
}
pub(crate) fn locate(doc: &Document, point: Point) -> Option<Position> {
let text = doc.inline(point.block)?;
let before = text.as_str().get(..point.offset as usize)?;
Some(Position {
path: path_of(doc, point.block)?,
offset: before.encode_utf16().count() as u32,
})
}
fn block_at(doc: &Document, path: &[u32]) -> Result<BlockId, HeadlessError> {
let missing = || HeadlessError::InvalidPosition(format!("no block at {path:?}"));
let mut parent = None;
for &index in path {
let child = doc
.children(parent)
.get(index as usize)
.ok_or_else(missing)?;
parent = Some(*child);
}
parent.ok_or_else(missing)
}
fn path_of(doc: &Document, block: BlockId) -> Option<Vec<u32>> {
let mut path = Vec::new();
let mut current = Some(block);
while let Some(id) = current {
path.push(doc.index_in_parent(id)? as u32);
current = doc.block(id)?.parent();
}
path.reverse();
Some(path)
}
fn utf16_to_utf8(text: &str, units: u32) -> Option<u32> {
let mut counted = 0;
for (byte, character) in text.char_indices() {
if counted == units {
return Some(byte as u32);
}
counted += character.len_utf16() as u32;
}
(counted == units).then_some(text.len() as u32)
}
#[cfg(test)]
mod tests {
use super::utf16_to_utf8;
#[test]
fn converts_utf16_offsets_to_utf8() {
let text = "a😀é";
assert_eq!(utf16_to_utf8(text, 0), Some(0));
assert_eq!(utf16_to_utf8(text, 1), Some(1));
assert_eq!(utf16_to_utf8(text, 2), None, "inside the surrogate pair");
assert_eq!(utf16_to_utf8(text, 3), Some(5));
assert_eq!(utf16_to_utf8(text, 4), Some(7));
assert_eq!(utf16_to_utf8(text, 5), None, "past the end");
}
}