jj_lib/diff_presentation/
mod.rs1#![expect(missing_docs)]
18
19use std::borrow::Borrow;
20use std::mem;
21
22use bstr::BString;
23use itertools::Itertools as _;
24use pollster::FutureExt as _;
25
26use crate::backend::BackendResult;
27use crate::conflicts::MaterializedFileValue;
28use crate::diff::CompareBytesExactly;
29use crate::diff::CompareBytesIgnoreAllWhitespace;
30use crate::diff::CompareBytesIgnoreWhitespaceAmount;
31use crate::diff::ContentDiff;
32use crate::diff::DiffHunk;
33use crate::diff::DiffHunkKind;
34use crate::diff::find_line_ranges;
35use crate::merge::Diff;
36use crate::repo_path::RepoPath;
37
38pub mod unified;
39#[derive(Clone, Copy, Debug, Eq, PartialEq)]
43pub enum DiffTokenType {
44 Matching,
45 Different,
46}
47
48type DiffTokenVec<'content> = Vec<(DiffTokenType, &'content [u8])>;
49
50#[derive(Clone, Debug)]
51pub struct FileContent<T> {
52 pub is_binary: bool,
54 pub contents: T,
55}
56
57pub fn file_content_for_diff<T>(
58 path: &RepoPath,
59 file: &mut MaterializedFileValue,
60 map_resolved: impl FnOnce(BString) -> T,
61) -> BackendResult<FileContent<T>> {
62 const PEEK_SIZE: usize = 8000;
66 let contents = BString::new(file.read_all(path).block_on()?);
70 let start = &contents[..PEEK_SIZE.min(contents.len())];
71 Ok(FileContent {
72 is_binary: start.contains(&b'\0'),
73 contents: map_resolved(contents),
74 })
75}
76
77#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
78pub enum LineCompareMode {
79 #[default]
81 Exact,
82 IgnoreAllSpace,
84 IgnoreSpaceChange,
86}
87
88pub fn diff_by_line<'input, T: AsRef<[u8]> + ?Sized + 'input>(
89 inputs: impl IntoIterator<Item = &'input T>,
90 options: &LineCompareMode,
91) -> ContentDiff<'input> {
92 match options {
97 LineCompareMode::Exact => {
98 ContentDiff::for_tokenizer(inputs, find_line_ranges, CompareBytesExactly)
99 }
100 LineCompareMode::IgnoreAllSpace => {
101 ContentDiff::for_tokenizer(inputs, find_line_ranges, CompareBytesIgnoreAllWhitespace)
102 }
103 LineCompareMode::IgnoreSpaceChange => {
104 ContentDiff::for_tokenizer(inputs, find_line_ranges, CompareBytesIgnoreWhitespaceAmount)
105 }
106 }
107}
108
109pub fn unzip_diff_hunks_to_lines<'content, I>(diff_hunks: I) -> Diff<Vec<DiffTokenVec<'content>>>
111where
112 I: IntoIterator,
113 I::Item: Borrow<DiffHunk<'content>>,
114{
115 let mut left_lines: Vec<DiffTokenVec<'content>> = vec![];
116 let mut right_lines: Vec<DiffTokenVec<'content>> = vec![];
117 let mut left_tokens: DiffTokenVec<'content> = vec![];
118 let mut right_tokens: DiffTokenVec<'content> = vec![];
119
120 for hunk in diff_hunks {
121 let hunk = hunk.borrow();
122 match hunk.kind {
123 DiffHunkKind::Matching => {
124 debug_assert!(hunk.contents.iter().all_equal());
126 for token in hunk.contents[0].split_inclusive(|b| *b == b'\n') {
127 left_tokens.push((DiffTokenType::Matching, token));
128 right_tokens.push((DiffTokenType::Matching, token));
129 if token.ends_with(b"\n") {
130 left_lines.push(mem::take(&mut left_tokens));
131 right_lines.push(mem::take(&mut right_tokens));
132 }
133 }
134 }
135 DiffHunkKind::Different => {
136 let [left, right] = hunk.contents[..]
137 .try_into()
138 .expect("hunk should have exactly two inputs");
139 for token in left.split_inclusive(|b| *b == b'\n') {
140 left_tokens.push((DiffTokenType::Different, token));
141 if token.ends_with(b"\n") {
142 left_lines.push(mem::take(&mut left_tokens));
143 }
144 }
145 for token in right.split_inclusive(|b| *b == b'\n') {
146 right_tokens.push((DiffTokenType::Different, token));
147 if token.ends_with(b"\n") {
148 right_lines.push(mem::take(&mut right_tokens));
149 }
150 }
151 }
152 }
153 }
154
155 if !left_tokens.is_empty() {
156 left_lines.push(left_tokens);
157 }
158 if !right_tokens.is_empty() {
159 right_lines.push(right_tokens);
160 }
161 Diff::new(left_lines, right_lines)
162}