🐛 fix(vscode): 重写注释直接调用 /ecc:writing-code-comments,不再自己读技能文件

This commit is contained in:
cruldra committed 2026-10-03 03:22:20 +08:00
1 parent f2490b0505
commit 5f1352df86
3 files changed
+10 -72

No files matched your search

+5 -40
View File
@@ -3,19 +3,13 @@
* 认 Python 的三引号、井号行注释,以及块注释和双斜杠行注释。认不出就不改。
*/
import { readFile } from 'node:fs/promises'
import { homedir } from 'node:os'
import { join } from 'node:path'
import { commonIndent, splitLines } from './editText'
/** 贴给模型的代码最多这么多行,再长就截断并在提示里说明。 */
export const COMMENT_CONTEXT_LINES = 120
/** 运行时按这个顺序找技能,用到的第一份。不把技能正文放进仓库。 */
export const COMMENT_SKILL_PATHS = [
join(homedir(), '.claude/plugins/marketplaces/ecc/skills/writing-code-comments/SKILL.md'),
join(homedir(), '.claude/writing-code-comments/SKILL.md'),
] as const
/** 跟在对话框里输入这个命令一样,由 Claude Code 自己加载写注释的技能。 */
export const COMMENT_SKILL = '/ecc:writing-code-comments'
const DOUBLE_DOC = '"""'
const SINGLE_DOC = '\'' + '\'' + '\''
@@ -28,31 +22,6 @@ export function commentSource(lines: readonly string[]): string {
return lines.map(line => line.slice(Math.min(width, line.length))).join('\n')
}
/** 去掉技能文件开头的 frontmatter,剩下的才是给模型的规范。 */
export function skillBody(markdown: string): string {
const text = markdown.charCodeAt(0) === 0xFEFF ? markdown.slice(1) : markdown
const fence = text.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/)
return (fence ? text.slice(fence[0].length) : text).trim()
}
function missingFile(err: unknown): boolean {
return typeof err === 'object' && err !== null && 'code' in err && err.code === 'ENOENT'
}
/** 读到的第一份技能正文;两处都没有就是 undefined。读失败(不是没有文件)照样抛。 */
export async function loadCommentSkill(read: (path: string) => Promise<string> = path => readFile(path, 'utf8')): Promise<string | undefined> {
for (const path of COMMENT_SKILL_PATHS) {
try {
return skillBody(await read(path))
}
catch (err) {
if (!missingFile(err))
throw err
}
}
return undefined
}
interface DocShell {
prefix: string
quote: string
@@ -307,8 +276,6 @@ function clip(text: string, what: string): { text: string, note: string } {
}
export interface CommentRewriteInput {
/** 去掉 frontmatter 的技能正文;没有技能就不组提示 */
skill: string | undefined
/** 去掉共同缩进的注释原文 */
comment: string
/** 整份文件,用来在是模块说明时拿掉这段注释 */
@@ -320,10 +287,8 @@ export interface CommentRewriteInput {
following?: string
}
/** 发给模型的整段话。注释认不出,或没有技能,就不组。 */
/** 发给模型的整段话。开头是技能命令,Claude Code 自己加载技能。注释认不出就不组。 */
export function commentRewritePrompt(input: CommentRewriteInput): string | undefined {
if (input.skill === undefined)
return undefined
const body = commentProse(input.comment)
if (body === undefined)
return undefined
@@ -334,8 +299,8 @@ export function commentRewritePrompt(input: CommentRewriteInput): string | undef
? clip(input.following, '这块代码')
: { text: '(后面没有紧挨着的代码)', note: '' }
return [
input.skill,
'只按上面的技能重写这一段注释。用中文。不要改代码,不要加技能没要求的中文名记号。只输出新注释的正文,不要定界符,不要解释。',
COMMENT_SKILL,
'只按这个技能重写这一段注释。用中文。不要改代码,不要加技能没要求的中文名记号。只输出新注释的正文,不要定界符,不要解释。',
`这段注释现在的正文:\n${body}`,
`它说明的代码:\n${context.text}${context.note}`,
].join('\n\n')