quadraui 0.6.1

Cross-platform Rust UI toolkit: write one AppLogic and run it unmodified on TUI, GTK4 (Linux), macOS, and Windows backends
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
437
//! Pure-computation diff module.
//!
//! Implements a Myers LCS diff and provides [`compute_hunks`] — the
//! public entry point for building [`crate::primitives::diff_view::DiffHunk`]
//! slices from two text strings.
//!
//! This module has **no platform dependencies** and is unconditionally
//! compiled (no feature gate). The diff algorithm is ported from the
//! reference implementation in vimcode (`src/core/engine/mod.rs`).
//!
//! # Algorithm overview
//!
//! 1. Split `left` and `right` into lines.
//! 2. Run Myers diff (`lcs_diff`) → per-line status arrays `(da, db)`.
//! 3. Run `build_aligned_diff(da, db)` → paired `(aligned_a, aligned_b)`
//!    sequences of equal length where each position maps one left line to
//!    one right line (with `None` for padding rows).
//! 4. Classify each position as `Same`, `Changed`, `Removed`, or `Added`.
//! 5. Group into hunks with `CONTEXT_LINES` = 3 lines of context around
//!    each change region.

use crate::primitives::diff_view::{DiffHunk, DiffRow, DiffRowKind};

/// Number of unchanged context lines to include around each change region.
const CONTEXT_LINES: usize = 3;

// ── Internal types ────────────────────────────────────────────────────────────

/// Internal per-line diff status (mirrors vimcode's `DiffLine`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LineDiff {
    Same,
    Removed,
    Added,
}

/// Internal aligned-diff entry (mirrors vimcode's `AlignedDiffEntry`).
/// `source_line` is the 0-based index into the original line slice, or
/// `None` for a padding row.
#[derive(Debug, Clone, Copy)]
struct AlignedEntry {
    source_line: Option<usize>,
}

// ── Myers diff ────────────────────────────────────────────────────────────────

/// Myers LCS diff. Returns `(da, db)` where each element is
/// `Same / Removed / Added`.
///
/// Falls back to all-`Same` if the edit distance exceeds `MAX_EDIT_DIST`
/// (avoids pathological runtime on completely unrelated files).
fn lcs_diff<'a>(a: &[&'a str], b: &[&'a str]) -> (Vec<LineDiff>, Vec<LineDiff>) {
    let n = a.len();
    let m = b.len();
    if n == 0 && m == 0 {
        return (vec![], vec![]);
    }
    if n == 0 {
        return (vec![], vec![LineDiff::Added; m]);
    }
    if m == 0 {
        return (vec![LineDiff::Removed; n], vec![]);
    }

    const MAX_EDIT_DIST: usize = 2_000;
    let max_d = (n + m).min(MAX_EDIT_DIST);

    let offset = max_d;
    let v_size = 2 * max_d + 1;
    let mut v = vec![0usize; v_size];

    let mut trace: Vec<Vec<usize>> = Vec::with_capacity(max_d);

    let mut found_d = None;
    'outer: for d in 0..=max_d {
        trace.push(v.clone());

        for k in (-(d as isize)..=(d as isize)).step_by(2) {
            let ki = (k + offset as isize) as usize;

            let mut x = if d == 0 {
                0
            } else if k == -(d as isize) || (k != d as isize && v[ki - 1] < v[ki + 1]) {
                v[ki + 1]
            } else {
                v[ki - 1] + 1
            };

            let mut y = (x as isize - k) as usize;

            while x < n && y < m && a[x] == b[y] {
                x += 1;
                y += 1;
            }

            v[ki] = x;

            if x >= n && y >= m {
                found_d = Some(d);
                break 'outer;
            }
        }
    }

    if found_d.is_none() {
        return (vec![LineDiff::Same; n], vec![LineDiff::Same; m]);
    }
    let d = found_d.unwrap();

    #[derive(Clone, Copy)]
    enum Edit {
        Insert(usize),
        Delete(usize),
    }
    let mut edits: Vec<Edit> = Vec::with_capacity(d);
    let mut cx = n;
    let mut cy = m;

    for d_step in (1..=d).rev() {
        let v_d = &trace[d_step];
        let k = cx as isize - cy as isize;
        let ki = (k + offset as isize) as usize;

        let is_insert =
            k == -(d_step as isize) || (k != d_step as isize && v_d[ki - 1] < v_d[ki + 1]);

        let prev_k = if is_insert { k + 1 } else { k - 1 };
        let prev_ki = (prev_k + offset as isize) as usize;
        let prev_x = v_d[prev_ki];
        let prev_y = (prev_x as isize - prev_k) as usize;

        if is_insert {
            edits.push(Edit::Insert(prev_y));
        } else {
            edits.push(Edit::Delete(prev_x));
        }

        cx = prev_x;
        cy = prev_y;
    }
    edits.reverse();

    let mut da = vec![LineDiff::Same; n];
    let mut db = vec![LineDiff::Same; m];
    for edit in &edits {
        match *edit {
            Edit::Delete(x) => da[x] = LineDiff::Removed,
            Edit::Insert(y) => db[y] = LineDiff::Added,
        }
    }

    (da, db)
}

