Skip to content

Commit 3e0e203

Browse files
committed
debug templates
1 parent cd48255 commit 3e0e203

File tree

1 file changed

+84
-36
lines changed

1 file changed

+84
-36
lines changed

src/leetCodeExecutor.ts

Lines changed: 84 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,13 @@ using namespace std;
460460
const beforeEnd = codeTemplate.substring(0, endIndex + endMarker.length);
461461
const afterEnd = codeTemplate.substring(endIndex + endMarker.length);
462462

463-
const debugTemplate = this.generateCppDebugTemplate(parsedArgs);
463+
const debugTemplate = this.generateCppDebugTemplate(parsedArgs, codeTemplate);
464464
return beforeEnd + debugTemplate + afterEnd;
465465
} else {
466466
// Если маркер не найден, добавляем в конец файла
467467
const debugTemplate = `
468468
// @lc code=end
469-
` + this.generateCppDebugTemplate(parsedArgs);
469+
` + this.generateCppDebugTemplate(parsedArgs, codeTemplate);
470470
return codeTemplate + debugTemplate;
471471
}
472472
}
@@ -491,13 +491,13 @@ using namespace std;
491491
const beforeEnd = codeTemplate.substring(0, endIndex + endMarker.length);
492492
const afterEnd = codeTemplate.substring(endIndex + endMarker.length);
493493

494-
const debugTemplate = this.generateCppDebugTemplate(parsedArgs);
494+
const debugTemplate = this.generateCppDebugTemplate(parsedArgs, codeTemplate);
495495
return beforeEnd + debugTemplate + afterEnd;
496496
} else {
497497
// Если маркер не найден, добавляем в конец файла
498498
const debugTemplate = `
499499
// @lc code=end
500-
` + this.generateCppDebugTemplate(parsedArgs);
500+
` + this.generateCppDebugTemplate(parsedArgs, codeTemplate);
501501
return codeTemplate + debugTemplate;
502502
}
503503
}
@@ -826,7 +826,30 @@ using namespace std;
826826
return elements;
827827
}
828828

