visi_core/core/engine/sheet/
edit.rs1use super::super::column::{ColumnPosition, DataColumn};
8use super::{CellRef, CellType, Direction, ResultData, Sheet, TextCellRef};
9
10pub fn get_word_boundaries_from_str(text: &str, char_offset: usize) -> (usize, usize) {
19 if text.is_empty() {
20 return (0, 0);
21 }
22
23 let chars: Vec<char> = text.chars().collect();
24 let len = chars.len();
25 let offset = char_offset.min(len);
26
27 let is_word_char = |c: char| c.is_alphanumeric() || c == '_';
28
29 let (on_idx, on_c) = if offset < len {
30 if chars[offset].is_whitespace() && offset > 0 && is_word_char(chars[offset - 1]) {
31 (offset - 1, chars[offset - 1])
32 } else {
33 (offset, chars[offset])
34 }
35 } else if offset > 0 {
36 (offset - 1, chars[offset - 1])
37 } else {
38 return (0, 0);
39 };
40
41 if on_c.is_whitespace() {
42 let mut start = on_idx;
43 while start > 0 && chars[start - 1].is_whitespace() {
44 start -= 1;
45 }
46 let mut end = on_idx + 1;
47 while end < len && chars[end].is_whitespace() {
48 end += 1;
49 }
50 return (start, end);
51 }
52
53 if is_word_char(on_c) {
54 let mut start = on_idx;
55 while start > 0 && is_word_char(chars[start - 1]) {
56 start -= 1;
57 }
58 let mut end = on_idx + 1;
59 while end < len && is_word_char(chars[end]) {
60 end += 1;
61 }
62 return (start, end);
63 }
64
65 let mut start = on_idx;
66 while start > 0 && !is_word_char(chars[start - 1]) && !chars[start - 1].is_whitespace() {
67 start -= 1;
68 }
69 let mut end = on_idx + 1;
70 while end < len && !is_word_char(chars[end]) && !chars[end].is_whitespace() {
71 end += 1;
72 }
73 (start, end)
74}
75impl Sheet {
76 pub fn get_result_data(&self, cell: &CellRef) -> ResultData {
86 let col = self.columns.get(cell.col);
87 if let Some(col) = col {
88 col.data.get(cell.row).unwrap_or(ResultData::None)
89 } else {
90 ResultData::None
91 }
92 }
93
94 pub(super) fn inherited_date_format(&self, ast: &crate::core::parser::Expr) -> Option<String> {
108 use crate::core::parser::{Expr, Op};
109 match ast {
110 Expr::CellRef {
111 sheet, row, col, ..
112 } if sheet.is_none() => self
113 .get_cell_style(*row, *col)
114 .and_then(|s| s.num_format.clone())
115 .filter(|code| crate::core::date::is_date_code(code)),
116 Expr::BinaryOp {
117 op: Op::Add | Op::Sub,
118 left,
119 right,
120 } => {
121 let left_fmt = self.inherited_date_format(left);
122 let right_fmt = self.inherited_date_format(right);
123 match (left_fmt, right_fmt) {
124 (Some(fmt), None) | (None, Some(fmt)) => Some(fmt),
127 _ => None,
129 }
130 }
131 _ => None,
132 }
133 }
134
135 pub fn get_display_string(&self, cell: &CellRef) -> String {
144 let value = self.get_result_data(cell);
145 let Some(code) = self
146 .get_cell_style(cell.row, cell.col)
147 .and_then(|s| s.num_format.as_deref())
148 else {
149 return value.to_string();
150 };
151 if !crate::core::date::is_date_code(code) {
152 return value.to_string();
153 }
154 let serial = match value {
156 ResultData::Float(f) => f,
157 ResultData::Integer(i) => i as f64,
158 _ => return value.to_string(),
159 };
160 if serial < 0.0 {
161 return value.to_string();
162 }
163 crate::core::date::render_date_code(
164 crate::core::date::excel_serial_to_date(serial),
165 code,
166 crate::core::date::StringCase::Title,
167 )
168 }
169
170 pub fn get_cell_type(&self, cell: &CellRef) -> CellType {
172 let col = self.columns.get(cell.col);
173 if let Some(col) = col {
174 col.cell_types
175 .get(cell.row)
176 .copied()
177 .unwrap_or(CellType::Empty)
178 } else {
179 CellType::Empty
180 }
181 }
182
183 pub fn set_cell_type(&mut self, row: usize, col: usize, cell_type: CellType) {
185 if let Some(column) = self.columns.get_mut(col)
186 && row < column.cell_types.len()
187 {
188 column.cell_types[row] = cell_type;
189 column.mark_dirty(row);
190 }
191 }
192
193 pub fn set_cell_with_type(&mut self, row: usize, col: usize, src: String, cell_type: CellType) {
195 let table_clone = self.clone();
196 if let Some(column) = self.columns.get_mut(col)
197 && row < column.src.len()
198 {
199 column.src[row] = src.clone();
200 column.cell_types[row] = cell_type;
201 let compiled = crate::core::parser::compile_formula(&src, &[table_clone]);
202 column.compiled_src[row] = compiled;
203 column.mark_dirty(row);
204
205 self.uncommitted_actions
206 .push(crate::core::SheetAction::SetCellSrc {
207 sheet_name: self.name.clone(),
208 col,
209 row,
210 src,
211 });
212 }
213 }
214
215 pub fn set_cell_src(&mut self, row: usize, col: usize, src: String) {
220 let table_clone = self.clone();
221 if let Some(column) = self.columns.get_mut(col)
222 && row < column.src.len()
223 {
224 column.src[row] = src.clone();
225 column.cell_types[row] = CellType::Auto;
226 let compiled = crate::core::parser::compile_formula(&src, &[table_clone]);
227 column.compiled_src[row] = compiled;
228 column.mark_dirty(row);
229
230 self.uncommitted_actions
231 .push(crate::core::SheetAction::SetCellSrc {
232 sheet_name: self.name.clone(),
233 col,
234 row,
235 src,
236 });
237 }
238 }
239
240 pub fn insert(&mut self, pos: TextCellRef, input: &str) {
247 let TextCellRef {
248 row,
249 col,
250 char_offset,
251 } = pos;
252 let table_clone = self.clone();
253 let existing_col = self.columns.get_mut(col);
254 match existing_col {
255 Some(existing_column) => {
256 existing_column.insert(ColumnPosition { row, char_offset }, input);
257 let src = existing_column.src[row].clone();
258 let compiled = crate::core::parser::compile_formula(&src, &[table_clone]);
259 existing_column.compiled_src[row] = compiled;
260 existing_column.mark_dirty(row);
261 self.uncommitted_actions
262 .push(crate::core::SheetAction::SetCellSrc {
263 sheet_name: self.name.clone(),
264 col,
265 row,
266 src,
267 });
268 }
269 None => {
270 println!("Warning: column {} does not exist", col)
271 }
272 }
273 }
274
275 pub fn delete_one_before(&mut self, pos: TextCellRef) {
277 let char_offset = pos.char_offset;
278 let start = if char_offset > 0 {
279 TextCellRef {
280 row: pos.row,
281 col: pos.col,
282 char_offset: char_offset - 1,
283 }
284 } else {
285 pos.clone()
286 };
287 let end = pos;
288 self.delete(start, end);
289 }
290
291 pub fn delete(&mut self, start: TextCellRef, end: TextCellRef) {
298 if start.col > end.col || (start.col == end.col && start.row > end.row) {
300 return;
301 }
302 let table_clone = self.clone();
303 if start.col == end.col {
304 let start_index = start.row;
305 let end_index = end.row;
306
307 if let Some(column) = self.columns.get_mut(start.col) {
308 if start.row == end.row && start_index < column.src.len() {
309 let src = &mut column.src[start_index];
310 let end_offset = std::cmp::min(end.char_offset, src.len());
311 if start.char_offset < end_offset {
312 src.replace_range(start.char_offset..end_offset, "");
313 column.dirty_indices.push(start_index);
314 let updated_src = src.clone();
315 let compiled =
316 crate::core::parser::compile_formula(&updated_src, &[table_clone]);
317 column.compiled_src[start_index] = compiled;
318 }
319 } else if start_index < column.len() && end_index >= start_index {
320 column.drain_rows(start_index..=end_index);
321 }
322 }
323 } else {
324 for col in start.col..=end.col {
325 if let Some(column) = self.columns.get_mut(col) {
326 let start_index = if col == start.col { col } else { 0 };
327
328 let end_index = if col == end.col {
329 col
330 } else {
331 column.src.len() - 1
332 };
333
334 if start_index < column.len() && end_index >= start_index {
335 column.drain_rows(start_index..=end_index);
336 }
337 }
338 }
339 }
340 }
341
342 pub fn extend(&mut self, direction: Direction) {
347 if self.columns.is_empty() {
348 return;
349 }
350 let row_count = self.columns[0].src.len();
351 const MAX_COLS: usize = 26;
352 match direction {
353 Direction::Up => {
354 for column in &mut self.columns {
355 column.insert_row(0);
356 }
357 self.uncommitted_actions
358 .push(crate::core::SheetAction::InsertRow {
359 sheet_name: self.name.clone(),
360 index: 0,
361 });
362 }
363 Direction::Down => {
364 for column in &mut self.columns {
365 column.push_row();
366 }
367 self.uncommitted_actions
368 .push(crate::core::SheetAction::InsertRow {
369 sheet_name: self.name.clone(),
370 index: row_count,
371 });
372 }
373 Direction::Left => {
374 if self.columns.len() < MAX_COLS {
375 self.columns.insert(0, DataColumn::new(row_count));
376 self.uncommitted_actions
377 .push(crate::core::SheetAction::InsertCol {
378 sheet_name: self.name.clone(),
379 index: 0,
380 });
381 }
382 }
383 Direction::Right => {
384 if self.columns.len() < MAX_COLS {
385 self.columns.push(DataColumn::new(row_count));
386 self.uncommitted_actions
387 .push(crate::core::SheetAction::InsertCol {
388 sheet_name: self.name.clone(),
389 index: self.columns.len() - 1,
390 });
391 }
392 }
393 Direction::None => {}
394 }
395 }
396
397 pub fn ensure_capacity(&mut self, target_row: usize, target_col: usize) {
399 let current_rows = self.row_count();
400 let needed_rows = target_row + 1;
401 let final_rows = current_rows.max(needed_rows);
402
403 while self.columns.len() <= target_col {
404 let col_idx = self.columns.len();
405 let mut col = DataColumn::new(final_rows);
406 col.name = crate::core::parser::col_idx_to_letters(col_idx);
407 self.columns.push(col);
408 }
409
410 if final_rows > current_rows {
411 for col in &mut self.columns {
412 col.resize_rows(final_rows);
413 }
414 }
415 }
416
417 pub fn get_cell_style(&self, row: usize, col: usize) -> Option<&crate::core::CellStyle> {
422 self.columns
423 .get(col)
424 .and_then(|column| column.styles.get(row))
425 .and_then(|opt| opt.as_ref())
426 }
427
428 pub fn set_cell_style(&mut self, row: usize, col: usize, style: crate::core::CellStyle) {
431 self.ensure_capacity(row, col);
432 if let Some(column) = self.columns.get_mut(col)
433 && row < column.styles.len()
434 {
435 if style.is_empty() {
436 column.styles[row] = None;
437 } else {
438 column.styles[row] = Some(style);
439 }
440 }
441 }
442
443 pub fn update_cell_style<F>(&mut self, row: usize, col: usize, f: F)
448 where
449 F: FnOnce(&mut crate::core::CellStyle),
450 {
451 self.ensure_capacity(row, col);
452 if let Some(column) = self.columns.get_mut(col)
453 && row < column.styles.len()
454 {
455 let mut current = column.styles[row].clone().unwrap_or_default();
456 f(&mut current);
457 if current.is_empty() {
458 column.styles[row] = None;
459 } else {
460 column.styles[row] = Some(current);
461 }
462 }
463 }
464
465 pub fn clear_cell_style(&mut self, row: usize, col: usize) {
467 if let Some(column) = self.columns.get_mut(col)
468 && row < column.styles.len()
469 {
470 column.styles[row] = None;
471 }
472 }
473
474 pub fn insert_row(&mut self, index: usize) {
477 let row_count = self.row_count();
478 if index >= row_count {
479 for column in &mut self.columns {
480 column.push_row();
481 }
482 self.uncommitted_actions
483 .push(crate::core::SheetAction::InsertRow {
484 sheet_name: self.name.clone(),
485 index: row_count,
486 });
487 } else {
488 for column in &mut self.columns {
489 column.insert_row(index);
490 }
491 self.uncommitted_actions
492 .push(crate::core::SheetAction::InsertRow {
493 sheet_name: self.name.clone(),
494 index,
495 });
496 }
497 }
498
499 pub fn delete_row(&mut self, index: usize) {
506 let row_count = self.row_count();
507 if index < row_count {
508 for column in &mut self.columns {
509 column.remove_row(index);
510 }
511 self.uncommitted_actions
512 .push(crate::core::SheetAction::DeleteRow {
513 sheet_name: self.name.clone(),
514 index,
515 });
516 self.mark_all_dirty();
517 }
518 }
519
520 pub fn insert_cells_shift_down(
538 &mut self,
539 row: usize,
540 first_col: usize,
541 last_col: usize,
542 count: usize,
543 ) {
544 let last_col = last_col.min(self.columns.len().saturating_sub(1));
545 if count == 0 || self.columns.is_empty() || first_col > last_col {
546 return;
547 }
548 for column in &mut self.columns {
551 for _ in 0..count {
552 column.push_row();
553 }
554 }
555 for column in &mut self.columns[first_col..=last_col] {
556 for _ in 0..count {
557 column.insert_row(row);
558 column.remove_row(column.len() - 1);
561 }
562 }
563 self.uncommitted_actions
564 .push(crate::core::SheetAction::InsertRow {
565 sheet_name: self.name.clone(),
566 index: row,
567 });
568 self.mark_all_dirty();
569 }
570
571 pub fn delete_cells_shift_up(
577 &mut self,
578 row: usize,
579 first_col: usize,
580 last_col: usize,
581 count: usize,
582 ) {
583 let last_col = last_col.min(self.columns.len().saturating_sub(1));
584 if count == 0 || self.columns.is_empty() || first_col > last_col || row >= self.row_count()
585 {
586 return;
587 }
588 for column in &mut self.columns[first_col..=last_col] {
589 for _ in 0..count {
590 if row < column.len() {
591 column.remove_row(row);
592 column.push_row();
595 }
596 }
597 }
598 self.uncommitted_actions
599 .push(crate::core::SheetAction::DeleteRow {
600 sheet_name: self.name.clone(),
601 index: row,
602 });
603 self.mark_all_dirty();
604 }
605
606 pub fn delete_col(&mut self, index: usize) {
610 if index < self.columns.len() {
611 self.columns.remove(index);
612 self.uncommitted_actions
613 .push(crate::core::SheetAction::DeleteCol {
614 sheet_name: self.name.clone(),
615 index,
616 });
617 self.mark_all_dirty();
618 }
619 }
620
621 pub fn insert_col(&mut self, index: usize) {
624 let row_count = self.row_count();
625 let new_col = DataColumn::new(row_count);
626 let col_count = self.columns.len();
627 if index >= col_count {
628 self.columns.push(new_col);
629 self.uncommitted_actions
630 .push(crate::core::SheetAction::InsertCol {
631 sheet_name: self.name.clone(),
632 index: col_count,
633 });
634 } else {
635 self.columns.insert(index, new_col);
636 self.uncommitted_actions
637 .push(crate::core::SheetAction::InsertCol {
638 sheet_name: self.name.clone(),
639 index,
640 });
641 }
642 self.mark_all_dirty();
643 }
644
645 pub fn columns(&self) -> &[DataColumn] {
652 &self.columns
653 }
654
655 pub fn row_count(&self) -> usize {
658 self.columns.first().map(|c| c.src.len()).unwrap_or(0)
659 }
660
661 pub fn col_count(&self) -> usize {
663 self.columns.len()
664 }
665}