| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use oxy_core::repo::{DiffHunk, FileDiff}; |
| 5 | | use oxy_core::state::{FileContent, Snapshot}; |
| 6 | | use similar::{Algorithm, DiffOp}; |
| 7 | | use std::collections::BTreeMap; |
| 8 | |
|
| 9 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 10 | | pub enum Kind { |
| 11 | | Context, |
| 12 | | Add, |
| 13 | | Del, |
| 14 | | |
| 15 | | Gap, |
| 16 | | } |
| 17 | |
|
| 18 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 19 | | pub struct Line { |
| 20 | | pub kind: Kind, |
| 21 | | pub old_no: Option<u32>, |
| 22 | | pub new_no: Option<u32>, |
| 23 | | |
| 24 | | pub text: String, |
| 25 | | |
| 26 | | |
| 27 | | pub segments: Vec<Segment>, |
| 28 | | } |
| 29 | |
|
| 30 | |
|
| 31 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 32 | | pub struct Segment { |
| 33 | | pub text: String, |
| 34 | | pub changed: bool, |
| 35 | | } |
| 36 | |
|
| 37 | |
|
| 38 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 39 | | pub struct Options { |
| 40 | | |
| 41 | | pub ignore_whitespace: bool, |
| 42 | | } |
| 43 | |
|
| 44 | | impl Line { |
| 45 | 352× | fn new(kind: Kind, old_no: Option<u32>, new_no: Option<u32>, text: String) -> Line { |
| 46 | 352× | Line { kind, old_no, new_no, text, segments: vec![] } |
| 47 | 352× | } |
| 48 | | } |
| 49 | |
|
| 50 | | impl Kind { |
| 51 | 82× | pub fn class(self) -> &'static str { |
| 52 | 82× | match self { |
| 53 | 29× | Kind::Context => "ctx", |
| 54 | 42× | Kind::Add => "add", |
| 55 | 10× | Kind::Del => "del", |
| 56 | 1× | Kind::Gap => "gap", |
| 57 | | } |
| 58 | 82× | } |
| 59 | |
|
| 60 | 90× | pub fn sign(self) -> &'static str { |
| 61 | 90× | match self { |
| 62 | 32× | Kind::Context => " ", |
| 63 | 44× | Kind::Add => "+", |
| 64 | 11× | Kind::Del => "-", |
| 65 | 3× | Kind::Gap => "…", |
| 66 | | } |
| 67 | 90× | } |
| 68 | | } |
| 69 | |
|
| 70 | 345× | fn text(b: &[u8]) -> String { |
| 71 | 345× | String::from_utf8_lossy(b).trim_end_matches(['\n', '\r']).to_string() |
| 72 | 345× | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 2× | pub fn unified(base: &[u8], hunks: &[DiffHunk], context: usize) -> Vec<Line> { |
| 78 | 2× | unified_with(base, hunks, context, Options::default()) |
| 79 | 2× | } |
| 80 | |
|
| 81 | |
|
| 82 | 133× | pub fn unified_with(base: &[u8], hunks: &[DiffHunk], context: usize, opts: Options) -> Vec<Line> { |
| 83 | 133× | let base_lines: Vec<String> = oxy_core::diff::split_lines(base).into_iter().map(text).collect(); |
| 84 | | |
| 85 | | |
| 86 | 133× | let mut removed = std::collections::HashSet::new(); |
| 87 | 133× | let mut added: BTreeMap<u32, Vec<String>> = BTreeMap::new(); |
| 88 | 144× | for h in hunks { |
| 89 | 144× | for (line, _) in &h.removed { |
| 90 | 24× | removed.insert(*line); |
| 91 | 24× | } |
| 92 | 167× | added.entry(h.base_line).or_default().extend(h.added.iter().map(|b| text(b))); |
| 93 | | } |
| 94 | | |
| 95 | 133× | let mut rows: Vec<Line> = Vec::new(); |
| 96 | 133× | let mut new_no: u32 = 0; |
| 97 | 133× | if let Some(lines) = added.get(&0) { |
| 98 | 72× | for l in lines { |
| 99 | 72× | new_no += 1; |
| 100 | 72× | rows.push(Line::new(Kind::Add, None, Some(new_no), l.clone())); |
| 101 | 72× | } |
| 102 | 78× | } |
| 103 | 178× | for (i, l) in base_lines.iter().enumerate() { |
| 104 | 178× | let old_no = i as u32 + 1; |
| 105 | 178× | if removed.contains(&old_no) { |
| 106 | 24× | rows.push(Line::new(Kind::Del, Some(old_no), None, l.clone())); |
| 107 | 154× | } else { |
| 108 | 154× | new_no += 1; |
| 109 | 154× | rows.push(Line::new(Kind::Context, Some(old_no), Some(new_no), l.clone())); |
| 110 | 154× | } |
| 111 | 178× | if let Some(lines) = added.get(&old_no) { |
| 112 | 95× | for a in lines { |
| 113 | 95× | new_no += 1; |
| 114 | 95× | rows.push(Line::new(Kind::Add, None, Some(new_no), a.clone())); |
| 115 | 95× | } |
| 116 | 95× | } |
| 117 | | } |
| 118 | | |
| 119 | 138× | for (anchor, lines) in &added { |
| 120 | 138× | if *anchor as usize > base_lines.len() { |
| 121 | 0× | for a in lines { |
| 122 | 0× | new_no += 1; |
| 123 | 0× | rows.push(Line::new(Kind::Add, None, Some(new_no), a.clone())); |
| 124 | 0× | } |
| 125 | 138× | } |
| 126 | | } |
| 127 | 133× | with_context(refine(rows, opts), context) |
| 128 | 133× | } |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | 133× | fn refine(rows: Vec<Line>, opts: Options) -> Vec<Line> { |
| 135 | 133× | let mut out: Vec<Line> = Vec::with_capacity(rows.len()); |
| 136 | 133× | let mut i = 0; |
| 137 | 425× | while i < rows.len() { |
| 138 | 509× | let changed = |k: usize| matches!(rows[k].kind, Kind::Del | Kind::Add); |
| 139 | 292× | if !changed(i) { |
| 140 | 154× | out.push(rows[i].clone()); |
| 141 | 154× | i += 1; |
| 142 | 154× | continue; |
| 143 | 138× | } |
| 144 | | |
| 145 | | |
| 146 | 217× | let end = (i..rows.len()).find(|&k| !changed(k)).unwrap_or(rows.len()); |
| 147 | 191× | let mut dels: Vec<Line> = rows[i..end].iter().filter(|l| l.kind == Kind::Del).cloned().collect(); |
| 148 | 191× | let mut adds: Vec<Line> = rows[i..end].iter().filter(|l| l.kind == Kind::Add).cloned().collect(); |
| 149 | 138× | let adds_end = end; |
| 150 | 138× | let pairs = dels.len().min(adds.len()); |
| 151 | 138× | let mut same_but_space = vec![false; pairs]; |
| 152 | 138× | for k in 0..pairs { |
| 153 | 20× | if opts.ignore_whitespace && squash(&dels[k].text) == squash(&adds[k].text) { |
| 154 | 2× | same_but_space[k] = true; |
| 155 | 18× | } else if let Some((d, a)) = words(&dels[k].text, &adds[k].text) { |
| 156 | 11× | dels[k].segments = d; |
| 157 | 11× | adds[k].segments = a; |
| 158 | 11× | } |
| 159 | | } |
| 160 | 138× | if same_but_space.iter().any(|x| *x) { |
| 161 | | |
| 162 | | |
| 163 | 2× | let (mut pd, mut pa): (Vec<Line>, Vec<Line>) = (Vec::new(), Vec::new()); |
| 164 | 4× | for k in 0..pairs { |
| 165 | 4× | if same_but_space[k] { |
| 166 | 2× | out.append(&mut pd); |
| 167 | 2× | out.append(&mut pa); |
| 168 | 2× | out.push(Line::new(Kind::Context, dels[k].old_no, adds[k].new_no, adds[k].text.clone())); |
| 169 | 2× | } else { |
| 170 | 2× | pd.push(dels[k].clone()); |
| 171 | 2× | pa.push(adds[k].clone()); |
| 172 | 2× | } |
| 173 | | } |
| 174 | 2× | out.append(&mut pd); |
| 175 | 2× | out.extend(dels.drain(pairs..)); |
| 176 | 2× | out.append(&mut pa); |
| 177 | 2× | out.extend(adds.drain(pairs..)); |
| 178 | 136× | } else { |
| 179 | 136× | out.extend(dels); |
| 180 | 136× | out.extend(adds); |
| 181 | 136× | } |
| 182 | 138× | i = adds_end; |
| 183 | | } |
| 184 | 133× | out |
| 185 | 133× | } |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | 36× | fn tokens(s: &str) -> Vec<&str> { |
| 190 | 36× | let mut out = Vec::new(); |
| 191 | 36× | let mut start = 0; |
| 192 | 456× | let class = |c: char| { |
| 193 | 456× | if c.is_alphanumeric() || c == '_' { |
| 194 | 263× | 0 |
| 195 | 193× | } else if c.is_whitespace() { |
| 196 | 97× | 1 |
| 197 | | } else { |
| 198 | 96× | 2 |
| 199 | | } |
| 200 | 456× | }; |
| 201 | 36× | let mut prev: Option<u8> = None; |
| 202 | 456× | for (i, c) in s.char_indices() { |
| 203 | 456× | let k = class(c); |
| 204 | 456× | if prev.is_some_and(|p| p != k || k == 2) { |
| 205 | 183× | out.push(&s[start..i]); |
| 206 | 183× | start = i; |
| 207 | 273× | } |
| 208 | 456× | prev = Some(k); |
| 209 | | } |
| 210 | 36× | if start < s.len() { |
| 211 | 36× | out.push(&s[start..]); |
| 212 | 36× | } |
| 213 | 36× | out |
| 214 | 36× | } |
| 215 | |
|
| 216 | |
|
| 217 | 8× | fn squash(s: &str) -> String { |
| 218 | 8× | s.split_whitespace().collect() |
| 219 | 8× | } |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | 18× | fn words(old: &str, new: &str) -> Option<(Vec<Segment>, Vec<Segment>)> { |
| 224 | 18× | if old.len() + new.len() > 2_000 { |
| 225 | 0× | return None; |
| 226 | 18× | } |
| 227 | 18× | let (ot, nt) = (tokens(old), tokens(new)); |
| 228 | 18× | let diff = similar::TextDiff::configure().algorithm(Algorithm::Patience).diff_slices(&ot, &nt); |
| 229 | 18× | if diff.ratio() < 0.4 { |
| 230 | 7× | return None; |
| 231 | 11× | } |
| 232 | 11× | let (mut d, mut a) = (Vec::new(), Vec::new()); |
| 233 | 167× | let push = |v: &mut Vec<Segment>, text: &str, changed: bool| match v.last_mut() { |
| 234 | 145× | Some(last) if last.changed == changed => last.text.push_str(text), |
| 235 | 58× | _ => v.push(Segment { text: text.to_string(), changed }), |
| 236 | 167× | }; |
| 237 | 98× | for change in diff.iter_all_changes() { |
| 238 | 98× | match change.tag() { |
| 239 | 69× | similar::ChangeTag::Equal => { |
| 240 | 69× | push(&mut d, change.value(), false); |
| 241 | 69× | push(&mut a, change.value(), false); |
| 242 | 69× | } |
| 243 | 13× | similar::ChangeTag::Delete => push(&mut d, change.value(), true), |
| 244 | 16× | similar::ChangeTag::Insert => push(&mut a, change.value(), true), |
| 245 | | } |
| 246 | | } |
| 247 | 11× | Some((d, a)) |
| 248 | 18× | } |
| 249 | |
|
| 250 | |
|
| 251 | 133× | fn with_context(rows: Vec<Line>, context: usize) -> Vec<Line> { |
| 252 | 343× | let changed: Vec<bool> = rows.iter().map(|r| r.kind != Kind::Context).collect(); |
| 253 | 133× | let mut keep = vec![false; rows.len()]; |
| 254 | 343× | for (i, c) in changed.iter().enumerate() { |
| 255 | 343× | if *c { |
| 256 | 187× | let lo = i.saturating_sub(context); |
| 257 | 187× | let hi = (i + context + 1).min(rows.len()); |
| 258 | 611× | for k in keep.iter_mut().take(hi).skip(lo) { |
| 259 | 611× | *k = true; |
| 260 | 611× | } |
| 261 | 156× | } |
| 262 | | } |
| 263 | 133× | let mut out = Vec::new(); |
| 264 | 133× | let mut gap = 0; |
| 265 | 343× | for (row, k) in rows.into_iter().zip(keep) { |
| 266 | 343× | if k { |
| 267 | 336× | if gap > 0 { |
| 268 | 2× | out.push(Line::new(Kind::Gap, None, None, format!("{gap} unchanged lines"))); |
| 269 | 2× | gap = 0; |
| 270 | 334× | } |
| 271 | 336× | out.push(row); |
| 272 | 7× | } else { |
| 273 | 7× | gap += 1; |
| 274 | 7× | } |
| 275 | | } |
| 276 | 133× | if gap > 0 { |
| 277 | 3× | out.push(Line::new(Kind::Gap, None, None, format!("{gap} unchanged lines"))); |
| 278 | 130× | } |
| 279 | 133× | out |
| 280 | 133× | } |
| 281 | |
|
| 282 | |
|
| 283 | 243× | pub fn hunks_between(old: &[u8], new: &[u8]) -> Vec<DiffHunk> { |
| 284 | 243× | let old_lines = oxy_core::diff::split_lines(old); |
| 285 | 243× | let new_lines = oxy_core::diff::split_lines(new); |
| 286 | 243× | let ops = similar::capture_diff_slices(Algorithm::Myers, &old_lines, &new_lines); |
| 287 | 243× | let mut hunks: Vec<DiffHunk> = Vec::new(); |
| 288 | 393× | for op in ops { |
| 289 | 393× | match op { |
| 290 | 142× | DiffOp::Equal { .. } => {} |
| 291 | 4× | DiffOp::Delete { old_index, old_len, .. } => { |
| 292 | 4× | hunks.push(DiffHunk { |
| 293 | 4× | base_line: (old_index + old_len) as u32, |
| 294 | 4× | added: vec![], |
| 295 | 4× | removed: (old_index..old_index + old_len).map(|i| (i as u32 + 1, old_lines[i].to_vec())).collect(), |
| 296 | | }); |
| 297 | | } |
| 298 | 234× | DiffOp::Insert { old_index, new_index, new_len } => { |
| 299 | 234× | hunks.push(DiffHunk { |
| 300 | 234× | base_line: old_index as u32, |
| 301 | 847× | added: (new_index..new_index + new_len).map(|i| new_lines[i].to_vec()).collect(), |
| 302 | 234× | removed: vec![], |
| 303 | | }); |
| 304 | | } |
| 305 | 13× | DiffOp::Replace { old_index, old_len, new_index, new_len } => { |
| 306 | 13× | hunks.push(DiffHunk { |
| 307 | 13× | base_line: (old_index + old_len) as u32, |
| 308 | 15× | added: (new_index..new_index + new_len).map(|i| new_lines[i].to_vec()).collect(), |
| 309 | 15× | removed: (old_index..old_index + old_len).map(|i| (i as u32 + 1, old_lines[i].to_vec())).collect(), |
| 310 | | }); |
| 311 | | } |
| 312 | | } |
| 313 | | } |
| 314 | 243× | hunks |
| 315 | 243× | } |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | 17× | pub fn change_files(change: &oxy_core::objects::Change, base: &oxy_core::state::State, base_snapshot: &Snapshot) -> Vec<FileDiff> { |
| 320 | 17× | oxy_core::repo::diff_files_of(change, base, base_snapshot) |
| 321 | 17× | } |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | 218× | pub fn snapshot_diff(base: &Snapshot, head: &Snapshot) -> (Vec<FileDiff>, BTreeMap<String, Vec<u8>>) { |
| 326 | 218× | let mut files = Vec::new(); |
| 327 | 218× | let mut bases = BTreeMap::new(); |
| 328 | 837× | let text_of = |f: &oxy_core::state::FileState| match &f.content { |
| 329 | 805× | FileContent::Text { bytes, .. } => Some(bytes.clone()), |
| 330 | 32× | _ => None, |
| 331 | 837× | }; |
| 332 | 524× | for (path, hf) in &head.files { |
| 333 | 524× | if hf.kind == oxy_core::objects::NodeKind::Dir { |
| 334 | 49× | continue; |
| 335 | 475× | } |
| 336 | 475× | match base.files.get(path) { |
| 337 | 116× | None => { |
| 338 | 116× | let new = text_of(hf).unwrap_or_default(); |
| 339 | 116× | files.push(FileDiff { path: path.clone(), kind: "added".into(), hunks: hunks_between(b"", &new), from: None }); |
| 340 | 116× | } |
| 341 | 359× | Some(bf) => { |
| 342 | 359× | let (old, new) = (text_of(bf), text_of(hf)); |
| 343 | 359× | if old != new { |
| 344 | 118× | let old_b = old.clone().unwrap_or_default(); |
| 345 | 118× | let hunks = hunks_between(&old_b, &new.unwrap_or_default()); |
| 346 | 118× | if let Some(o) = old { |
| 347 | 118× | bases.insert(path.clone(), o); |
| 348 | 118× | } |
| 349 | 118× | files.push(FileDiff { path: path.clone(), kind: "modified".into(), hunks, from: None }); |
| 350 | 241× | } else if bf.mode != hf.mode { |
| 351 | 0× | files.push(FileDiff { path: path.clone(), kind: "mode".into(), hunks: vec![], from: None }); |
| 352 | 241× | } |
| 353 | | } |
| 354 | | } |
| 355 | | } |
| 356 | 411× | for (path, bf) in &base.files { |
| 357 | 411× | if bf.kind != oxy_core::objects::NodeKind::Dir && !head.files.contains_key(path) { |
| 358 | 3× | let old = text_of(bf).unwrap_or_default(); |
| 359 | 3× | bases.insert(path.clone(), old.clone()); |
| 360 | 3× | files.push(FileDiff { path: path.clone(), kind: "removed".into(), hunks: hunks_between(&old, b""), from: None }); |
| 361 | 408× | } |
| 362 | | } |
| 363 | 218× | files.sort_by(|a, b| a.path.cmp(&b.path)); |
| 364 | 218× | (files, bases) |
| 365 | 218× | } |
| 366 | |
|
| 367 | | #[cfg(test)] |
| 368 | | mod tests { |
| 369 | | use super::*; |
| 370 | |
|
| 371 | | #[test] |
| 372 | 1× | fn unified_rows_with_context() { |
| 373 | 1× | let base = b"a\nb\nc\nd\ne\nf\ng\nh\n"; |
| 374 | 1× | let hunks = hunks_between(base, b"a\nb\nc\nX\ne\nf\ng\nh\nZ\n"); |
| 375 | 1× | let rows = unified(base, &hunks, 1); |
| 376 | 8× | let s: Vec<String> = rows.iter().map(|r| format!("{}{}", r.kind.sign(), r.text)).collect(); |
| 377 | 1× | assert_eq!(s, ["…2 unchanged lines", " c", "-d", "+X", " e", "…2 unchanged lines", " h", "+Z"]); |
| 378 | 1× | assert_eq!(rows[2].old_no, Some(4)); |
| 379 | 1× | assert_eq!(rows[3].new_no, Some(4)); |
| 380 | 1× | assert_eq!(rows.last().unwrap().new_no, Some(9)); |
| 381 | 1× | } |
| 382 | |
|
| 383 | | #[test] |
| 384 | 1× | fn added_file() { |
| 385 | 1× | let hunks = hunks_between(b"", b"one\ntwo\n"); |
| 386 | 1× | let rows = unified(b"", &hunks, 3); |
| 387 | 1× | assert_eq!(rows.iter().map(|r| r.kind).collect::<Vec<_>>(), [Kind::Add, Kind::Add]); |
| 388 | 1× | } |
| 389 | | } |
| 390 | |
|
| 391 | | #[cfg(test)] |
| 392 | | mod refine_tests { |
| 393 | | use super::*; |
| 394 | |
|
| 395 | 4× | fn rows(base: &str, new: &str, opts: Options) -> Vec<Line> { |
| 396 | 4× | unified_with(base.as_bytes(), &hunks_between(base.as_bytes(), new.as_bytes()), 3, opts) |
| 397 | 4× | } |
| 398 | |
|
| 399 | | #[test] |
| 400 | 1× | fn changed_words_are_marked_within_a_line() { |
| 401 | 1× | let r = rows("let answer = 42;\n", "let answer = 43;\n", Options::default()); |
| 402 | 1× | let del = r.iter().find(|l| l.kind == Kind::Del).unwrap(); |
| 403 | 2× | let add = r.iter().find(|l| l.kind == Kind::Add).unwrap(); |
| 404 | 1× | assert_eq!(del.segments.iter().filter(|s| s.changed).map(|s| s.text.as_str()).collect::<Vec<_>>(), ["42"]); |
| 405 | 1× | assert_eq!(add.segments.iter().filter(|s| s.changed).map(|s| s.text.as_str()).collect::<Vec<_>>(), ["43"]); |
| 406 | 3× | assert_eq!(add.segments.iter().map(|s| s.text.as_str()).collect::<String>(), add.text, "segments cover the line"); |
| 407 | | |
| 408 | 1× | let r = rows("alpha beta\n", "gamma delta\n", Options::default()); |
| 409 | 2× | assert!(r.iter().all(|l| l.segments.is_empty())); |
| 410 | 1× | } |
| 411 | |
|
| 412 | | #[test] |
| 413 | 1× | fn whitespace_changes_can_be_hidden() { |
| 414 | 1× | let base = "fn main() {\n a();\n b();\n}\n"; |
| 415 | 1× | let new = "fn main() {\n a();\n c();\n}\n"; |
| 416 | 1× | let shown = rows(base, new, Options::default()); |
| 417 | 6× | assert_eq!(shown.iter().filter(|l| l.kind == Kind::Add).count(), 2); |
| 418 | 1× | let hidden = rows(base, new, Options { ignore_whitespace: true }); |
| 419 | 5× | let kinds: Vec<(Kind, &str)> = hidden.iter().map(|l| (l.kind, l.text.trim())).collect(); |
| 420 | 1× | assert_eq!( |
| 421 | | kinds, |
| 422 | | [(Kind::Context, "fn main() {"), (Kind::Context, "a();"), (Kind::Del, "b();"), (Kind::Add, "c();"), (Kind::Context, "}")], |
| 423 | | "only the real change remains, in order" |
| 424 | | ); |
| 425 | 2× | let ctx = hidden.iter().find(|l| l.text.trim() == "a();").unwrap(); |
| 426 | 1× | assert_eq!((ctx.old_no, ctx.new_no), (Some(2), Some(2))); |
| 427 | 1× | } |
| 428 | | } |