Skip to content

Commit e637a54

Browse files
committed
feat:TCVB添加自动格式修复函数,增加成功率
1 parent 5b4d50c commit e637a54

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/cvbManager.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,50 @@ export class TCVB {
250250
private m_arrOperations: TcvbOperation[] = [];
251251

252252
constructor(tcStrContent: string) {
253-
this.parse(tcStrContent);
253+
try {
254+
this.parse(tcStrContent);
255+
} catch (error) {
256+
const fixedContent = TCVB.autoFixTCVBContent(tcStrContent);
257+
this.parse(fixedContent);
258+
}
259+
}
260+
261+
public static autoFixTCVBContent(original: string): string {
262+
let content = original;
263+
264+
// Remove leading spaces before ## directives
265+
content = content.replace(/^(\s+)##/gm, '##');
266+
267+
// Close unclosed code blocks, inspired by the file-ending reference regex
268+
content = content.replace(/(```[^\n]*\n[\s\S]*?)(?=^##|\n## END_TCVB|(?![\s\S]))/gm, '$1\n```');
269+
270+
// Ensure END_TCVB is present
271+
if (!/^##\s*END_TCVB\s*$/m.test(content)) {
272+
content += '\n## END_TCVB';
273+
}
274+
275+
// Fix GLOBAL-REPLACE operations
276+
content = content.replace(
277+
/## OPERATION:GLOBAL-REPLACE\n([\s\S]*?)(?=\n## OPERATION:|\n## FILE:|\n## END_TCVB|$)/g,
278+
(match) => {
279+
const hasOldContent = /## OLD_CONTENT/.test(match);
280+
const hasNewContent = /## NEW_CONTENT/.test(match);
281+
282+
if (!hasOldContent && hasNewContent) {
283+
// Convert to CREATE if only NEW_CONTENT is present
284+
const newContentMatch = match.match(/## NEW_CONTENT\n```([\s\S]*?)```/);
285+
const newContentCode = newContentMatch ? newContentMatch[1] : '';
286+
return `## OPERATION:CREATE\n\`\`\`\n${newContentCode}\n\`\`\``;
287+
} else if (hasOldContent && !hasNewContent) {
288+
// Add empty NEW_CONTENT if only OLD_CONTENT is present
289+
return match + '\n## NEW_CONTENT\n```\n```';
290+
}
291+
// Return unchanged if both are present or both are missing (assuming minimal valid structure)
292+
return match;
293+
}
294+
);
295+
296+
return content;
254297
}
255298

256299
private parse(tcStrContent: string): void {

0 commit comments

Comments
 (0)