Skip to content

Commit 10508fb

Browse files
committed
fix:新增一种缺失OLD_CONTENT情况下的自动修复
1 parent 4dba0ad commit 10508fb

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/cvbManager.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,40 @@ export class TCVB {
290290
}
291291
);
292292

293+
// 新增:处理GLOBAL-REPLACE中缺失OLD_CONTENT的情况
294+
content = content.replace(
295+
/## OPERATION:GLOBAL-REPLACE\n([\s\S]*?)(?=\n## NEW_CONTENT|\n## OPERATION:|\n## FILE:|\n## END_TCVB|$)/g,
296+
(match, codeSection) => {
297+
// 如果已经有OLD_CONTENT标记,不做处理
298+
if (/## OLD_CONTENT/.test(match) || /## OLD_CONTENT/.test(match)) {
299+
return match;
300+
}
301+
302+
const trimmed = codeSection.trim();
303+
// 如果OPERATION和NEW_CONTENT之间有非空内容,视为缺失OLD_CONTENT
304+
if (trimmed) {
305+
// 处理代码块包裹
306+
const hasStart = trimmed.startsWith('```');
307+
const hasEnd = trimmed.endsWith('```');
308+
309+
let fixedCode = codeSection.trimEnd();
310+
311+
if (!hasStart && !hasEnd) {
312+
fixedCode = `## OLD_CONTENT\n\`\`\`\n${fixedCode}\n\`\`\``;
313+
} else if (hasStart && !hasEnd) {
314+
fixedCode = `## OLD_CONTENT\n${fixedCode}\n\`\`\``;
315+
} else if (!hasStart && hasEnd) {
316+
fixedCode = `## OLD_CONTENT\n\`\`\`\n${fixedCode}`;
317+
} else {
318+
fixedCode = `## OLD_CONTENT\n${fixedCode}`;
319+
}
320+
321+
return `## OPERATION:GLOBAL-REPLACE\n${fixedCode}`;
322+
}
323+
return match;
324+
}
325+
);
326+
293327
// 规则4/10:修复代码块包裹问题(精确处理需要代码块的操作)
294328
content = content.replace(
295329
/(## (?:OLD_CONTENT|NEW_CONTENT|OPERATION:CREATE)\n)([\s\S]*?)(?=\n## |\n## END_TCVB|$)/gis,
@@ -349,7 +383,6 @@ export class TCVB {
349383

350384
return content;
351385
}
352-
353386
private parse(tcStrContent: string): void {
354387

355388
tcStrContent = normalizeAllLineEndings(tcStrContent);

src/test/fixapi.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,39 @@ console.log('test')
140140
});
141141
});
142142

143+
// 测试用例3: GLOBAL-REPLACE中缺失OLD_CONTENT但实际有内容
144+
test('Case6: GLOBAL-REPLACE with implicit OLD_CONTENT', () => {
145+
const input = `
146+
## FILE: d:\\lab\\GPT-SoVITS_yefanFork\\webui.py
147+
## OPERATION:GLOBAL-REPLACE
148+
from tools import my_utils
149+
from tools.i18n.i18n import I18nAuto, scan_language_list
150+
## NEW_CONTENT
151+
from tools import my_utils
152+
from tools.i18n.i18n import I18nAuto, scan_language_list
153+
from webui_config import grTextBox_autoSave, config_saver`;
154+
155+
const expected = `
156+
## FILE: d:\\lab\\GPT-SoVITS_yefanFork\\webui.py
157+
## OPERATION:GLOBAL-REPLACE
158+
## OLD_CONTENT
159+
\`\`\`
160+
from tools import my_utils
161+
from tools.i18n.i18n import I18nAuto, scan_language_list
162+
\`\`\`
163+
## NEW_CONTENT
164+
\`\`\`
165+
from tools import my_utils
166+
from tools.i18n.i18n import I18nAuto, scan_language_list
167+
from webui_config import grTextBox_autoSave, config_saver
168+
\`\`\`
169+
## END_TCVB`.trim();
170+
171+
assert.strictEqual(TCVB.autoFixTCVBContent(input).trim(), expected);
172+
});
173+
143174
// 测试用例5:无效的闭合顺序
144-
test('Case6: miss markdown', () => {
175+
test('Case7: miss markdown', () => {
145176
const input = `
146177
## BEGIN_TCVB
147178
## FILE:d:/lab/GPT-SoVITS_yefanFork/tools/subfix_webui.py

0 commit comments

Comments
 (0)