text-document-search 1.3.0

Find and replace use cases for text-document
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Generated by Qleany v1.4.8 from feature_use_case.tera
use super::search_helpers::{build_full_text_from_blocks, find_all_matches};
use crate::ReplaceResultDto;
use crate::ReplaceTextDto;
use anyhow::{Result, anyhow};
use common::database::CommandUnitOfWork;
use common::direct_access::block::block_repository::BlockRelationshipField;
use common::direct_access::document::document_repository::DocumentRelationshipField;
use common::direct_access::frame::frame_repository::FrameRelationshipField;
use common::direct_access::root::root_repository::RootRelationshipField;
#[allow(unused_imports)]
use common::entities::{Block, Document, Frame, InlineContent, InlineElement, Root};
use common::snapshot::EntityTreeSnapshot;
use common::types::{EntityId, ROOT_ENTITY_ID};
use common::undo_redo::UndoRedoCommand;
use std::any::Any;

pub trait ReplaceTextUnitOfWorkFactoryTrait: Send + Sync {
    fn create(&self) -> Box<dyn ReplaceTextUnitOfWorkTrait>;
}

#[macros::uow_action(entity = "Root", action = "Get")]
#[macros::uow_action(entity = "Root", action = "GetRelationship")]
#[macros::uow_action(entity = "Document", action = "Get")]
#[macros::uow_action(entity = "Document", action = "Update")]
#[macros::uow_action(entity = "Document", action = "GetRelationship")]
#[macros::uow_action(entity = "Document", action = "Snapshot")]
#[macros::uow_action(entity = "Document", action = "Restore")]
#[macros::uow_action(entity = "Frame", action = "Get")]
#[macros::uow_action(entity = "Frame", action = "GetRelationship")]
#[macros::uow_action(entity = "Block", action = "Get")]
#[macros::uow_action(entity = "Block", action = "GetMulti")]
#[macros::uow_action(entity = "Block", action = "Update")]
#[macros::uow_action(entity = "Block", action = "UpdateMulti")]
#[macros::uow_action(entity = "Block", action = "GetRelationship")]
#[macros::uow_action(entity = "InlineElement", action = "Get")]
#[macros::uow_action(entity = "InlineElement", action = "GetMulti")]
#[macros::uow_action(entity = "InlineElement", action = "Update")]
pub trait ReplaceTextUnitOfWorkTrait: CommandUnitOfWork {}

/// Fetch all blocks from the document via the UoW, sort them, and build the full text.
/// Returns the full text string and the sorted blocks (needed by replace logic).
fn fetch_blocks_and_build_text(
    uow: &dyn ReplaceTextUnitOfWorkTrait,
) -> Result<(String, Vec<Block>)> {
    let root = uow
        .get_root(&ROOT_ENTITY_ID)?
        .ok_or_else(|| anyhow!("Root entity not found"))?;

    let doc_ids = uow.get_root_relationship(&root.id, &RootRelationshipField::Document)?;
    let doc_id = *doc_ids
        .first()
        .ok_or_else(|| anyhow!("Root has no document"))?;

    let frame_ids = uow.get_document_relationship(&doc_id, &DocumentRelationshipField::Frames)?;

    let mut all_block_ids: Vec<EntityId> = Vec::new();
    for frame_id in &frame_ids {
        let block_ids = uow.get_frame_relationship(frame_id, &FrameRelationshipField::Blocks)?;
        all_block_ids.extend(block_ids);
    }

    let blocks_opt = uow.get_block_multi(&all_block_ids)?;
    let mut blocks: Vec<Block> = blocks_opt.into_iter().flatten().collect();
    blocks.sort_by_key(|b| b.document_position);

    let full_text = build_full_text_from_blocks(&blocks);

    Ok((full_text, blocks))
}

/// Find the block containing the given document position from a sorted list of blocks.
/// Returns (block_index, offset_within_block).
fn find_block_for_position(blocks: &[Block], position: usize) -> Option<(usize, usize)> {
    for (i, block) in blocks.iter().enumerate() {
        let block_start = block.document_position as usize;
        let block_end = block_start + block.text_length as usize;
        if position >= block_start && position < block_end {
            let offset = position - block_start;
            return Some((i, offset));
        }
    }
    None
}

