1use std::cell::{Cell, OnceCell};
4use std::collections::{HashMap, HashSet};
5use std::sync::Arc;
6
7use parking_lot::Mutex;
8
9use frontend::commands::{block_commands, document_commands, frame_commands, list_commands};
10use frontend::common::format_runs::{FormatRun, ImageAnchor, synth_element_id};
11use frontend::common::types::EntityId;
12
13use crate::convert::to_usize;
14use crate::flow::{
15 AddressablePiece, BlockSnapshot, FragmentContent, ListInfo, TableCellContext, TableCellRef,
16};
17use crate::inner::TextDocumentInner;
18use crate::text_frame::TextFrame;
19use crate::text_list::TextList;
20use crate::text_table::TextTable;
21use crate::{BlockFormat, ListStyle, TextFormat};
22
23#[derive(Clone)]
30pub struct TextBlock {
31 pub(crate) doc: Arc<Mutex<TextDocumentInner>>,
32 pub(crate) block_id: usize,
33}
34
35impl TextBlock {
36 pub fn text(&self) -> String {
40 let inner = self.doc.lock();
41 let store = inner.ctx.db_context.get_store();
42 block_commands::get_block(&inner.ctx, &(self.block_id as u64))
43 .ok()
44 .flatten()
45 .map(|b| {
46 let entity: common::entities::Block = b.into();
47 common::database::rope_helpers::block_content_via_store(&entity, store)
48 })
49 .unwrap_or_default()
50 }
51
52 pub fn length(&self) -> usize {
54 let inner = self.doc.lock();
55 let store = inner.ctx.db_context.get_store();
56 block_commands::get_block(&inner.ctx, &(self.block_id as u64))
57 .ok()
58 .flatten()
59 .map(|b| {
60 let entity: common::entities::Block = b.into();
61 to_usize(common::database::rope_helpers::block_char_length(
62 &entity, store,
63 ))
64 })
65 .unwrap_or(0)
66 }
67
68 pub fn is_empty(&self) -> bool {
70 let inner = self.doc.lock();
71 let store = inner.ctx.db_context.get_store();
72 block_commands::get_block(&inner.ctx, &(self.block_id as u64))
73 .ok()
74 .flatten()
75 .map(|b| {
76 let entity: common::entities::Block = b.into();
77 common::database::rope_helpers::block_char_length(&entity, store) == 0
78 })
79 .unwrap_or(true)
80 }
81
82 pub fn is_valid(&self) -> bool {
84 let inner = self.doc.lock();
85 block_commands::get_block(&inner.ctx, &(self.block_id as u64))
86 .ok()
87 .flatten()
88 .is_some()
89 }
90
91 pub fn id(&self) -> usize {
95 self.block_id
96 }
97
98 pub fn position(&self) -> usize {
102 let inner = self.doc.lock();
103 let Some(mut dto) = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
104 .ok()
105 .flatten()
106 else {
107 return 0;
108 };
109 let store = inner.ctx.db_context.get_store();
110 crate::inner::refresh_block_position(&mut dto, store);
111 to_usize(dto.document_position)
112 }
113
114 pub fn block_number(&self) -> usize {
118 let inner = self.doc.lock();
119 compute_block_number(&inner, self.block_id as u64)
120 }
121
122 pub fn next(&self) -> Option<TextBlock> {
125 let inner = self.doc.lock();
126 let all_blocks = block_commands::get_all_block(&inner.ctx).ok()?;
127 let mut sorted: Vec<_> = all_blocks.into_iter().collect();
128 let store = inner.ctx.db_context.get_store();
129 crate::inner::refresh_block_positions(&mut sorted, store);
130 sorted.sort_by_key(|b| b.document_position);
131 let idx = sorted.iter().position(|b| b.id == self.block_id as u64)?;
132 sorted.get(idx + 1).map(|b| TextBlock {
133 doc: Arc::clone(&self.doc),
134 block_id: b.id as usize,
135 })
136 }
137
138 pub fn previous(&self) -> Option<TextBlock> {
141 let inner = self.doc.lock();
142 let all_blocks = block_commands::get_all_block(&inner.ctx).ok()?;
143 let mut sorted: Vec<_> = all_blocks.into_iter().collect();
144 let store = inner.ctx.db_context.get_store();
145 crate::inner::refresh_block_positions(&mut sorted, store);
146 sorted.sort_by_key(|b| b.document_position);
147 let idx = sorted.iter().position(|b| b.id == self.block_id as u64)?;
148 if idx == 0 {
149 return None;
150 }
151 sorted.get(idx - 1).map(|b| TextBlock {
152 doc: Arc::clone(&self.doc),
153 block_id: b.id as usize,
154 })
155 }
156
157 pub fn frame(&self) -> TextFrame {
161 let inner = self.doc.lock();
162 let frame_id = find_parent_frame(&inner, self.block_id as u64);
163 TextFrame {
164 doc: Arc::clone(&self.doc),
165 frame_id: frame_id.map(|id| id as usize).unwrap_or(0),
166 }
167 }
168
169 pub fn table_cell(&self) -> Option<TableCellRef> {
175 let inner = self.doc.lock();
176 let frame_id = find_parent_frame(&inner, self.block_id as u64)?;
177
178 let frame_dto = frame_commands::get_frame(&inner.ctx, &frame_id)
181 .ok()
182 .flatten()?;
183
184 if let Some(table_entity_id) = frame_dto.table {
185 let table_dto =
189 frontend::commands::table_commands::get_table(&inner.ctx, &{ table_entity_id })
190 .ok()
191 .flatten()?;
192 for &cell_id in &table_dto.cells {
193 if let Some(cell_dto) =
194 frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{
195 cell_id
196 })
197 .ok()
198 .flatten()
199 && cell_dto.cell_frame == Some(frame_id)
200 {
201 return Some(TableCellRef {
202 table: TextTable {
203 doc: Arc::clone(&self.doc),
204 table_id: table_entity_id as usize,
205 },
206 row: to_usize(cell_dto.row),
207 column: to_usize(cell_dto.column),
208 });
209 }
210 }
211 }
212
213 let all_tables =
216 frontend::commands::table_commands::get_all_table(&inner.ctx).unwrap_or_default();
217 for table_dto in &all_tables {
218 for &cell_id in &table_dto.cells {
219 if let Some(cell_dto) =
220 frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{
221 cell_id
222 })
223 .ok()
224 .flatten()
225 && cell_dto.cell_frame == Some(frame_id)
226 {
227 return Some(TableCellRef {
228 table: TextTable {
229 doc: Arc::clone(&self.doc),
230 table_id: table_dto.id as usize,
231 },
232 row: to_usize(cell_dto.row),
233 column: to_usize(cell_dto.column),
234 });
235 }
236 }
237 }
238
239 None
240 }
241
242 pub fn block_format(&self) -> BlockFormat {
246 let inner = self.doc.lock();
247 block_commands::get_block(&inner.ctx, &(self.block_id as u64))
248 .ok()
249 .flatten()
250 .map(|b| BlockFormat::from(&b))
251 .unwrap_or_default()
252 }
253
254 pub fn char_format_at(&self, offset: usize) -> Option<TextFormat> {
261 let inner = self.doc.lock();
262 let fragments = build_fragments(&inner, self.block_id as u64);
263 for frag in &fragments {
264 match frag {
265 FragmentContent::Text {
266 format,
267 offset: frag_offset,
268 length,
269 ..
270 } => {
271 if offset >= *frag_offset && offset < frag_offset + length {
272 return Some(format.clone());
273 }
274 }
275 FragmentContent::Image {
276 format,
277 offset: frag_offset,
278 ..
279 }
280 | FragmentContent::FootnoteReference {
281 format,
282 offset: frag_offset,
283 ..
284 } => {
285 if offset == *frag_offset {
286 return Some(format.clone());
287 }
288 }
289 }
290 }
291 None
292 }
293
294 pub fn fragments(&self) -> Vec<FragmentContent> {
307 let inner = self.doc.lock();
308 build_fragments(&inner, self.block_id as u64)
309 }
310
311 pub fn display_fragments(&self) -> Vec<FragmentContent> {
319 let inner = self.doc.lock();
320 let fragments = build_raw_fragments(
321 &inner,
322 self.block_id as u64,
323 None,
324 &SnapshotLookups::default(),
325 );
326 let spans = crate::highlight::merged_spans_for_block(
329 &inner,
330 self.block_id,
331 &crate::highlight::HighlightMask::ALL,
332 );
333 if !spans.is_empty() {
334 return crate::highlight::merge_highlight_spans(fragments, &spans);
335 }
336 fragments
337 }
338
339 pub fn addressable_inline_pieces(&self) -> Vec<AddressablePiece> {
358 let inner = self.doc.lock();
359 let Some(block_dto) = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
360 .ok()
361 .flatten()
362 else {
363 return Vec::new();
364 };
365 let entity: common::entities::Block = block_dto.into();
366 let store = inner.ctx.db_context.get_store();
367 let plain_text = common::database::rope_helpers::block_content_via_store(&entity, store);
368 common::format_runs_query::addressable_inline_pieces_for_block(store, &entity, &plain_text)
369 .into_iter()
370 .map(|p| AddressablePiece {
371 start: to_usize(p.start as i64),
372 end: to_usize(p.end as i64),
373 content: p.content,
374 format: TextFormat::from(&p.format),
375 })
376 .collect()
377 }
378
379 pub fn list(&self) -> Option<TextList> {
383 let inner = self.doc.lock();
384 let block_dto = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
385 .ok()
386 .flatten()?;
387 let list_id = block_dto.list?;
388 Some(TextList {
389 doc: Arc::clone(&self.doc),
390 list_id: list_id as usize,
391 })
392 }
393
394 pub fn list_item_index(&self) -> Option<usize> {
396 let inner = self.doc.lock();
397 let block_dto = block_commands::get_block(&inner.ctx, &(self.block_id as u64))
398 .ok()
399 .flatten()?;
400 let list_id = block_dto.list?;
401 Some(compute_list_item_index(
402 &inner,
403 list_id,
404 self.block_id as u64,
405 ))
406 }
407
408 pub fn snapshot(&self) -> BlockSnapshot {
412 let inner = self.doc.lock();
413 build_block_snapshot(
414 &inner,
415 self.block_id as u64,
416 crate::highlight::SnapshotHighlights {
417 kind: inner.highlight_kind,
418 mask: &crate::highlight::HighlightMask::ALL,
419 suppress_paint: false,
420 },
421 )
422 .unwrap_or_else(|| BlockSnapshot {
423 block_id: self.block_id,
424 position: 0,
425 length: 0,
426 text: String::new(),
427 fragments: Vec::new(),
428 block_format: BlockFormat::default(),
429 list_info: None,
430 parent_frame_id: None,
431 table_cell: None,
432 paint_highlights: Vec::new(),
433 })
434 }
435}
436
437pub(crate) fn find_parent_frame(inner: &TextDocumentInner, block_id: u64) -> Option<EntityId> {
443 whole_document_read();
444 let all_frames = frame_commands::get_all_frame(&inner.ctx).ok()?;
445 let block_entity_id = block_id as EntityId;
446 for frame in &all_frames {
447 if frame.blocks.contains(&block_entity_id) {
448 return Some(frame.id as EntityId);
449 }
450 }
451 None
452}
453
454fn document_has_no_tables(inner: &TextDocumentInner) -> bool {
459 inner.ctx.db_context.get_store().tables.read().is_empty()
460}
461
462fn find_table_cell_context(inner: &TextDocumentInner, block_id: u64) -> Option<TableCellContext> {
465 if document_has_no_tables(inner) {
469 return None;
470 }
471 whole_document_read();
472 let frame_id = find_parent_frame(inner, block_id)?;
473
474 let frame_dto = frame_commands::get_frame(&inner.ctx, &frame_id)
475 .ok()
476 .flatten()?;
477
478 if let Some(table_entity_id) = frame_dto.table {
480 let table_dto =
481 frontend::commands::table_commands::get_table(&inner.ctx, &{ table_entity_id })
482 .ok()
483 .flatten()?;
484 for &cell_id in &table_dto.cells {
485 if let Some(cell_dto) =
486 frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{ cell_id })
487 .ok()
488 .flatten()
489 && cell_dto.cell_frame == Some(frame_id)
490 {
491 return Some(TableCellContext {
492 table_id: table_entity_id as usize,
493 row: to_usize(cell_dto.row),
494 column: to_usize(cell_dto.column),
495 });
496 }
497 }
498 }
499
500 let all_tables =
502 frontend::commands::table_commands::get_all_table(&inner.ctx).unwrap_or_default();
503 for table_dto in &all_tables {
504 for &cell_id in &table_dto.cells {
505 if let Some(cell_dto) =
506 frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{ cell_id })
507 .ok()
508 .flatten()
509 && cell_dto.cell_frame == Some(frame_id)
510 {
511 return Some(TableCellContext {
512 table_id: table_dto.id as usize,
513 row: to_usize(cell_dto.row),
514 column: to_usize(cell_dto.column),
515 });
516 }
517 }
518 }
519
520 None
521}
522
523#[derive(Default)]
540pub(crate) struct SnapshotLookups {
541 frames: OnSecondAsk<FrameLookup>,
542 cells: OnSecondAsk<CellLookup>,
543 list_item_indices: OnSecondAsk<HashMap<(EntityId, EntityId), usize>>,
544 default_language: OnceCell<Option<String>>,
545 footnote_markers: OnceCell<HashMap<String, String>>,
546 self_footnote_numbers: OnceCell<HashMap<String, usize>>,
547}
548
549struct OnSecondAsk<T> {
556 asked: Cell<bool>,
557 index: OnceCell<T>,
558}
559
560impl<T> Default for OnSecondAsk<T> {
561 fn default() -> Self {
562 Self {
563 asked: Cell::new(false),
564 index: OnceCell::new(),
565 }
566 }
567}
568
569impl<T> OnSecondAsk<T> {
570 fn get(&self, build: impl FnOnce() -> T) -> Option<&T> {
573 if let Some(index) = self.index.get() {
574 return Some(index);
575 }
576 if !self.asked.replace(true) {
577 return None;
578 }
579 Some(self.index.get_or_init(build))
580 }
581
582 fn force(&self, build: impl FnOnce() -> T) -> &T {
585 self.asked.set(true);
586 self.index.get_or_init(build)
587 }
588}
589
590fn whole_document_read() {
595 #[cfg(test)]
596 tests::WHOLE_DOCUMENT_READS.with(|reads| reads.set(reads.get() + 1));
597}
598
599struct FrameLookup {
601 parent_of_block: HashMap<EntityId, EntityId>,
604 table_of_frame: HashMap<EntityId, Option<EntityId>>,
606}
607
608impl FrameLookup {
609 fn build(inner: &TextDocumentInner) -> Self {
610 whole_document_read();
611 let all_frames = frame_commands::get_all_frame(&inner.ctx).unwrap_or_default();
612 let mut parent_of_block = HashMap::new();
613 let mut table_of_frame = HashMap::with_capacity(all_frames.len());
614 for frame in &all_frames {
615 for &block_id in &frame.blocks {
616 parent_of_block.entry(block_id).or_insert(frame.id);
617 }
618 table_of_frame.insert(frame.id, frame.table);
619 }
620 Self {
621 parent_of_block,
622 table_of_frame,
623 }
624 }
625}
626
627struct CellLookup {
630 tables: HashSet<EntityId>,
631 in_table: HashMap<(EntityId, EntityId), TableCellContext>,
633 anywhere: HashMap<EntityId, TableCellContext>,
635}
636
637impl CellLookup {
638 fn build(inner: &TextDocumentInner) -> Self {
639 whole_document_read();
640 let all_tables =
641 frontend::commands::table_commands::get_all_table(&inner.ctx).unwrap_or_default();
642 let mut tables = HashSet::with_capacity(all_tables.len());
643 let mut in_table = HashMap::new();
644 let mut anywhere = HashMap::new();
645 for table_dto in &all_tables {
646 tables.insert(table_dto.id);
647 for &cell_id in &table_dto.cells {
648 let Some(cell_dto) =
649 frontend::commands::table_cell_commands::get_table_cell(&inner.ctx, &{
650 cell_id
651 })
652 .ok()
653 .flatten()
654 else {
655 continue;
656 };
657 let Some(frame_id) = cell_dto.cell_frame else {
658 continue;
659 };
660 let context = TableCellContext {
661 table_id: table_dto.id as usize,
662 row: to_usize(cell_dto.row),
663 column: to_usize(cell_dto.column),
664 };
665 in_table
666 .entry((table_dto.id, frame_id))
667 .or_insert_with(|| context.clone());
668 anywhere.entry(frame_id).or_insert(context);
669 }
670 }
671 Self {
672 tables,
673 in_table,
674 anywhere,
675 }
676 }
677}
678
679impl SnapshotLookups {
680 fn parent_frame(&self, inner: &TextDocumentInner, block_id: u64) -> Option<EntityId> {
682 match self.frames.get(|| FrameLookup::build(inner)) {
683 Some(frames) => frames.parent_of_block.get(&block_id).copied(),
684 None => find_parent_frame(inner, block_id),
685 }
686 }
687
688 fn table_cell_context(
690 &self,
691 inner: &TextDocumentInner,
692 block_id: u64,
693 ) -> Option<TableCellContext> {
694 if document_has_no_tables(inner) {
695 return None;
696 }
697 let Some(cells) = self.cells.get(|| CellLookup::build(inner)) else {
698 return find_table_cell_context(inner, block_id);
699 };
700 let frames = self.frames.force(|| FrameLookup::build(inner));
701 let frame_id = *frames.parent_of_block.get(&block_id)?;
702 if let Some(Some(table_id)) = frames.table_of_frame.get(&frame_id) {
705 if !cells.tables.contains(table_id) {
706 return None;
707 }
708 if let Some(context) = cells.in_table.get(&(*table_id, frame_id)) {
709 return Some(context.clone());
710 }
711 }
712 cells.anywhere.get(&frame_id).cloned()
713 }
714
715 fn list_item_index(
718 &self,
719 inner: &TextDocumentInner,
720 list_id: EntityId,
721 block_id: u64,
722 ) -> usize {
723 let build = || {
724 whole_document_read();
725 let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
726 let store = inner.ctx.db_context.get_store();
727 crate::inner::refresh_block_positions(&mut all_blocks, store);
728 let mut members: HashMap<EntityId, Vec<(i64, EntityId)>> = HashMap::new();
731 for block in &all_blocks {
732 if let Some(list) = block.list {
733 members
734 .entry(list)
735 .or_default()
736 .push((block.document_position, block.id));
737 }
738 }
739 let mut indices = HashMap::new();
740 for (list, mut blocks) in members {
741 blocks.sort_by_key(|&(position, _)| position);
742 for (index, (_, id)) in blocks.into_iter().enumerate() {
743 indices.entry((list, id)).or_insert(index);
744 }
745 }
746 indices
747 };
748 match self.list_item_indices.get(build) {
749 Some(indices) => indices.get(&(list_id, block_id)).copied().unwrap_or(0),
750 None => compute_list_item_index(inner, list_id, block_id),
751 }
752 }
753
754 fn default_language(&self, inner: &TextDocumentInner) -> Option<String> {
756 self.default_language
757 .get_or_init(|| {
758 whole_document_read();
759 document_commands::get_document(&inner.ctx, &inner.document_id)
760 .ok()
761 .flatten()
762 .and_then(|d| d.default_language)
763 })
764 .clone()
765 }
766
767 fn footnote_markers(&self, inner: &TextDocumentInner) -> &HashMap<String, String> {
769 self.footnote_markers.get_or_init(|| {
770 whole_document_read();
771 inner
772 .ctx
773 .db_context
774 .get_store()
775 .footnote_markers
776 .read()
777 .clone()
778 })
779 }
780
781 fn self_footnote_numbers(&self, inner: &TextDocumentInner) -> &HashMap<String, usize> {
783 self.self_footnote_numbers
784 .get_or_init(|| document_self_footnote_numbers(inner.ctx.db_context.get_store()))
785 }
786}
787
788fn compute_block_number(inner: &TextDocumentInner, block_id: u64) -> usize {
790 let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
791 let store = inner.ctx.db_context.get_store();
792 crate::inner::refresh_block_positions(&mut all_blocks, store);
793 let mut sorted: Vec<_> = all_blocks.iter().collect();
794 sorted.sort_by_key(|b| b.document_position);
795 sorted.iter().position(|b| b.id == block_id).unwrap_or(0)
796}
797
798pub(crate) fn build_fragments(inner: &TextDocumentInner, block_id: u64) -> Vec<FragmentContent> {
801 build_fragments_with_text(
802 inner,
803 block_id,
804 None,
805 crate::highlight::SnapshotHighlights {
806 kind: inner.highlight_kind,
807 mask: &crate::highlight::HighlightMask::ALL,
808 suppress_paint: false,
809 },
810 &SnapshotLookups::default(),
811 )
812}
813
814pub(crate) fn build_fragments_with_text(
820 inner: &TextDocumentInner,
821 block_id: u64,
822 prefetched_text: Option<&str>,
823 hl: crate::highlight::SnapshotHighlights,
824 lookups: &SnapshotLookups,
825) -> Vec<FragmentContent> {
826 let fragments = build_raw_fragments(inner, block_id, prefetched_text, lookups);
827
828 if hl.kind == crate::highlight::HighlighterKind::Metric {
834 let spans = crate::highlight::merged_spans_for_block(inner, block_id as usize, hl.mask);
835 if !spans.is_empty() {
836 return crate::highlight::merge_highlight_spans(fragments, &spans);
837 }
838 }
839
840 fragments
841}
842
843fn document_self_footnote_numbers(
868 store: &common::database::Store,
869) -> std::collections::HashMap<String, usize> {
870 whole_document_read();
871 let definition_blocks: std::collections::HashSet<common::types::EntityId> = store
872 .frames
873 .read()
874 .values()
875 .filter(|f| f.footnote_label.is_some())
876 .flat_map(|f| f.child_order.iter().copied())
877 .filter(|child| *child > 0)
878 .map(|child| child as common::types::EntityId)
879 .collect();
880
881 let mut ordered: Vec<(i64, common::types::EntityId)> = store
882 .blocks
883 .read()
884 .values()
885 .filter(|b| !definition_blocks.contains(&b.id))
886 .map(|b| (b.document_position, b.id))
887 .collect();
888 ordered.sort_unstable();
889
890 let refs = store.block_footnote_refs.read();
891 let mut numbers: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
892 let mut next = 1usize;
893 for (_, block_id) in ordered {
894 let Some(anchors) = refs.get(&block_id) else {
895 continue;
896 };
897 let mut in_block: Vec<_> = anchors.iter().collect();
898 in_block.sort_by_key(|a| a.byte_offset);
899 for anchor in in_block {
900 numbers.entry(anchor.label.clone()).or_insert_with(|| {
901 let n = next;
902 next += 1;
903 n
904 });
905 }
906 }
907 numbers
908}
909
910fn build_raw_fragments(
925 inner: &TextDocumentInner,
926 block_id: u64,
927 prefetched_text: Option<&str>,
928 lookups: &SnapshotLookups,
929) -> Vec<FragmentContent> {
930 let _block_dto = match block_commands::get_block(&inner.ctx, &block_id)
931 .ok()
932 .flatten()
933 {
934 Some(b) => b,
935 None => return Vec::new(),
936 };
937
938 let plain_owned;
939 let plain: &str = match prefetched_text {
940 Some(t) => t,
941 None => {
942 let entity: common::entities::Block = _block_dto.clone().into();
943 plain_owned = common::database::rope_helpers::block_content_via_store(
944 &entity,
945 inner.ctx.db_context.get_store(),
946 );
947 &plain_owned
948 }
949 };
950
951 let (runs, images, notes) = {
952 let store = inner.ctx.db_context.get_store();
953 let runs: Vec<FormatRun> = store
954 .format_runs
955 .read()
956 .get(&block_id)
957 .cloned()
958 .unwrap_or_default();
959 let images: Vec<ImageAnchor> = store
960 .block_images
961 .read()
962 .get(&block_id)
963 .cloned()
964 .unwrap_or_default();
965 let notes = store
966 .block_footnote_refs
967 .read()
968 .get(&block_id)
969 .cloned()
970 .unwrap_or_default();
971 (runs, images, notes)
972 };
973
974 let anchors = frontend::common::format_runs::block_anchors(&images, ¬es);
979 let pieces = frontend::common::format_runs::merge_runs_and_anchors(plain, &runs, &anchors);
980
981 let mut fragments = Vec::with_capacity(pieces.len());
982 let mut char_offset: usize = 0;
983
984 for piece in pieces {
985 match piece {
986 frontend::common::format_runs::InlinePiece::Text { start, end, format } => {
987 let text = &plain[start as usize..end as usize];
988 let length = text.chars().count();
989 let word_starts = compute_word_starts(text);
990 fragments.push(FragmentContent::Text {
991 text: text.to_string(),
992 format: format.map(TextFormat::from).unwrap_or_default(),
993 offset: char_offset,
994 length,
995 element_id: synth_element_id(block_id, start),
996 word_starts,
997 });
998 char_offset += length;
999 }
1000 frontend::common::format_runs::InlinePiece::FootnoteRef(note) => {
1001 fragments.push(FragmentContent::FootnoteReference {
1002 label: note.label.clone(),
1003 marker: lookups
1027 .footnote_markers(inner)
1028 .get(¬e.label)
1029 .cloned()
1030 .unwrap_or_else(|| {
1031 lookups
1032 .self_footnote_numbers(inner)
1033 .get(¬e.label)
1034 .map(|n| n.to_string())
1035 .unwrap_or_else(|| note.label.clone())
1036 }),
1037 format: TextFormat::from(¬e.format),
1038 offset: char_offset,
1039 element_id: synth_element_id(block_id, note.byte_offset),
1040 });
1041 char_offset += 1;
1044 }
1045 frontend::common::format_runs::InlinePiece::Image(img) => {
1046 fragments.push(FragmentContent::Image {
1047 name: img.name.clone(),
1048 alt: img.alt.clone(),
1049 width: img.width as u32,
1050 height: img.height as u32,
1051 quality: img.quality as u32,
1052 format: TextFormat::from(&img.format),
1053 offset: char_offset,
1054 element_id: synth_element_id(block_id, img.byte_offset),
1055 });
1056 char_offset += 1;
1059 }
1060 }
1061 }
1062
1063 fragments
1064}
1065
1066fn compute_word_starts(text: &str) -> Vec<u8> {
1072 use unicode_segmentation::UnicodeSegmentation;
1073 let mut result = Vec::new();
1074 let mut byte_to_char: Vec<(usize, usize)> = Vec::new();
1078 for (ci, (bi, _)) in text.char_indices().enumerate() {
1079 byte_to_char.push((bi, ci));
1080 }
1081 for (byte_off, _word) in text.unicode_word_indices() {
1082 let char_idx = byte_to_char
1083 .iter()
1084 .find(|(bi, _)| *bi == byte_off)
1085 .map(|(_, ci)| *ci)
1086 .unwrap_or(0);
1087 if let Ok(idx) = u8::try_from(char_idx) {
1094 result.push(idx);
1095 } else {
1096 break;
1097 }
1098 }
1099 result
1100}
1101
1102fn compute_list_item_index(inner: &TextDocumentInner, list_id: EntityId, block_id: u64) -> usize {
1104 whole_document_read();
1105 let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
1106 let store = inner.ctx.db_context.get_store();
1107 crate::inner::refresh_block_positions(&mut all_blocks, store);
1108 let mut list_blocks: Vec<_> = all_blocks
1109 .iter()
1110 .filter(|b| b.list == Some(list_id))
1111 .collect();
1112 list_blocks.sort_by_key(|b| b.document_position);
1113 list_blocks
1114 .iter()
1115 .position(|b| b.id == block_id)
1116 .unwrap_or(0)
1117}
1118
1119const MAX_ROMAN_MARKER: usize = 3999;
1124
1125pub(crate) fn format_list_marker(
1129 list_dto: &frontend::list::dtos::ListDto,
1130 item_index: usize,
1131) -> String {
1132 let index = i64::try_from(item_index).unwrap_or(i64::MAX);
1133 let number = list_dto.start.unwrap_or(1).saturating_add(index);
1134 let letter = |first: u8| match u8::try_from(number) {
1135 Ok(n @ 1..=26) => ((first + (n - 1)) as char).to_string(),
1136 _ => format!("{number}"),
1137 };
1138 let numeral = |to_roman: fn(usize) -> String| match usize::try_from(number) {
1139 Ok(n @ 1..=MAX_ROMAN_MARKER) => to_roman(n),
1140 _ => format!("{number}"),
1141 };
1142 let marker_body = match list_dto.style {
1143 ListStyle::Disc => "\u{2022}".to_string(), ListStyle::Circle => "\u{25E6}".to_string(), ListStyle::Square => "\u{25AA}".to_string(), ListStyle::Decimal => format!("{number}"),
1147 ListStyle::LowerAlpha => letter(b'a'),
1148 ListStyle::UpperAlpha => letter(b'A'),
1149 ListStyle::LowerRoman => numeral(to_roman_lower),
1150 ListStyle::UpperRoman => numeral(to_roman_upper),
1151 };
1152 format!("{}{marker_body}{}", list_dto.prefix, list_dto.suffix)
1153}
1154
1155fn to_roman_upper(mut n: usize) -> String {
1156 const VALUES: &[(usize, &str)] = &[
1157 (1000, "M"),
1158 (900, "CM"),
1159 (500, "D"),
1160 (400, "CD"),
1161 (100, "C"),
1162 (90, "XC"),
1163 (50, "L"),
1164 (40, "XL"),
1165 (10, "X"),
1166 (9, "IX"),
1167 (5, "V"),
1168 (4, "IV"),
1169 (1, "I"),
1170 ];
1171 let mut result = String::new();
1172 for &(val, sym) in VALUES {
1173 while n >= val {
1174 result.push_str(sym);
1175 n -= val;
1176 }
1177 }
1178 result
1179}
1180
1181fn to_roman_lower(n: usize) -> String {
1182 to_roman_upper(n).to_lowercase()
1183}
1184
1185fn build_list_info(
1187 inner: &TextDocumentInner,
1188 block_dto: &frontend::block::dtos::BlockDto,
1189 lookups: &SnapshotLookups,
1190) -> Option<ListInfo> {
1191 let list_id = block_dto.list?;
1192 let list_dto = list_commands::get_list(&inner.ctx, &{ list_id })
1193 .ok()
1194 .flatten()?;
1195
1196 let item_index = lookups.list_item_index(inner, list_id, block_dto.id);
1197 let marker = format_list_marker(&list_dto, item_index);
1198
1199 Some(ListInfo {
1200 list_id: list_id as usize,
1201 style: list_dto.style.clone(),
1202 indent: list_dto.indent as u8,
1203 marker,
1204 item_index,
1205 })
1206}
1207
1208pub(crate) fn build_block_snapshot(
1210 inner: &TextDocumentInner,
1211 block_id: u64,
1212 hl: crate::highlight::SnapshotHighlights,
1213) -> Option<BlockSnapshot> {
1214 build_block_snapshot_with_position_and_parent(
1215 inner,
1216 block_id,
1217 None,
1218 None,
1219 hl,
1220 &SnapshotLookups::default(),
1221 )
1222}
1223
1224pub(crate) fn build_block_snapshot_with_position(
1231 inner: &TextDocumentInner,
1232 block_id: u64,
1233 computed_position: Option<usize>,
1234 hl: crate::highlight::SnapshotHighlights,
1235 lookups: &SnapshotLookups,
1236) -> Option<BlockSnapshot> {
1237 build_block_snapshot_with_position_and_parent(
1238 inner,
1239 block_id,
1240 computed_position,
1241 None,
1242 hl,
1243 lookups,
1244 )
1245}
1246
1247pub(crate) fn build_block_snapshot_with_position_and_parent(
1258 inner: &TextDocumentInner,
1259 block_id: u64,
1260 computed_position: Option<usize>,
1261 parent_frame_hint: Option<EntityId>,
1262 hl: crate::highlight::SnapshotHighlights,
1263 lookups: &SnapshotLookups,
1264) -> Option<BlockSnapshot> {
1265 let mut block_dto = block_commands::get_block(&inner.ctx, &block_id)
1266 .ok()
1267 .flatten()?;
1268 let store_for_pos = inner.ctx.db_context.get_store();
1269 crate::inner::refresh_block_position(&mut block_dto, store_for_pos);
1270
1271 let mut block_format = BlockFormat::from(&block_dto);
1272 if block_format.language.is_none() {
1276 block_format.language = lookups.default_language(inner);
1277 }
1278 let list_info = build_list_info(inner, &block_dto, lookups);
1279
1280 let parent_frame_id = parent_frame_hint
1281 .or_else(|| lookups.parent_frame(inner, block_id))
1282 .map(|id| id as usize);
1283 let table_cell = lookups.table_cell_context(inner, block_id);
1284
1285 let position = if common::database::rope_helpers::rope_positions_match_flow(store_for_pos) {
1296 to_usize(block_dto.document_position)
1297 } else {
1298 computed_position.unwrap_or_else(|| to_usize(block_dto.document_position))
1299 };
1300
1301 let entity: common::entities::Block = block_dto.clone().into();
1305 let store = inner.ctx.db_context.get_store();
1306 let text = common::database::rope_helpers::block_content_via_store(&entity, store);
1307 let length = to_usize(common::database::rope_helpers::block_char_length(
1308 &entity, store,
1309 ));
1310 let fragments = build_fragments_with_text(inner, block_id, Some(&text), hl, lookups);
1311
1312 let paint_highlights =
1317 if hl.kind == crate::highlight::HighlighterKind::PaintOnly && !hl.suppress_paint {
1318 let spans = crate::highlight::merged_spans_for_block(inner, block_id as usize, hl.mask);
1319 crate::highlight::extract_paint_spans(&spans, length)
1320 } else {
1321 Vec::new()
1322 };
1323
1324 Some(BlockSnapshot {
1325 block_id: block_id as usize,
1326 position,
1327 length,
1328 text,
1329 fragments,
1330 block_format,
1331 list_info,
1332 parent_frame_id,
1333 table_cell,
1334 paint_highlights,
1335 })
1336}
1337
1338pub(crate) fn build_blocks_snapshot_for_frame(
1341 inner: &TextDocumentInner,
1342 frame_id: u64,
1343 hl: crate::highlight::SnapshotHighlights,
1344 lookups: &SnapshotLookups,
1345) -> Vec<BlockSnapshot> {
1346 let frame_dto = match frame_commands::get_frame(&inner.ctx, &(frame_id as EntityId))
1347 .ok()
1348 .flatten()
1349 {
1350 Some(f) => f,
1351 None => return Vec::new(),
1352 };
1353
1354 let mut block_dtos: Vec<_> = frame_dto
1355 .blocks
1356 .iter()
1357 .filter_map(|&id| {
1358 block_commands::get_block(&inner.ctx, &{ id })
1359 .ok()
1360 .flatten()
1361 })
1362 .collect();
1363 let store = inner.ctx.db_context.get_store();
1364 crate::inner::refresh_block_positions(&mut block_dtos, store);
1365 block_dtos.sort_by_key(|b| b.document_position);
1366
1367 block_dtos
1368 .iter()
1369 .filter_map(|b| {
1370 build_block_snapshot_with_position_and_parent(inner, b.id, None, None, hl, lookups)
1371 })
1372 .collect()
1373}
1374
1375pub(crate) fn build_blocks_snapshot_for_frame_with_positions(
1382 inner: &TextDocumentInner,
1383 frame_id: u64,
1384 start_pos: usize,
1385 hl: crate::highlight::SnapshotHighlights,
1386 lookups: &SnapshotLookups,
1387) -> (Vec<BlockSnapshot>, usize) {
1388 let frame_dto = match frame_commands::get_frame(&inner.ctx, &(frame_id as EntityId))
1389 .ok()
1390 .flatten()
1391 {
1392 Some(f) => f,
1393 None => return (Vec::new(), start_pos),
1394 };
1395
1396 let mut block_dtos: Vec<_> = frame_dto
1397 .blocks
1398 .iter()
1399 .filter_map(|&id| {
1400 block_commands::get_block(&inner.ctx, &{ id })
1401 .ok()
1402 .flatten()
1403 })
1404 .collect();
1405 let store = inner.ctx.db_context.get_store();
1406 crate::inner::refresh_block_positions(&mut block_dtos, store);
1407 block_dtos.sort_by_key(|b| b.document_position);
1408
1409 let mut running_pos = start_pos;
1410 let mut snapshots = Vec::with_capacity(block_dtos.len());
1411 for b in &block_dtos {
1412 if let Some(snap) =
1413 build_block_snapshot_with_position(inner, b.id, Some(running_pos), hl, lookups)
1414 {
1415 running_pos += snap.length + 1; snapshots.push(snap);
1417 }
1418 }
1419 (snapshots, running_pos)
1420}
1421
1422#[cfg(test)]
1423mod tests {
1424 use crate::TextDocument;
1425 use crate::flow::FlowElementSnapshot;
1426 use std::cell::Cell;
1427
1428 thread_local! {
1429 pub(super) static WHOLE_DOCUMENT_READS: Cell<usize> = const { Cell::new(0) };
1431 }
1432
1433 fn novel(paragraphs: usize) -> String {
1436 let mut text = String::new();
1437 for i in 0..paragraphs {
1438 if i % 7 == 3 {
1439 text.push_str(&format!("Paragraph {i}, with a note[^n{i}].\n\n"));
1440 text.push_str(&format!("[^n{i}]: Note {i}.\n\n"));
1441 } else if i % 17 == 5 {
1442 text.push_str(&format!("> Quoted paragraph {i}.\n\n"));
1443 } else {
1444 text.push_str(&format!("Paragraph {i} of the text.\n\n"));
1445 }
1446 if i % 11 == 4 {
1447 text.push_str(&format!("- item {i}\n- item {i}, second\n\n"));
1448 }
1449 if i % 13 == 6 {
1450 text.push_str(&format!("| a{i} | b |\n| c | d |\n\n"));
1451 }
1452 }
1453 text
1454 }
1455
1456 fn all_blocks(elements: &[FlowElementSnapshot], out: &mut Vec<crate::flow::BlockSnapshot>) {
1458 for element in elements {
1459 match element {
1460 FlowElementSnapshot::Block(block) => out.push(block.clone()),
1461 FlowElementSnapshot::Table(table) => {
1462 for cell in &table.cells {
1463 out.extend(cell.blocks.iter().cloned());
1464 }
1465 }
1466 FlowElementSnapshot::Frame(frame) => all_blocks(&frame.elements, out),
1467 }
1468 }
1469 }
1470
1471 #[test]
1477 fn a_flow_snapshot_agrees_with_a_snapshot_of_each_block_alone() {
1478 let mixed = "# Title\n\nA note[^a] and another[^b].\n\n- one\n- two\n- three\n\n\
1479 Between lists.\n\n1. first\n2. second\n\n> Quoted, with a note[^c].\n>\n\
1480 > - quoted item\n> - quoted item two\n>\n> > nested quote\n\n\
1481 | h1 | h2 |\n|----|----|\n| a | b[^a] |\n| c | d |\n\n\
1482 - again one\n- again two\n\n> | q1 | q2 |\n> | q3 | q4 |\n\n\
1483 Last paragraph[^d].\n\n[^a]: Note a, citing [^b].\n\n[^b]: Note b.\n\n\
1484 [^c]: Note c.\n\n[^d]: Note d.\n";
1485 let html = "<p>Before.</p><table><tr><td><p>c1 p1</p><p>c1 p2</p></td><td><ul>\
1486 <li>in cell</li><li>in cell 2</li></ul></td></tr><tr><td>plain</td><td>\
1487 <p>x</p><p>y</p></td></tr></table><ol><li>o1</li><li>o2</li></ol>\
1488 <blockquote><p>q</p><ul><li>qi</li></ul></blockquote><p>After.</p>";
1489
1490 let from_djot = |text: &str| {
1491 let doc = TextDocument::new();
1492 doc.set_djot_sync(text).unwrap();
1493 doc
1494 };
1495 let numbered_by_the_document = from_djot(mixed);
1496 let partly_by_the_host = from_djot(mixed);
1497 partly_by_the_host.set_footnote_markers(
1498 [("a", "i"), ("c", "iii")]
1499 .into_iter()
1500 .map(|(label, marker)| (label.to_string(), marker.to_string()))
1501 .collect(),
1502 );
1503 partly_by_the_host.set_default_language("fr").unwrap();
1504 let with_cells_of_paragraphs = TextDocument::new();
1505 with_cells_of_paragraphs
1506 .set_html(html)
1507 .unwrap()
1508 .wait()
1509 .unwrap();
1510 let long = from_djot(&novel(120));
1511
1512 for (label, doc) in [
1513 ("notes numbered by the document", &numbered_by_the_document),
1514 ("notes partly numbered by the host", &partly_by_the_host),
1515 ("cells holding paragraphs", &with_cells_of_paragraphs),
1516 ("a long text", &long),
1517 ] {
1518 let mut blocks = Vec::new();
1519 all_blocks(&doc.snapshot_flow().elements, &mut blocks);
1520 assert!(
1521 blocks.iter().any(|b| b.table_cell.is_some())
1522 && blocks.iter().any(|b| b.list_info.is_some()),
1523 "{label}: the text holds table cells and list items"
1524 );
1525 for from_flow in blocks {
1526 let alone = doc
1527 .block_by_id(from_flow.block_id)
1528 .expect("a block of the flow exists")
1529 .snapshot();
1530 assert_eq!(
1531 alone, from_flow,
1532 "{label}: block {} alone and in the flow",
1533 from_flow.block_id
1534 );
1535 }
1536 }
1537 }
1538
1539 fn reads_for_one_snapshot(doc: &TextDocument) -> (usize, crate::flow::FlowSnapshot) {
1541 WHOLE_DOCUMENT_READS.with(|reads| reads.set(0));
1542 let flow = doc.snapshot_flow();
1543 (WHOLE_DOCUMENT_READS.with(Cell::get), flow)
1544 }
1545
1546 #[test]
1552 fn a_flow_snapshot_reads_each_whole_document_fact_a_fixed_number_of_times() {
1553 let mut counts = Vec::new();
1554 for paragraphs in [60, 480] {
1555 let doc = TextDocument::new();
1556 doc.set_djot_sync(&novel(paragraphs)).unwrap();
1557 let (numbered_by_the_document, flow) = reads_for_one_snapshot(&doc);
1558
1559 let blocks: Vec<_> = flow
1560 .elements
1561 .iter()
1562 .filter_map(|element| match element {
1563 FlowElementSnapshot::Block(block) => Some(block),
1564 _ => None,
1565 })
1566 .collect();
1567 assert!(
1568 blocks.iter().filter(|b| b.list_info.is_some()).count() > 4,
1569 "the text holds list items"
1570 );
1571 assert!(
1572 flow.elements
1573 .iter()
1574 .filter(|e| matches!(e, FlowElementSnapshot::Table(_)))
1575 .count()
1576 > 2,
1577 "the text holds tables"
1578 );
1579 assert!(
1580 flow.elements
1581 .iter()
1582 .any(|e| matches!(e, FlowElementSnapshot::Frame(_))),
1583 "the text holds quotations"
1584 );
1585
1586 let markers = (0..paragraphs)
1588 .filter(|i| i % 7 == 3)
1589 .enumerate()
1590 .map(|(k, i)| (format!("n{i}"), (k + 1).to_string()))
1591 .collect();
1592 doc.set_footnote_markers(markers);
1593 let (numbered_by_the_host, _) = reads_for_one_snapshot(&doc);
1594
1595 counts.push((paragraphs, numbered_by_the_document, numbered_by_the_host));
1596 }
1597 let (small, small_self, small_host) = counts[0];
1598 let (large, large_self, large_host) = counts[1];
1599 println!(
1600 "whole-document reads per flow snapshot: {small_self} and {small_host} (notes \
1601 numbered by the document, by the host) at {small} paragraphs, {large_self} and \
1602 {large_host} at {large}"
1603 );
1604 assert_eq!(
1605 large_self, small_self,
1606 "a flow snapshot of {large} paragraphs read whole-document facts {large_self} \
1607 times against {small_self} for {small}: one of them is being read again for \
1608 each block that needs it"
1609 );
1610 assert_eq!(
1611 large_host, small_host,
1612 "with host footnote markers, a flow snapshot of {large} paragraphs read \
1613 whole-document facts {large_host} times against {small_host} for {small}"
1614 );
1615 assert!(
1619 small_self <= 12 && small_host <= 12,
1620 "{small_self} and {small_host} whole-document reads for one snapshot"
1621 );
1622 }
1623}