Skip to content

Commit 1ee654c

Browse files
committed
fix:新增一种缺失OLD_CONTENT情况下的自动修复
1 parent a96e76c commit 1ee654c

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/cvbManager.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,28 @@ export class TCVB {
294294
content = content.replace(
295295
/## OPERATION:GLOBAL-REPLACE\n([\s\S]*?)(?=\n## NEW_CONTENT|\n## OPERATION:|\n## FILE:|\n## END_TCVB|$)/g,
296296
(match, codeSection) => {
297-
// 如果已经有OLD_CONTENT标记,不做处理
298-
if (/## OLD_CONTENT/.test(match) || /## OLD_CONTENT/.test(match)) {
297+
// 确保我们不会匹配到 NEW_CONTENT 之后的内容
298+
const beforeNewContent = codeSection.split('## NEW_CONTENT')[0];
299+
const trimmed = beforeNewContent.trim();
300+
301+
// 如果只有空白或没有实际内容,转换为CREATE操作
302+
if (!trimmed) {
303+
// 找到后续的NEW_CONTENT内容
304+
const newContentMatch = match.match(/## NEW_CONTENT\n([\s\S]*)/);
305+
if (newContentMatch) {
306+
const newContent = newContentMatch[1].trim();
307+
return `## OPERATION:CREATE\n\`\`\`\n${newContent}\n\`\`\``;
308+
}
299309
return match;
300310
}
301311

302-
const trimmed = codeSection.trim();
303-
// 如果OPERATION和NEW_CONTENT之间有非空内容,视为缺失OLD_CONTENT
304-
if (trimmed) {
312+
// 如果有内容但缺少OLD_CONTENT标记
313+
if (!/## OLD_CONTENT/.test(trimmed)) {
305314
// 处理代码块包裹
306315
const hasStart = trimmed.startsWith('```');
307316
const hasEnd = trimmed.endsWith('```');
308317

309-
let fixedCode = codeSection.trimEnd();
318+
let fixedCode = beforeNewContent.trimEnd();
310319

311320
if (!hasStart && !hasEnd) {
312321
fixedCode = `## OLD_CONTENT\n\`\`\`\n${fixedCode}\n\`\`\``;
@@ -318,12 +327,17 @@ export class TCVB {
318327
fixedCode = `## OLD_CONTENT\n${fixedCode}`;
319328
}
320329

321-
return `## OPERATION:GLOBAL-REPLACE\n${fixedCode}`;
330+
// 保留原有的NEW_CONTENT部分
331+
const newContentPart = match.includes('## NEW_CONTENT')
332+
? match.split('## NEW_CONTENT')[1]
333+
: '';
334+
335+
return `## OPERATION:GLOBAL-REPLACE\n${fixedCode}` +
336+
(newContentPart ? `\n## NEW_CONTENT${newContentPart}` : '');
322337
}
323338
return match;
324339
}
325340
);
326-
327341
// 规则4/10:修复代码块包裹问题(精确处理需要代码块的操作)
328342
content = content.replace(
329343
/(## (?:OLD_CONTENT|NEW_CONTENT|OPERATION:CREATE)\n)([\s\S]*?)(?=\n## |\n## END_TCVB|$)/gis,

0 commit comments

Comments
 (0)