diff --git a/src/features/vcs/CommitView.module.css b/src/features/vcs/CommitView.module.css index b8112eede..c1f917a9f 100644 --- a/src/features/vcs/CommitView.module.css +++ b/src/features/vcs/CommitView.module.css @@ -354,3 +354,15 @@ [role="treeitem"][aria-selected="true"] .stageButton { visibility: visible; } +.inspectionFix { + margin-left: var(--space-2); + padding: 0; + border: none; + background: none; + color: var(--link); + font: inherit; + cursor: pointer; +} +.inspectionFix:hover { + text-decoration: underline; +} diff --git a/src/features/vcs/CommitView.tsx b/src/features/vcs/CommitView.tsx index 6a0af37d6..f444cab91 100644 --- a/src/features/vcs/CommitView.tsx +++ b/src/features/vcs/CommitView.tsx @@ -15,7 +15,7 @@ import { type MouseEvent, type PointerEvent, } from "react"; -import { tx } from "@/platform/i18n"; +import { tx, txTemplate } from "@/platform/i18n"; import clsx from "clsx"; import { executeAction } from "@/platform/actions"; import { basename, dirname } from "@/platform/project"; @@ -961,6 +961,14 @@ export function ChangesTree({ mode }: { mode: "commit" | "local" | "dialog" }) { ); } +const MISSING_BLANK_LINE = "Missing blank line between subject and body"; + +/** The quick fix for {@link MISSING_BLANK_LINE}: an empty line after the subject. */ +export function addBlankLineAfterSubject(message: string): string { + const end = message.indexOf("\n"); + return end < 0 ? message : `${message.slice(0, end)}\n${message.slice(end)}`; +} + /** Commit message inspections (Settings › Version Control › Commit). */ export function messageProblems( message: string, @@ -976,8 +984,7 @@ export function messageProblems( const out: string[] = []; if (c.limitSubject && (lines[0]?.length ?? 0) > c.subjectLimit) out.push(`Subject line is longer than ${c.subjectLimit} characters`); - if (c.blankLineAfterSubject && lines.length > 1 && lines[1]!.trim() !== "") - out.push("Missing blank line between subject and body"); + if (c.blankLineAfterSubject && lines.length > 1 && lines[1]!.trim() !== "") out.push(MISSING_BLANK_LINE); if (c.limitBody) { const long = lines.slice(1).findIndex((l) => l.length > c.bodyLimit); if (long >= 0) out.push(`Body line ${long + 2} is longer than ${c.bodyLimit} characters`); @@ -1160,7 +1167,29 @@ export function CommitMessageField({ )} {problems.length > 0 && (
- {problems.join(" · ")} + {problems.map((p, i) => ( + + {i > 0 && " · "} + {txTemplate(p)} + {p === MISSING_BLANK_LINE && ( + + )} + + ))}
)} {checkProblems.length > 0 && ( diff --git a/src/features/vcs/__tests__/workflow.test.ts b/src/features/vcs/__tests__/workflow.test.ts index c0b3ceb55..0a0be5f62 100644 --- a/src/features/vcs/__tests__/workflow.test.ts +++ b/src/features/vcs/__tests__/workflow.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import type { BranchInfo } from "@/bindings"; -import { messageProblems, wrapAtMargin } from "../CommitView"; +import { addBlankLineAfterSubject, messageProblems, wrapAtMargin } from "../CommitView"; import { linkifyIssues } from "../issueLinks"; import { isProtectedBranch, @@ -41,6 +41,14 @@ describe("commit message inspections", () => { expect(messageProblems("x".repeat(20), { ...cfg, limitSubject: false })).toEqual([]); }); + it("adds the missing blank line after the subject", () => { + const fixed = addBlankLineAfterSubject("111\n111"); + expect(fixed).toBe("111\n\n111"); + expect(messageProblems(fixed, cfg)).toEqual([]); + expect(addBlankLineAfterSubject("subject\nline 2\nline 3")).toBe("subject\n\nline 2\nline 3"); + expect(addBlankLineAfterSubject("subject")).toBe("subject"); + }); + it("wraps body lines at the last space before the margin", () => { const text = "subject\n\nhello world again"; expect(wrapAtMargin(text, text.length, 12)).toEqual({ text: "subject\n\nhello world\nagain", caret: text.length }); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 4edfce68a..f3969ce08 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2158,5 +2158,8 @@ "The interface could not be extracted": "The interface could not be extracted", "Extract Interface is not supported for {name}": "Extract Interface is not supported for {name}", "Package for new interface": "Package for new interface", - "Untitled files were not saved": "Untitled files were not saved" + "Untitled files were not saved": "Untitled files were not saved", + "Missing blank line between subject and body": "Missing blank line between subject and body", + "Body line {0} is longer than {1} characters": "Body line {0} is longer than {1} characters", + "Add blank line": "Add blank line" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 584c3655b..52dd7ac9c 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -8541,5 +8541,8 @@ "The interface could not be extracted": "无法提取接口", "Extract Interface is not supported for {name}": "{name} 不支持提取接口", "Package for new interface": "新接口的软件包", - "Untitled files were not saved": "未命名文件未保存" + "Untitled files were not saved": "未命名文件未保存", + "Missing blank line between subject and body": "主题和正文之间缺少空行", + "Body line {0} is longer than {1} characters": "正文第 {0} 行超过 {1} 个字符", + "Add blank line": "添加空行" }