From 245679cef81ed5bec953cf286cacbdfc09fa0fc2 Mon Sep 17 00:00:00 2001 From: Bert Verhelst Date: Mon, 16 Nov 2026 19:27:28 +0100 Subject: [PATCH] fix(vcs): the inline blame label can be made not clickable (#3810) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The inline blame label was always a button that swallowed its events, so a click past the end of a line could open the commit instead of placing the caret. A new setting, vcs.git.inlineBlameClickable (Settings › Version Control › Git, on by default), makes the label plain text when off: no role, tab stop or handlers, and its events go to the editor. The uncommitted-changes label, which has no commit to open, now hands its events to the editor too. Open editors update without another git blame. Closes #3802 Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com> --- crates/ide-core/src/workflow_settings.rs | 4 ++ docs/en/guide/vcs.mdx | 2 +- docs/zh/guide/vcs.mdx | 2 +- src/bindings.ts | 5 ++ src/features/settings/pages/vcs.tsx | 23 +++++++- .../vcs/__tests__/inlineBlame.test.ts | 54 ++++++++++++++++++- src/features/vcs/inlineBlame.ts | 28 +++++++++- src/platform/clientSettings.ts | 1 + src/platform/locales/text/en.json | 3 +- src/platform/locales/text/zh.json | 3 +- src/platform/workflowSettings.test.ts | 8 +++ src/platform/workflowSettings.ts | 3 ++ 12 files changed, 127 insertions(+), 9 deletions(-) diff --git a/crates/ide-core/src/workflow_settings.rs b/crates/ide-core/src/workflow_settings.rs index 764b3f4a4..14e257a6f 100644 --- a/crates/ide-core/src/workflow_settings.rs +++ b/crates/ide-core/src/workflow_settings.rs @@ -43,6 +43,9 @@ impl Default for VcsSettings { pub struct GitSettings { /// Show blame after the current editor line. pub inline_blame: bool, + /// A click (or Enter / Space) on the inline blame label opens its commit; off, the + /// label is plain text and a click on it places the caret as past the line end. + pub inline_blame_clickable: bool, pub show_branch_in_project_view: bool, /// Path to the git executable; empty = auto-detect. pub executable: String, @@ -78,6 +81,7 @@ impl Default for GitSettings { fn default() -> Self { Self { inline_blame: false, + inline_blame_clickable: true, show_branch_in_project_view: false, executable: String::new(), update_method: "merge".into(), diff --git a/docs/en/guide/vcs.mdx b/docs/en/guide/vcs.mdx index 37b082af3..08cd724a5 100644 --- a/docs/en/guide/vcs.mdx +++ b/docs/en/guide/vcs.mdx @@ -214,7 +214,7 @@ Turn on **Combine stashes and shelves in one tab** in the Git settings to show b ## Blame and history -- **Annotate with Git Blame** shows author and date per line in the gutter (also from the gutter's context menu). Click an entry to open the commit in the log. **Show inline blame for the current line** in the Git settings shows the author at the end of the caret line. +- **Annotate with Git Blame** shows author and date per line in the gutter (also from the gutter's context menu). Click an entry to open the commit in the log. **Show inline blame for the current line** in the Git settings shows the author at the end of the caret line; a click on it opens the commit unless **Open the commit when the inline blame is clicked** is off. - **Annotate Previous Revision** in an annotated line's menu opens the file as it was before that commit, annotated, next to the commit's version. - **Annotate at Revision…** picks any revision the file has and shows it annotated next to the working copy. - **Show History** opens the file's history in a tab of the Git tool window. diff --git a/docs/zh/guide/vcs.mdx b/docs/zh/guide/vcs.mdx index fe57c4306..4969cbd1f 100644 --- a/docs/zh/guide/vcs.mdx +++ b/docs/zh/guide/vcs.mdx @@ -212,7 +212,7 @@ Git 工具窗口的 **日志** 标签页显示提交图。 ## Blame 与历史 -- **使用 Git Blame 注解** 在边栏中逐行显示作者和日期(也可以从边栏右键菜单打开)。点击某条记录会在日志中打开对应提交。在 Git 设置中打开 **为当前行显示内联 Git Blame**,会在光标所在行末尾显示作者。 +- **使用 Git Blame 注解** 在边栏中逐行显示作者和日期(也可以从边栏右键菜单打开)。点击某条记录会在日志中打开对应提交。在 Git 设置中打开 **为当前行显示内联 Git Blame**,会在光标所在行末尾显示作者;点击它会打开对应提交,关闭 **点击内联 Git Blame 时打开提交** 后则不会。 - 注解行的右键菜单中,**标注上一版本** 会打开该提交之前版本的文件(已注解),与这个提交的版本并排显示。 - **标注指定版本…** 可选择文件的任意版本,把该版本(已注解)与工作区文件并排显示。 - **显示历史记录** 在 Git 工具窗口的标签页中打开文件历史。 diff --git a/src/bindings.ts b/src/bindings.ts index ebbc2fa15..8de53b79d 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -6025,6 +6025,11 @@ export type GitOperation = export type GitSettings = { /** Show blame after the current editor line. */ inlineBlame?: boolean, + /** + * A click (or Enter / Space) on the inline blame label opens its commit; off, the + * label is plain text and a click on it places the caret as past the line end. + */ + inlineBlameClickable?: boolean, showBranchInProjectView?: boolean, /** Path to the git executable; empty = auto-detect. */ executable?: string, diff --git a/src/features/settings/pages/vcs.tsx b/src/features/settings/pages/vcs.tsx index be590a6f1..d749b87b1 100644 --- a/src/features/settings/pages/vcs.tsx +++ b/src/features/settings/pages/vcs.tsx @@ -204,6 +204,22 @@ function LargeFilesRow() { ); } +function InlineBlameRows() { + const [show] = useSetting("vcs.git.inlineBlame"); + return ( + <> + + + + + + ); +} + function PushDialogRows() { const [show] = useSetting("vcs.git.showPushDialog"); return ( @@ -493,7 +509,12 @@ export const VCS_PAGES: PageSpec[] = [ keywords: ["Test", "git version"], component: GitExecutableRow, }, - { kind: "toggle", key: "vcs.git.inlineBlame", label: "Show inline blame for the current line" }, + { + kind: "custom", + label: "Show inline blame for the current line", + keywords: ["Open the commit when the inline blame is clicked"], + component: InlineBlameRows, + }, { kind: "toggle", key: "vcs.git.showBranchInProjectView", diff --git a/src/features/vcs/__tests__/inlineBlame.test.ts b/src/features/vcs/__tests__/inlineBlame.test.ts index c97168346..6e9e8037f 100644 --- a/src/features/vcs/__tests__/inlineBlame.test.ts +++ b/src/features/vcs/__tests__/inlineBlame.test.ts @@ -47,8 +47,18 @@ const uncommitted: BlameLine = { commit: "0".repeat(40), author: "Not Committed const ctx = { doc: 1, path: "/repo/file.ts", language: "typescript", uiMode: null, readOnly: false }; let view: EditorView | undefined; -function settings(enabled: boolean) { - useProject.setState({ settings: { vcs: { git: { inlineBlame: enabled } } } as Settings }); +function settings(enabled: boolean, clickable = true) { + useProject.setState({ + settings: { vcs: { git: { inlineBlame: enabled, inlineBlameClickable: clickable } } } as Settings, + }); +} + +/** Presses the primary button on the label; jsdom has no layout, so the click resolves to `pos` + the way a browser resolves a point past the line end. */ +function pressLabel(v: EditorView, pos: number) { + vi.spyOn(v, "posAndSideAtCoords").mockReturnValue({ pos, assoc: -1 }); + label()!.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0, detail: 1 })); + document.dispatchEvent(new MouseEvent("mouseup", { bubbles: true, button: 0 })); } function editor(doc = "one\ntwo\nthree", context = ctx) { @@ -268,6 +278,46 @@ describe("current line decoration", () => { expect(mocks.blame).toHaveBeenCalledTimes(1); }); + it("leaves a click on a non-clickable label to the editor, which puts the caret at the line end (#3802)", async () => { + settings(true, false); + const v = editor(); + v.dispatch({ selection: { anchor: 1 } }); + await tick(); + expect(hint()?.textContent).toContain("Alice Smith"); + expect(label()?.hasAttribute("role")).toBe(false); + expect(label()?.hasAttribute("tabindex")).toBe(false); + label()!.click(); + label()!.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); + pressLabel(v, 3); + expect(v.state.selection.main.head).toBe(3); + expect(mocks.action).not.toHaveBeenCalled(); + }); + + it("leaves a click on the uncommitted-changes label, which opens nothing, to the editor", async () => { + const v = editor(); + v.dispatch({ selection: { anchor: 9 } }); + await tick(); + expect(hint()?.textContent).toBe("You, now · Uncommitted changes"); + expect(label()?.hasAttribute("role")).toBe(false); + pressLabel(v, 13); + expect(v.state.selection.main.head).toBe(13); + }); + + it("applies the clickable setting to an open editor without another git call", async () => { + editor(); + await tick(); + expect(label()?.getAttribute("role")).toBe("button"); + settings(true, false); + await vi.advanceTimersByTimeAsync(0); + expect(label()?.hasAttribute("role")).toBe(false); + settings(true, true); + await vi.advanceTimersByTimeAsync(0); + expect(label()?.getAttribute("role")).toBe("button"); + label()!.click(); + expect(mocks.action).toHaveBeenCalledWith("vcs.showCommit", alice.commit); + expect(mocks.blame).toHaveBeenCalledTimes(1); + }); + it("responds to the setting immediately", async () => { settings(false); editor(); diff --git a/src/features/vcs/inlineBlame.ts b/src/features/vcs/inlineBlame.ts index 67b63629a..cac8021d4 100644 --- a/src/features/vcs/inlineBlame.ts +++ b/src/features/vcs/inlineBlame.ts @@ -110,6 +110,8 @@ class BlameWidget extends WidgetType { readonly line: BlameLine, /** The annotated line has no text: no gap before the label, or the caret would look indented. */ readonly blank: boolean, + /** Version Control › Git › Open the commit when the inline blame is clicked. */ + readonly clickable: boolean, ) { super(); } @@ -117,6 +119,7 @@ class BlameWidget extends WidgetType { eq(other: BlameWidget) { return ( this.blank === other.blank && + this.clickable === other.clickable && formatInlineBlame(other.line) === formatInlineBlame(this.line) && other.line.commit === this.line.commit ); @@ -182,7 +185,7 @@ class BlameWidget extends WidgetType { this.observer = new MutationObserver(() => this.stepPastLineEnd(el)); this.observer.observe(line, { childList: true, subtree: true, characterData: true }); }, 0); - if (!isUncommitted(this.line)) { + if (this.clickable && !isUncommitted(this.line)) { // The inner box is the visible, pointer-hit region; the anchor is zero-width. inner.setAttribute("role", "button"); inner.tabIndex = 0; @@ -199,6 +202,13 @@ class BlameWidget extends WidgetType { return el; } + /** A plain-text label (clicks switched off, or uncommitted changes with no commit to open) + leaves its events to the editor: a click places the caret as on the empty space past the + line end instead of being swallowed by the widget. */ + ignoreEvent(): boolean { + return this.clickable && !isUncommitted(this.line); + } + destroy(): void { this.observer?.disconnect(); this.observer = undefined; @@ -310,6 +320,16 @@ export function inlineBlame(ctx: EditorDocContext): Extension { (s) => s.vcs.git.inlineBlame, () => this.invalidate(), ), + // Only the label's behaviour changes: redraw it from the loaded blame. + onWorkflowSettingsChange( + (s) => s.vcs.git.inlineBlameClickable, + () => { + this.clear(); + clearTimeout(this.timer); + ++this.generation; + void this.refresh(); + }, + ), useI18n.subscribe(() => this.invalidate()), ]; this.schedule(); @@ -381,7 +401,11 @@ export function inlineBlame(ctx: EditorDocContext): Extension { this.line = lines[caret.number - 1]; if (this.line) { const widget = Decoration.widget({ - widget: new BlameWidget(this.line, caret.text.trim().length === 0), + widget: new BlameWidget( + this.line, + caret.text.trim().length === 0, + workflowSettings().vcs.git.inlineBlameClickable, + ), // After a plugin's line-end message (side 1): git hint | lens | blame. side: 2, }).range(caret.to); diff --git a/src/platform/clientSettings.ts b/src/platform/clientSettings.ts index c8931f0d0..370b888ca 100644 --- a/src/platform/clientSettings.ts +++ b/src/platform/clientSettings.ts @@ -90,6 +90,7 @@ export const CLIENT_SETTINGS_KEYS = [ "externalDiff", "vcs.showDirtyDirectories", "vcs.git.inlineBlame", + "vcs.git.inlineBlameClickable", "vcs.git.showBranchInProjectView", "vcs.git.showPushDialog", "vcs.git.pushDialogOnlyProtected", diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index dfe73c854..3bf5961b8 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2293,5 +2293,6 @@ "Choose Directory Archive…": "Choose Directory Archive…", "This is a MySQL dump: restore it into a MySQL data source.": "This is a MySQL dump: restore it into a MySQL data source.", "This is a pg_dump archive: restore it with pg_restore.": "This is a pg_dump archive: restore it with pg_restore.", - "This is a plain SQL dump: restore it with psql.": "This is a plain SQL dump: restore it with psql." + "This is a plain SQL dump: restore it with psql.": "This is a plain SQL dump: restore it with psql.", + "Open the commit when the inline blame is clicked": "Open the commit when the inline blame is clicked" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index bd9ca355c..8e65ad2ef 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -8678,5 +8678,6 @@ "Choose Directory Archive…": "选择目录归档…", "This is a MySQL dump: restore it into a MySQL data source.": "这是 MySQL 转储:请恢复到 MySQL 数据源。", "This is a pg_dump archive: restore it with pg_restore.": "这是 pg_dump 归档:请使用 pg_restore 恢复。", - "This is a plain SQL dump: restore it with psql.": "这是纯 SQL 转储:请使用 psql 恢复。" + "This is a plain SQL dump: restore it with psql.": "这是纯 SQL 转储:请使用 psql 恢复。", + "Open the commit when the inline blame is clicked": "点击内联 Git Blame 时打开提交" } diff --git a/src/platform/workflowSettings.test.ts b/src/platform/workflowSettings.test.ts index 6f0f92407..782fd757f 100644 --- a/src/platform/workflowSettings.test.ts +++ b/src/platform/workflowSettings.test.ts @@ -9,6 +9,14 @@ describe("validateWorkflow", () => { expect(validateWorkflow({ vcs: { git: { inlineBlame: "true" } } }).vcs.git.inlineBlame).toBe(false); }); + it("keeps the inline blame label clickable unless switched off", () => { + const clickable = (inlineBlameClickable: unknown) => + validateWorkflow({ vcs: { git: { inlineBlameClickable } } }).vcs.git.inlineBlameClickable; + expect(DEFAULT_WORKFLOW.vcs.git.inlineBlameClickable).toBe(true); + expect(clickable(false)).toBe(false); + expect(clickable("no")).toBe(true); + }); + it("keeps the double-click choice unset until it is made", () => { expect(DEFAULT_WORKFLOW.vcs.commit.doubleClickOpensDiff).toBeNull(); expect(validateWorkflow(undefined).vcs.commit.doubleClickOpensDiff).toBeNull(); diff --git a/src/platform/workflowSettings.ts b/src/platform/workflowSettings.ts index 1d053dce0..40ea5aec3 100644 --- a/src/platform/workflowSettings.ts +++ b/src/platform/workflowSettings.ts @@ -19,6 +19,7 @@ export interface IssueLinkValid { export interface GitSettingsValid { inlineBlame: boolean; + inlineBlameClickable: boolean; showBranchInProjectView: boolean; executable: string; updateMethod: UpdateMethod; @@ -234,6 +235,7 @@ export const DEFAULT_WORKFLOW: WorkflowSettings = { historyLimit: 1000, git: { inlineBlame: false, + inlineBlameClickable: true, showBranchInProjectView: false, executable: "", updateMethod: "merge", @@ -391,6 +393,7 @@ export function validateVcs(raw: unknown): VcsSettingsValid { historyLimit: num(v.historyLimit, d.historyLimit, L.historyLimit, true), git: { inlineBlame: bool(g.inlineBlame, d.git.inlineBlame), + inlineBlameClickable: bool(g.inlineBlameClickable, d.git.inlineBlameClickable), showBranchInProjectView: bool(g.showBranchInProjectView, d.git.showBranchInProjectView), executable: text(g.executable, d.git.executable).trim(), updateMethod: oneOf(g.updateMethod, ["merge", "rebase"] as const, d.git.updateMethod),