use std::cell::{Cell, OnceCell};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use parking_lot::Mutex;
use frontend::commands::{block_commands, document_commands, frame_commands, list_commands};
use frontend::common::format_runs::{FormatRun, ImageAnchor, synth_element_id};
use frontend::common::types::EntityId;
use crate::convert::to_usize;
use crate::flow::{
AddressablePiece, BlockSnapshot, FragmentContent, ListInfo, TableCellContext, TableCellRef,
};
use crate::inner::TextDocumentInner;
use crate::text_frame::TextFrame;
use crate::text_list::TextList;
use crate::text_table::TextTable;
use crate::{BlockFormat, ListStyle, TextFormat};
#[derive(Clone)]
pub struct TextBlock {
pub(crate) doc: Arc<Mutex<TextDocumentInner>>,
pub(crate) block_id: usize,
}
impl TextBlock {
pub fn text(&self) -> String {
let inner = self.doc.lock();
let store = inner.ctx.db_context.get_store();
block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
.map(|b| {
let entity: common::entities::Block = b.into();
common::database::rope_helpers::block_content_via_store(&entity, store)
})
.unwrap_or_default()
}
pub fn length(&self) -> usize {
let inner = self.doc.lock();
let store = inner.ctx.db_context.get_store();
block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
.map(|b| {
let entity: common::entities::Block = b.into();
to_usize(common::database::rope_helpers::block_char_length(
&entity, store,
))
})
.unwrap_or(0)
}
pub fn is_empty(&self) -> bool {
let inner = self.doc.lock();
let store = inner.ctx.db_context.get_store();
block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
.map(|b| {
let entity: common::entities::Block = b.into();
common::database::rope_helpers::block_char_length(&entity, store) == 0
})
.unwrap_or(true)
}
pub fn is_valid(&self) -> bool {
let inner = self.doc.lock();
block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
.is_some()
}
pub fn id(&self) -> usize {
self.block_id
}
pub fn position(&self) -> usize {
let inner = self.doc.lock();
let Some(mut dto) = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
else {
return 0;
};
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_position(&mut dto, store);
to_usize(dto.document_position)
}
pub fn block_number(&self) -> usize {
let inner = self.doc.lock();
compute_block_number(&inner, self.block_id as u64)
}
pub fn next(&self) -> Option<TextBlock> {
let inner = self.doc.lock();
let all_blocks = block_commands::get_all_block(&inner.ctx).ok()?;
let mut sorted: Vec<_> = all_blocks.into_iter().collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut sorted, store);
sorted.sort_by_key(|b| b.document_position);
let idx = sorted.iter().position(|b| b.id == self.block_id as u64)?;
sorted.get(idx + 1).map(|b| TextBlock {
doc: Arc::clone(&self.doc),
block_id: b.id as usize,
})
}
pub fn previous(&self) -> Option<TextBlock> {
let inner = self.doc.lock();
let all_blocks = block_commands::get_all_block(&inner.ctx).ok()?;
let mut sorted: Vec<_> = all_blocks.into_iter().collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut sorted, store);
sorted.sort_by_key(|b| b.document_position);
let idx = sorted.iter().position(|b| b.id == self.block_id as u64)?;
if idx == 0 {
return None;
}
sorted.get(idx - 1).map(|b| TextBlock {
doc: Arc::clone(&self.doc),
block_id: b.id as usize,
})
}
pub fn frame(&self) -> TextFrame {
let inner = self.doc.lock();
let frame_id = find_parent_frame(&inner, self.block_id as u64);
TextFrame {
doc: Arc::clone(&self.doc),
frame_id: frame_id.map(|id| id as usize).unwrap_or(0),
}
}
pub fn table_cell(&self) -> Option<TableCellRef> {
let inner = self.doc.lock();
let frame_id = find_parent_frame(&inner, self.block_id as u64)?;
let frame_dto = frame_commands::get_frame(&inner.ctx, &frame_id)
.ok()
.flatten()?;
if let Some(table_entity_id) = frame_dto.table {
let table_dto =
frontend::commands::table_commands::get_table(&inner.ctx, &{ table_entity_id })
.ok()
.flatten()?;
for &cell_id in &table_dto.cells {
if let Some(cell_dto) =
frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{
cell_id
})
.ok()
.flatten()
&& cell_dto.cell_frame == Some(frame_id)
{
return Some(TableCellRef {
table: TextTable {
doc: Arc::clone(&self.doc),
table_id: table_entity_id as usize,
},
row: to_usize(cell_dto.row),
column: to_usize(cell_dto.column),
});
}
}
}
let all_tables =
frontend::commands::table_commands::get_all_table(&inner.ctx).unwrap_or_default();
for table_dto in &all_tables {
for &cell_id in &table_dto.cells {
if let Some(cell_dto) =
frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{
cell_id
})
.ok()
.flatten()
&& cell_dto.cell_frame == Some(frame_id)
{
return Some(TableCellRef {
table: TextTable {
doc: Arc::clone(&self.doc),
table_id: table_dto.id as usize,
},
row: to_usize(cell_dto.row),
column: to_usize(cell_dto.column),
});
}
}
}
None
}
pub fn block_format(&self) -> BlockFormat {
let inner = self.doc.lock();
block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
.map(|b| BlockFormat::from(&b))
.unwrap_or_default()
}
pub fn char_format_at(&self, offset: usize) -> Option<TextFormat> {
let inner = self.doc.lock();
let fragments = build_fragments(&inner, self.block_id as u64);
for frag in &fragments {
match frag {
FragmentContent::Text {
format,
offset: frag_offset,
length,
..
} => {
if offset >= *frag_offset && offset < frag_offset + length {
return Some(format.clone());
}
}
FragmentContent::Image {
format,
offset: frag_offset,
..
}
| FragmentContent::FootnoteReference {
format,
offset: frag_offset,
..
} => {
if offset == *frag_offset {
return Some(format.clone());
}
}
}
}
None
}
pub fn fragments(&self) -> Vec<FragmentContent> {
let inner = self.doc.lock();
build_fragments(&inner, self.block_id as u64)
}
pub fn display_fragments(&self) -> Vec<FragmentContent> {
let inner = self.doc.lock();
let fragments = build_raw_fragments(
&inner,
self.block_id as u64,
None,
&SnapshotLookups::default(),
);
let spans = crate::highlight::merged_spans_for_block(
&inner,
self.block_id,
&crate::highlight::HighlightMask::ALL,
);
if !spans.is_empty() {
return crate::highlight::merge_highlight_spans(fragments, &spans);
}
fragments
}
pub fn addressable_inline_pieces(&self) -> Vec<AddressablePiece> {
let inner = self.doc.lock();
let Some(block_dto) = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()
else {
return Vec::new();
};
let entity: common::entities::Block = block_dto.into();
let store = inner.ctx.db_context.get_store();
let plain_text = common::database::rope_helpers::block_content_via_store(&entity, store);
common::format_runs_query::addressable_inline_pieces_for_block(store, &entity, &plain_text)
.into_iter()
.map(|p| AddressablePiece {
start: to_usize(p.start as i64),
end: to_usize(p.end as i64),
content: p.content,
format: TextFormat::from(&p.format),
})
.collect()
}
pub fn list(&self) -> Option<TextList> {
let inner = self.doc.lock();
let block_dto = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()?;
let list_id = block_dto.list?;
Some(TextList {
doc: Arc::clone(&self.doc),
list_id: list_id as usize,
})
}
pub fn list_item_index(&self) -> Option<usize> {
let inner = self.doc.lock();
let block_dto = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
.ok()
.flatten()?;
let list_id = block_dto.list?;
Some(compute_list_item_index(
&inner,
list_id,
self.block_id as u64,
))
}
pub fn snapshot(&self) -> BlockSnapshot {
let inner = self.doc.lock();
build_block_snapshot(
&inner,
self.block_id as u64,
crate::highlight::SnapshotHighlights {
kind: inner.highlight_kind,
mask: &crate::highlight::HighlightMask::ALL,
suppress_paint: false,
},
)
.unwrap_or_else(|| BlockSnapshot {
block_id: self.block_id,
position: 0,
length: 0,
text: String::new(),
fragments: Vec::new(),
block_format: BlockFormat::default(),
list_info: None,
parent_frame_id: None,
table_cell: None,
paint_highlights: Vec::new(),
})
}
}
pub(crate) fn find_parent_frame(inner: &TextDocumentInner, block_id: u64) -> Option<EntityId> {
whole_document_read();
let all_frames = frame_commands::get_all_frame(&inner.ctx).ok()?;
let block_entity_id = block_id as EntityId;
for frame in &all_frames {
if frame.blocks.contains(&block_entity_id) {
return Some(frame.id as EntityId);
}
}
None
}
fn document_has_no_tables(inner: &TextDocumentInner) -> bool {
inner.ctx.db_context.get_store().tables.read().is_empty()
}
fn find_table_cell_context(inner: &TextDocumentInner, block_id: u64) -> Option<TableCellContext> {
if document_has_no_tables(inner) {
return None;
}
whole_document_read();
let frame_id = find_parent_frame(inner, block_id)?;
let frame_dto = frame_commands::get_frame(&inner.ctx, &frame_id)
.ok()
.flatten()?;
if let Some(table_entity_id) = frame_dto.table {
let table_dto =
frontend::commands::table_commands::get_table(&inner.ctx, &{ table_entity_id })
.ok()
.flatten()?;
for &cell_id in &table_dto.cells {
if let Some(cell_dto) =
frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{ cell_id })
.ok()
.flatten()
&& cell_dto.cell_frame == Some(frame_id)
{
return Some(TableCellContext {
table_id: table_entity_id as usize,
row: to_usize(cell_dto.row),
column: to_usize(cell_dto.column),
});
}
}
}
let all_tables =
frontend::commands::table_commands::get_all_table(&inner.ctx).unwrap_or_default();
for table_dto in &all_tables {
for &cell_id in &table_dto.cells {
if let Some(cell_dto) =
frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{ cell_id })
.ok()
.flatten()
&& cell_dto.cell_frame == Some(frame_id)
{
return Some(TableCellContext {
table_id: table_dto.id as usize,
row: to_usize(cell_dto.row),
column: to_usize(cell_dto.column),
});
}
}
}
None
}
#[derive(Default)]
pub(crate) struct SnapshotLookups {
frames: OnSecondAsk<FrameLookup>,
cells: OnSecondAsk<CellLookup>,
list_item_indices: OnSecondAsk<HashMap<(EntityId, EntityId), usize>>,
default_language: OnceCell<Option<String>>,
footnote_markers: OnceCell<HashMap<String, String>>,
self_footnote_numbers: OnceCell<HashMap<String, usize>>,
}
struct OnSecondAsk<T> {
asked: Cell<bool>,
index: OnceCell<T>,
}
impl<T> Default for OnSecondAsk<T> {
fn default() -> Self {
Self {
asked: Cell::new(false),
index: OnceCell::new(),
}
}
}
impl<T> OnSecondAsk<T> {
fn get(&self, build: impl FnOnce() -> T) -> Option<&T> {
if let Some(index) = self.index.get() {
return Some(index);
}
if !self.asked.replace(true) {
return None;
}
Some(self.index.get_or_init(build))
}
fn force(&self, build: impl FnOnce() -> T) -> &T {
self.asked.set(true);
self.index.get_or_init(build)
}
}
fn whole_document_read() {
#[cfg(test)]
tests::WHOLE_DOCUMENT_READS.with(|reads| reads.set(reads.get() + 1));
}
struct FrameLookup {
parent_of_block: HashMap<EntityId, EntityId>,
table_of_frame: HashMap<EntityId, Option<EntityId>>,
}
impl FrameLookup {
fn build(inner: &TextDocumentInner) -> Self {
whole_document_read();
let all_frames = frame_commands::get_all_frame(&inner.ctx).unwrap_or_default();
let mut parent_of_block = HashMap::new();
let mut table_of_frame = HashMap::with_capacity(all_frames.len());
for frame in &all_frames {
for &block_id in &frame.blocks {
parent_of_block.entry(block_id).or_insert(frame.id);
}
table_of_frame.insert(frame.id, frame.table);
}
Self {
parent_of_block,
table_of_frame,
}
}
}
struct CellLookup {
tables: HashSet<EntityId>,
in_table: HashMap<(EntityId, EntityId), TableCellContext>,
anywhere: HashMap<EntityId, TableCellContext>,
}
impl CellLookup {
fn build(inner: &TextDocumentInner) -> Self {
whole_document_read();
let all_tables =
frontend::commands::table_commands::get_all_table(&inner.ctx).unwrap_or_default();
let mut tables = HashSet::with_capacity(all_tables.len());
let mut in_table = HashMap::new();
let mut anywhere = HashMap::new();
for table_dto in &all_tables {
tables.insert(table_dto.id);
for &cell_id in &table_dto.cells {
let Some(cell_dto) =
frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{
cell_id
})
.ok()
.flatten()
else {
continue;
};
let Some(frame_id) = cell_dto.cell_frame else {
continue;
};
let context = TableCellContext {
table_id: table_dto.id as usize,
row: to_usize(cell_dto.row),
column: to_usize(cell_dto.column),
};
in_table
.entry((table_dto.id, frame_id))
.or_insert_with(|| context.clone());
anywhere.entry(frame_id).or_insert(context);
}
}
Self {
tables,
in_table,
anywhere,
}
}
}
impl SnapshotLookups {
fn parent_frame(&self, inner: &TextDocumentInner, block_id: u64) -> Option<EntityId> {
match self.frames.get(|| FrameLookup::build(inner)) {
Some(frames) => frames.parent_of_block.get(&block_id).copied(),
None => find_parent_frame(inner, block_id),
}
}
fn table_cell_context(
&self,
inner: &TextDocumentInner,
block_id: u64,
) -> Option<TableCellContext> {
if document_has_no_tables(inner) {
return None;
}
let Some(cells) = self.cells.get(|| CellLookup::build(inner)) else {
return find_table_cell_context(inner, block_id);
};
let frames = self.frames.force(|| FrameLookup::build(inner));
let frame_id = *frames.parent_of_block.get(&block_id)?;
if let Some(Some(table_id)) = frames.table_of_frame.get(&frame_id) {
if !cells.tables.contains(table_id) {
return None;
}
if let Some(context) = cells.in_table.get(&(*table_id, frame_id)) {
return Some(context.clone());
}
}
cells.anywhere.get(&frame_id).cloned()
}
fn list_item_index(
&self,
inner: &TextDocumentInner,
list_id: EntityId,
block_id: u64,
) -> usize {
let build = || {
whole_document_read();
let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut all_blocks, store);
let mut members: HashMap<EntityId, Vec<(i64, EntityId)>> = HashMap::new();
for block in &all_blocks {
if let Some(list) = block.list {
members
.entry(list)
.or_default()
.push((block.document_position, block.id));
}
}
let mut indices = HashMap::new();
for (list, mut blocks) in members {
blocks.sort_by_key(|&(position, _)| position);
for (index, (_, id)) in blocks.into_iter().enumerate() {
indices.entry((list, id)).or_insert(index);
}
}
indices
};
match self.list_item_indices.get(build) {
Some(indices) => indices.get(&(list_id, block_id)).copied().unwrap_or(0),
None => compute_list_item_index(inner, list_id, block_id),
}
}
fn default_language(&self, inner: &TextDocumentInner) -> Option<String> {
self.default_language
.get_or_init(|| {
whole_document_read();
document_commands::get_document(&inner.ctx, &inner.document_id)
.ok()
.flatten()
.and_then(|d| d.default_language)
})
.clone()
}
fn footnote_markers(&self, inner: &TextDocumentInner) -> &HashMap<String, String> {
self.footnote_markers.get_or_init(|| {
whole_document_read();
inner
.ctx
.db_context
.get_store()
.footnote_markers
.read()
.clone()
})
}
fn self_footnote_numbers(&self, inner: &TextDocumentInner) -> &HashMap<String, usize> {
self.self_footnote_numbers
.get_or_init(|| document_self_footnote_numbers(inner.ctx.db_context.get_store()))
}
}
fn compute_block_number(inner: &TextDocumentInner, block_id: u64) -> usize {
let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut all_blocks, store);
let mut sorted: Vec<_> = all_blocks.iter().collect();
sorted.sort_by_key(|b| b.document_position);
sorted.iter().position(|b| b.id == block_id).unwrap_or(0)
}
pub(crate) fn build_fragments(inner: &TextDocumentInner, block_id: u64) -> Vec<FragmentContent> {
build_fragments_with_text(
inner,
block_id,
None,
crate::highlight::SnapshotHighlights {
kind: inner.highlight_kind,
mask: &crate::highlight::HighlightMask::ALL,
suppress_paint: false,
},
&SnapshotLookups::default(),
)
}
pub(crate) fn build_fragments_with_text(
inner: &TextDocumentInner,
block_id: u64,
prefetched_text: Option<&str>,
hl: crate::highlight::SnapshotHighlights,
lookups: &SnapshotLookups,
) -> Vec<FragmentContent> {
let fragments = build_raw_fragments(inner, block_id, prefetched_text, lookups);
if hl.kind == crate::highlight::HighlighterKind::Metric {
let spans = crate::highlight::merged_spans_for_block(inner, block_id as usize, hl.mask);
if !spans.is_empty() {
return crate::highlight::merge_highlight_spans(fragments, &spans);
}
}
fragments
}
fn document_self_footnote_numbers(
store: &common::database::Store,
) -> std::collections::HashMap<String, usize> {
whole_document_read();
let definition_blocks: std::collections::HashSet<common::types::EntityId> = store
.frames
.read()
.values()
.filter(|f| f.footnote_label.is_some())
.flat_map(|f| f.child_order.iter().copied())
.filter(|child| *child > 0)
.map(|child| child as common::types::EntityId)
.collect();
let mut ordered: Vec<(i64, common::types::EntityId)> = store
.blocks
.read()
.values()
.filter(|b| !definition_blocks.contains(&b.id))
.map(|b| (b.document_position, b.id))
.collect();
ordered.sort_unstable();
let refs = store.block_footnote_refs.read();
let mut numbers: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
let mut next = 1usize;
for (_, block_id) in ordered {
let Some(anchors) = refs.get(&block_id) else {
continue;
};
let mut in_block: Vec<_> = anchors.iter().collect();
in_block.sort_by_key(|a| a.byte_offset);
for anchor in in_block {
numbers.entry(anchor.label.clone()).or_insert_with(|| {
let n = next;
next += 1;
n
});
}
}
numbers
}
fn build_raw_fragments(
inner: &TextDocumentInner,
block_id: u64,
prefetched_text: Option<&str>,
lookups: &SnapshotLookups,
) -> Vec<FragmentContent> {
let _block_dto = match block_commands::get_block(&inner.ctx, &block_id)
.ok()
.flatten()
{
Some(b) => b,
None => return Vec::new(),
};
let plain_owned;
let plain: &str = match prefetched_text {
Some(t) => t,
None => {
let entity: common::entities::Block = _block_dto.clone().into();
plain_owned = common::database::rope_helpers::block_content_via_store(
&entity,
inner.ctx.db_context.get_store(),
);
&plain_owned
}
};
let (runs, images, notes) = {
let store = inner.ctx.db_context.get_store();
let runs: Vec<FormatRun> = store
.format_runs
.read()
.get(&block_id)
.cloned()
.unwrap_or_default();
let images: Vec<ImageAnchor> = store
.block_images
.read()
.get(&block_id)
.cloned()
.unwrap_or_default();
let notes = store
.block_footnote_refs
.read()
.get(&block_id)
.cloned()
.unwrap_or_default();
(runs, images, notes)
};
let anchors = frontend::common::format_runs::block_anchors(&images, ¬es);
let pieces = frontend::common::format_runs::merge_runs_and_anchors(plain, &runs, &anchors);
let mut fragments = Vec::with_capacity(pieces.len());
let mut char_offset: usize = 0;
for piece in pieces {
match piece {
frontend::common::format_runs::InlinePiece::Text { start, end, format } => {
let text = &plain[start as usize..end as usize];
let length = text.chars().count();
let word_starts = compute_word_starts(text);
fragments.push(FragmentContent::Text {
text: text.to_string(),
format: format.map(TextFormat::from).unwrap_or_default(),
offset: char_offset,
length,
element_id: synth_element_id(block_id, start),
word_starts,
});
char_offset += length;
}
frontend::common::format_runs::InlinePiece::FootnoteRef(note) => {
fragments.push(FragmentContent::FootnoteReference {
label: note.label.clone(),
marker: lookups
.footnote_markers(inner)
.get(¬e.label)
.cloned()
.unwrap_or_else(|| {
lookups
.self_footnote_numbers(inner)
.get(¬e.label)
.map(|n| n.to_string())
.unwrap_or_else(|| note.label.clone())
}),
format: TextFormat::from(¬e.format),
offset: char_offset,
element_id: synth_element_id(block_id, note.byte_offset),
});
char_offset += 1;
}
frontend::common::format_runs::InlinePiece::Image(img) => {
fragments.push(FragmentContent::Image {
name: img.name.clone(),
alt: img.alt.clone(),
width: img.width as u32,
height: img.height as u32,
quality: img.quality as u32,
format: TextFormat::from(&img.format),
offset: char_offset,
element_id: synth_element_id(block_id, img.byte_offset),
});
char_offset += 1;
}
}
}
fragments
}
fn compute_word_starts(text: &str) -> Vec<u8> {
use unicode_segmentation::UnicodeSegmentation;
let mut result = Vec::new();
let mut byte_to_char: Vec<(usize, usize)> = Vec::new();
for (ci, (bi, _)) in text.char_indices().enumerate() {
byte_to_char.push((bi, ci));
}
for (byte_off, _word) in text.unicode_word_indices() {
let char_idx = byte_to_char
.iter()
.find(|(bi, _)| *bi == byte_off)
.map(|(_, ci)| *ci)
.unwrap_or(0);
if let Ok(idx) = u8::try_from(char_idx) {
result.push(idx);
} else {
break;
}
}
result
}
fn compute_list_item_index(inner: &TextDocumentInner, list_id: EntityId, block_id: u64) -> usize {
whole_document_read();
let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut all_blocks, store);
let mut list_blocks: Vec<_> = all_blocks
.iter()
.filter(|b| b.list == Some(list_id))
.collect();
list_blocks.sort_by_key(|b| b.document_position);
list_blocks
.iter()
.position(|b| b.id == block_id)
.unwrap_or(0)
}
pub(crate) fn format_list_marker(
list_dto: &frontend::list::dtos::ListDto,
item_index: usize,
) -> String {
let number = item_index + 1; let marker_body = match list_dto.style {
ListStyle::Disc => "\u{2022}".to_string(), ListStyle::Circle => "\u{25E6}".to_string(), ListStyle::Square => "\u{25AA}".to_string(), ListStyle::Decimal => format!("{number}"),
ListStyle::LowerAlpha => {
if number <= 26 {
((b'a' + (number as u8 - 1)) as char).to_string()
} else {
format!("{number}")
}
}
ListStyle::UpperAlpha => {
if number <= 26 {
((b'A' + (number as u8 - 1)) as char).to_string()
} else {
format!("{number}")
}
}
ListStyle::LowerRoman => to_roman_lower(number),
ListStyle::UpperRoman => to_roman_upper(number),
};
format!("{}{marker_body}{}", list_dto.prefix, list_dto.suffix)
}
fn to_roman_upper(mut n: usize) -> String {
const VALUES: &[(usize, &str)] = &[
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I"),
];
let mut result = String::new();
for &(val, sym) in VALUES {
while n >= val {
result.push_str(sym);
n -= val;
}
}
result
}
fn to_roman_lower(n: usize) -> String {
to_roman_upper(n).to_lowercase()
}
fn build_list_info(
inner: &TextDocumentInner,
block_dto: &frontend::block::dtos::BlockDto,
lookups: &SnapshotLookups,
) -> Option<ListInfo> {
let list_id = block_dto.list?;
let list_dto = list_commands::get_list(&inner.ctx, &{ list_id })
.ok()
.flatten()?;
let item_index = lookups.list_item_index(inner, list_id, block_dto.id);
let marker = format_list_marker(&list_dto, item_index);
Some(ListInfo {
list_id: list_id as usize,
style: list_dto.style.clone(),
indent: list_dto.indent as u8,
marker,
item_index,
})
}
pub(crate) fn build_block_snapshot(
inner: &TextDocumentInner,
block_id: u64,
hl: crate::highlight::SnapshotHighlights,
) -> Option<BlockSnapshot> {
build_block_snapshot_with_position_and_parent(
inner,
block_id,
None,
None,
hl,
&SnapshotLookups::default(),
)
}
pub(crate) fn build_block_snapshot_with_position(
inner: &TextDocumentInner,
block_id: u64,
computed_position: Option<usize>,
hl: crate::highlight::SnapshotHighlights,
lookups: &SnapshotLookups,
) -> Option<BlockSnapshot> {
build_block_snapshot_with_position_and_parent(
inner,
block_id,
computed_position,
None,
hl,
lookups,
)
}
pub(crate) fn build_block_snapshot_with_position_and_parent(
inner: &TextDocumentInner,
block_id: u64,
computed_position: Option<usize>,
parent_frame_hint: Option<EntityId>,
hl: crate::highlight::SnapshotHighlights,
lookups: &SnapshotLookups,
) -> Option<BlockSnapshot> {
let mut block_dto = block_commands::get_block(&inner.ctx, &block_id)
.ok()
.flatten()?;
let store_for_pos = inner.ctx.db_context.get_store();
crate::inner::refresh_block_position(&mut block_dto, store_for_pos);
let mut block_format = BlockFormat::from(&block_dto);
if block_format.language.is_none() {
block_format.language = lookups.default_language(inner);
}
let list_info = build_list_info(inner, &block_dto, lookups);
let parent_frame_id = parent_frame_hint
.or_else(|| lookups.parent_frame(inner, block_id))
.map(|id| id as usize);
let table_cell = lookups.table_cell_context(inner, block_id);
let position = if common::database::rope_helpers::rope_positions_match_flow(store_for_pos) {
to_usize(block_dto.document_position)
} else {
computed_position.unwrap_or_else(|| to_usize(block_dto.document_position))
};
let entity: common::entities::Block = block_dto.clone().into();
let store = inner.ctx.db_context.get_store();
let text = common::database::rope_helpers::block_content_via_store(&entity, store);
let length = to_usize(common::database::rope_helpers::block_char_length(
&entity, store,
));
let fragments = build_fragments_with_text(inner, block_id, Some(&text), hl, lookups);
let paint_highlights =
if hl.kind == crate::highlight::HighlighterKind::PaintOnly && !hl.suppress_paint {
let spans = crate::highlight::merged_spans_for_block(inner, block_id as usize, hl.mask);
crate::highlight::extract_paint_spans(&spans, length)
} else {
Vec::new()
};
Some(BlockSnapshot {
block_id: block_id as usize,
position,
length,
text,
fragments,
block_format,
list_info,
parent_frame_id,
table_cell,
paint_highlights,
})
}
pub(crate) fn build_blocks_snapshot_for_frame(
inner: &TextDocumentInner,
frame_id: u64,
hl: crate::highlight::SnapshotHighlights,
lookups: &SnapshotLookups,
) -> Vec<BlockSnapshot> {
let frame_dto = match frame_commands::get_frame(&inner.ctx, &(frame_id as EntityId))
.ok()
.flatten()
{
Some(f) => f,
None => return Vec::new(),
};
let mut block_dtos: Vec<_> = frame_dto
.blocks
.iter()
.filter_map(|&id| {
block_commands::get_block(&inner.ctx, &{ id })
.ok()
.flatten()
})
.collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut block_dtos, store);
block_dtos.sort_by_key(|b| b.document_position);
block_dtos
.iter()
.filter_map(|b| {
build_block_snapshot_with_position_and_parent(inner, b.id, None, None, hl, lookups)
})
.collect()
}
pub(crate) fn build_blocks_snapshot_for_frame_with_positions(
inner: &TextDocumentInner,
frame_id: u64,
start_pos: usize,
hl: crate::highlight::SnapshotHighlights,
lookups: &SnapshotLookups,
) -> (Vec<BlockSnapshot>, usize) {
let frame_dto = match frame_commands::get_frame(&inner.ctx, &(frame_id as EntityId))
.ok()
.flatten()
{
Some(f) => f,
None => return (Vec::new(), start_pos),
};
let mut block_dtos: Vec<_> = frame_dto
.blocks
.iter()
.filter_map(|&id| {
block_commands::get_block(&inner.ctx, &{ id })
.ok()
.flatten()
})
.collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut block_dtos, store);
block_dtos.sort_by_key(|b| b.document_position);
let mut running_pos = start_pos;
let mut snapshots = Vec::with_capacity(block_dtos.len());
for b in &block_dtos {
if let Some(snap) =
build_block_snapshot_with_position(inner, b.id, Some(running_pos), hl, lookups)
{
running_pos += snap.length + 1; snapshots.push(snap);
}
}
(snapshots, running_pos)
}
#[cfg(test)]
mod tests {
use crate::TextDocument;
use crate::flow::FlowElementSnapshot;
use std::cell::Cell;
thread_local! {
pub(super) static WHOLE_DOCUMENT_READS: Cell<usize> = const { Cell::new(0) };
}
fn novel(paragraphs: usize) -> String {
let mut text = String::new();
for i in 0..paragraphs {
if i % 7 == 3 {
text.push_str(&format!("Paragraph {i}, with a note[^n{i}].\n\n"));
text.push_str(&format!("[^n{i}]: Note {i}.\n\n"));
} else if i % 17 == 5 {
text.push_str(&format!("> Quoted paragraph {i}.\n\n"));
} else {
text.push_str(&format!("Paragraph {i} of the text.\n\n"));
}
if i % 11 == 4 {
text.push_str(&format!("- item {i}\n- item {i}, second\n\n"));
}
if i % 13 == 6 {
text.push_str(&format!("| a{i} | b |\n| c | d |\n\n"));
}
}
text
}
fn all_blocks(elements: &[FlowElementSnapshot], out: &mut Vec<crate::flow::BlockSnapshot>) {
for element in elements {
match element {
FlowElementSnapshot::Block(block) => out.push(block.clone()),
FlowElementSnapshot::Table(table) => {
for cell in &table.cells {
out.extend(cell.blocks.iter().cloned());
}
}
FlowElementSnapshot::Frame(frame) => all_blocks(&frame.elements, out),
}
}
}
#[test]
fn a_flow_snapshot_agrees_with_a_snapshot_of_each_block_alone() {
let mixed = "# Title\n\nA note[^a] and another[^b].\n\n- one\n- two\n- three\n\n\
Between lists.\n\n1. first\n2. second\n\n> Quoted, with a note[^c].\n>\n\
> - quoted item\n> - quoted item two\n>\n> > nested quote\n\n\
| h1 | h2 |\n|----|----|\n| a | b[^a] |\n| c | d |\n\n\
- again one\n- again two\n\n> | q1 | q2 |\n> | q3 | q4 |\n\n\
Last paragraph[^d].\n\n[^a]: Note a, citing [^b].\n\n[^b]: Note b.\n\n\
[^c]: Note c.\n\n[^d]: Note d.\n";
let html = "<p>Before.</p><table><tr><td><p>c1 p1</p><p>c1 p2</p></td><td><ul>\
<li>in cell</li><li>in cell 2</li></ul></td></tr><tr><td>plain</td><td>\
<p>x</p><p>y</p></td></tr></table><ol><li>o1</li><li>o2</li></ol>\
<blockquote><p>q</p><ul><li>qi</li></ul></blockquote><p>After.</p>";
let from_djot = |text: &str| {
let doc = TextDocument::new();
doc.set_djot_sync(text).unwrap();
doc
};
let numbered_by_the_document = from_djot(mixed);
let partly_by_the_host = from_djot(mixed);
partly_by_the_host.set_footnote_markers(
[("a", "i"), ("c", "iii")]
.into_iter()
.map(|(label, marker)| (label.to_string(), marker.to_string()))
.collect(),
);
partly_by_the_host.set_default_language("fr").unwrap();
let with_cells_of_paragraphs = TextDocument::new();
with_cells_of_paragraphs
.set_html(html)
.unwrap()
.wait()
.unwrap();
let long = from_djot(&novel(120));
for (label, doc) in [
("notes numbered by the document", &numbered_by_the_document),
("notes partly numbered by the host", &partly_by_the_host),
("cells holding paragraphs", &with_cells_of_paragraphs),
("a long text", &long),
] {
let mut blocks = Vec::new();
all_blocks(&doc.snapshot_flow().elements, &mut blocks);
assert!(
blocks.iter().any(|b| b.table_cell.is_some())
&& blocks.iter().any(|b| b.list_info.is_some()),
"{label}: the text holds table cells and list items"
);
for from_flow in blocks {
let alone = doc
.block_by_id(from_flow.block_id)
.expect("a block of the flow exists")
.snapshot();
assert_eq!(
alone, from_flow,
"{label}: block {} alone and in the flow",
from_flow.block_id
);
}
}
}
fn reads_for_one_snapshot(doc: &TextDocument) -> (usize, crate::flow::FlowSnapshot) {
WHOLE_DOCUMENT_READS.with(|reads| reads.set(0));
let flow = doc.snapshot_flow();
(WHOLE_DOCUMENT_READS.with(Cell::get), flow)
}
#[test]
fn a_flow_snapshot_reads_each_whole_document_fact_a_fixed_number_of_times() {
let mut counts = Vec::new();
for paragraphs in [60, 480] {
let doc = TextDocument::new();
doc.set_djot_sync(&novel(paragraphs)).unwrap();
let (numbered_by_the_document, flow) = reads_for_one_snapshot(&doc);
let blocks: Vec<_> = flow
.elements
.iter()
.filter_map(|element| match element {
FlowElementSnapshot::Block(block) => Some(block),
_ => None,
})
.collect();
assert!(
blocks.iter().filter(|b| b.list_info.is_some()).count() > 4,
"the text holds list items"
);
assert!(
flow.elements
.iter()
.filter(|e| matches!(e, FlowElementSnapshot::Table(_)))
.count()
> 2,
"the text holds tables"
);
assert!(
flow.elements
.iter()
.any(|e| matches!(e, FlowElementSnapshot::Frame(_))),
"the text holds quotations"
);
let markers = (0..paragraphs)
.filter(|i| i % 7 == 3)
.enumerate()
.map(|(k, i)| (format!("n{i}"), (k + 1).to_string()))
.collect();
doc.set_footnote_markers(markers);
let (numbered_by_the_host, _) = reads_for_one_snapshot(&doc);
counts.push((paragraphs, numbered_by_the_document, numbered_by_the_host));
}
let (small, small_self, small_host) = counts[0];
let (large, large_self, large_host) = counts[1];
println!(
"whole-document reads per flow snapshot: {small_self} and {small_host} (notes \
numbered by the document, by the host) at {small} paragraphs, {large_self} and \
{large_host} at {large}"
);
assert_eq!(
large_self, small_self,
"a flow snapshot of {large} paragraphs read whole-document facts {large_self} \
times against {small_self} for {small}: one of them is being read again for \
each block that needs it"
);
assert_eq!(
large_host, small_host,
"with host footnote markers, a flow snapshot of {large} paragraphs read \
whole-document facts {large_host} times against {small_host} for {small}"
);
assert!(
small_self <= 12 && small_host <= 12,
"{small_self} and {small_host} whole-document reads for one snapshot"
);
}
}