diff --git a/docs/en/guide/vcs.mdx b/docs/en/guide/vcs.mdx index 27518b82f..863e670a1 100644 --- a/docs/en/guide/vcs.mdx +++ b/docs/en/guide/vcs.mdx @@ -247,6 +247,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; a click on it opens the commit unless **Open the commit when the inline blame is clicked** is off. +- **Options › Colors** in the gutter's context menu tints the gutter by **Author**, by **Order** of changes (the default) or not at all (**Hide**) in every annotated editor. - **Options › Names** in the gutter's context menu shows authors as **Initials**, **Last Name** (the default), **First Name** or **Full Name** in every annotated editor. - **Annotate Revision** in an annotated line's menu opens the file as it was at that commit, annotated, next to the working copy. A file renamed since then is read under its name at that commit. - **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. diff --git a/docs/zh/guide/vcs.mdx b/docs/zh/guide/vcs.mdx index 1dd05efdc..44ecde10b 100644 --- a/docs/zh/guide/vcs.mdx +++ b/docs/zh/guide/vcs.mdx @@ -245,6 +245,7 @@ Git 工具窗口的 **日志** 标签页显示提交图。 ## Blame 与历史 - **使用 Git Blame 注解** 在边栏中逐行显示作者和日期(也可以从边栏右键菜单打开)。点击某条记录会在日志中打开对应提交。在 Git 设置中打开 **为当前行显示内联 Git Blame**,会在光标所在行末尾显示作者;点击它会打开对应提交,关闭 **点击内联 Git Blame 时打开提交** 后则不会。 +- 边栏右键菜单的 **选项 › 颜色** 可让所有已注解的编辑器按 **作者**、按变更 **顺序**(默认)上色,或不上色(**隐藏**)。 - 边栏右键菜单的 **选项 › 姓名** 可让所有已注解的编辑器以 **首字母缩写**、**姓**(默认)、**名** 或 **全名**显示作者。 - 注解行的右键菜单中,**注解此修订版** 会打开该提交时的文件(已注解),与工作区文件并排显示。此后重命名过的文件按它在该提交中的名称读取。 - 注解行的右键菜单中,**标注上一版本** 会打开该提交之前版本的文件(已注解),与这个提交的版本并排显示。 diff --git a/src/features/vcs/__tests__/blame.test.tsx b/src/features/vcs/__tests__/blame.test.tsx index 85c00f01f..907c69b94 100644 --- a/src/features/vcs/__tests__/blame.test.tsx +++ b/src/features/vcs/__tests__/blame.test.tsx @@ -8,10 +8,12 @@ import { useProject } from "@/platform/project"; import { annotationsShown, blameSlot, + readAnnotateColors, readAnnotateColumns, readAnnotateNames, shortAuthorName, toggleAnnotations, + useAnnotateColors, useAnnotateColumns, useAnnotateNames, } from "../blame"; @@ -258,6 +260,7 @@ describe("annotate Options", () => { localStorage.clear(); useAnnotateColumns.setState(readAnnotateColumns()); useAnnotateNames.setState({ names: readAnnotateNames() }); + useAnnotateColors.setState({ colors: readAnnotateColors() }); }); async function annotated(doc = "first\nsecond\nthird") { @@ -494,6 +497,64 @@ describe("annotate Options", () => { } }); + it("tints by commit order by default and switches every annotated editor from Options › Colors", async () => { + const thirdSha = "9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b"; + mocks.blame.mockResolvedValue({ + status: "ok", + data: [...lines, { commit: thirdSha, author: "Dan Crankshaw", timeMs: Date.UTC(2024, 1, 1), summary: "Older" }], + }); + view = await annotated("first\nsecond\nthird\nfourth"); + const historical = new EditorView({ + parent: document.body, + state: EditorState.create({ doc: "one\ntwo\nthree\nfour", extensions: [blameSlot()] }), + }); + const bg = (v: EditorView, commit: string) => + v.dom.querySelector(`.cm-vcs-blame [data-commit="${commit}"]`)!.style.getPropertyValue("--blame-bg"); + try { + await toggleAnnotations(historical, "/repo/src/old.py", "one\ntwo\nthree\nfour", sha); + const colors = options(view).find((i) => i.label === "Colors")!.submenu!; + expect(colors.map((i) => [i.label, i.checked])).toEqual([ + ["Author", false], + ["Order", true], + ["Hide", false], + ]); + const order = [sha, otherSha, thirdSha].map((c) => bg(view!, c)); + expect(new Set(order).size).toBe(3); + + await pick(colors[0]!); + for (const v of [view, historical]) { + expect(bg(v, otherSha)).toBeTruthy(); + expect(bg(v, otherSha)).toBe(bg(v, thirdSha)); + expect(bg(v, sha)).not.toBe(bg(v, otherSha)); + } + // IDEA hands sorted authors the palette colours in turn: Aleks the first, Dan the second. + expect(bg(view, sha)).toContain("--project-color-5"); + expect(bg(view, otherSha)).toContain("--project-color-7"); + expect(localStorage.getItem("openidea.vcs.annotateColors")).toBe("author"); + expect( + options(view) + .find((i) => i.label === "Colors")! + .submenu!.map((i) => i.checked), + ).toEqual([true, false, false]); + + await pick(colors[2]!); + for (const v of [view, historical]) expect([sha, otherSha, thirdSha].map((c) => bg(v, c))).toEqual(["", "", ""]); + + localStorage.setItem("openidea.vcs.annotateColors", "order"); + await act(async () => { + window.dispatchEvent(new StorageEvent("storage", { key: "openidea.vcs.annotateColors" })); + await vi.advanceTimersByTimeAsync(20); + }); + expect([sha, otherSha, thirdSha].map((c) => bg(view!, c))).toEqual(order); + for (const raw of ["", "ORDER", "none", '"author"']) { + localStorage.setItem("openidea.vcs.annotateColors", raw); + expect(readAnnotateColors()).toBe("order"); + } + } finally { + historical.destroy(); + } + }); + it("shortens names as IDEA does", () => { const all = (author: string) => (["initials", "lastName", "firstName", "fullName"] as const).map((t) => shortAuthorName(author, t)); diff --git a/src/features/vcs/blame.ts b/src/features/vcs/blame.ts index 696c47f09..29eb4e1e3 100644 --- a/src/features/vcs/blame.ts +++ b/src/features/vcs/blame.ts @@ -177,6 +177,57 @@ function setAnnotateNames(names: AnnotateNames, save = true) { } } +/** How the gutter is tinted (IDEA's annotate Options › Colors; Order by default, as there); kept across sessions. */ +const COLORS_KEY = "openidea.vcs.annotateColors"; +const COLOR_MODES = ["author", "order", "hide"] as const; +type AnnotateColors = (typeof COLOR_MODES)[number]; + +export function readAnnotateColors(): AnnotateColors { + try { + const raw = localStorage.getItem(COLORS_KEY); + return COLOR_MODES.find((m) => m === raw) ?? "order"; + } catch { + return "order"; + } +} + +export const useAnnotateColors = create<{ colors: AnnotateColors }>(() => ({ colors: readAnnotateColors() })); + +if (typeof window !== "undefined") + window.addEventListener("storage", (e) => { + if (e.key === COLORS_KEY) setAnnotateColors(readAnnotateColors(), false); + }); + +function setAnnotateColors(colors: AnnotateColors, save = true) { + if (useAnnotateColors.getState().colors === colors) return; + useAnnotateColors.setState({ colors }); + if (!save) return; + try { + localStorage.setItem(COLORS_KEY, colors); + } catch { + // ignore + } +} + +/** IDEA's ColorMode.AUTHOR: authors sorted by name take the palette colours in turn, keyed by commit. */ +function authorBackgrounds(lines: readonly BlameLine[]): Map { + const committed = lines.filter((l) => !isUncommitted(l)); + const authors = [...new Set(committed.map((l) => l.author))].sort(); + const tint = new Map( + authors.map((a, i) => [ + a, + `color-mix(in srgb, ${BLAME_PALETTE[i % BLAME_PALETTE.length]!} 18%, var(--bg-editor-gutter))`, + ]), + ); + return new Map(committed.map((l) => [l.commit, tint.get(l.author)!])); +} + +function backgrounds(lines: readonly BlameLine[]): Map { + const { colors } = useAnnotateColors.getState(); + if (colors === "hide") return new Map(); + return colors === "author" ? authorBackgrounds(lines) : commitBackgrounds(lines); +} + /** Characters IDEA's ShortNameType splits an email's local part and initials on. */ const NAME_DELIMITERS = /[.,<>()":_-]/g; @@ -251,11 +302,13 @@ const NAME_LABELS: Record = { firstName: "First Name", fullName: "Full Name", }; +const COLOR_LABELS: Record = { author: "Author", order: "Order", hide: "Hide" }; function optionsMenu(): MenuItem { const c = useAnnotateColumns.getState(); const shown = COLUMNS.filter((k) => c[k]).length; const { names } = useAnnotateNames.getState(); + const { colors } = useAnnotateColors.getState(); const { ignoreWhitespace, movement } = workflowSettings().vcs.git.annotate; return { label: "Options", @@ -266,6 +319,14 @@ function optionsMenu(): MenuItem { disabled: c[k] && shown === 1, run: () => setAnnotateColumn(k, !c[k]), })), + { + label: "Colors", + submenu: COLOR_MODES.map((m) => ({ + label: COLOR_LABELS[m], + checked: colors === m, + run: () => setAnnotateColors(m), + })), + }, { label: "Names", submenu: NAME_TYPES.map((t) => ({ @@ -308,7 +369,7 @@ interface BlameState { } function build(lines: BlameLine[], doc: Text): BlameState { - const bgs = commitBackgrounds(lines); + const bgs = backgrounds(lines); const { names } = useAnnotateNames.getState(); const labels = new Map(); const out = []; @@ -388,6 +449,7 @@ class BlameRefresher implements PluginValue { () => this.schedule(0), ), useAnnotateNames.subscribe(() => this.schedule(0)), + useAnnotateColors.subscribe(() => this.schedule(0)), ]; if (this.live) { this.off.push( diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 3d3c96299..7c1cf516f 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -771,6 +771,9 @@ "Ignore Whitespaces": "Ignore Whitespaces", "Detect Movements Within File": "Detect Movements Within File", "Detect Movements Across Files": "Detect Movements Across Files", + "Colors": "Colors", + "Order": "Order", + "Hide": "Hide", "Heap: {label}": "Heap: {label}", "Writing the heap dump…": "Writing the heap dump…", "Linking objects…": "Linking objects…", diff --git a/src/platform/locales/text/ru.json b/src/platform/locales/text/ru.json index e5c5b4d6f..6a9e85f31 100644 --- a/src/platform/locales/text/ru.json +++ b/src/platform/locales/text/ru.json @@ -2092,6 +2092,8 @@ "Ignore Whitespaces": "Игнорировать пробелы", "Detect Movements Within File": "Определять перемещения внутри файла", "Detect Movements Across Files": "Определять перемещения между файлами", + "Colors": "Цвета", + "Order": "Порядок", "Names": "Имена", "Initials": "Инициалы", "Last Name": "Фамилия", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 7819a8ce5..019fdbd50 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -2706,6 +2706,8 @@ "Review comment": "審查評論", "Reviewers": "審查者", "Revision": "修訂", + "Colors": "顏色", + "Order": "順序", "Names": "姓名", "Initials": "首字母縮寫", "Last Name": "姓", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index d2470dfb6..8c92abab3 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -2190,6 +2190,8 @@ "Ignore Whitespaces": "忽略空白", "Detect Movements Within File": "检测文件内的行移动", "Detect Movements Across Files": "检测跨文件的行移动", + "Colors": "颜色", + "Order": "顺序", "Names": "姓名", "Initials": "首字母缩写", "Last Name": "姓",