riffdiff 3.6.1

A diff filter highlighting changed line parts
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
use threadpool::ThreadPool;

use crate::conflicts_highlighter::ConflictsHighlighter;
use crate::constants::NORMAL;
use crate::constants::NO_EOF_NEWLINE_COLOR;
use crate::hunk_header::HunkHeader;
use crate::lines_highlighter::{LineAcceptance, LinesHighlighter, Response};
use crate::plusminus_lines_highlighter::PlusMinusLinesHighlighter;
use crate::refiner::Formatter;
use crate::string_future::StringFuture;

#[derive(Debug)]
pub(crate) struct HunkLinesHighlighter {
    lines_highlighter: Option<Box<dyn LinesHighlighter>>,

    // This will have to be rendered at the top of our returned result.
    hunk_header: Option<String>,

    /// Calculated by HunkHeader::parse()
    initial_line_counts: Vec<usize>,

    /// We'll count these values down as we consume lines.
    remaining_line_counts: Vec<usize>,

    formatter: Formatter,
}

impl LinesHighlighter for HunkLinesHighlighter {
    fn consume_line(&mut self, line: &str, thread_pool: &ThreadPool) -> Result<Response, String> {
        let mut return_me = vec![];

        // Always start by rendering the hunk header
        if let Some(hunk_header) = &self.hunk_header {
            return_me.push(StringFuture::from_string(hunk_header.to_string() + "\n"));
            self.hunk_header = None;
        }

        let prefix_length = self.remaining_line_counts.len() - 1;
        let spaces_only = " ".repeat(prefix_length);
        let prefix = if line.len() >= prefix_length {
            line.split_at(prefix_length).0
        } else {
            spaces_only.as_str()
        };

        if line.starts_with('\\') {
            return_me.append(&mut self.consume_line_internal(line, thread_pool)?);
            return Ok(Response {
                line_accepted: LineAcceptance::AcceptedWantMore,
                highlighted: return_me,
            });
        }

        if !self.more_lines_expected() {
            if let Some(ref mut lines_highlighter) = self.lines_highlighter {
                let mut result = lines_highlighter.consume_eof(thread_pool)?;
                return_me.append(&mut result);
            }

            return Ok(Response {
                line_accepted: LineAcceptance::RejectedDone,
                highlighted: return_me,
            });
        }

        self.decrease_remaining_line_counts(prefix)?;

        // It wasn't a nnaeof line, and we're still expecting more lines.
        return_me.append(&mut self.consume_line_internal(line, thread_pool)?);
        return Ok(Response {
            line_accepted: LineAcceptance::AcceptedWantMore,
            highlighted: return_me,
        });
    }

    fn consume_eof(&mut self, thread_pool: &ThreadPool) -> Result<Vec<StringFuture>, String> {
        if self.more_lines_expected() {
            return Err(format!(
                "Still expecting more lines, but got EOF: {}",
                self.describe_remaining_line_counts()
            ));
        }

        return self.drain(thread_pool);
    }
}

impl HunkLinesHighlighter {
    /// Create a new LinesHighlighter from a line of input.
    ///
    /// Returns None if this line doesn't start a new LinesHighlighter.
    pub(crate) fn from_line(
        line: &str,
        formatter: Formatter,
        file_url: &Option<url::Url>,
    ) -> Result<Option<Self>, String>
    where
        Self: Sized,
    {
        if let Some(hunk_header) = HunkHeader::parse(line) {
            return Ok(Some(HunkLinesHighlighter {
                hunk_header: Some(hunk_header.render(file_url)?),
                remaining_line_counts: hunk_header.linecounts.clone(),
                initial_line_counts: hunk_header.linecounts,
                lines_highlighter: None,
                formatter,
            }));
        }

        return Ok(None);
    }