/// Check if a match is entirely within a single block.
/// Returns Some((block_index, offset_in_block)) if so, None if it spans blocks or newlines.
fn match_in_single_block(
    blocks: &[Block],
    match_pos: usize,
    match_len: usize,
) -> Option<(usize, usize)> {
    // In the full text, blocks are joined by '\n'. We need to check that
    // match_pos..match_pos+match_len falls entirely within one block's text
    // (not crossing a '\n' separator).
    if let Some((block_idx, offset)) = find_block_for_position(blocks, match_pos) {
        let block = &blocks[block_idx];
        let block_end_offset = block.text_length as usize;
        // Check the match fits entirely within this block
        if offset + match_len <= block_end_offset {
            return Some((block_idx, offset));
        }
    }
    None
}

fn execute_replace(
    uow: &mut Box<dyn ReplaceTextUnitOfWorkTrait>,
    dto: &ReplaceTextDto,
) -> Result<(ReplaceResultDto, EntityTreeSnapshot)> {
    // Get Root -> Document
    let root = uow
        .get_root(&ROOT_ENTITY_ID)?
        .ok_or_else(|| anyhow!("Root entity not found"))?;
    let doc_ids = uow.get_root_relationship(&root.id, &RootRelationshipField::Document)?;
    let doc_id = *doc_ids
        .first()
        .ok_or_else(|| anyhow!("Root has no document"))?;

    // Snapshot for undo before mutation
    let snapshot = uow.snapshot_document(&[doc_id])?;

    // Build full text and get blocks (called once)
    let (full_text, blocks) = fetch_blocks_and_build_text(uow.as_ref())?;

    // Find all matches
    let all_matches = find_all_matches(
        &full_text,
        &dto.query,
        dto.case_sensitive,
        dto.whole_word,
        dto.use_regex,
    )?;

    if all_matches.is_empty() {
        return Ok((
            ReplaceResultDto {
                replacements_count: 0,
                skipped_cross_block: 0,
            },
            snapshot,
        ));
    }

    // Filter to only matches within a single block (skip cross-block matches)
    let mut valid_matches: Vec<(usize, usize, usize, usize)> = Vec::new(); // (match_pos, match_len, block_idx, block_offset)
    let mut skipped_cross_block: i64 = 0;
    for &(match_pos, match_len) in &all_matches {
        if let Some((block_idx, block_offset)) =
            match_in_single_block(&blocks, match_pos, match_len)
        {
            valid_matches.push((match_pos, match_len, block_idx, block_offset));
        } else {
            skipped_cross_block += 1;
        }
    }

    // If not replace_all, only keep the first match
    if !dto.replace_all {
        valid_matches.truncate(1);
    }

    if valid_matches.is_empty() {
        return Ok((
            ReplaceResultDto {
                replacements_count: 0,
                skipped_cross_block,
            },
            snapshot,
        ));
    }

    let replacement = &dto.replacement;
    let replacement_char_len = replacement.chars().count() as i64;
    let replacements_count = valid_matches.len() as i64;

    // Process matches from last to first to avoid position shifting issues
    // We need to track cumulative delta per block for subsequent block position updates
    let mut cumulative_delta: i64 = 0;

    // Process in reverse order
    for &(_match_pos, match_len, block_idx, block_offset) in valid_matches.iter().rev() {
        let match_char_len = match_len as i64;
        let delta = replacement_char_len - match_char_len;

        // Re-read the block (it may have been updated by a previous iteration in the same block)
        let block = uow
            .get_block(&blocks[block_idx].id)?
            .ok_or_else(|| anyhow!("Block not found"))?;

        // Get elements for this block
        let element_ids =
            uow.get_block_relationship(&block.id, &BlockRelationshipField::Elements)?;
        let elements_opt = uow.get_inline_element_multi(&element_ids)?;
        let elements: Vec<InlineElement> = elements_opt.into_iter().flatten().collect();

        // Replace within inline elements
        // Walk elements to find the one containing the match
        let mut running: usize = 0;
        let mut replaced = false;
        for elem in &elements {
            let elem_len = match &elem.content {
                InlineContent::Text(s) => s.chars().count(),
                InlineContent::Image { .. } => 1,
                InlineContent::Empty => 0,
            };
            let elem_start = running;
            let elem_end = running + elem_len;

            // Check if this element contains (or overlaps with) the match range
            let match_start_in_block = block_offset;
            let match_end_in_block = block_offset + match_len;

            let overlap_start = std::cmp::max(match_start_in_block, elem_start);
            let overlap_end = std::cmp::min(match_end_in_block, elem_end);

            if overlap_start < overlap_end && !replaced {
                if let InlineContent::Text(s) = &elem.content {
                    let chars: Vec<char> = s.chars().collect();
                    let local_start = overlap_start - elem_start;
                    let local_end = overlap_end - elem_start;

                    // Check if the entire match is within this element
                    if match_start_in_block >= elem_start && match_end_in_block <= elem_end {
                        // Entire match is in this element: replace
                        let before: String = chars[..local_start].iter().collect();
                        let after: String = chars[local_end..].iter().collect();
                        let new_text = format!("{}{}{}", before, replacement, after);

                        let mut updated = elem.clone();
                        updated.content = InlineContent::Text(new_text);
                        updated.updated_at = chrono::Utc::now();
                        uow.update_inline_element(&updated)?;
                        replaced = true;
                    } else {
                        // Match spans multiple elements. Put replacement in first overlapping,
                        // clear overlapping parts from subsequent elements.
                        let before: String = chars[..local_start].iter().collect();
                        let after: String = chars[local_end..].iter().collect();
                        let new_text = format!("{}{}{}", before, replacement, after);

                        let mut updated = elem.clone();
                        updated.content = InlineContent::Text(new_text);
                        updated.updated_at = chrono::Utc::now();
                        uow.update_inline_element(&updated)?;
                        replaced = true;
                    }
                }
            } else if overlap_start < overlap_end && replaced {
                // This element overlaps with the match but replacement was already placed.
                // Clear the overlapping portion.
                if let InlineContent::Text(s) = &elem.content {
                    let chars: Vec<char> = s.chars().collect();
                    let local_start = overlap_start - elem_start;
                    let local_end = overlap_end - elem_start;
                    let before: String = chars[..local_start].iter().collect();
                    let after: String = chars[local_end..].iter().collect();
                    let new_text = format!("{}{}", before, after);

                    let mut updated = elem.clone();
                    updated.content = InlineContent::Text(new_text);
                    updated.updated_at = chrono::Utc::now();
                    uow.update_inline_element(&updated)?;
                }
            }

            running += elem_len;
        }

        // Update block cached fields
        let plain_chars: Vec<char> = block.plain_text.chars().collect();
        let before: String = plain_chars[..block_offset].iter().collect();
        let after: String = plain_chars[(block_offset + match_len)..].iter().collect();
        let new_plain = format!("{}{}{}", before, replacement, after);

        let mut updated_block = block.clone();
        updated_block.plain_text = new_plain.clone();
        updated_block.text_length = new_plain.chars().count() as i64;
        updated_block.updated_at = chrono::Utc::now();
        uow.update_block(&updated_block)?;

        cumulative_delta += delta;
    }

    // Now update subsequent blocks' document_position and Document.character_count.
    // We need to compute delta per block group. Since we processed in reverse,
    // we need to compute cumulative deltas per block and shift subsequent blocks.
    //
    // Simpler approach: compute total delta and use block positions.
    // For each block, count how many matches occurred BEFORE it (by document position)
    // and compute the cumulative shift.

    // Re-read all blocks to get current state
    let mut all_block_ids: Vec<EntityId> = Vec::new();
    let frame_ids = uow.get_document_relationship(&doc_id, &DocumentRelationshipField::Frames)?;
    for frame_id in &frame_ids {
        let block_ids = uow.get_frame_relationship(frame_id, &FrameRelationshipField::Blocks)?;
        all_block_ids.extend(block_ids);
    }
    let blocks_opt = uow.get_block_multi(&all_block_ids)?;
    let mut current_blocks: Vec<Block> = blocks_opt.into_iter().flatten().collect();
    current_blocks.sort_by_key(|b| b.document_position);

    // Compute position adjustments: for each block, determine how much to shift
    // based on matches that occurred in earlier blocks.
    // valid_matches is sorted by match_pos (ascending from find_all_matches).
    // Group matches by block_idx and compute delta per block.
    let mut delta_by_block: std::collections::HashMap<usize, i64> =
        std::collections::HashMap::new();
    for &(_match_pos, match_len, block_idx, _block_offset) in &valid_matches {
        let delta = replacement_char_len - match_len as i64;
        *delta_by_block.entry(block_idx).or_insert(0) += delta;
    }

    // For each block in sorted order, compute cumulative shift from all blocks before it
    let mut cumulative_shift: i64 = 0;
    let mut blocks_to_update: Vec<Block> = Vec::new();
    for block in current_blocks.iter() {
        // Find original block index mapping: blocks[i].id == current_blocks[i].id
        // since both are sorted by document_position and IDs haven't changed.
        // Find the original index for this block by matching id
        let orig_idx = blocks.iter().position(|b| b.id == block.id);

        if let Some(oidx) = orig_idx {
            // Add delta from this block to cumulative shift for subsequent blocks
            if let Some(&d) = delta_by_block.get(&oidx) {
                // This block itself was modified; its text_length already reflects changes.
                // But its document_position needs adjustment based on prior cumulative shift.
                if cumulative_shift != 0 {
                    let mut ub = block.clone();
                    ub.document_position += cumulative_shift;
                    ub.updated_at = chrono::Utc::now();
                    blocks_to_update.push(ub);
                }
                cumulative_shift += d;
            } else {
                // No matches in this block, just shift if needed
                if cumulative_shift != 0 {
                    let mut ub = block.clone();
                    ub.document_position += cumulative_shift;
                    ub.updated_at = chrono::Utc::now();
                    blocks_to_update.push(ub);
                }
            }
        } else if cumulative_shift != 0 {
            let mut ub = block.clone();
            ub.document_position += cumulative_shift;
            ub.updated_at = chrono::Utc::now();
            blocks_to_update.push(ub);
        }
    }

    if !blocks_to_update.is_empty() {
        uow.update_block_multi(&blocks_to_update)?;
    }

    // Update Document.character_count
    let total_delta = cumulative_delta;
    let mut document = uow
        .get_document(&doc_id)?
        .ok_or_else(|| anyhow!("Document not found"))?;
    document.character_count += total_delta;
    document.updated_at = chrono::Utc::now();
    uow.update_document(&document)?;

    Ok((
        ReplaceResultDto {
            replacements_count,
            skipped_cross_block,
        },
        snapshot,
    ))
}

