diffs-rs 0.1.1

Fast terminal diff viewer and embeddable Rust diff engine
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use std::{collections::BTreeMap, num::NonZeroU32, ops::Range};

use anyhow::{Context, Result};

use crate::{
    model::{Row, RowKind},
    source::{CheckedPair, Gap},
    storage::{Document, FileIndex},
};

mod reading;
pub(crate) use reading::SourceOrder;

/// A stable source destination, independent of layout and context expansion.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "delivery-trace", derive(serde::Serialize))]
#[cfg_attr(feature = "delivery-trace", serde(untagged))]
pub enum LineId {
    Patch(usize),
    Context {
        file: usize,
        old: NonZeroU32,
        new: NonZeroU32,
    },
}

impl From<usize> for LineId {
    fn from(id: usize) -> Self {
        Self::Patch(id)
    }
}

impl LineId {
    /// Checks positive coordinates; source correspondence is checked separately.
    pub fn context(file: usize, old: u32, new: u32) -> Option<Self> {
        Some(Self::Context {
            file,
            old: NonZeroU32::new(old)?,
            new: NonZeroU32::new(new)?,
        })
    }

    pub(crate) fn patch(self) -> Option<usize> {
        match self {
            Self::Patch(id) => Some(id),
            Self::Context { .. } => None,
        }
    }

    pub(crate) fn belongs_to(self, file: &crate::storage::FileIndex) -> bool {
        match self {
            Self::Patch(id) => (file.source_base..file.source_end()).contains(&id),
            Self::Context { file: id, .. } => id == file.id,
        }
    }
}

#[derive(Clone, Copy)]
enum Part {
    Patch,
    Hidden(usize),
    Context { gap: usize, offset: u32 },
    NoNewline,
}

struct Segment {
    start: usize,
    patch: usize,
    part: Part,
}

#[derive(Default)]
struct Rows {
    segments: Vec<Segment>,
    len: usize,
    hunks: Vec<usize>,
}

impl Rows {
    fn push(&mut self, patch: usize, len: usize, part: Part) -> Result<()> {
        if len != 0 {
            self.segments.push(Segment {
                start: self.len,
                patch,
                part,
            });
            self.len = self
                .len
                .checked_add(len)
                .context("Expanded row count overflow")?;
        }
        Ok(())
    }

    fn segment(&self, row: usize) -> Option<&Segment> {
        if row >= self.len {
            return None;
        }
        self.segments.get(
            self.segments
                .partition_point(|segment| segment.start <= row)
                .checked_sub(1)?,
        )
    }

    fn patch_row(&self, row: usize) -> usize {
        let index = self
            .segments
            .partition_point(|segment| segment.patch <= row)
            .saturating_sub(1);
        let segment = &self.segments[index];
        segment.start
            + if matches!(segment.part, Part::Patch) {
                row - segment.patch
            } else {
                0
            }
    }
}

struct Expansion {
    source: Gap,
    revealed: u32,
}

impl Expansion {
    fn leading(&self) -> bool {
        self.source.before == Some(0)
    }
    fn exposed(&self) -> Range<u32> {
        if self.leading() {
            self.source.len - self.revealed..self.source.len
        } else {
            0..self.revealed
        }
    }
    fn line(&self, file: usize, offset: u32) -> LineId {
        LineId::context(
            file,
            self.source.old + offset + 1,
            self.source.new + offset + 1,
        )
        .expect("Validated source line is positive")
    }
}

struct FileView {
    gaps: Vec<Expansion>,
    rows: [Rows; 2],
}

impl FileView {
    fn insertion(file: &FileIndex, gap: &Gap, split: bool) -> (usize, usize) {
        let end = gap
            .before
            .map_or_else(|| file.rows(split).len(), |h| file.hunk_row(h, split));
        let replaces = end.checked_sub(1).is_some_and(|row| {
            file.rows(split)
                .get(row)
                .is_some_and(|row| row.kind == RowKind::Gap)
        });
        (end - usize::from(replaces), end)
    }