/// Align the two per-file diff-status arrays so that `Same` lines appear at
/// the same visual row. Padding entries (`source_line = None`) are inserted
/// on the shorter side of a change run.
///
/// Returns `(aligned_a, aligned_b)` — both the same length.
fn build_aligned_diff(da: &[LineDiff], db: &[LineDiff]) -> (Vec<AlignedEntry>, Vec<AlignedEntry>) {
    let mut aligned_a = Vec::new();
    let mut aligned_b = Vec::new();
    let mut i = 0;
    let mut j = 0;

    while i < da.len() || j < db.len() {
        if i < da.len() && j < db.len() && da[i] == LineDiff::Same && db[j] == LineDiff::Same {
            aligned_a.push(AlignedEntry {
                source_line: Some(i),
            });
            aligned_b.push(AlignedEntry {
                source_line: Some(j),
            });
            i += 1;
            j += 1;
            continue;
        }

        let mut removed = Vec::new();
        let mut added = Vec::new();
        while i < da.len() && da[i] != LineDiff::Same {
            removed.push(i);
            i += 1;
        }
        while j < db.len() && db[j] != LineDiff::Same {
            added.push(j);
            j += 1;
        }

        if removed.is_empty() && added.is_empty() {
            if i < da.len() {
                aligned_a.push(AlignedEntry {
                    source_line: Some(i),
                });
                aligned_b.push(AlignedEntry { source_line: None });
                i += 1;
            }
            if j < db.len() {
                aligned_a.push(AlignedEntry { source_line: None });
                aligned_b.push(AlignedEntry {
                    source_line: Some(j),
                });
                j += 1;
            }
            continue;
        }

        let max_len = removed.len().max(added.len());
        for k in 0..max_len {
            if k < removed.len() {
                aligned_a.push(AlignedEntry {
                    source_line: Some(removed[k]),
                });
            } else {
                aligned_a.push(AlignedEntry { source_line: None });
            }
            if k < added.len() {
                aligned_b.push(AlignedEntry {
                    source_line: Some(added[k]),
                });
            } else {
                aligned_b.push(AlignedEntry { source_line: None });
            }
        }
    }

    (aligned_a, aligned_b)
}

// ── Public API ────────────────────────────────────────────────────────────────

