diff: Suppress separator-only word diffs

f83525c5b7e4bfe99f49fc2b726dc69ee82f2c51
Require word-diff similarity matches to include at least one relevant
token so punctuation-only line shape does not brighten whole deleted
lines.

Assisted-by: Codex:gpt-5.5
Alexis Sellier committed ago 1 parent e8888f59
crates/diff/src/word.rs +33 -6
33 33
struct Token<'a> {
34 34
    /// Row this token came from.
35 35
    row: RowId,
36 36
    /// Byte range within the row's text.
37 37
    range: Range<usize>,
38 +
    /// Category used for similarity checks.
39 +
    class: Class,
38 40
    /// Token text (a slice of the row's text).
39 41
    text: &'a str,
40 42
}
41 43
44 +
/// One dynamic-programming cell for the token LCS.
45 +
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
46 +
struct Cell {
47 +
    /// Length of the best LCS ending at this cell.
48 +
    len: u32,
49 +
    /// Whether that LCS contains at least one relevant token.
50 +
    is_relevant: bool,
51 +
}
52 +
42 53
/// Token category, used to decide where a token boundary falls.
43 54
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
44 55
enum Class {
45 56
    /// Run of `[A-Za-z0-9_]`.
46 57
    Ident,
59 70
            Self::Space
60 71
        } else {
61 72
            Self::Other
62 73
        }
63 74
    }
75 +
76 +
    /// Returns whether this class can anchor a meaningful word-diff pair.
77 +
    fn is_relevant(self) -> bool {
78 +
        matches!(self, Self::Ident)
79 +
    }
64 80
}
65 81
66 82
/// Iterator over tokens in one diff line's text.
67 83
struct Tokens<'a> {
68 84
    /// Row identifier carried into each yielded token.
93 109
        self.cursor = end;
94 110
95 111
        Some(Token {
96 112
            row: self.row,
97 113
            range: start..end,
114 +
            class,
98 115
            text: &self.text[start..end],
99 116
        })
100 117
    }
101 118
}
102 119
168 185
    }
169 186
170 187
    // Build the standard LCS length table over token texts.
171 188
    let rows = old.len() + 1;
172 189
    let cols = new.len() + 1;
173 -
    let mut table = vec![vec![0_u32; cols]; rows];
190 +
    let mut table = vec![vec![Cell::default(); cols]; rows];
174 191
    for i in 1..rows {
175 192
        for j in 1..cols {
193 +
            let prev = table[i - 1][j - 1];
194 +
            let top = table[i - 1][j];
195 +
            let left = table[i][j - 1];
176 196
            table[i][j] = if old[i - 1].text == new[j - 1].text {
177 -
                table[i - 1][j - 1] + 1
197 +
                Cell {
198 +
                    len: prev.len + 1,
199 +
                    is_relevant: prev.is_relevant || old[i - 1].class.is_relevant(),
200 +
                }
201 +
            } else if top.len >= left.len {
202 +
                top
178 203
            } else {
179 -
                table[i - 1][j].max(table[i][j - 1])
204 +
                left
180 205
            };
181 206
        }
182 207
    }
183 208
184 209
    // Similarity gate: the LCS length must cover at least the configured
185 210
    // fraction of the longer line, or the lines are too different to usefully
186 211
    // word-diff against each other.
187 -
    let common = table[old.len()][new.len()] as usize;
212 +
    let common = table[old.len()][new.len()];
188 213
    let max_len = old.len().max(new.len());
189 -
    if common * SIMILARITY_DENOMINATOR < max_len * SIMILARITY_NUMERATOR {
214 +
    if common.len as usize * SIMILARITY_DENOMINATOR < max_len * SIMILARITY_NUMERATOR
215 +
        || !common.is_relevant
216 +
    {
190 217
        return;
191 218
    }
192 219
193 220
    // Walk the table backwards from the bottom-right corner. Each step that
194 221
    // advances along only one axis is an old-only or new-only token, whose
197 224
    let mut j = new.len();
198 225
    while i > 0 && j > 0 {
199 226
        if old[i - 1].text == new[j - 1].text {
200 227
            i -= 1;
201 228
            j -= 1;
202 -
        } else if table[i - 1][j] >= table[i][j - 1] {
229 +
        } else if table[i - 1][j].len >= table[i][j - 1].len {
203 230
            i -= 1;
204 231
            changes
205 232
                .entry(old[i].row)
206 233
                .or_default()
207 234
                .push(old[i].range.clone());
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0