    fn build(&self, file: &FileIndex) -> Result<[Rows; 2]> {
        let mut layouts = [Rows::default(), Rows::default()];
        for (layout, split) in [false, true].into_iter().enumerate() {
            let rows = &mut layouts[layout];
            let mut next = 0;
            for (index, gap) in self.gaps.iter().enumerate() {
                let (start, end) = Self::insertion(file, &gap.source, split);
                rows.push(
                    next,
                    start.checked_sub(next).context("Context gaps overlap")?,
                    Part::Patch,
                )?;
                let hidden = gap.source.len - gap.revealed;
                if gap.leading() && hidden != 0 {
                    rows.push(start, 1, Part::Hidden(index))?;
                }
                rows.push(
                    start,
                    gap.revealed as usize,
                    Part::Context {
                        gap: index,
                        offset: gap.exposed().start,
                    },
                )?;
                if !gap.leading() && hidden != 0 {
                    rows.push(start, 1, Part::Hidden(index))?;
                }
                if gap.source.no_newline && hidden == 0 {
                    rows.push(start, 1, Part::NoNewline)?;
                }
                next = end;
            }
            rows.push(next, file.rows(split).len() - next, Part::Patch)?;
            rows.hunks = (0..file.hunks.len())
                .map(|h| rows.patch_row(file.hunk_row(h, split)))
                .collect();
        }
        Ok(layouts)
    }
}

#[derive(Default)]
pub(crate) struct View {
    files: BTreeMap<usize, FileView>,
    extra: Vec<[usize; 2]>,
    source_revision: reading::Revision,
}

impl View {
    #[cfg(feature = "owner-census")]
    pub(crate) fn census(&self) -> serde_json::Value {
        let mut ranges = 0;
        let mut exposed = 0u64;
        let mut segments = 0;
        let mut allocations = self.extra.capacity() * size_of::<[usize; 2]>();
        for file in self.files.values() {
            ranges += file.gaps.len();
            exposed += file
                .gaps
                .iter()
                .map(|gap| u64::from(gap.revealed))
                .sum::<u64>();
            allocations += file.gaps.capacity() * size_of::<Expansion>();
            for rows in &file.rows {
                segments += rows.segments.len();
                allocations += rows.segments.capacity() * size_of::<Segment>()
                    + rows.hunks.capacity() * size_of::<usize>();
            }
        }
        serde_json::json!({
            "files": self.files.len(), "gap_ranges": ranges, "exposed_lines": exposed,
            "layout_segments": segments, "vector_capacity_bytes": allocations,
            "inline_file_values_bytes": self.files.len() * size_of::<FileView>(),
            "scope": "View vectors and occupied file values only; excludes map-node overhead, source descriptors, caches, and allocator overhead",
        })
    }

    pub(crate) fn row(&self, doc: &Document, row: usize, split: bool) -> Option<Row<LineId>> {
        let file = self.file_at(doc, row, split)?;
        let local = row - self.file_row(doc, file.id, split);
        let Some(view) = self.files.get(&file.id) else {
            return file
                .rows(split)
                .get(local)
                .map(|row| row.map(LineId::Patch));
        };
        let segment = view.rows[usize::from(split)].segment(local)?;
        let kind = match segment.part {
            Part::Patch => {
                return file
                    .rows(split)
                    .get(segment.patch + local - segment.start)
                    .map(|row| row.map(LineId::Patch));
            }
            Part::Hidden(_) => RowKind::Gap,
            Part::Context { gap, offset } => {
                let id = view.gaps[gap].line(file.id, offset + (local - segment.start) as u32);
                RowKind::Code {
                    left: Some(id),
                    right: Some(id),
                }
            }
            Part::NoNewline => RowKind::NoNewline {
                left: true,
                right: true,
            },
        };
        Some(Row {
            file: file.id,
            kind,
        })
    }

    pub(crate) fn len(&self, doc: &Document, split: bool) -> usize {
        doc.row_len(split)
            + self
                .extra
                .last()
                .map_or(0, |extra| extra[usize::from(split)])
    }