    fn consume_line_internal(
        &mut self,
        line: &str,
        thread_pool: &ThreadPool,
    ) -> Result<Vec<StringFuture>, String> {
        let mut return_me = vec![];

        // The `- 1` here is because there's one line count per column, plus
        // one for the result. So `- 1` gives us the prefix length.
        let prefix_length = self.remaining_line_counts.len() - 1;

        let spaces_only = " ".repeat(prefix_length);

        if let Some(lines_highlighter) = &mut self.lines_highlighter {
            let mut result = lines_highlighter.consume_line(line, thread_pool)?;
            return_me.append(&mut result.highlighted);
            match result.line_accepted {
                LineAcceptance::AcceptedWantMore => { /* Just keep going */ }
                LineAcceptance::AcceptedDone => {
                    self.lines_highlighter = None;
                }
                LineAcceptance::RejectedDone => {
                    // Drop our lines_highlighter and retry the line
                    self.lines_highlighter = None;
                    return_me.append(&mut self.consume_line_internal(line, thread_pool)?);
                    return Ok(return_me);
                }
            }
            return Ok(return_me);
        }

        if prefix_length == 2 {
            if let Some(highlighter) = ConflictsHighlighter::from_line(line) {
                self.lines_highlighter = Some(Box::new(highlighter));
                return Ok(return_me);
            }
        }
        if let Some(highlighter) =
            PlusMinusLinesHighlighter::from_line(line, prefix_length, self.formatter.clone())
        {
            self.lines_highlighter = Some(Box::new(highlighter));
            return Ok(return_me);
        }

        // It wasn't a plusminus line (including nnaeof lines), and we're still
        // expecting more lines. It must be a context line.

        // Context lines
        if line.is_empty() || line.starts_with(&spaces_only) {
            return_me.append(&mut self.drain(thread_pool)?);

            // FIXME: Consider whether we should be coalescing the plain lines?
            // Maybe that would improve performance? Measure and find out!
            return_me.push(StringFuture::from_string(line.to_string() + "\n"));

            return Ok(return_me);
        }

        if !line.starts_with('\\') {
            // All other cases should have been handled above
            return Err(format!(
                "Expected line at {} to start with \"\\\\\"",
                self.describe_remaining_line_counts()
            ));
        }

        return_me.push(StringFuture::from_string(format!(
            "{NO_EOF_NEWLINE_COLOR}{line}{NORMAL}\n"
        )));
        return Ok(return_me);
    }

    fn decrease_remaining_line_counts(&mut self, prefix: &str) -> Result<(), String> {
        if prefix.contains('+') || prefix.chars().all(|c| c == ' ') {
            // Any additions always count towards the last (additions) line
            // count. Context lines (space only) also count towards the last
            // count.
            let remaining_line_count = self.remaining_line_counts.last_mut().unwrap();
            if *remaining_line_count == 0 {
                return Err("Got more + lines than expected".to_string());
            }
            *remaining_line_count -= 1;

            for (pos, plus_or_space) in prefix.chars().enumerate() {
                if plus_or_space != ' ' {
                    continue;
                }

                let remaining_line_count = &mut self.remaining_line_counts[pos];
                if *remaining_line_count == 0 {
                    return Err(format!(
                        "Got more lines than expected for version (+ context column) {:?}",
                        pos + 1,
                    ));
                }

                *remaining_line_count -= 1;
            }

            return Ok(());
        }

        // Now, also decrease any `-` columns
        for (pos, minus_or_space) in prefix.chars().enumerate() {
            if minus_or_space != '-' {
                continue;
            }

            let remaining_line_count = &mut self.remaining_line_counts[pos];
            if *remaining_line_count == 0 {
                return Err(format!(
                    "Got more lines than expected for version (minus / space column) {:?}",
                    pos + 1,
                ));
            }

            *remaining_line_count -= 1;
        }

        return Ok(());
    }

    /// Return something along the lines of `[<± > done, < ±> 3 more expected, <++> 1 more expected]`
    fn describe_remaining_line_counts(&self) -> String {
        let mut return_me = String::new();
        for (pos, remaining_line_count) in self.remaining_line_counts.iter().enumerate() {
            let prefix = if pos == 0 { "" } else { ", " };

            let description = if pos == self.remaining_line_counts.len() - 1 {
                "+".repeat(self.remaining_line_counts.len() - 1)
            } else {
                let before = " ".repeat(pos);
                let after = " ".repeat(self.remaining_line_counts.len() - pos - 2);
                format!("{before}±{after}")
            };

            let done = format!(
                "{}/{} done",
                self.initial_line_counts[pos] - remaining_line_count,
                self.initial_line_counts[pos]
            );

            return_me.push_str(&format!("{prefix}<{description}> {done}"));
        }
        return format!("[{return_me}]");
    }

    fn more_lines_expected(&self) -> bool {
        for remaining_line_count in &self.remaining_line_counts {
            if *remaining_line_count != 0 {
                return true;
            }
        }
        return false;
    }

    fn drain(&mut self, thread_pool: &ThreadPool) -> Result<Vec<StringFuture>, String> {
        let return_me = if let Some(lines_highlighter) = &mut self.lines_highlighter {
            lines_highlighter.consume_eof(thread_pool)?
        } else {
            vec![]
        };

        self.lines_highlighter = None;
        return Ok(return_me);
    }
}

#[cfg(test)]
mod tests {
    use crate::refiner::tests::FORMATTER;
    use crate::{line_collector::NO_EOF_NEWLINE_MARKER_HOLDER, lines_highlighter::LineAcceptance};

    use super::*;

