feat(vcs): Options submenu in the annotate gutter (date, author, revision) (#4367)

The annotate gutter's context menu gets Options with Date, Author and Revision check items; Revision shows the 8-character commit hash. Date and Author stay on by default, the last shown column can't be turned off, and the choice is remembered and applied to every annotated editor, including other windows. Options also shows on uncommitted lines.

Closes #4366

Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
This commit is contained in:
Mansur Akhmadi
2026-11-19 02:40:20 +08:00
committed by GitHub
co-authored by mansur-akhmadi
parent 5c3b144db9
commit dfd7a177f5
2 changed files with 189 additions and 4 deletions
+103 -2
View File
@@ -4,7 +4,7 @@ import { act, fireEvent, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { BlameLine, Commit } from "@/bindings";
import { useI18n } from "@/platform/i18n";
import { annotationsShown, blameSlot, toggleAnnotations } from "../blame";
import { annotationsShown, blameSlot, readAnnotateColumns, toggleAnnotations, useAnnotateColumns } from "../blame";
import { closeBlamePopup } from "../BlamePopup";
import { useVcs } from "../store";
@@ -184,6 +184,107 @@ describe("git blame gutter and commit popup", () => {
)!;
fireEvent.contextMenu(edited);
const items = mocks.menu.mock.calls[0]![1] as { label?: string }[];
expect(items.map((i) => i.label)).toEqual(["Close Annotations"]);
expect(items.map((i) => i.label)).toEqual(["Options", "Close Annotations"]);
});
});
describe("annotate Options", () => {
type Item = { label?: string; checked?: boolean; disabled?: boolean; run?: () => void; submenu?: Item[] };
beforeEach(() => {
localStorage.clear();
useAnnotateColumns.setState(readAnnotateColumns());
});
async function annotated(doc = "first\nsecond\nthird") {
const v = new EditorView({ parent: document.body, state: EditorState.create({ doc, extensions: [blameSlot()] }) });
await toggleAnnotations(v, "/repo/src/main.py");
return v;
}
function options(v: EditorView): Item[] {
mocks.menu.mockClear();
fireEvent.contextMenu(v.dom.querySelector(".cm-vcs-blame .cm-gutterElement [data-commit]")!);
const items = mocks.menu.mock.calls[0]![1] as Item[];
return items.find((i) => i.label === "Options")!.submenu!;
}
const hidden = (v: EditorView) =>
["date", "author", "revision"].filter((c) => v.dom.classList.contains(`cm-vcs-blame-hide-${c}`));
it("shows date and author by default and adds the revision from Options", async () => {
view = await annotated();
expect(options(view).map((i) => [i.label, i.checked])).toEqual([
["Date", true],
["Author", true],
["Revision", false],
]);
expect(hidden(view)).toEqual(["revision"]);
expect(view.dom.querySelector(`[data-commit="${sha}"] .cm-vcs-blame-revision`)?.textContent).toBe("f122f5af");
act(() => options(view!)[2]!.run!());
expect(hidden(view)).toEqual([]);
expect(options(view).map((i) => i.checked)).toEqual([true, true, true]);
act(() => options(view!)[1]!.run!());
expect(hidden(view)).toEqual(["author"]);
});
it("keeps the last shown column", async () => {
view = await annotated();
act(() => options(view!)[1]!.run!());
const last = options(view);
expect(last.map((i) => [i.checked, i.disabled])).toEqual([
[true, true],
[false, false],
[false, false],
]);
act(() => last[0]!.run!());
expect(hidden(view)).toEqual(["author", "revision"]);
});
it("updates every annotated editor and remembers the choice", async () => {
view = await annotated();
const other = await annotated("a\nb\nc");
try {
act(() => options(view!)[0]!.run!());
expect(hidden(other)).toEqual(["date", "revision"]);
expect(JSON.parse(localStorage.getItem("openidea.vcs.annotateColumns")!)).toEqual({
date: false,
author: true,
revision: false,
});
expect(readAnnotateColumns()).toEqual({ date: false, author: true, revision: false });
} finally {
other.destroy();
}
});
it("follows a choice made in another window", async () => {
view = await annotated();
localStorage.setItem("openidea.vcs.annotateColumns", '{"date":true,"author":false,"revision":true}');
act(() => {
window.dispatchEvent(new StorageEvent("storage", { key: "openidea.vcs.annotateColumns" }));
});
expect(hidden(view)).toEqual(["author"]);
expect(options(view).map((i) => i.checked)).toEqual([true, false, true]);
});
it("falls back to the defaults for a stored value it cannot use", () => {
const defaults = { date: true, author: true, revision: false };
for (const raw of ["{", "[]", "null", '"x"', '{"date":false,"author":false,"revision":false}']) {
localStorage.setItem("openidea.vcs.annotateColumns", raw);
expect(readAnnotateColumns()).toEqual(defaults);
}
localStorage.setItem("openidea.vcs.annotateColumns", '{"date":"no","revision":true}');
expect(readAnnotateColumns()).toEqual({ date: true, author: true, revision: true });
});
it("leaves the editor's classes alone once annotations are closed", async () => {
view = await annotated();
await toggleAnnotations(view, "/repo/src/main.py");
expect(hidden(view)).toEqual([]);
act(() => useAnnotateColumns.setState({ date: false, author: true, revision: true }));
expect(hidden(view)).toEqual([]);
});
});
+86 -2
View File
@@ -2,6 +2,7 @@
import { writeClipboardText } from "@/platform/clipboard";
import { Compartment, Prec, RangeSet, StateEffect, StateField, type Extension, type Text } from "@codemirror/state";
import { EditorView, GutterMarker, ViewPlugin, gutter, type PluginValue, type ViewUpdate } from "@codemirror/view";
import { create } from "zustand";
import type { BlameLine } from "@/bindings";
import { executeAction } from "@/platform/actions";
import { tx } from "@/platform/i18n";
@@ -88,7 +89,10 @@ class BlameMarker extends GutterMarker {
const date = document.createElement("span");
date.className = "cm-vcs-blame-date";
date.textContent = formatDate(l.timeMs);
el.append(date, author);
const revision = document.createElement("span");
revision.className = "cm-vcs-blame-revision";
revision.textContent = l.commit.slice(0, 8);
el.append(date, author, revision);
if (this.bg) {
el.style.setProperty("--blame-bg", this.bg);
el.style.backgroundColor = this.bg;
@@ -98,7 +102,82 @@ class BlameMarker extends GutterMarker {
}
}
const spacer = new BlameMarker({ commit: "f", author: "Mmmmmmmm", timeMs: Date.UTC(2020, 11, 28), summary: "" });
const spacer = new BlameMarker({
commit: "ffffffff",
author: "Mmmmmmmm",
timeMs: Date.UTC(2020, 11, 28),
summary: "",
});
/** The columns the gutter shows (IDEA's annotate Options); kept across sessions. */
const COLUMNS = ["date", "author", "revision"] as const;
type AnnotateColumn = (typeof COLUMNS)[number];
type AnnotateColumns = Record<AnnotateColumn, boolean>;
const COLUMNS_KEY = "openidea.vcs.annotateColumns";
const DEFAULT_COLUMNS: AnnotateColumns = { date: true, author: true, revision: false };
export function readAnnotateColumns(): AnnotateColumns {
try {
const raw: unknown = JSON.parse(localStorage.getItem(COLUMNS_KEY) ?? "null");
const o = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {};
const out = { ...DEFAULT_COLUMNS };
for (const k of COLUMNS) if (typeof o[k] === "boolean") out[k] = o[k];
return COLUMNS.some((k) => out[k]) ? out : { ...DEFAULT_COLUMNS };
} catch {
return { ...DEFAULT_COLUMNS };
}
}
export const useAnnotateColumns = create<AnnotateColumns>(() => readAnnotateColumns());
// Other windows (project, diff) annotate too.
if (typeof window !== "undefined")
window.addEventListener("storage", (e) => {
if (e.key === COLUMNS_KEY) useAnnotateColumns.setState(readAnnotateColumns());
});
/** Shows or hides a column in every annotated editor; the last shown column stays. */
function setAnnotateColumn(column: AnnotateColumn, shown: boolean) {
const next = { ...useAnnotateColumns.getState(), [column]: shown };
if (!COLUMNS.some((k) => next[k])) return;
useAnnotateColumns.setState(next);
try {
localStorage.setItem(COLUMNS_KEY, JSON.stringify(next));
} catch {
// ignore
}
}
/** Hides the columns turned off in Options, here and in editors annotated before the change. */
const annotateColumns = ViewPlugin.define(
(view) => ({ destroy: useAnnotateColumns.subscribe(() => view.dispatch({})) }),
{
provide: () =>
EditorView.editorAttributes.of(() => {
const c = useAnnotateColumns.getState();
const hidden = COLUMNS.filter((k) => !c[k]);
return hidden.length ? { class: hidden.map((k) => `cm-vcs-blame-hide-${k}`).join(" ") } : null;
}),
},
);
const COLUMN_LABELS: Record<AnnotateColumn, string> = { date: "Date", author: "Author", revision: "Revision" };
function optionsMenu() {
const c = useAnnotateColumns.getState();
const shown = COLUMNS.filter((k) => c[k]).length;
return {
label: "Options",
submenu: COLUMNS.map((k) => ({
label: COLUMN_LABELS[k],
checked: c[k],
// At least one column stays, as in IDEA.
disabled: c[k] && shown === 1,
run: () => setAnnotateColumn(k, !c[k]),
})),
};
}
const setBlame = StateEffect.define<BlameLine[]>();
@@ -178,6 +257,9 @@ const blameTheme = EditorView.baseTheme({
},
".cm-vcs-blame-date": { minWidth: "7ch", fontVariantNumeric: "tabular-nums" },
".cm-vcs-blame-author": { minWidth: "9ch" },
".cm-vcs-blame-revision": { fontFamily: "var(--font-mono)" },
"&.cm-vcs-blame-hide-date .cm-vcs-blame-date, &.cm-vcs-blame-hide-author .cm-vcs-blame-author, &.cm-vcs-blame-hide-revision .cm-vcs-blame-revision":
{ display: "none" },
".cm-vcs-blame .cm-gutterElement:hover .cm-vcs-blame-line, .cm-vcs-blame .cm-gutterElement.cm-vcs-blame-same-commit .cm-vcs-blame-line":
{
color: "var(--fg-strong)",
@@ -307,6 +389,7 @@ function blameExtension(lines: BlameLine[], path: string, live: boolean): Extens
{ kind: "separator" as const },
]
: []),
optionsMenu(),
{ label: "Close Annotations", run: () => setAnnotations(view, null, path, false) },
]);
return true;
@@ -315,6 +398,7 @@ function blameExtension(lines: BlameLine[], path: string, live: boolean): Extens
}),
),
ViewPlugin.define((view) => new BlameInteraction(view)),
annotateColumns,
// Static content (a merge pane's side) is not re-blamed on edits / HEAD moves.
...(live ? [ViewPlugin.define((view) => new BlameRefresher(view, path))] : []),
blameTheme,