Skip to main content

expand

Function expand 

Source
pub fn expand(
    ops: &[Op],
    a: &[Range<usize>],
    b: &[Range<usize>],
    inner: impl FnMut(Range<usize>, Range<usize>) -> Vec<Op>,
) -> Vec<Op>
Expand description

Expands a script over groups (e.g. lines) into one over their items (e.g. tokens), for coarse-to-fine diffing (SPEC §5.6). a and b give each group’s contiguous range of items.

Equal and moved groups pair their items in order (leftovers are deleted or inserted); deleted and inserted groups expand item by item; each replaced run of groups is handed to inner as item ranges, and its script (relative to those ranges) is spliced in.

use fastanim_diff::{Differ, Op, expand};

let (a, b) = (["x", "y", "z"], ["x", "w", "z"]);
let lines_a = [0..2, 2..3]; // "x y", "z"
let lines_b = [0..2, 2..3]; // "x w", "z"
let ka: Vec<_> = lines_a.iter().map(|l| &a[l.clone()]).collect();
let kb: Vec<_> = lines_b.iter().map(|l| &b[l.clone()]).collect();
let outer = Differ::new(&ka, &kb, |l| *l).run();
let ops = expand(&outer, &lines_a, &lines_b, |ra, rb| {
    Differ::new(&a[ra], &b[rb], |t| *t).run()
});
assert_eq!(ops[0], Op::Equal { a: 0, b: 0 });
assert!(ops.contains(&Op::Equal { a: 2, b: 2 }));