    /// Based on `testdata/adds-only.diff`
    #[test]
    fn test_happy_path() {
        let thread_pool = ThreadPool::new(1);

        let mut test_me =
            HunkLinesHighlighter::from_line("@@ -1,2 +1,2 @@", FORMATTER.clone(), &None)
                .unwrap()
                .unwrap();

        // First call to consume_line() should get us the hunk header
        let mut result = test_me
            .consume_line("-Hello, my name is Johan", &thread_pool)
            .unwrap();

        // Expect to get the hunk header back immediately, no matter what else
        // we got.
        assert_eq!(result.line_accepted, LineAcceptance::AcceptedWantMore);
        assert_eq!(result.highlighted.len(), 1);
        assert_eq!(
            result.highlighted[0].get(),
            "\u{1b}[36m@@ -1,2 +1,2 @@\u{1b}[0m\n"
        );

        let result = test_me
            .consume_line("+Hello, my first name is Johan", &thread_pool)
            .unwrap();
        assert_eq!(result.line_accepted, LineAcceptance::AcceptedWantMore);
        assert_eq!(result.highlighted.len(), 0);

        let mut result = test_me.consume_line(" I like pie.", &thread_pool).unwrap();
        assert_eq!(result.line_accepted, LineAcceptance::AcceptedWantMore);

        assert_eq!(result.highlighted.len(), 2);
        assert_eq!(
            result.highlighted[0].get(),
            concat!(
                "\u{1b}[31m-\u{1b}[33mHello, my name is Johan\u{1b}[0m\n",
                "\u{1b}[32m+\u{1b}[33mHello, my \u{1b}[7m\u{1b}[32mfirst\u{1b}[27m \u{1b}[33mname is Johan\u{1b}[0m\n"
            )
        );
        assert_eq!(result.highlighted[1].get(), " I like pie.\n");

        let result = test_me.consume_eof(&thread_pool).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_decrease_remaining_line_count() {
        let mut test_me =
            HunkLinesHighlighter::from_line("@@ -1,2 +1,2 @@", FORMATTER.clone(), &None)
                .unwrap()
                .unwrap();
        assert_eq!(test_me.remaining_line_counts, vec![2, 2]);

        test_me.decrease_remaining_line_counts("+").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![2, 1],
            "With a + line, we should decrease the last line count"
        );

        test_me.decrease_remaining_line_counts("-").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![1, 1],
            "With a - line, we should decrease the first line count"
        );
    }

    // `\ No newline at end of file` test, based on
    // `testdata/add-remove-trailing-newline.diff`.
    #[test]
    fn test_nnaeol() {
        {
            let mut no_eof_newline_marker = NO_EOF_NEWLINE_MARKER_HOLDER.lock().unwrap();
            *no_eof_newline_marker = Some("\\ No newline at end of file".to_string());
        }

        let mut test_me =
            HunkLinesHighlighter::from_line("@@ -1,1 +1,2 @@", FORMATTER.clone(), &None)
                .unwrap()
                .unwrap();
        assert_eq!(test_me.remaining_line_counts, vec![1, 2]);

        let thread_pool = ThreadPool::new(1);
        test_me.consume_line(" Hello", &thread_pool).unwrap();
        let result = test_me
            .consume_line("+No trailing newline", &thread_pool)
            .unwrap();

        // We should now be expecting the `\ No newline at end of file` line
        assert_eq!(result.line_accepted, LineAcceptance::AcceptedWantMore);
        assert_eq!(result.highlighted.len(), 0);

        assert_eq!(test_me.remaining_line_counts, vec![0, 0]);

        let mut result = test_me
            .consume_line("\\ No newline at end of file", &thread_pool)
            .unwrap();
        assert_eq!(result.line_accepted, LineAcceptance::AcceptedWantMore);
        assert_eq!(result.highlighted.len(), 1);
        assert_eq!(
            result.highlighted[0].get(),
            "\u{1b}[32m+No trailing newline\u{1b}[0m\u{1b}[31m\u{1b}[7m⏎\u{1b}[0m\n\u{1b}[2m\\ No newline at end of file\u{1b}[0m\n",
        );

        assert!(!test_me.more_lines_expected());

        let result = test_me.consume_eof(&thread_pool).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_decrease_remaining_line_count_merge() {
        let mut test_me =
            HunkLinesHighlighter::from_line("@@@ -1,5 -1,5 +1,5 @@@", FORMATTER.clone(), &None)
                .unwrap()
                .unwrap();
        assert_eq!(test_me.remaining_line_counts, vec![5, 5, 5]);

        test_me.decrease_remaining_line_counts(" -").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![5, 4, 5],
            "Minus counts should have gone down"
        );

        test_me.decrease_remaining_line_counts("- ").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![4, 4, 5],
            "Minus counts should have gone down"
        );

        test_me.decrease_remaining_line_counts("--").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![3, 3, 5],
            "Minus and space counts should have gone down"
        );

        test_me.decrease_remaining_line_counts("++").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![3, 3, 4],
            "With a + line, we should decrease the last line count"
        );

        test_me.decrease_remaining_line_counts(" +").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![2, 3, 3],
            "First count should have gone down because of the space in its column. Last count should have gone down because of the + in its column."
        );

        test_me.decrease_remaining_line_counts("  ").unwrap();
        assert_eq!(
            test_me.remaining_line_counts,
            vec![1, 2, 2],
            "All counts should drop on context (space only) lines"
        );
    }
}