/// Compute diff hunks from two text strings.
///
/// Splits `left` and `right` on `\n`, runs the Myers LCS diff, aligns the
/// result into paired rows, classifies each row, and groups the rows into
/// hunks with [`CONTEXT_LINES`] lines of context around each change region.
///
/// Returns an empty `Vec` when the two strings are identical.
pub fn compute_hunks(left: &str, right: &str) -> Vec<DiffHunk> {
    let left_lines: Vec<&str> = left.split('\n').collect();
    let right_lines: Vec<&str> = right.split('\n').collect();

    let (da, db) = lcs_diff(&left_lines, &right_lines);
    let (aligned_a, aligned_b) = build_aligned_diff(&da, &db);

    let n = aligned_a.len();
    if n == 0 {
        return vec![];
    }

    // Build flat row list with kind classification.
    let mut rows: Vec<DiffRow> = Vec::with_capacity(n);
    for idx in 0..n {
        let la = &aligned_a[idx];
        let lb = &aligned_b[idx];
        let left_text = la.source_line.map(|li| left_lines[li].to_string());
        let right_text = lb.source_line.map(|ri| right_lines[ri].to_string());

        let kind = match (la.source_line, lb.source_line) {
            (Some(li), Some(_)) => {
                // Both sides have content. Same-Same pairs come from matching
                // lines (da[li] == LineDiff::Same). Removed-Added pairs come
                // from a change hunk and classify as Changed.
                if li < da.len() && da[li] == LineDiff::Same {
                    DiffRowKind::Same
                } else {
                    DiffRowKind::Changed
                }
            }
            (Some(_), None) => DiffRowKind::Removed,
            (None, Some(_)) => DiffRowKind::Added,
            (None, None) => DiffRowKind::Same, // degenerate padding; treat as Same
        };

        rows.push(DiffRow {
            left: left_text,
            right: right_text,
            kind,
        });
    }

    // Find all "changed" row indices.
    let change_indices: Vec<usize> = rows
        .iter()
        .enumerate()
        .filter(|(_, r)| r.kind != DiffRowKind::Same)
        .map(|(i, _)| i)
        .collect();

    if change_indices.is_empty() {
        return vec![];
    }

    // Expand each change index into a context interval [start, end].
    let mut intervals: Vec<(usize, usize)> = change_indices
        .iter()
        .map(|&i| {
            let start = i.saturating_sub(CONTEXT_LINES);
            let end = (i + CONTEXT_LINES).min(n - 1);
            (start, end)
        })
        .collect();

    // Merge overlapping intervals.
    intervals.sort_unstable_by_key(|&(s, _)| s);
    let mut merged: Vec<(usize, usize)> = Vec::new();
    for (start, end) in intervals {
        if let Some(last) = merged.last_mut() {
            if start <= last.1 + 1 {
                last.1 = last.1.max(end);
                continue;
            }
        }
        merged.push((start, end));
    }

    // Build one DiffHunk per merged interval.
    merged
        .into_iter()
        .map(|(start, end)| {
            let hunk_rows = rows[start..=end].to_vec();

            // 1-based left_start: first row with left content.
            let left_start = hunk_rows
                .iter()
                .find_map(|r| r.left.as_ref().map(|_| ()))
                .map(|_| rows[..start].iter().filter(|r| r.left.is_some()).count() + 1)
                .unwrap_or(1);

            // 1-based right_start: first row with right content.
            let right_start = hunk_rows
                .iter()
                .find_map(|r| r.right.as_ref().map(|_| ()))
                .map(|_| rows[..start].iter().filter(|r| r.right.is_some()).count() + 1)
                .unwrap_or(1);

            DiffHunk {
                left_start,
                right_start,
                rows: hunk_rows,
            }
        })
        .collect()
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compute_hunks_identical() {
        let text = "line one\nline two\nline three";
        let hunks = compute_hunks(text, text);
        assert!(
            hunks.is_empty(),
            "identical texts should produce no hunks, got {hunks:?}"
        );
    }

    #[test]
    fn test_compute_hunks_both_empty() {
        let hunks = compute_hunks("", "");
        assert!(hunks.is_empty(), "both-empty should produce no hunks");
    }

    #[test]
    fn test_compute_hunks_add_only() {
        // Left is truly empty (no lines), right has 3 lines.
        // lcs_diff handles n=0 specially — all right lines are Added.
        let right = "line one\nline two\nline three";
        let left_lines: Vec<&str> = vec![];
        let right_lines: Vec<&str> = right.split('\n').collect();
        let (da, db) = lcs_diff(&left_lines, &right_lines);
        // da is empty (no left lines), all db entries are Added.
        assert!(da.is_empty());
        assert!(db.iter().all(|d| *d == LineDiff::Added));

        // compute_hunks with left="" splits into [""], giving one empty left
        // line, but right has 3 different lines — all rows should be non-Same.
        let hunks = compute_hunks("", right);
        assert!(
            !hunks.is_empty(),
            "add-only diff should produce at least one hunk"
        );
        let has_change = hunks
            .iter()
            .any(|h| h.rows.iter().any(|r| r.kind != DiffRowKind::Same));
        assert!(
            has_change,
            "add-only diff should have at least one non-Same row"
        );
    }

    #[test]
    fn test_compute_hunks_remove_only() {
        // Left has 3 lines, right is truly empty.
        let left = "line one\nline two\nline three";
        let left_lines: Vec<&str> = left.split('\n').collect();
        let right_lines: Vec<&str> = vec![];
        let (da, db) = lcs_diff(&left_lines, &right_lines);
        // All left lines are Removed, db is empty.
        assert!(da.iter().all(|d| *d == LineDiff::Removed));
        assert!(db.is_empty());

        // compute_hunks with right="" produces similar all-removal result.
        let hunks = compute_hunks(left, "");
        assert!(
            !hunks.is_empty(),
            "remove-only diff should produce at least one hunk"
        );
        let has_change = hunks
            .iter()
            .any(|h| h.rows.iter().any(|r| r.kind != DiffRowKind::Same));
        assert!(
            has_change,
            "remove-only diff should have at least one non-Same row"
        );
    }

    #[test]
    fn test_compute_hunks_mixed() {
        let left = "fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n\nfn main() {\n    println!(\"{}\", add(3, 4));\n}";
        let right = "fn add(a: i32, b: i32) -> i64 {\n    (a + b) as i64\n}\n\nfn subtract(a: i32, b: i32) -> i32 {\n    a - b\n}\n\nfn main() {\n    println!(\"{}\", add(3, 4));\n    println!(\"{}\", subtract(10, 3));\n}";
        let hunks = compute_hunks(left, right);
        assert!(
            !hunks.is_empty(),
            "mixed diff should produce at least one hunk"
        );
        // There should be at least one Changed or Added row somewhere.
        let has_change = hunks
            .iter()
            .any(|h| h.rows.iter().any(|r| r.kind != DiffRowKind::Same));
        assert!(has_change, "mixed diff should contain non-Same rows");
    }
}