    pub(crate) fn source_row(&self, doc: &Document, id: LineId, split: bool) -> Option<usize> {
        match id {
            LineId::Patch(id) => Some(self.patch_row(doc, doc.row_for_source(id, split), split)),
            LineId::Context { file, old, new } => {
                let (old, new) = (old.get(), new.get());
                let view = self.files.get(&file)?;
                let gap = view
                    .gaps
                    .partition_point(|gap| gap.source.old < old)
                    .checked_sub(1)?;
                let source = &view.gaps[gap];
                let offset = old - source.source.old - 1;
                if !source.exposed().contains(&offset)
                    || new.checked_sub(source.source.new) != Some(offset + 1)
                {
                    return None;
                }
                let (slot, _) = FileView::insertion(&doc.files[file], &source.source, split);
                let segments = &view.rows[usize::from(split)].segments;
                let first = segments.partition_point(|segment| segment.patch < slot);
                let segment = segments[first..].iter().take_while(|segment| segment.patch == slot).find(|segment|
                    matches!(segment.part, Part::Context { gap: index, .. } if index == gap))?;
                Some(
                    self.file_row(doc, file, split)
                        + segment.start
                        + (offset - source.exposed().start) as usize,
                )
            }
        }
    }

    pub(crate) fn file_row(&self, doc: &Document, file: usize, split: bool) -> usize {
        doc.files[file].row_base(split)
            + file
                .checked_sub(1)
                .map_or(0, |index| self.extra_at(index, split))
    }

    pub(crate) fn boundary(
        &self,
        doc: &Document,
        row: usize,
        split: bool,
        forward: bool,
    ) -> Option<usize> {
        if self.files.is_empty() {
            return doc.boundary(row, split, forward, false);
        }
        let first = self.file_at(doc, row, split)?.id;
        let mut index = first;
        loop {
            let file = &doc.files[index];
            let base = self.file_row(doc, index, split);
            let found = if let Some(view) = self.files.get(&index) {
                let hunks = &view.rows[usize::from(split)].hunks;
                if index != first {
                    if forward {
                        hunks.first().copied()
                    } else {
                        hunks.last().copied()
                    }
                } else if forward {
                    hunks
                        .get(hunks.partition_point(|h| base + *h <= row))
                        .copied()
                } else {
                    hunks
                        .partition_point(|h| base + *h < row)
                        .checked_sub(1)
                        .map(|i| hunks[i])
                }
            } else if index == first {
                file.rows(split).boundary(row - base, forward, false)
            } else {
                file.rows(split).first_boundary(forward, false)
            };
            if let Some(local) = found {
                return Some(base + local);
            }
            index = if forward {
                index.checked_add(1).filter(|i| *i < doc.files.len())?
            } else {
                index.checked_sub(1)?
            };
        }
    }

    fn extra_at(&self, file: usize, split: bool) -> usize {
        self.extra
            .get(file)
            .or_else(|| self.extra.last())
            .map_or(0, |extra| extra[usize::from(split)])
    }

