Skip to content

Commit 91a8875

Browse files
committed
fix: 完善自我修正函数
1 parent dc72abf commit 91a8875

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

src/cvbManager.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,38 +262,63 @@ export class TCVB {
262262
public static autoFixTCVBContent(original: string): string {
263263
let content = original;
264264

265-
// Remove leading spaces before ## directives
265+
// 移除##指令前的空格
266266
content = content.replace(/^(\s+)##/gm, '##');
267-
268-
// Close unclosed code blocks, inspired by the file-ending reference regex
269-
content = content.replace(/(```[^\n]*\n[\s\S]*?)(?=^##|\n## END_TCVB|(?![\s\S]))/gm, '$1\n```');
270-
271-
// Ensure END_TCVB is present
267+
268+
// 规则4/10:修复代码块包裹问题(精确处理需要代码块的操作)
269+
content = content.replace(
270+
/(## (?:OLD_CONTENT|NEW_CONTENT|OPERATION:CREATE)\n)([\s\S]*?)(?=\n## |\n## END_TCVB|$)/gis,
271+
(match, directive, codeSection) => {
272+
// 检测代码块是否被正确包裹
273+
const trimmed = codeSection.trim();
274+
const hasStart = trimmed.startsWith('```');
275+
const hasEnd = trimmed.endsWith('```');
276+
277+
// 情况1:完全未包裹的代码块
278+
if (!hasStart && !hasEnd) {
279+
return `${directive}\n\`\`\`\n${codeSection.trim()}\n\`\`\``;
280+
}
281+
282+
// 情况2:只有开始标记
283+
if (hasStart && !hasEnd) {
284+
return `${directive}\n${codeSection}\n\`\`\``;
285+
}
286+
287+
// 情况3:只有结束标记(异常情况)
288+
if (!hasStart && hasEnd) {
289+
return `${directive}\n\`\`\`\n${codeSection}`;
290+
}
291+
292+
return match;
293+
}
294+
);
295+
296+
// 确保存在END_TCVB标记
272297
if (!/^##\s*END_TCVB\s*$/m.test(content)) {
273-
content += '\n## END_TCVB';
298+
content += '\n## END_TCVB';
274299
}
275-
276-
// Fix GLOBAL-REPLACE operations
300+
301+
// 严格标准化END_TCVB标记(处理中间/前后空格)
302+
content = content.replace(/^##\s*END_TCVB\s*$/gm, '## END_TCVB');
303+
304+
// 修复GLOBAL-REPLACE操作
277305
content = content.replace(
278-
/## OPERATION:GLOBAL-REPLACE\n([\s\S]*?)(?=\n## OPERATION:|\n## FILE:|\n## END_TCVB|$)/g,
279-
(match) => {
280-
const hasOldContent = /## OLD_CONTENT/.test(match);
281-
const hasNewContent = /## NEW_CONTENT/.test(match);
306+
/## OPERATION:GLOBAL-REPLACE\n([\s\S]*?)(?=\n## OPERATION:|\n## FILE:|\n## END_TCVB|$)/g,
307+
(match) => {
308+
const hasOldContent = /## OLD_CONTENT/.test(match);
309+
const hasNewContent = /## NEW_CONTENT/.test(match);
282310

283-
if (!hasOldContent && hasNewContent) {
284-
// Convert to CREATE if only NEW_CONTENT is present
285-
const newContentMatch = match.match(/## NEW_CONTENT\n```([\s\S]*?)```/);
286-
const newContentCode = newContentMatch ? newContentMatch[1] : '';
287-
return `## OPERATION:CREATE\n\`\`\`\n${newContentCode}\n\`\`\``;
288-
} else if (hasOldContent && !hasNewContent) {
289-
// Add empty NEW_CONTENT if only OLD_CONTENT is present
290-
return match + '\n## NEW_CONTENT\n```\n```';
311+
if (!hasOldContent && hasNewContent) {
312+
const newContentMatch = match.match(/## NEW_CONTENT\n```([\s\S]*?)```/);
313+
const newContentCode = newContentMatch ? newContentMatch[1] : '';
314+
return `## OPERATION:CREATE\n\`\`\`\n${newContentCode}\n\`\`\``;
315+
} else if (hasOldContent && !hasNewContent) {
316+
return match + '\n## NEW_CONTENT\n```\n```';
317+
}
318+
return match;
291319
}
292-
// Return unchanged if both are present or both are missing (assuming minimal valid structure)
293-
return match;
294-
}
295320
);
296-
321+
297322
return content;
298323
}
299324

0 commit comments

Comments
 (0)