fix: Add async to registerQuickFixCommand and generatePrompt

- Modified registerQuickFixCommand to be an async function
- Updated generatePrompt to also be async and handle active editor context
- Improved error handling and context management in prompt generation
This commit is contained in:
bobo.yang 2024-06-18 15:18:05 +08:00
parent 549528bd7e
commit 76771f1392

View File

@ -349,7 +349,7 @@ export function registerFixCommand(context: vscode.ExtensionContext) {
);
}
export function registerQuickFixCommand(context: vscode.ExtensionContext) {
export async function registerQuickFixCommand(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand(
"DevChat.quickFix",
async (diagnosticMessage: string, code: string, surroundingCode: string) => {
@ -359,7 +359,7 @@ export function registerQuickFixCommand(context: vscode.ExtensionContext) {
}
const language = DevChatConfig.getInstance().get('language');
const prompt = generatePrompt(code, surroundingCode, diagnosticMessage, language);
const prompt = await generatePrompt(code, surroundingCode, diagnosticMessage, language);
chatWithDevChat(ExtensionContextHolder.provider?.view()!, prompt);
}
);
@ -375,6 +375,19 @@ async function waitForPanelActivation() {
});
}
function generatePrompt(code: string, surroundingCode: string, diagnosticMessage: string, language: string) {
return `current edit file is:\n\`\`\`\n${code}\n\`\`\`\n\nThere is an error in the above code:\n\`\`\`\n${surroundingCode}\n\`\`\`\n\nHow do I fix this problem in the above code?: ${diagnosticMessage}, please output steps to fix it. ${language === "zh" ? "结果输出请使用中文。" : ""} `;
async function generatePrompt(code: string, surroundingCode: string, diagnosticMessage: string, language: string) {
const editor = vscode.window.activeTextEditor;
if (editor) {
const selectedText = editor.document.getText(editor.selection);
await sendCodeSelectMessage(
ExtensionContextHolder.provider?.view()!,
editor.document.fileName,
code,
0
);
// wait 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));
return `Context code is current edit file.\n\nThere is an error in the context code:\n\`\`\`\n${surroundingCode}\n\`\`\`\n\nHow do I fix this problem in the above code?: ${diagnosticMessage}, please output steps to fix it. ${language === "zh" ? "结果输出请使用中文。" : ""} `;
}
return `current edit file is:\n\`\`\`\n${code}\n\`\`\`\n\nThere is an error in the above code:\n\`\`\`\n${surroundingCode}\n\`\`\`\n\nHow do I fix this problem in the above code?: ${diagnosticMessage}, please output steps to fix it. ${language === "zh" ? "结果输出请使用中文。" : ""} `;
}