    fn file_at<'a>(&self, doc: &'a Document, row: usize, split: bool) -> Option<&'a FileIndex> {
        doc.files
            .get(
                doc.files.partition_point(|file| {
                    file.row_end(split) + self.extra_at(file.id, split) <= row
                }),
            )
            .map(|f| f.as_ref())
    }

    pub(crate) fn patch_row(&self, doc: &Document, row: usize, split: bool) -> usize {
        let Some(file) = doc
            .files
            .get(doc.files.partition_point(|f| f.row_end(split) <= row))
        else {
            return row;
        };
        let local = row - file.row_base(split);
        self.file_row(doc, file.id, split)
            + self
                .files
                .get(&file.id)
                .map_or(local, |view| view.rows[usize::from(split)].patch_row(local))
    }

    pub(crate) fn original_row(&self, doc: &Document, row: usize, split: bool) -> usize {
        let Some(file) = self.file_at(doc, row, split) else {
            return row;
        };
        let local = row - self.file_row(doc, file.id, split);
        let original = self
            .files
            .get(&file.id)
            .and_then(|view| view.rows[usize::from(split)].segment(local))
            .map_or(local, |segment| {
                segment.patch
                    + if matches!(segment.part, Part::Patch) {
                        local - segment.start
                    } else {
                        0
                    }
            });
        file.row_base(split) + original.min(file.rows(split).len().saturating_sub(1))
    }

    pub(crate) fn set(
        &mut self,
        doc: &Document,
        file: usize,
        pair: Option<&CheckedPair>,
    ) -> Result<()> {
        let view = pair
            .filter(|pair| !pair.gaps.is_empty())
            .map(|pair| {
                let mut view = FileView {
                    gaps: pair
                        .gaps
                        .iter()
                        .map(|gap| Expansion {
                            source: *gap,
                            revealed: 0,
                        })
                        .collect(),
                    rows: [Rows::default(), Rows::default()],
                };
                view.rows = view.build(&doc.files[file])?;
                Ok::<_, anyhow::Error>(view)
            })
            .transpose()?;
        self.install(doc, file, view)?;
        self.source_revision.invalidate();
        Ok(())
    }

    fn install(&mut self, doc: &Document, file: usize, view: Option<FileView>) -> Result<()> {
        let old: [usize; 2] = std::array::from_fn(|layout| {
            self.files.get(&file).map_or(0, |view| {
                view.rows[layout].len - doc.files[file].rows(layout == 1).len()
            })
        });
        let new: [usize; 2] = std::array::from_fn(|layout| {
            view.as_ref().map_or(0, |view| {
                view.rows[layout].len - doc.files[file].rows(layout == 1).len()
            })
        });
        if self.extra.is_empty() && view.is_none() {
            return Ok(());
        }
        self.extra.resize(
            doc.files.len(),
            self.extra.last().copied().unwrap_or([0; 2]),
        );
        for layout in 0..2 {
            doc.row_len(layout == 1)
                .checked_add(self.extra.last().map_or(0, |extra| extra[layout]) - old[layout])
                .and_then(|n| n.checked_add(new[layout]))
                .context("Expanded document row count overflow")?;
        }
        for extra in &mut self.extra[file..] {
            for layout in 0..2 {
                extra[layout] = extra[layout] - old[layout] + new[layout];
            }
        }
        if let Some(view) = view {
            self.files.insert(file, view);
        } else {
            self.files.remove(&file);
        }
        Ok(())
    }

    pub(crate) fn gap(
        &self,
        doc: &Document,
        row: usize,
        split: bool,
    ) -> Option<(usize, usize, u32)> {
        let file = self.file_at(doc, row, split)?;
        let view = self.files.get(&file.id)?;
        let segment =
            view.rows[usize::from(split)].segment(row - self.file_row(doc, file.id, split))?;
        let Part::Hidden(gap) = segment.part else {
            return None;
        };
        Some((
            file.id,
            gap,
            view.gaps[gap].source.len - view.gaps[gap].revealed,
        ))
    }

    pub(crate) fn expand(&mut self, doc: &Document, file: usize, gap: usize) -> Result<LineId> {
        let view = self
            .files
            .get_mut(&file)
            .context("Context gap is unavailable")?;
        let item = view
            .gaps
            .get_mut(gap)
            .context("Context gap is outside the file")?;
        let before = item.revealed;
        item.revealed += 20.min(item.source.len - item.revealed);
        let offset = if item.leading() {
            item.exposed().start
        } else {
            item.revealed - 1
        };
        let selected = item.line(file, offset);
        let built = view.build(&doc.files[file]);
        let rows = match built {
            Ok(rows) => rows,
            Err(error) => {
                view.gaps[gap].revealed = before;
                return Err(error);
            }
        };
        let old: [usize; 2] = std::array::from_fn(|layout| view.rows[layout].len);
        let growth: [usize; 2] = std::array::from_fn(|layout| rows[layout].len - old[layout]);
        for layout in 0..2 {
            if doc
                .row_len(layout == 1)
                .checked_add(self.extra.last().map_or(0, |e| e[layout]))
                .and_then(|n| n.checked_add(growth[layout]))
                .is_none()
            {
                view.gaps[gap].revealed = before;
                anyhow::bail!("Expanded document row count overflow");
            }
        }
        view.rows = rows;
        self.extra.resize(
            doc.files.len(),
            self.extra.last().copied().unwrap_or([0; 2]),
        );
        for extra in &mut self.extra[file..] {
            for layout in 0..2 {
                extra[layout] += growth[layout];
            }
        }
        self.source_revision.invalidate();
        Ok(selected)
    }
}