text_document/
text_table.rs1use std::sync::Arc;
4
5use parking_lot::Mutex;
6
7use frontend::commands::{block_commands, frame_commands, table_cell_commands, table_commands};
8use frontend::common::types::EntityId;
9
10use crate::convert::to_usize;
11use crate::flow::{BlockSnapshot, CellFormat, TableFormat, TableSnapshot};
12use crate::inner::TextDocumentInner;
13use crate::text_block::TextBlock;
14use crate::text_frame::{cell_dto_to_format, table_dto_to_format};
15
16#[derive(Clone)]
20pub struct TextTable {
21 pub(crate) doc: Arc<Mutex<TextDocumentInner>>,
22 pub(crate) table_id: usize,
23}
24
25impl TextTable {
26 pub fn id(&self) -> usize {
28 self.table_id
29 }
30
31 pub fn rows(&self) -> usize {
33 let inner = self.doc.lock();
34 table_commands::get_table(&inner.ctx, &(self.table_id as u64))
35 .ok()
36 .flatten()
37 .map(|t| to_usize(t.rows))
38 .unwrap_or(0)
39 }
40
41 pub fn columns(&self) -> usize {
43 let inner = self.doc.lock();
44 table_commands::get_table(&inner.ctx, &(self.table_id as u64))
45 .ok()
46 .flatten()
47 .map(|t| to_usize(t.columns))
48 .unwrap_or(0)
49 }
50
51 pub fn cell(&self, row: usize, col: usize) -> Option<TextTableCell> {
53 let inner = self.doc.lock();
54 let table_dto = table_commands::get_table(&inner.ctx, &(self.table_id as u64))
55 .ok()
56 .flatten()?;
57
58 for &cell_id in &table_dto.cells {
59 if let Some(cell_dto) = table_cell_commands::get_table_cell(&inner.ctx, &{ cell_id })
60 .ok()
61 .flatten()
62 && cell_dto.row as usize == row
63 && cell_dto.column as usize == col
64 {
65 return Some(TextTableCell {
66 doc: Arc::clone(&self.doc),
67 cell_id: cell_dto.id as usize,
68 });
69 }
70 }
71 None
72 }
73
74 pub fn column_widths(&self) -> Vec<i32> {
76 let inner = self.doc.lock();
77 table_commands::get_table(&inner.ctx, &(self.table_id as u64))
78 .ok()
79 .flatten()
80 .map(|t| t.column_widths.iter().map(|&v| v as i32).collect())
81 .unwrap_or_default()
82 }
83
84 pub fn format(&self) -> TableFormat {
86 let inner = self.doc.lock();
87 table_commands::get_table(&inner.ctx, &(self.table_id as u64))
88 .ok()
89 .flatten()
90 .map(|t| table_dto_to_format(&t))
91 .unwrap_or_default()
92 }
93
94 pub fn snapshot(&self) -> TableSnapshot {
96 let inner = self.doc.lock();
97 crate::text_frame::build_table_snapshot(
98 &inner,
99 self.table_id as u64,
100 crate::highlight::SnapshotHighlights {
101 kind: inner.highlight_kind,
102 mask: &crate::highlight::HighlightMask::ALL,
103 suppress_paint: false,
104 },
105 &crate::text_block::SnapshotLookups::default(),
106 )
107 .unwrap_or_else(|| TableSnapshot {
108 table_id: self.table_id,
109 rows: 0,
110 columns: 0,
111 column_widths: Vec::new(),
112 format: TableFormat::default(),
113 cells: Vec::new(),
114 })
115 }
116}
117
118#[derive(Clone)]
120pub struct TextTableCell {
121 pub(crate) doc: Arc<Mutex<TextDocumentInner>>,
122 pub(crate) cell_id: usize,
123}
124
125impl TextTableCell {
126 pub fn id(&self) -> usize {
128 self.cell_id
129 }
130
131 pub fn row(&self) -> usize {
133 let inner = self.doc.lock();
134 table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
135 .ok()
136 .flatten()
137 .map(|c| to_usize(c.row))
138 .unwrap_or(0)
139 }
140
141 pub fn column(&self) -> usize {
143 let inner = self.doc.lock();
144 table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
145 .ok()
146 .flatten()
147 .map(|c| to_usize(c.column))
148 .unwrap_or(0)
149 }
150
151 pub fn row_span(&self) -> usize {
153 let inner = self.doc.lock();
154 table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
155 .ok()
156 .flatten()
157 .map(|c| to_usize(c.row_span))
158 .unwrap_or(1)
159 }
160
161 pub fn column_span(&self) -> usize {
163 let inner = self.doc.lock();
164 table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
165 .ok()
166 .flatten()
167 .map(|c| to_usize(c.column_span))
168 .unwrap_or(1)
169 }
170
171 pub fn format(&self) -> CellFormat {
173 let inner = self.doc.lock();
174 table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
175 .ok()
176 .flatten()
177 .map(|c| cell_dto_to_format(&c))
178 .unwrap_or_default()
179 }
180
181 pub fn blocks(&self) -> Vec<TextBlock> {
183 let inner = self.doc.lock();
184 let cell_dto = match table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
185 .ok()
186 .flatten()
187 {
188 Some(c) => c,
189 None => return Vec::new(),
190 };
191
192 let cell_frame_id = match cell_dto.cell_frame {
193 Some(id) => id,
194 None => return Vec::new(),
195 };
196
197 let frame_dto = match frame_commands::get_frame(&inner.ctx, &(cell_frame_id as EntityId))
198 .ok()
199 .flatten()
200 {
201 Some(f) => f,
202 None => return Vec::new(),
203 };
204
205 let mut block_dtos: Vec<_> = frame_dto
207 .blocks
208 .iter()
209 .filter_map(|&id| {
210 block_commands::get_block(&inner.ctx, &{ id })
211 .ok()
212 .flatten()
213 })
214 .collect();
215 let store = inner.ctx.db_context.get_store();
216 crate::inner::refresh_block_positions(&mut block_dtos, store);
217 block_dtos.sort_by_key(|b| b.document_position);
218
219 block_dtos
220 .iter()
221 .map(|b| TextBlock {
222 doc: Arc::clone(&self.doc),
223 block_id: b.id as usize,
224 })
225 .collect()
226 }
227
228 pub fn snapshot_blocks(&self) -> Vec<BlockSnapshot> {
230 let inner = self.doc.lock();
231 let cell_dto = match table_cell_commands::get_table_cell(&inner.ctx, &(self.cell_id as u64))
232 .ok()
233 .flatten()
234 {
235 Some(c) => c,
236 None => return Vec::new(),
237 };
238
239 match cell_dto.cell_frame {
240 Some(frame_id) => crate::text_block::build_blocks_snapshot_for_frame(
241 &inner,
242 frame_id,
243 crate::highlight::SnapshotHighlights {
244 kind: inner.highlight_kind,
245 mask: &crate::highlight::HighlightMask::ALL,
246 suppress_paint: false,
247 },
248 &crate::text_block::SnapshotLookups::default(),
249 ),
250 None => Vec::new(),
251 }
252 }
253}