use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Weak};
use parking_lot::Mutex;
pub(crate) type QueuedEvents = Vec<(crate::events::DocumentEvent, Vec<CallbackEntry>)>;
use crate::Result;
use frontend::AppContext;
use frontend::EventHubClient;
use frontend::commands::undo_redo_commands;
use frontend::common::types::EntityId;
use frontend::event_hub_client::SubscriptionToken;
use crate::DocumentEvent;
use crate::highlight::HighlightRegistry;
pub(crate) struct CursorData {
pub position: usize,
pub anchor: usize,
pub cell_selection_override: Option<crate::flow::CellRange>,
pub content_locale: Option<String>,
pub whole_text_selected: bool,
pub anchor_in_table: Option<usize>,
}
#[derive(Clone)]
pub(crate) struct CallbackEntry {
pub alive: Weak<AtomicBool>,
pub callback: Arc<dyn Fn(DocumentEvent) + Send + Sync>,
pub since: u64,
}
impl CallbackEntry {
fn is_live(&self) -> bool {
self.alive
.upgrade()
.is_some_and(|alive| alive.load(std::sync::atomic::Ordering::Relaxed))
}
}
pub(crate) struct TextDocumentInner {
pub ctx: AppContext,
pub event_client: EventHubClient,
pub stack_id: u64,
pub streaming_stack_id: Option<u64>,
#[allow(dead_code)] pub root_id: EntityId,
pub document_id: EntityId,
pub modified: bool,
pub content_revision: u64,
pub cursors: Vec<Weak<Mutex<CursorData>>>,
pub pending_events: Vec<DocumentEvent>,
pub callbacks: Vec<CallbackEntry>,
pub callback_cursor: usize,
pub poll_cursor: usize,
pub events_queued: u64,
pub event_holds: usize,
pub delivering_held_events: bool,
pub resource_cache: HashMap<String, u64>,
pub plain_text_cache: Option<String>,
pub last_block_count: usize,
pub last_child_order: Vec<i64>,
pub highlights: HighlightRegistry,
pub highlight_kind: crate::highlight::HighlighterKind,
pub backend: Option<crate::DocumentBackend>,
pub own_operations: std::collections::HashSet<String>,
pub history_resets: HashMap<String, Arc<AtomicBool>>,
pub long_op_subscriptions: Vec<SubscriptionToken>,
pub emptied_text: Option<EmptiedText>,
pub transient: Option<crate::transient::TransientLayer>,
pub reading_recorded: bool,
pub edit_carets: std::collections::BTreeMap<u64, EditCaret>,
pub commands_seen: u64,
pub edit_block_depth: usize,
pub edit_block_caret: Option<EditCaret>,
}
pub(crate) struct EditCaret {
cursor: Weak<Mutex<CursorData>>,
position: usize,
anchor: usize,
anchor_in_table: Option<usize>,
cell_selection_override: Option<crate::flow::CellRange>,
}
impl EditCaret {
fn of(cursor: &Arc<Mutex<CursorData>>) -> Self {
let data = cursor.lock();
EditCaret {
cursor: Arc::downgrade(cursor),
position: data.position,
anchor: data.anchor,
anchor_in_table: data.anchor_in_table,
cell_selection_override: data.cell_selection_override.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct EmptiedText {
block_id: EntityId,
block: common::entities::Block,
list: Option<common::entities::List>,
frames: Vec<common::entities::Frame>,
}
impl EmptiedText {
pub(crate) fn of(store: &common::database::Store) -> Option<Self> {
use common::entities::{Block, Frame, List};
let main_frame = store
.documents
.read()
.values()
.next()
.and_then(|document| document.frames.first().copied())?;
let frames = store.frames.read();
let mut sub_frames: Vec<Frame> = Vec::new();
let mut only_block: Option<EntityId> = None;
let mut pending: Vec<EntityId> = vec![main_frame];
let mut seen: std::collections::HashSet<EntityId> = std::collections::HashSet::new();
while let Some(frame_id) = pending.pop() {
if !seen.insert(frame_id) {
return None;
}
let frame = frames.get(&frame_id)?;
if frame.table.is_some() || frame.footnote_label.is_some() {
return None;
}
if frame_id != main_frame {
sub_frames.push(Frame {
id: 0,
created_at: Default::default(),
updated_at: Default::default(),
parent_frame: None,
blocks: Vec::new(),
child_order: Vec::new(),
byte_range: (0, 0),
..frame.clone()
});
}
for entry in frame.child_order.iter().rev() {
if *entry > 0 {
if only_block.is_some() {
return None;
}
only_block = Some(*entry as EntityId);
} else if *entry < 0 {
pending.push(entry.unsigned_abs() as EntityId);
}
}
}
drop(frames);
let block_id = only_block?;
let block = store.blocks.read().get(&block_id)?.clone();
if common::database::rope_helpers::block_char_length(&block, store) != 0 {
return None;
}
let list = block.list.and_then(|list_id| {
store.lists.read().get(&list_id).map(|list| List {
id: 0,
created_at: Default::default(),
updated_at: Default::default(),
..list.clone()
})
});
Some(EmptiedText {
block_id,
block: Block {
id: 0,
created_at: Default::default(),
updated_at: Default::default(),
document_position: 0,
list: None,
..block
},
list,
frames: sub_frames,
})
}
}
impl TextDocumentInner {
pub fn prune_dead_cursors(&mut self) {
self.cursors.retain(|w| w.strong_count() > 0);
}
pub fn adjust_cursors(&mut self, edit_pos: usize, removed: usize, added: usize) {
if let Some(layer) = self.transient.as_mut() {
layer.record(edit_pos, removed, added);
}
self.prune_dead_cursors();
for weak in &self.cursors {
if let Some(cursor) = weak.upgrade() {
let mut data = cursor.lock();
data.position = adjust_offset(data.position, edit_pos, removed, added);
data.anchor = adjust_offset(data.anchor, edit_pos, removed, added);
data.anchor_in_table = data
.anchor_in_table
.map(|origin| adjust_offset(origin, edit_pos, removed, added));
data.cell_selection_override = None;
data.whole_text_selected = false;
}
}
}
pub(crate) fn note_edit_caret(&mut self, cursor: &Arc<Mutex<CursorData>>) {
let recorded = undo_redo_commands::commands_recorded(&self.ctx, Some(self.stack_id));
if recorded == self.commands_seen {
return;
}
self.commands_seen = recorded;
let caret = EditCaret::of(cursor);
if self.edit_block_depth > 0 {
self.edit_block_caret = Some(caret);
} else if let Some(seq) = undo_redo_commands::head_seq(&self.ctx, Some(self.stack_id)) {
self.keep_edit_caret(seq, caret);
}
}
pub(crate) fn end_edit_block_caret(&mut self) {
self.edit_block_depth = self.edit_block_depth.saturating_sub(1);
if self.edit_block_depth == 0
&& let Some(caret) = self.edit_block_caret.take()
&& let Some(seq) = undo_redo_commands::head_seq(&self.ctx, Some(self.stack_id))
{
self.keep_edit_caret(seq, caret);
}
}
fn keep_edit_caret(&mut self, seq: u64, caret: EditCaret) {
self.edit_carets.insert(seq, caret);
let keep = 2 * undo_redo_commands::get_stack_size(&self.ctx, self.stack_id) + 64;
while self.edit_carets.len() > keep {
self.edit_carets.pop_first();
}
}
pub(crate) fn edit_caret_cursor(
&self,
seq: Option<u64>,
) -> Option<(Arc<Mutex<CursorData>>, usize)> {
let caret = self.edit_carets.get(&seq?)?;
Some((caret.cursor.upgrade()?, caret.position))
}
pub(crate) fn restore_edit_caret(&self, seq: Option<u64>, max_position: Option<usize>) {
let Some(caret) = seq.and_then(|seq| self.edit_carets.get(&seq)) else {
return;
};
let Some(cursor) = caret.cursor.upgrade() else {
return;
};
let clamp = |p: usize| max_position.map_or(p, |max| p.min(max));
let mut data = cursor.lock();
data.position = clamp(caret.position);
data.anchor = clamp(caret.anchor);
data.anchor_in_table = caret.anchor_in_table.map(clamp);
data.cell_selection_override = caret.cell_selection_override.clone();
data.whole_text_selected = false;
}
pub fn register_cursor(&mut self, position: usize) -> Arc<Mutex<CursorData>> {
self.prune_dead_cursors();
let data = Arc::new(Mutex::new(CursorData {
position,
anchor: position,
content_locale: None,
cell_selection_override: None,
whole_text_selected: false,
anchor_in_table: None,
}));
self.cursors.push(Arc::downgrade(&data));
data
}
pub fn queue_event(&mut self, event: DocumentEvent) {
match event {
DocumentEvent::ContentsChanged { .. } => {
self.content_revision = self.content_revision.wrapping_add(1);
}
DocumentEvent::UndoRedoChanged { .. } => {
self.end_whole_text_selections();
self.commands_seen =
undo_redo_commands::commands_recorded(&self.ctx, Some(self.stack_id));
}
DocumentEvent::DocumentReset => self.emptied_text = None,
_ => {}
}
self.pending_events.push(event);
self.events_queued += 1;
}
pub(crate) fn is_the_emptied_text(&self) -> bool {
self.emptied_text.as_ref().is_some_and(|emptied| {
EmptiedText::of(self.ctx.db_context.get_store()).as_ref() == Some(emptied)
})
}
pub(crate) fn end_whole_text_selections(&mut self) {
for weak in &self.cursors {
if let Some(cursor) = weak.upgrade() {
cursor.lock().whole_text_selected = false;
}
}
}
pub fn drain_poll_events(&mut self) -> Vec<DocumentEvent> {
if self.event_holds > 0 {
return Vec::new();
}
if self.poll_cursor >= self.pending_events.len() {
self.trim_delivered_events();
return Vec::new();
}
let events = self.pending_events[self.poll_cursor..].to_vec();
self.poll_cursor = self.pending_events.len();
self.trim_delivered_events();
events
}
pub fn take_queued_events(&mut self) -> QueuedEvents {
if self.event_holds > 0 || self.delivering_held_events {
return Vec::new();
}
self.take_events_now()
}
fn take_events_now(&mut self) -> QueuedEvents {
if self.callback_cursor >= self.pending_events.len() {
return Vec::new();
}
self.callbacks.retain(CallbackEntry::is_live);
let first = self.events_queued - self.pending_events.len() as u64;
let start = self.callback_cursor;
self.callback_cursor = self.pending_events.len();
if self.callbacks.is_empty() {
return Vec::new();
}
self.pending_events[start..]
.iter()
.zip(first + start as u64..)
.filter_map(|(event, seq)| {
let targets: Vec<CallbackEntry> = self
.callbacks
.iter()
.filter(|entry| entry.since <= seq)
.cloned()
.collect();
(!targets.is_empty()).then(|| (event.clone(), targets))
})
.collect()
}
fn trim_delivered_events(&mut self) {
let min_cursor = self.callback_cursor.min(self.poll_cursor);
if min_cursor > 0 {
self.pending_events.drain(..min_cursor);
self.callback_cursor -= min_cursor;
self.poll_cursor -= min_cursor;
}
}
pub fn invalidate_text_cache(&mut self) {
self.plain_text_cache = None;
}
pub fn check_block_count_changed(&mut self) {
let doc = frontend::commands::document_commands::get_document(&self.ctx, &self.document_id);
if let Ok(Some(doc)) = doc {
let new_count = doc.block_count as usize;
if new_count != self.last_block_count {
self.last_block_count = new_count;
self.queue_event(crate::DocumentEvent::BlockCountChanged(new_count));
}
}
}
pub fn check_flow_changed(&mut self) {
let current = self.main_frame_child_order();
if current == self.last_child_order {
return;
}
let old = &self.last_child_order;
let new = ¤t;
let prefix_len = old
.iter()
.zip(new.iter())
.take_while(|(a, b)| a == b)
.count();
let old_remaining = old.len() - prefix_len;
let new_remaining = new.len() - prefix_len;
let suffix_len = old[prefix_len..]
.iter()
.rev()
.zip(new[prefix_len..].iter().rev())
.take_while(|(a, b)| a == b)
.count();
let removed_count = old_remaining - suffix_len;
let inserted_count = new_remaining - suffix_len;
if removed_count > 0 {
self.queue_event(crate::DocumentEvent::FlowElementsRemoved {
flow_index: prefix_len,
count: removed_count,
});
}
if inserted_count > 0 {
self.queue_event(crate::DocumentEvent::FlowElementsInserted {
flow_index: prefix_len,
count: inserted_count,
});
}
self.last_child_order = current;
}
pub fn reset_cached_child_order(&mut self) {
self.last_child_order = self.main_frame_child_order();
}
fn main_frame_child_order(&self) -> Vec<i64> {
let frames = frontend::commands::document_commands::get_document_relationship(
&self.ctx,
&self.document_id,
&frontend::document::dtos::DocumentRelationshipField::Frames,
)
.unwrap_or_default();
let main_frame_id = match frames.first() {
Some(&id) => id,
None => return Vec::new(),
};
frontend::commands::frame_commands::get_frame(&self.ctx, &(main_frame_id as u64))
.ok()
.flatten()
.map(|f| f.child_order)
.unwrap_or_default()
}
pub fn plain_text(&mut self) -> Result<std::borrow::Cow<'_, str>> {
if !self.own_operations.is_empty() {
let dto = frontend::commands::document_io_commands::export_plain_text(&self.ctx)?;
return Ok(std::borrow::Cow::Owned(dto.plain_text));
}
let text = match self.plain_text_cache.take() {
Some(text) => text,
None => {
frontend::commands::document_io_commands::export_plain_text(&self.ctx)?.plain_text
}
};
Ok(std::borrow::Cow::Borrowed(
self.plain_text_cache.insert(text).as_str(),
))
}
pub fn initialize_in(backend: &crate::DocumentBackend) -> Result<Self> {
let ctx = AppContext::new_sharing(backend.shared_ctx());
let mut inner = Self::initialize_with_pump(ctx, false)?;
inner.backend = Some(backend.clone());
Ok(inner)
}
pub fn initialize(ctx: AppContext) -> Result<Self> {
Self::initialize_with_pump(ctx, true)
}
fn initialize_with_pump(ctx: AppContext, own_pump: bool) -> Result<Self> {
use frontend::block::dtos::CreateBlockDto;
use frontend::commands::{
block_commands, document_commands, frame_commands, root_commands, undo_redo_commands,
};
use frontend::document::dtos::CreateDocumentDto;
use frontend::frame::dtos::CreateFrameDto;
use frontend::root::dtos::CreateRootDto;
let event_client = EventHubClient::new(&ctx.event_hub);
if own_pump {
event_client.start(ctx.shutdown_rx.clone());
}
let stack_id = undo_redo_commands::create_new_stack(&ctx);
let root = root_commands::create_orphan_root(&ctx, &CreateRootDto::default())?;
let doc = document_commands::create_document(
&ctx,
Some(stack_id),
&CreateDocumentDto::default(),
root.id,
-1,
)?;
let frame = frame_commands::create_frame(
&ctx,
Some(stack_id),
&CreateFrameDto::default(),
doc.id,
-1,
)?;
let block = block_commands::create_block(
&ctx,
Some(stack_id),
&CreateBlockDto::default(),
frame.id,
-1,
)?;
common::database::rope_helpers::rope_append_empty_block(
ctx.db_context.get_store(),
block.id,
);
let mut frame_update: frontend::frame::dtos::UpdateFrameDto = frame.into();
frame_update.child_order = vec![block.id as i64];
frame_commands::update_frame(&ctx, Some(stack_id), &frame_update)?;
undo_redo_commands::clear_stack(&ctx, stack_id);
LIVE_DOCUMENTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let commands_seen = undo_redo_commands::commands_recorded(&ctx, Some(stack_id));
Ok(Self {
ctx,
event_client,
stack_id,
streaming_stack_id: None,
root_id: root.id,
document_id: doc.id,
modified: false,
content_revision: 0,
cursors: Vec::new(),
pending_events: Vec::new(),
callbacks: Vec::new(),
callback_cursor: 0,
poll_cursor: 0,
events_queued: 0,
event_holds: 0,
delivering_held_events: false,
resource_cache: HashMap::new(),
plain_text_cache: None,
last_block_count: 1, last_child_order: vec![block.id as i64],
highlights: HighlightRegistry::default(),
highlight_kind: crate::highlight::HighlighterKind::None,
long_op_subscriptions: Vec::new(),
backend: None,
own_operations: std::collections::HashSet::new(),
history_resets: HashMap::new(),
emptied_text: None,
transient: None,
reading_recorded: false,
edit_carets: std::collections::BTreeMap::new(),
commands_seen,
edit_block_depth: 0,
edit_block_caret: None,
})
}
}
impl TextDocumentInner {
pub fn owns_operation(&self, op_id: &str) -> bool {
self.backend.is_none() || self.own_operations.contains(op_id)
}
}
pub(crate) static LIVE_DOCUMENTS: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
impl Drop for TextDocumentInner {
fn drop(&mut self) {
LIVE_DOCUMENTS.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
if self.backend.is_none() {
self.ctx.shutdown();
}
}
}
pub(crate) fn dispatch_queued_events(queued: QueuedEvents) {
for (event, callbacks) in queued {
for entry in &callbacks {
if entry.is_live() {
(entry.callback)(event.clone());
}
}
}
}
pub(crate) fn release_event_hold(doc: &Arc<Mutex<TextDocumentInner>>) {
let mut inner = doc.lock();
inner.event_holds -= 1;
if inner.event_holds > 0 || inner.delivering_held_events {
return;
}
inner.delivering_held_events = true;
loop {
let queued = inner.take_events_now();
if queued.is_empty() {
inner.delivering_held_events = false;
return;
}
drop(inner);
let delivered = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
dispatch_queued_events(queued)
}));
inner = doc.lock();
if let Err(panic) = delivered {
inner.delivering_held_events = false;
drop(inner);
if !std::thread::panicking() {
std::panic::resume_unwind(panic);
}
return;
}
if inner.event_holds > 0 {
inner.delivering_held_events = false;
return;
}
}
}
pub(crate) fn document_counts(inner: &TextDocumentInner) -> Option<(usize, usize)> {
let doc = frontend::commands::document_commands::get_document(&inner.ctx, &inner.document_id)
.ok()
.flatten()?;
Some((
usize::try_from(doc.character_count).unwrap_or(0),
usize::try_from(doc.block_count).unwrap_or(0),
))
}
pub(crate) fn adjust_offset(offset: usize, edit_pos: usize, removed: usize, added: usize) -> usize {
if offset <= edit_pos {
offset
} else if offset <= edit_pos + removed {
edit_pos + added
} else {
offset - removed + added
}
}
pub(crate) fn refresh_block_positions(
dtos: &mut [frontend::block::dtos::BlockDto],
store: &common::database::Store,
) {
common::database::rope_helpers::refresh_positions_from_rope(
store,
dtos.iter_mut()
.map(|dto| (dto.id, &mut dto.document_position)),
);
}
pub(crate) fn block_at_caret_dto(
ctx: &AppContext,
position: usize,
) -> Result<frontend::document_inspection::BlockInfoDto> {
let at = |p: usize| {
frontend::commands::document_inspection_commands::get_block_at_position(
ctx,
&frontend::document_inspection::GetBlockAtPositionDto { position: p as i64 },
)
};
let store = ctx.db_context.get_store();
let past_anchor =
common::database::rope_helpers::snap_off_table_anchor(store, position as i64, true);
if past_anchor != position as i64 {
return Ok(at(past_anchor.max(0) as usize)?);
}
let info = at(position)?;
if position > 0 && (info.block_start as usize) > position {
return Ok(at(position - 1)?);
}
Ok(info)
}
pub(crate) fn refresh_block_position(
dto: &mut frontend::block::dtos::BlockDto,
store: &common::database::Store,
) {
refresh_block_positions(std::slice::from_mut(dto), store);
}