Debian Codegraph @

testdata/issue-with-incomplete-output.riff-output

raw
1 diff --git xsrc/plusminus_header_highlighter.rs xsrc/plusminus_header_highlighter.rs
2 index 5bb5056..c354931 100644
3 --- xsrc/plusminus_header_highlighter.rs
4 +++ xsrc/plusminus_header_highlighter.rs
5 @@ -6,7 +6,7 @@ use crate::lines_highlighter::{LinesHighlighter, Response};
6 use crate::refiner::to_highlighted_tokens;
7 use crate::string_future::StringFuture;
8 use crate::token_collector::{
9 - lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,
10 + align_tabs, lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,
11 LINE_STYLE_OLD_FILENAME,
12 };
13
14 @@ -92,8 +92,11 @@ impl PlusMinusHeaderHighlighter {
15 to_highlighted_tokens(&self.old_name, &self.new_name);
16
17 lowlight_timestamp(&mut old_tokens);
18 - unhighlight_git_prefix(&mut old_tokens);
19 lowlight_timestamp(&mut new_tokens);
20 +
21 + align_tabs(&mut old_tokens, &mut new_tokens);
22 +
23 + unhighlight_git_prefix(&mut old_tokens);
24 unhighlight_git_prefix(&mut new_tokens);
25
26 let old_filename = render(&LINE_STYLE_OLD_FILENAME, "--- ", &old_tokens);
27 @@ -108,3 +111,34 @@ impl PlusMinusHeaderHighlighter {
28 return highlighted;
29 }
30 }
31 +
32 +#[cfg(test)]
33 +mod tests {
34 + use crate::ansi::remove_ansi_escape_codes;
35 +
36 + use super::*;
37 +
38 + #[test]
39 + fn test_align_timestamps() {
40 + let mut test_me =
41 + PlusMinusHeaderHighlighter::from_line("--- x.txt\t2023-12-15 15:43:29").unwrap();
42 + let mut response = test_me
43 + .consume_line(
44 + "+++ /Users/johan/src/riff/README.md\t2024-01-29 14:56:40",
45 + &ThreadPool::new(1),
46 + )
47 + .unwrap();
48 + assert_eq!(LineAcceptance::AcceptedDone, response.line_accepted);
49 + assert_eq!(1, response.highlighted.len());
50 +
51 + let mut highlighted = response.highlighted[0].get().to_string().into_bytes();
52 + remove_ansi_escape_codes(&mut highlighted);
53 + let plain = String::from_utf8(highlighted).unwrap();
54 +
55 + assert_eq!(
56 + "--- x.txt 2023-12-15 15:43:29\n\
57 + +++ /Users/johan/src/riff/README.md 2024-01-29 14:56:40\n",
58 + plain.as_str()
59 + );
60 + }
61 +}
62 diff --git xsrc/token_collector.rs xsrc/token_collector.rs
63 index b58c50f..e4d4152 100644
64 --- xsrc/token_collector.rs
65 +++ xsrc/token_collector.rs
66 @@ -1,3 +1,5 @@
67 +use std::cmp;
68 +
69 use crate::ansi::AnsiStyle;
70 use crate::ansi::Color::Default;
71 use crate::ansi::Color::Green;
72 @@ -311,6 +313,38 @@ pub fn highlight_nonleading_tabs(tokens: &mut [StyledToken]) {
73 }
74 }
75
76 +pub(crate) fn align_tabs(old: &mut [StyledToken], new: &mut [StyledToken]) {
77 + let old_tab_index_token = old.iter().position(|token| token.token == "\t");
78 + if old_tab_index_token.is_none() {
79 + return;
80 + }
81 + let old_tab_index_token = old_tab_index_token.unwrap();
82 + let old_tab_index_char = old
83 + .iter()
84 + .take(old_tab_index_token)
85 + .map(|token| token.token.chars().count())
86 + .sum::<usize>();
87 +
88 + let new_tab_index_token = new.iter().position(|token| token.token == "\t");
89 + if new_tab_index_token.is_none() {
90 + return;
91 + }
92 + let new_tab_index_token = new_tab_index_token.unwrap();
93 + let new_tab_index_char = new
94 + .iter()
95 + .take(new_tab_index_token)
96 + .map(|token| token.token.chars().count())
97 + .sum::<usize>();
98 +
99 + let old_spaces =
100 + " ".repeat(2 + cmp::max(old_tab_index_char, new_tab_index_char) - old_tab_index_char);
101 + let new_spaces =
102 + " ".repeat(2 + cmp::max(old_tab_index_char, new_tab_index_char) - new_tab_index_char);
103 +
104 + old[old_tab_index_token].token = old_spaces;
105 + new[new_tab_index_token].token = new_spaces;
106 +}
107 +
108 /// Highlight single space between two highlighted tokens
109 pub fn bridge_consecutive_highlighted_tokens(tokens: &mut [StyledToken]) {
110 enum FoundState {