pub struct ReplaceTextUseCase {
    uow_factory: Box<dyn ReplaceTextUnitOfWorkFactoryTrait>,
    undo_snapshot: Option<EntityTreeSnapshot>,
    last_dto: Option<ReplaceTextDto>,
}

impl ReplaceTextUseCase {
    pub fn new(uow_factory: Box<dyn ReplaceTextUnitOfWorkFactoryTrait>) -> Self {
        ReplaceTextUseCase {
            uow_factory,
            undo_snapshot: None,
            last_dto: None,
        }
    }

    pub fn execute(&mut self, dto: &ReplaceTextDto) -> Result<ReplaceResultDto> {
        let mut uow = self.uow_factory.create();
        uow.begin_transaction()?;

        let (result, snapshot) = execute_replace(&mut uow, dto)?;
        self.undo_snapshot = Some(snapshot);
        self.last_dto = Some(dto.clone());

        uow.commit()?;
        Ok(result)
    }
}

impl UndoRedoCommand for ReplaceTextUseCase {
    fn undo(&mut self) -> Result<()> {
        let snapshot = self
            .undo_snapshot
            .as_ref()
            .ok_or_else(|| anyhow!("No snapshot available for undo"))?
            .clone();

        let mut uow = self.uow_factory.create();
        uow.begin_transaction()?;
        uow.restore_document(&snapshot)?;
        uow.commit()?;
        Ok(())
    }

    fn redo(&mut self) -> Result<()> {
        let dto = self
            .last_dto
            .as_ref()
            .ok_or_else(|| anyhow!("No DTO available for redo"))?
            .clone();

        let mut uow = self.uow_factory.create();
        uow.begin_transaction()?;
        let (_, snapshot) = execute_replace(&mut uow, &dto)?;
        self.undo_snapshot = Some(snapshot);
        uow.commit()?;
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}