From 5cbcdd08f4ff6ff9b3d41fb7c611c4d8bae3da2e Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:22:32 +0800 Subject: [PATCH] fix(vcs): open the annotate gutter's Show Diff at the clicked line's change Show Diff from the blame gutter always opened at the commit's first change. git blame's porcelain header already names each line's number in the attributed commit; it is now kept as BlameLine.originLine, and Show Diff opens the diff at that line of the commit's version (IDEA scrolls to it the same way). Fixes #7169 --- crates/ide-vcs/src/blame.rs | 35 ++++++++++++------- crates/ide-vcs/src/lib.rs | 4 +++ crates/ide-vcs/src/write.rs | 1 + src/bindings.ts | 4 +++ .../vcs/__tests__/annotateRevision.test.ts | 7 ++++ src/features/vcs/annotateRevision.ts | 8 +++-- src/features/vcs/blame.ts | 5 ++- 7 files changed, 48 insertions(+), 16 deletions(-) diff --git a/crates/ide-vcs/src/blame.rs b/crates/ide-vcs/src/blame.rs index 1ffe43676..dcbcf26a6 100644 --- a/crates/ide-vcs/src/blame.rs +++ b/crates/ide-vcs/src/blame.rs @@ -16,17 +16,17 @@ struct CommitMeta { /// Parses `git blame --porcelain` output into one entry per final line. pub(crate) fn parse_porcelain(out: &str, root: &Path, relative_path: &str) -> Vec { let mut metas: HashMap = HashMap::new(); - let mut lines: Vec<(u32, String, Option)> = Vec::new(); - let mut current: Option<(u32, String)> = None; + let mut lines: Vec<(u32, String, Option, Option)> = Vec::new(); + let mut current: Option<(u32, String, Option)> = None; for line in out.split('\n') { if line.starts_with('\t') { - if let Some((number, id)) = current.take() { + if let Some((number, id, origin_line)) = current.take() { let origin = metas.get(&id).and_then(|m| m.origin_path.clone()); - lines.push((number, id, origin)); + lines.push((number, id, origin, origin_line)); } continue; } - if let Some((_, id)) = current.as_ref() { + if let Some((_, id, _)) = current.as_ref() { let meta = metas.entry(id.clone()).or_default(); if let Some(v) = line.strip_prefix("author ") { meta.author = v.to_string(); @@ -48,7 +48,7 @@ pub(crate) fn parse_porcelain(out: &str, root: &Path, relative_path: &str) -> Ve } // Header: [] let mut parts = line.split(' '); - let (Some(sha), Some(_orig), Some(final_line)) = (parts.next(), parts.next(), parts.next()) else { + let (Some(sha), Some(orig_line), Some(final_line)) = (parts.next(), parts.next(), parts.next()) else { continue; }; if sha.len() < 40 || !sha.bytes().all(|b| b.is_ascii_hexdigit()) { @@ -56,14 +56,23 @@ pub(crate) fn parse_porcelain(out: &str, root: &Path, relative_path: &str) -> Ve } let Ok(final_line) = final_line.parse::() else { continue }; metas.entry(sha.to_string()).or_default(); - current = Some((final_line, sha.to_string())); + // 1-based in the porcelain header. + let origin_line = orig_line.parse::().ok().and_then(|n| n.checked_sub(1)); + current = Some((final_line, sha.to_string(), origin_line)); } lines.sort_by_key(|(n, ..)| *n); lines .into_iter() - .map(|(_, sha, origin_path)| { + .map(|(_, sha, origin_path, origin_line)| { let meta = metas.get(&sha).cloned().unwrap_or_default(); - BlameLine { commit: sha, author: meta.author, time_ms: meta.time_ms, summary: meta.summary, origin_path } + BlameLine { + commit: sha, + author: meta.author, + time_ms: meta.time_ms, + summary: meta.summary, + origin_path, + origin_line, + } }) .collect() } @@ -77,14 +86,16 @@ mod tests { let sha_a = "a".repeat(40); let sha_z = "0".repeat(40); let out = format!( - "{sha_a} 1 1 2\nauthor Ann\nauthor-mail \nauthor-time 100\nauthor-tz +0000\nsummary first\nfilename f\n\tl1\n\ - {sha_a} 2 2\n\tl2\n\ - {sha_z} 3 3 1\nauthor Not Committed Yet\nauthor-time 200\nsummary Version of f from f\nfilename f\n\tl3\n" + "{sha_a} 11 17 2\nauthor Ann\nauthor-mail \nauthor-time 100\nauthor-tz +0000\nsummary first\nfilename f\n\tl1\n\ + {sha_a} 12 18\n\tl2\n\ + {sha_z} 19 19 1\nauthor Not Committed Yet\nauthor-time 200\nsummary Version of f from f\nfilename f\n\tl3\n" ); let lines = parse_porcelain(&out, Path::new("root"), "f"); assert_eq!(lines.len(), 3); assert_eq!(lines[1].author, "Ann"); assert_eq!(lines[1].time_ms, 100_000.0); assert_eq!(lines[2].author, "Not Committed Yet"); + // Where each line is in the attributed commit's file, 0-based: Show Diff opens there. + assert_eq!(lines.iter().map(|l| l.origin_line).collect::>(), [Some(10), Some(11), Some(18)]); } } diff --git a/crates/ide-vcs/src/lib.rs b/crates/ide-vcs/src/lib.rs index 068f4302e..11ae58249 100644 --- a/crates/ide-vcs/src/lib.rs +++ b/crates/ide-vcs/src/lib.rs @@ -131,6 +131,10 @@ pub struct BlameLine { #[serde(skip_serializing_if = "Option::is_none")] #[specta(optional)] pub origin_path: Option, + /// The line's 0-based number in the attributed commit's version of the file. + #[serde(skip_serializing_if = "Option::is_none")] + #[specta(optional)] + pub origin_line: Option, } /// History for Selection: the commits that changed a block of lines, newest first. diff --git a/crates/ide-vcs/src/write.rs b/crates/ide-vcs/src/write.rs index 587cf4c08..7771583c6 100644 --- a/crates/ide-vcs/src/write.rs +++ b/crates/ide-vcs/src/write.rs @@ -287,6 +287,7 @@ fn uncommitted_lines(n: usize) -> Vec { time_ms: now, summary: String::new(), origin_path: None, + origin_line: None, }) .collect() } diff --git a/src/bindings.ts b/src/bindings.ts index 820ec1bb4..3b02bb06f 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -4638,6 +4638,8 @@ export type BlameLine_Deserialize = { summary: string, /** The file blamed for this line when it had another path in the attributed commit. */ originPath?: IpcPath | null, + /** The line's 0-based number in the attributed commit's version of the file. */ + originLine?: number | null, }; export type BlameLine_Serialize = { @@ -4647,6 +4649,8 @@ export type BlameLine_Serialize = { summary: string, /** The file blamed for this line when it had another path in the attributed commit. */ originPath?: IpcPath | null, + /** The line's 0-based number in the attributed commit's version of the file. */ + originLine?: number | null, }; /** History for Selection: the commits that changed a block of lines, newest first. */ diff --git a/src/features/vcs/__tests__/annotateRevision.test.ts b/src/features/vcs/__tests__/annotateRevision.test.ts index 61b79b6ae..18966ed0d 100644 --- a/src/features/vcs/__tests__/annotateRevision.test.ts +++ b/src/features/vcs/__tests__/annotateRevision.test.ts @@ -186,6 +186,13 @@ describe("show diff from the annotate gutter", () => { ); }); + it("opens at the clicked line's place in the commit's version of the file", async () => { + await showRevisionDiff("/repo/a.py", sha, 100); + expect(mocks.openDiff).toHaveBeenCalledWith(expect.objectContaining({ line: 100, lineSide: "right" }), { + preview: false, + }); + }); + it("shows the file as added when the commit added it", async () => { mocks.commitFiles.mockResolvedValue({ status: "ok", data: [{ path: "a.py", kind: "added", oldPath: null }] }); await showRevisionDiff("/repo/a.py", sha); diff --git a/src/features/vcs/annotateRevision.ts b/src/features/vcs/annotateRevision.ts index e5a4eee9f..848bf3e15 100644 --- a/src/features/vcs/annotateRevision.ts +++ b/src/features/vcs/annotateRevision.ts @@ -117,9 +117,10 @@ export async function annotatePreviousRevision(path: string, commit: string): Pr /** * The annotate gutter's Show Diff (IDEA's): what `commit` changed in the file, against its first * parent, so a file the commit added or renamed shows as such. The file's history gives its name - * at that commit when it was renamed later. + * at that commit when it was renamed later. `line` (0-based, in that commit's version of the file) + * is where the diff opens: the change the clicked line came from, as IDEA scrolls to it. */ -export async function showRevisionDiff(path: string, commit: string): Promise { +export async function showRevisionDiff(path: string, commit: string, line?: number | null): Promise { const repo = repoOf(path) ?? currentRepoRoot(); if (!repo) return; const relative = relativePath(ipcPath(repo).replace(/\/$/, ""), ipcPath(path)); @@ -142,7 +143,8 @@ export async function showRevisionDiff(path: string, commit: string): Promise f.path === name) ?? { path: name, kind: "modified", oldPath: null }; - await openDiff(commitFileDiffData({ ...found, repo }, changed), { preview: false }); + const data = commitFileDiffData({ ...found, repo }, changed); + await openDiff(line == null ? data : { ...data, line, lineSide: "right" }, { preview: false }); } catch (e) { notifyFailure(e, tx("Could not show diff")); } diff --git a/src/features/vcs/blame.ts b/src/features/vcs/blame.ts index acdd769cf..583fdce54 100644 --- a/src/features/vcs/blame.ts +++ b/src/features/vcs/blame.ts @@ -665,7 +665,10 @@ function blameExtension(lines: BlameLine[], source: BlameSource): Extension { { label: "Show Diff", icon: "diff", - run: () => void import("./annotateRevision").then((m) => m.showRevisionDiff(origin, l.commit)), + run: () => + void import("./annotateRevision").then((m) => + m.showRevisionDiff(origin, l.commit, l.originLine), + ), }, { label: "Copy Revision Number",