From b347c0309652d591f416b8ef9f385e2532361f73 Mon Sep 17 00:00:00 2001 From: Lars Jansen Date: Mon, 16 Nov 2026 11:10:52 +0100 Subject: [PATCH] feat: make the words that changed within a line stand out in a diff --- CONTRIBUTING.md | 3 +- README.md | 2 + ROADMAP.md | 3 +- crates/app/src/diff.rs | 62 +++++++++++-- crates/app/src/tests.rs | 31 +++++++ crates/app/src/theme.rs | 4 + crates/core/src/diff.rs | 188 +++++++++++++++++++++++++++++++++++++- crates/core/tests/repo.rs | 29 ++++++ 8 files changed, 308 insertions(+), 14 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b2dfb4f..2b3a122 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -59,7 +59,8 @@ There are two crates. working without code of our own. - `status.rs` and `diff.rs` parse `git status --porcelain=v2`, what commits changed (also to one file, through its renames), who last changed each line of a file, and unified diffs, and build - the patches for staging single lines. + the patches for staging single lines. `diff.rs` also pairs the lines a change removed and + added, and finds the words that changed between them, for the lines on screen. - `conflict.rs` splits a conflicted file at the markers git wrote into it, and puts it back together with ours, theirs or both for each conflict. - `watch.rs` watches the working tree and the refs, and says when something changed that git diff --git a/README.md b/README.md index aebbfc8..958b3b6 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ GitCraft is young. This is what it does today; [ROADMAP.md](ROADMAP.md) has what that name comes first, so typing its name takes you there. Large histories are searched as you type, without the window waiting. - **Commits**: message, author, parents and the files they changed, with the diff of each file. +- **Diffs**: in a line that changed, the words that changed stand out, so a renamed variable or a + new argument is easy to spot. - **File history**: right-click a file, in a commit or in your changes, for every commit that changed it, newest first, also from before it was renamed. Pick one to see its diff of the file, with the commit beside it. Following a file through a large history happens in the background. diff --git a/ROADMAP.md b/ROADMAP.md index ec70ea2..0c3b897 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -47,10 +47,11 @@ drive it the way a user does, and keeps every benchmark as fast as before. - [x] Search the graph by message, author or SHA, and jump to any branch or tag by its name - [x] File history: the commits that changed a file, followed through renames, each with its diff - [x] Blame: who last changed each line of a file, and in which commit +- [x] Better diffs, part 1: the words that changed within a line stand out ## Next -1. **Better diffs**: side by side, word-level changes, syntax colouring, images. +1. **Better diffs, part 2**: side by side, syntax colouring, images. 2. **Keyboard**: shortcuts for everything, and a command palette. 3. **Settings**: light and dark theme, font size, date format, default pull mode. 4. **Graph polish**: resizable columns, compact lanes, author avatars (optional, never by default diff --git a/crates/app/src/diff.rs b/crates/app/src/diff.rs index 5a410d2..538b422 100644 --- a/crates/app/src/diff.rs +++ b/crates/app/src/diff.rs @@ -1,7 +1,12 @@ -//! The diff of one file, with staging and discarding of hunks and single lines. +//! The diff of one file, with the words that changed within its lines, and staging and +//! discarding of hunks and single lines. +use std::ops::Range; + +use eframe::egui::cache::{ComputerMut, FrameCache}; +use eframe::egui::text::{LayoutJob, TextFormat}; use eframe::egui::{self, Align2, Color32, FontId, Key, Rect, RichText, Sense, Ui, Vec2}; -use gitcraft_core::diff::{LineKind, Source}; +use gitcraft_core::diff::{self, LineKind, Source}; use gitcraft_core::git::Op; use crate::app::{Action, Confirm, OpenFile}; @@ -31,6 +36,41 @@ fn act(file: &OpenFile, verb: &'static str, what: &str, selected: &dyn Fn(usize, Action::Run(Op::ApplyToIndex { patch, reverse }) } +/// Finds the words that changed between a removed and an added line, once while they are on screen. +#[derive(Debug, Default)] +struct Words; + +impl ComputerMut<[&str; 2], [Vec>; 2]> for Words { + fn compute(&mut self, [old, new]: [&str; 2]) -> [Vec>; 2] { + diff::words(old, new) + } +} + +/// A line's text after its `marker`, with its `words` that changed on the `background`. +fn line_text( + marker: &str, + text: &str, + words: &[Range], + background: Color32, + font: &FontId, + color: Color32, +) -> LayoutJob { + let mut job = LayoutJob::default(); + let mut add = |text: &str, background| { + job.append(text, 0.0, TextFormat { font_id: font.clone(), color, background, ..TextFormat::default() }); + }; + add(marker, Color32::TRANSPARENT); + let mut at = 0; + // A word can end in the carriage return that is not shown. + for word in words.iter().filter(|word| word.start < text.len()) { + add(&text[at..word.start], Color32::TRANSPARENT); + at = word.end.min(text.len()); + add(&text[word.start..at], background); + } + add(&text[at..], Color32::TRANSPARENT); + job +} + /// Shows the open file, with a way to close it when it fills the centre `alone`. pub fn show(ui: &mut Ui, file: &OpenFile, alone: bool, actions: &mut Vec) { let verbs = verbs(&file.source); @@ -100,10 +140,10 @@ pub fn show(ui: &mut Ui, file: &OpenFile, alone: bool, actions: &mut Vec continue; }; let line = &hunk.lines[l]; - let (background, marker) = match line.kind { - LineKind::Added => (theme::ADDED, "+"), - LineKind::Removed => (theme::REMOVED, "-"), - LineKind::Context | LineKind::NoNewline => (Color32::TRANSPARENT, " "), + let (background, word, marker) = match line.kind { + LineKind::Added => (theme::ADDED, theme::ADDED_WORD, "+ "), + LineKind::Removed => (theme::REMOVED, theme::REMOVED_WORD, "- "), + LineKind::Context | LineKind::NoNewline => (Color32::TRANSPARENT, Color32::TRANSPARENT, " "), }; painter.rect_filled(rect, 0.0, if picked(h, l) { theme::PICKED } else { background }); let number = |n: Option| n.map(|n| n.to_string()).unwrap_or_default(); @@ -116,8 +156,14 @@ pub fn show(ui: &mut Ui, file: &OpenFile, alone: bool, actions: &mut Vec font.clone(), Color32::DARK_GRAY, ); - let text = format!("{marker} {}", line.text.trim_end_matches('\r')); - painter.text(at(2.0 * NUMBER + 6.0), Align2::LEFT_CENTER, text, font.clone(), ui.visuals().text_color()); + let color = ui.visuals().text_color(); + let words = hunk.pair(l).map_or_else(Vec::new, |pair| { + let side = usize::from(line.kind == LineKind::Added); + ui.memory_mut(|m| m.caches.cache::>().get(pair)[side].clone()) + }); + let job = line_text(marker, line.text.trim_end_matches('\r'), &words, word, &font, color); + let galley = painter.layout_job(job); + painter.galley(at(2.0 * NUMBER + 6.0) - Vec2::Y * galley.size().y / 2.0, galley, color); let changed = matches!(line.kind, LineKind::Added | LineKind::Removed); if !verbs.is_empty() && changed && response.clicked() { actions.push(Action::ToggleLine(h, l)); diff --git a/crates/app/src/tests.rs b/crates/app/src/tests.rs index dcda5f6..0269c38 100644 --- a/crates/app/src/tests.rs +++ b/crates/app/src/tests.rs @@ -14,6 +14,7 @@ use tempfile::TempDir; use crate::app::{Action, Selection}; use crate::start::percent; +use crate::theme; use crate::ui::{App, KEYS}; /// A repository with one commit, with the tester's own git config kept out of the way. @@ -316,6 +317,36 @@ fn discarding_an_open_untracked_file_deletes_it_and_closes_it() { assert_eq!(ws.error, None); } +/// The parts of the text on screen that are painted on `background`. +fn painted_on(harness: &Harness<'_, App>, background: egui::Color32) -> Vec { + let jobs = harness.output().shapes.iter().filter_map(|clipped| match &clipped.shape { + egui::Shape::Text(text) => Some(&text.galley.job), + _ => None, + }); + jobs.flat_map(|job| { + let marked = job.sections.iter().filter(|section| section.format.background == background); + marked.map(|section| job.text[section.byte_range.start.0..section.byte_range.end.0].to_owned()) + }) + .collect() +} + +#[test] +fn a_changed_line_marks_the_words_that_changed() { + let (_dir, git) = repo(); + fs::write(git.dir().join("file.txt"), "let total = price * count;\nkeep\nold line\n").unwrap(); + git.run(&["commit", "-qam", "Total"]).unwrap(); + fs::write(git.dir().join("file.txt"), "let total = price * amount;\nkeep\nsomething else\n").unwrap(); + let mut harness = app(Some(git.dir())); + harness.get_by_label("Uncommitted changes").click(); + settle(&mut harness); + harness.get_by_label("file.txt").click(); + settle(&mut harness); + + // Only the word that changed stands out; a line rewritten as a whole has nothing to point at. + assert_eq!(painted_on(&harness, theme::REMOVED_WORD), ["count"]); + assert_eq!(painted_on(&harness, theme::ADDED_WORD), ["amount"]); +} + #[test] fn discard_single_lines_and_hunks() { let (_dir, git) = repo(); diff --git a/crates/app/src/theme.rs b/crates/app/src/theme.rs index 813c756..6e6761c 100644 --- a/crates/app/src/theme.rs +++ b/crates/app/src/theme.rs @@ -22,6 +22,10 @@ const LANES: [Color32; 10] = [ pub const ADDED: Color32 = Color32::from_rgb(0x1d, 0x3b, 0x2c); /// The background of a removed line. pub const REMOVED: Color32 = Color32::from_rgb(0x45, 0x22, 0x27); +/// The background of the words that changed in an added line. +pub const ADDED_WORD: Color32 = Color32::from_rgb(0x2c, 0x63, 0x45); +/// The background of the words that changed in a removed line. +pub const REMOVED_WORD: Color32 = Color32::from_rgb(0x74, 0x30, 0x39); /// The background of a conflict without a pick. pub const CONFLICT: Color32 = Color32::from_rgb(0x4a, 0x38, 0x1c); /// The background of a line picked for staging. diff --git a/crates/core/src/diff.rs b/crates/core/src/diff.rs index 3d4f006..f0b40e1 100644 --- a/crates/core/src/diff.rs +++ b/crates/core/src/diff.rs @@ -1,4 +1,7 @@ -//! Unified diffs from `git diff`, and patches built from parts of them for staging. +//! Unified diffs from `git diff`, the words that changed within lines, and patches built from +//! parts of them for staging. + +use std::ops::Range; use crate::Result; use crate::git::Git; @@ -27,6 +30,9 @@ pub struct Line { pub old: Option, /// The line number in the new version. pub new: Option, + /// The line in the same hunk that this one replaces or is replaced by: in a block of changes, + /// the first removed line pairs with the first added line, the second with the second. + pub pair: Option, } /// A block of changes with its context. @@ -38,6 +44,16 @@ pub struct Hunk { pub lines: Vec, } +impl Hunk { + /// The removed and the added text of line `l` and the line it pairs with, for [`words`]. + pub fn pair(&self, l: usize) -> Option<[&str; 2]> { + let line = &self.lines[l]; + let pair = &self.lines[line.pair?]; + let [old, new] = if line.kind == LineKind::Removed { [line, pair] } else { [pair, line] }; + Some([&old.text, &new.text]) + } +} + /// The diff of one file. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct FileDiff { @@ -107,6 +123,8 @@ impl FileDiff { pub fn parse(output: &str) -> Vec { let mut files: Vec = Vec::new(); let (mut old, mut new) = (0, 0); + // The removed lines of the block of changes so far, and how many lines it added. + let (mut removed, mut added) = (Vec::new(), 0); for text in output.split_terminator('\n') { let in_hunk = files.last().is_some_and(|f| !f.hunks.is_empty()); let marker = text.chars().next().unwrap_or(' '); @@ -115,7 +133,8 @@ impl FileDiff { continue; }; if let Some((o, n)) = text.strip_prefix("@@ ").and_then(hunk_start) { - (old, new) = (o, n); + (old, new, added) = (o, n, 0); + removed.clear(); file.hunks.push(Hunk { header: text.to_owned(), lines: Vec::new() }); } else if in_hunk && matches!(marker, ' ' | '+' | '-' | '\\') { let kind = match marker { @@ -132,12 +151,32 @@ impl FileDiff { }; old += u32::from(numbers.0.is_some()); new += u32::from(numbers.1.is_some()); - let hunk = file.hunks.last_mut().expect("in a hunk"); - hunk.lines.push(Line { + let lines = &mut file.hunks.last_mut().expect("in a hunk").lines; + let pair = match kind { + LineKind::Removed => { + removed.push(lines.len()); + None + } + LineKind::Added => { + added += 1; + removed.get(added - 1).copied() + } + LineKind::Context => { + removed.clear(); + added = 0; + None + } + LineKind::NoNewline => None, + }; + if let Some(p) = pair { + lines[p].pair = Some(lines.len()); + } + lines.push(Line { kind, text: text.get(1..).unwrap_or_default().to_owned(), old: numbers.0, new: numbers.1, + pair, }); } else { file.binary |= text.starts_with("Binary files ") || text == "GIT binary patch"; @@ -186,6 +225,101 @@ impl FileDiff { } } +/// Lines with more words than this, after what they start and end with in common, are only +/// compared on that start and end: the table that finds the words in common grows with the +/// product of the lengths. +const WORDS: usize = 256; + +/// The parts of `old` and `new` that the other does not have, as byte ranges in each: the words, +/// runs of white space and other characters left over once the longest run of them both have in +/// order is taken out. None when less than half of the two (white space aside) is the same, as +/// for a line rewritten as a whole. +/// +/// This is for a removed line and the added line it pairs with ([`Line::pair`]), on screen: its +/// time grows with the product of the lengths of the two, up to a few hundred words each. +pub fn words(old: &str, new: &str) -> [Vec>; 2] { + let (olds, news) = (tokens(old), tokens(new)); + let same = |i: usize, j: usize| old[olds[i].clone()] == new[news[j].clone()]; + let prefix = (0..olds.len().min(news.len())).take_while(|&i| same(i, i)).count(); + let suffix = + (0..olds.len().min(news.len()) - prefix).take_while(|&k| same(olds.len() - 1 - k, news.len() - 1 - k)).count(); + let (n, m) = (olds.len() - prefix - suffix, news.len() - prefix - suffix); + let mut kept = [vec![true; olds.len()], vec![true; news.len()]]; + kept[0][prefix..prefix + n].fill(false); + kept[1][prefix..prefix + m].fill(false); + if n.min(m) > 0 && n.max(m) <= WORDS { + // `table[i * (m + 1) + j]` is how many tokens the middles have in common from `i` and `j` on. + let at = |i: usize, j: usize| i * (m + 1) + j; + let mut table = vec![0u16; (n + 1) * (m + 1)]; + for i in (0..n).rev() { + for j in (0..m).rev() { + table[at(i, j)] = if same(prefix + i, prefix + j) { + table[at(i + 1, j + 1)] + 1 + } else { + table[at(i + 1, j)].max(table[at(i, j + 1)]) + }; + } + } + let (mut i, mut j) = (0, 0); + while i < n && j < m { + if same(prefix + i, prefix + j) { + (kept[0][prefix + i], kept[1][prefix + j]) = (true, true); + (i, j) = (i + 1, j + 1); + } else if table[at(i + 1, j)] >= table[at(i, j + 1)] { + i += 1; + } else { + j += 1; + } + } + } + let weight = |text: &str, tokens: &[Range], only_kept: Option<&[bool]>| -> usize { + let counts = |i: usize, token: &Range| { + only_kept.is_none_or(|kept| kept[i]) && !text[token.clone()].trim().is_empty() + }; + tokens.iter().enumerate().filter(|&(i, token)| counts(i, token)).map(|(_, token)| token.len()).sum() + }; + if 4 * weight(old, &olds, Some(&kept[0])) < weight(old, &olds, None) + weight(new, &news, None) { + return [Vec::new(), Vec::new()]; + } + [spans(&olds, &kept[0]), spans(&news, &kept[1])] +} + +/// A line split into words, runs of white space and single other characters, as byte ranges. +fn tokens(text: &str) -> Vec> { + let class = |c: char| { + if c.is_alphanumeric() || c == '_' { + 1 + } else if c.is_whitespace() { + 2 + } else { + 0 + } + }; + let mut tokens: Vec> = Vec::new(); + let mut last = 0; + for (i, c) in text.char_indices() { + let kind = class(c); + match tokens.last_mut() { + Some(token) if kind != 0 && kind == last => token.end = i + c.len_utf8(), + _ => tokens.push(i..i + c.len_utf8()), + } + last = kind; + } + tokens +} + +/// The tokens that are not kept, with the ones next to each other joined. +fn spans(tokens: &[Range], kept: &[bool]) -> Vec> { + let mut spans: Vec> = Vec::new(); + for (token, _) in tokens.iter().zip(kept).filter(|&(_, &kept)| !kept) { + match spans.last_mut() { + Some(span) if span.end == token.start => span.end = token.end, + _ => spans.push(token.clone()), + } + } + spans +} + /// The first old and new line numbers from the rest of a `@@ -a,b +c,d @@` line. fn hunk_start(header: &str) -> Option<(u32, u32)> { let mut ranges = header.split(' '); @@ -237,6 +371,52 @@ Binary files a/img.png and b/img.png differ ); } + #[test] + fn lines_that_replace_each_other_pair_up() { + let pairs = |diff: &str| -> Vec> { + FileDiff::parse(diff)[0].hunks[0].lines.iter().map(|line| line.pair).collect() + }; + // "-two" and "+TWO" replace each other, "+extra" and "-four" stand alone. + assert_eq!(pairs(DIFF), [None, Some(2), Some(1), None, None, None, None]); + let more = + "diff --git a/f b/f\n@@ -1,3 +1,4 @@\n-a\n-b\n+A\n+B\n+C\n x\n-y\n\\ No newline at end of file\n+Y\n"; + assert_eq!(pairs(more), [Some(2), Some(3), Some(0), Some(1), None, None, Some(8), None, Some(6)]); + } + + const NONE: [Vec; 2] = [Vec::new(), Vec::new()]; + + fn words_of(old: &str, new: &str) -> [Vec; 2] { + let [a, b] = words(old, new); + [a.into_iter().map(|r| old[r].to_owned()).collect(), b.into_iter().map(|r| new[r].to_owned()).collect()] + } + + #[test] + fn words_that_changed_within_a_line() { + assert_eq!(words_of("let total = price * count;", "let total = price * amount;"), [["count"], ["amount"]]); + // Words next to each other join; what only one side has stands alone. + assert_eq!(words_of("call(a, b)", "call(a, b, c)"), [vec![], vec![", c".to_owned()]]); + assert_eq!(words_of("if x == 1 { go() }", "if y == 1 { stop() }"), [["x", "go"], ["y", "stop"]]); + // White space counts as a change, not towards being alike. + assert_eq!(words_of("a b", "\ta b"), [vec![" ".to_owned()], vec!["\t".to_owned(), " ".to_owned()]]); + assert_eq!(words_of("caf\u{e9} au lait", "caf\u{e9} au chocolat"), [["lait"], ["chocolat"]]); + } + + #[test] + fn lines_with_little_in_common_have_no_words() { + assert_eq!(words_of("two", "TWO"), NONE); + assert_eq!(words_of(" }", " return x;"), NONE); + assert_eq!(words_of("", "new"), NONE); + } + + #[test] + fn long_lines_compare_on_what_they_start_and_end_with() { + let long = "word ".repeat(WORDS); + let [old, new] = words(&format!("start {long}end"), &format!("start {long}stop")); + assert_eq!((old.len(), new.len()), (1, 1)); + // Changes at both ends of a long middle leave too little in common to tell. + assert_eq!(words_of(&format!("a {long}b"), &format!("c {long}d")), NONE); + } + #[test] fn whole_hunk_patch_is_the_hunk() { let file = &FileDiff::parse(DIFF)[0]; diff --git a/crates/core/tests/repo.rs b/crates/core/tests/repo.rs index 98c8f9f..3fa2960 100644 --- a/crates/core/tests/repo.rs +++ b/crates/core/tests/repo.rs @@ -234,6 +234,35 @@ fn diffs_of_untracked_files_and_commits() { assert!(diff::read(&git, &Source::Untracked("bin.dat".into())).unwrap().binary); } +#[test] +fn a_changed_line_knows_its_words_that_changed() { + let (_dir, git) = repo(); + commit(&git, "f.rs", "fn total() {\n price * count\n}\n", "Total"); + write(&git, "f.rs", "fn total() -> u32 {\n price * amount\n}\n// done\n"); + + let diff = diff::read(&git, &Source::Unstaged("f.rs".into())).unwrap(); + let hunk = &diff.hunks[0]; + let words: Vec<_> = (0..hunk.lines.len()) + .filter(|&l| hunk.lines[l].kind != LineKind::Context) + .map(|l| { + let line = &hunk.lines[l]; + let [old, new] = hunk.pair(l).map_or_else(Default::default, |[old, new]| diff::words(old, new)); + let mine = if line.kind == LineKind::Removed { old } else { new }; + (line.kind, mine.into_iter().map(|r| &line.text[r]).collect::>()) + }) + .collect(); + assert_eq!( + words, + [ + (LineKind::Removed, vec![]), + (LineKind::Removed, vec!["count"]), + (LineKind::Added, vec!["-> u32 "]), + (LineKind::Added, vec!["amount"]), + (LineKind::Added, vec![]), + ] + ); +} + #[test] fn the_diff_of_a_file_a_commit_renamed_shows_only_what_changed() { let (_dir, git) = repo();