829-
private generateCppDebugTemplate(parsedArgs: { args: string[] }): string {
829+
private extractMethodName(codeTemplate: string): string {
830+
// Ищем публичный метод в классе Solution
831+
const methodPattern = /public:\s*[\w\s<>*&:\[\]]*\s+(\w+)\s*\(/;
832+
const match = codeTemplate.match(methodPattern);
833+
834+
if (match && match[1]) {
835+
console.log('🔧 Найден метод:', match[1]);
836+
return match[1];
837+
}
838+
839+
// Если не найден паттерн public:, ищем любой метод после класса Solution
840+
const anyMethodPattern = /class\s+Solution\s*{[^}]*?[\w\s<>*&:\[\]]*\s+(\w+)\s*\(/;
841+
const anyMatch = codeTemplate.match(anyMethodPattern);
842+
843+
if (anyMatch && anyMatch[1] && anyMatch[1] !== 'Solution') {
844+
console.log('🔧 Найден метод (альтернативный поиск):', anyMatch[1]);
845+
return anyMatch[1];
846+
}
847+
848+
console.log('⚠️ Метод не найден, используем someMethod');
849+
return 'someMethod';
850+
}
851+
852+
private generateCppDebugTemplate(parsedArgs: { args: string[] }, codeTemplate?: string): string {
830853
if (parsedArgs.args.length === 0) {
831854
return `
832855
@@ -842,45 +865,70 @@ int main()
842865
`;
843866
}
844867

845-
// Определяем типичные паттерны для LeetCode задач
846-
const hasArray = parsedArgs.args.some((arg) => arg.startsWith('{'));
847-
const hasNumbers = parsedArgs.args.some((arg) => /^\d+$/.test(arg));
848-
849868
let variableDeclarations = '';
850869
let methodCall = '';
851870

852-
if (hasArray && hasNumbers) {
853-
// Обычный паттерн: массив + число (например, Two Sum, Combination Sum)
854-
const arrayArgs = parsedArgs.args.filter((arg) => arg.startsWith('{'));
855-
const numberArgs = parsedArgs.args.filter((arg) => /^\d+$/.test(arg));
871+
// Анализируем каждый аргумент и создаем соответствующие переменные
872+
parsedArgs.args.forEach((arg, index) => {
873+
const cleanArg = arg.trim();
856874

857-
arrayArgs.forEach((arg, index) => {
858-
variableDeclarations += ` vector<int> arr${index + 1} = ${arg};\n`;
859-
});
875+
if (cleanArg.startsWith('{') && cleanArg.endsWith('}')) {
876+
// Это массив - определяем тип элементов
877+
const content = cleanArg.slice(1, -1).trim();
860878

861-
numberArgs.forEach((arg, index) => {
862-
variableDeclarations += ` int target${index + 1} = ${arg};\n`;
863-
});
879+
if (!content) {
880+
// Пустой массив
881+
variableDeclarations += ` vector<int> arr${index + 1} = {};\n`;
882+
} else if (content.includes('"')) {
883+
// Массив строк
884+
variableDeclarations += ` vector<string> arr${index + 1} = ${cleanArg};\n`;
885+
} else {
886+
// Массив чисел
887+
variableDeclarations += ` vector<int> arr${index + 1} = ${cleanArg};\n`;
888+
}
889+
} else if (cleanArg.startsWith('"') && cleanArg.endsWith('"')) {
890+
// Строка
891+
variableDeclarations += ` string str${index + 1} = ${cleanArg};\n`;
892+
} else if (/^-?\d+$/.test(cleanArg)) {
893+
// Целое число
894+
variableDeclarations += ` int num${index + 1} = ${cleanArg};\n`;
895+
} else if (/^-?\d*\.\d+$/.test(cleanArg)) {
896+
// Число с плавающей точкой
897+
variableDeclarations += ` double num${index + 1} = ${cleanArg};\n`;
898+
} else if (cleanArg === 'true' || cleanArg === 'false') {
899+
// Булево значение
900+
variableDeclarations += ` bool flag${index + 1} = ${cleanArg};\n`;
901+
} else {
902+
// Общий случай
903+
variableDeclarations += ` auto param${index + 1} = ${cleanArg};\n`;
904+
}
905+
});
864906

865-
if (arrayArgs.length === 1 && numberArgs.length === 1) {
866-
methodCall = ' // auto result = sol.someMethod(arr1, target1);';
907+
// Создаем комментарий для вызова метода
908+
const paramNames: string[] = [];
909+
parsedArgs.args.forEach((arg, index) => {
910+
const cleanArg = arg.trim();
911+
912+
if (cleanArg.startsWith('{')) {
913+
paramNames.push(`arr${index + 1}`);
914+
} else if (cleanArg.startsWith('"')) {
915+
paramNames.push(`str${index + 1}`);
916+
} else if (/^-?\d/.test(cleanArg)) {
917+
paramNames.push(`num${index + 1}`);
918+
} else if (cleanArg === 'true' || cleanArg === 'false') {
919+
paramNames.push(`flag${index + 1}`);
867920
} else {
868-
methodCall = ' // auto result = sol.someMethod(/* укажите нужные параметры */);';
921+
paramNames.push(`param${index + 1}`);
869922
}
870-
} else if (hasArray) {
871-
// Только массивы
872-
parsedArgs.args.forEach((arg, index) => {
873-
if (arg.startsWith('{')) {
874-
variableDeclarations += ` vector<int> arr${index + 1} = ${arg};\n`;
875-
}
876-
});
877-
methodCall = ' // auto result = sol.someMethod(arr1);';
923+
});
924+
925+
// Извлекаем название метода из кода
926+
const methodName = codeTemplate ? this.extractMethodName(codeTemplate) : 'someMethod';
927+
928+
if (paramNames.length > 0) {
929+
methodCall = ` auto result = sol.${methodName}(${paramNames.join(', ')});`;
878930
} else {
879-
// Только числа или другие типы
880-
parsedArgs.args.forEach((arg, index) => {
881-
variableDeclarations += ` auto param${index + 1} = ${arg};\n`;
882-
});
883-
methodCall = ' // auto result = sol.someMethod(param1);';
931+
methodCall = ` // auto result = sol.${methodName}(/* укажите нужные параметры */);`;
884932
}
885933

886934
return `
@@ -892,7 +940,7 @@ int main()
892940
// Тестовые данные:
893941
${variableDeclarations}
894942
${methodCall}
895-
// cout << "Result: " << result << endl;
943+
896944
897945
return 0;
898946
}

0 commit comments

Comments
 (0)