use canvist_model::BlockId;
use rustc_hash::FxHashMap;
use rustc_hash::FxHashSet;
use yrs::BranchID;
use yrs::ID;
use yrs::branch::Branch;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct BlockRef {
pub(crate) map: ID,
pub(crate) parent: Option<BlockId>,
pub(crate) text: Option<ID>,
pub(crate) attrs: Option<ID>,
pub(crate) children: Option<ID>,
}
impl BlockRef {
fn branches(self) -> impl Iterator<Item = ID> {
[Some(self.map), self.text, self.attrs, self.children]
.into_iter()
.flatten()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Owner {
Block(BlockId),
Text(BlockId),
Attrs(BlockId),
Children(BlockId),
Hidden(Option<BlockId>),
}
#[derive(Debug, Default)]
pub(crate) struct Registration {
pub(crate) blocks: Vec<(BlockId, BlockRef)>,
pub(crate) hidden: Vec<(ID, Option<BlockId>)>,
pub(crate) unmirrored: Vec<(Option<BlockId>, u32)>,
pub(crate) losers: Vec<(ID, Option<BlockId>)>,
}
#[derive(Debug, Default)]
pub(crate) struct Registry {
blocks: FxHashMap<BlockId, BlockRef>,
owners: FxHashMap<ID, Owner>,
unmirrored: FxHashMap<Option<BlockId>, u32>,
}
impl Registry {
pub(crate) fn block(&self, id: BlockId) -> Option<BlockRef> {
self.blocks.get(&id).copied()
}
pub(crate) fn owner(&self, branch: &ID) -> Option<Owner> {
self.owners.get(branch).copied()
}
pub(crate) fn block_of_map(&self, map: &ID) -> Option<BlockId> {
match self.owner(map)? {
Owner::Block(id) => Some(id),
_ => None,
}
}
pub(crate) fn is_mirrored(&self, parent: Option<BlockId>) -> bool {
self.unmirrored.get(&parent).is_none_or(|&count| count == 0)
}
pub(crate) fn register(&mut self, registration: Registration) {
for (id, block) in registration.blocks {
self.insert(id, block);
}
for (map, parent) in registration.hidden {
self.owners.insert(map, Owner::Hidden(parent));
}
for (parent, count) in registration.unmirrored {
*self.unmirrored.entry(parent).or_default() += count;
}
}
pub(crate) fn insert(&mut self, id: BlockId, block: BlockRef) {
self.owners.insert(block.map, Owner::Block(id));
block
.text
.map(|text| self.owners.insert(text, Owner::Text(id)));
block
.attrs
.map(|attrs| self.owners.insert(attrs, Owner::Attrs(id)));
block
.children
.map(|children| self.owners.insert(children, Owner::Children(id)));
self.blocks.insert(id, block);
}
pub(crate) fn remove(&mut self, ids: &FxHashSet<BlockId>) {
for &id in ids {
self.forget_block(id);
}
let had_hidden = ids
.iter()
.filter_map(|id| self.unmirrored.remove(&Some(*id)))
.any(|count| count > 0);
if had_hidden {
self.owners.retain(
|_, owner| !matches!(owner, Owner::Hidden(Some(parent)) if ids.contains(parent)),
);
}
}
pub(crate) fn remove_hidden(&mut self, map: &ID) {
if let Some(Owner::Hidden(parent)) = self.owners.remove(map) {
self.adjust_unmirrored(parent, -1);
}
}
pub(crate) fn forget_hidden(&mut self, map: &ID) {
if matches!(self.owners.get(map), Some(Owner::Hidden(_))) {
self.owners.remove(map);
}
}
pub(crate) fn insert_hidden(&mut self, map: ID, parent: Option<BlockId>) {
self.owners.insert(map, Owner::Hidden(parent));
self.adjust_unmirrored(parent, 1);
}
pub(crate) fn adjust_unmirrored(&mut self, parent: Option<BlockId>, delta: i32) {
let count = self.unmirrored.entry(parent).or_default();
*count = count.saturating_add_signed(delta);
}
pub(crate) fn set_unmirrored(&mut self, parent: Option<BlockId>, count: u32) {
self.unmirrored.insert(parent, count);
}
pub(crate) fn unmirrored_total(&self) -> u32 {
self.unmirrored.values().sum()
}
pub(crate) fn hidden_in(&self, parent: Option<BlockId>) -> Vec<ID> {
if self.is_mirrored(parent) {
return Vec::new();
}
self.owners
.iter()
.filter(|(_, owner)| **owner == Owner::Hidden(parent))
.map(|(map, _)| *map)
.collect()
}
pub(crate) fn set_attrs(&mut self, id: BlockId, attrs: Option<ID>) {
let Some(block) = self.blocks.get(&id).copied() else {
return;
};
if let Some(old) = block.attrs {
self.owners.remove(&old);
}
self.insert(id, BlockRef { attrs, ..block });
}
fn forget_block(&mut self, id: BlockId) {
let Some(block) = self.blocks.remove(&id) else {
return;
};
for branch in block.branches() {
self.owners.remove(&branch);
}
}
pub(crate) fn rebind(&mut self, copies: Vec<(ID, BlockRef)>) {
for (source, copy) in copies {
match self.owners.remove(&source) {
Some(Owner::Block(id)) => {
self.forget_block(id);
self.insert(id, copy);
}
Some(Owner::Hidden(parent)) => {
self.owners.insert(copy.map, Owner::Hidden(parent));
}
Some(owner) => {
self.owners.insert(source, owner);
}
None => {}
}
}
}
}
pub(crate) fn branch_id(branch: &impl AsRef<Branch>) -> Option<ID> {
match branch.as_ref().id() {
BranchID::Nested(id) => Some(id),
BranchID::Root(_) => None,
}
}