From 138209c627d40320a393076cb03e642bdced6b03 Mon Sep 17 00:00:00 2001 From: Muu Date: Fri, 27 Nov 2026 05:09:25 +0800 Subject: [PATCH] feat(vcs): show Today / Yesterday in the annotate gutter with pretty dates (#6110) The annotate gutter now shows Today / Yesterday when the global date format's "Use pretty formatting" is on, as IDEA's annotate does (FileAnnotation.formatDate -> DateFormatUtil.formatPrettyDate); older dates, and all dates with pretty formatting off, keep the short date. New formatPrettyDay next to formatTimestamp. Co-authored-by: muutot <92625353+muutot@users.noreply.github.com> --- docs/en/reference/settings.mdx | 2 +- docs/zh/reference/settings.mdx | 2 +- src/features/vcs/__tests__/blame.test.tsx | 21 +++++++++++++++++++++ src/features/vcs/blame.ts | 4 ++-- src/platform/dateFormat.test.ts | 14 ++++++++++++++ src/platform/dateFormat.ts | 13 +++++++++++++ 6 files changed, 52 insertions(+), 4 deletions(-) diff --git a/docs/en/reference/settings.mdx b/docs/en/reference/settings.mdx index c26aaf0a74..0c7db2222e 100644 --- a/docs/en/reference/settings.mdx +++ b/docs/en/reference/settings.mdx @@ -90,7 +90,7 @@ Settings › Appearance & Behavior › System Settings, unless noted. | `dateFormat.overrideSystem` | `false` | **Override system date and time format** (Date Formats page). | | `dateFormat.pattern` | `"dd.MM.yyyy"` | Date pattern with `dd`, `MM`, `MMM`, `yy`, `yyyy`. | | `dateFormat.use24Hour` | `true` | 24-hour time. | -| `dateFormat.pretty` | `true` | "Today", "Yesterday", "5 min ago". | +| `dateFormat.pretty` | `true` | "Today", "Yesterday", "5 min ago" (in lists with time, in the annotate gutter as the day alone). | ### `[appearance]` diff --git a/docs/zh/reference/settings.mdx b/docs/zh/reference/settings.mdx index 6365290a84..bab3290a8e 100644 --- a/docs/zh/reference/settings.mdx +++ b/docs/zh/reference/settings.mdx @@ -90,7 +90,7 @@ exclude = ["**/.git", "**/node_modules", "**/target", "**/dist"] | `dateFormat.overrideSystem` | `false` | **覆盖系统日期和时间格式**(日期格式页)。 | | `dateFormat.pattern` | `"dd.MM.yyyy"` | 日期格式,可用 `dd`、`MM`、`MMM`、`yy`、`yyyy`。 | | `dateFormat.use24Hour` | `true` | 使用 24 小时制。 | -| `dateFormat.pretty` | `true` | 显示“今天”“昨天”“5 分钟前”这类相对时间。 | +| `dateFormat.pretty` | `true` | 显示“今天”“昨天”“5 分钟前”这类相对时间(列表中带时间,注解装订区域仅显示日期)。 | ### `[appearance]` diff --git a/src/features/vcs/__tests__/blame.test.tsx b/src/features/vcs/__tests__/blame.test.tsx index 38a6f801d8..85c00f01ff 100644 --- a/src/features/vcs/__tests__/blame.test.tsx +++ b/src/features/vcs/__tests__/blame.test.tsx @@ -276,6 +276,27 @@ describe("annotate Options", () => { const hidden = (v: EditorView) => ["date", "author", "revision"].filter((c) => v.dom.classList.contains(`cm-vcs-blame-hide-${c}`)); + it("prints Today/Yesterday in the date cell when pretty formatting is on", async () => { + const today = Date.now(); + const yesterday = today - 86_400_000; + const recentLines: BlameLine[] = [ + { commit: sha, author: "Aleks Zlateski", timeMs: today, summary: "today" }, + { commit: otherSha, author: "Dan Crankshaw", timeMs: yesterday, summary: "yesterday" }, + ]; + mocks.blame.mockResolvedValue({ status: "ok", data: recentLines }); + useProject.setState({ settings: { general: { dateFormat: { pretty: true } } } }); + const created = new EditorView({ + parent: document.body, + state: EditorState.create({ doc: "first\nsecond\nthird", extensions: [blameSlot()] }), + }); + view = created; + await toggleAnnotations(created, "/repo/src/main.py"); + const cell = (commit: string) => + created.dom.querySelector(`.cm-vcs-blame [data-commit="${commit}"] .cm-vcs-blame-date`)?.textContent; + expect(cell(sha)).toBe("Today"); + expect(cell(otherSha)).toBe("Yesterday"); + }); + it("shows date and author by default and adds the revision from Options", async () => { view = await annotated(); expect( diff --git a/src/features/vcs/blame.ts b/src/features/vcs/blame.ts index 0599d40669..696c47f09b 100644 --- a/src/features/vcs/blame.ts +++ b/src/features/vcs/blame.ts @@ -11,7 +11,7 @@ import { onWorkflowSettingsChange, workflowSettings } from "@/platform/workflowS import { showContextMenu, type MenuItem } from "@/ui"; import { notifications } from "@/workbench/contributions"; import { closeBlamePopup, scheduleCloseBlamePopup, scheduleOpenBlamePopup } from "./BlamePopup"; -import { formatDate } from "./model"; +import { formatPrettyDay } from "@/platform/dateFormat"; import { saveVcsSetting } from "./settingsUi"; import { useVcs } from "./store"; @@ -318,7 +318,7 @@ function build(lines: BlameLine[], doc: Text): BlameState { let label = labels.get(line.commit); if (!label && !isUncommitted(line)) { label = { - date: formatDate(line.timeMs), + date: line.timeMs ? formatPrettyDay(line.timeMs) : "", author: shortAuthorName(line.author, names), revision: line.commit.slice(0, 8), }; diff --git a/src/platform/dateFormat.test.ts b/src/platform/dateFormat.test.ts index 32e9b9dfae..095e3662bd 100644 --- a/src/platform/dateFormat.test.ts +++ b/src/platform/dateFormat.test.ts @@ -5,6 +5,7 @@ import { formatDateTime, formatDay, formatPattern, + formatPrettyDay, formatTimestamp, } from "./dateFormat"; @@ -33,6 +34,19 @@ describe("dateFormat", () => { expect(formatTimestamp(d.getTime(), now, custom)).toBe("05.11.2026 09:07"); }); + it("shows Today/Yesterday without a clock time for blame, older dates keep the short date", () => { + const now = new Date(2026, 10, 13, 12, 0).getTime(); + const pretty = { ...system, pretty: true }; + const older = new Date(2026, 10, 5, 9, 7); + const short = { year: "2-digit", month: "2-digit", day: "2-digit" } as const; + expect(formatPrettyDay(new Date(2026, 10, 13, 9, 7).getTime(), now, pretty)).toBe("Today"); + expect(formatPrettyDay(new Date(2026, 10, 12, 23, 30).getTime(), now, pretty)).toBe("Yesterday"); + expect(formatPrettyDay(older.getTime(), now, pretty)).toBe(older.toLocaleDateString(undefined, short)); + expect(formatPrettyDay(new Date(2026, 10, 13, 9, 7).getTime(), now, { ...system, pretty: false })).toBe( + new Date(2026, 10, 13, 9, 7).toLocaleDateString(undefined, short), + ); + }); + it("formats another date with the same cached formatter", () => { const e = new Date(2025, 0, 31); formatDay(d, system); diff --git a/src/platform/dateFormat.ts b/src/platform/dateFormat.ts index 75d5f7cb12..02cbc0b32d 100644 --- a/src/platform/dateFormat.ts +++ b/src/platform/dateFormat.ts @@ -99,3 +99,16 @@ export function formatTimestamp(ms: number, now = Date.now(), o: DateFormatOptio } return formatDateTime(d, o); } + +/** + * Day without clock time (blame gutter): "Today" / "Yesterday" when pretty + * formatting is on, as IDEA's annotate shows; older dates keep the short date. + */ +export function formatPrettyDay(ms: number, now = Date.now(), o: DateFormatOptions = dateFormatOptions()): string { + const d = new Date(ms); + if (o.pretty) { + if (d.toDateString() === new Date(now).toDateString()) return tx("Today"); + if (d.toDateString() === new Date(now - 86_400_000).toDateString()) return tx("Yesterday"); + } + return formatDay(d, o, true); +}