diff --git a/src/extension.ts b/src/extension.ts index 8951f88..c36f4fc 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -22,7 +22,11 @@ let myStatusBarItem: vscode.StatusBarItem; export async function activate(context: vscode.ExtensionContext) { console.log('Congratulations, your extension "CodeGeeX" is now active!'); - await getOpenExtensionData(); + try { + await getOpenExtensionData(); + } catch (err) { + console.error(err); + } context.subscriptions.push( vscode.commands.registerCommand("codegeex.welcome-page", async () => { await welcomePage(context); @@ -34,6 +38,23 @@ export async function activate(context: vscode.ExtensionContext) { checkPrivacy(); let targetEditor: vscode.TextEditor; + const statusBarItemCommandId = "codegeex.disable-enable"; + context.subscriptions.push( + vscode.commands.registerCommand("codegeex.disable-enable", () => { + disableEnable(myStatusBarItem, g_isLoading, originalColor); + }) + ); + // create a new status bar item that we can now manage + myStatusBarItem = vscode.window.createStatusBarItem( + vscode.StatusBarAlignment.Right, + 100 + ); + myStatusBarItem.command = statusBarItemCommandId; + context.subscriptions.push(myStatusBarItem); + //initialiser statusbar + changeIconColor(enableExtension, myStatusBarItem, originalColor); + updateStatusBarItem(myStatusBarItem, g_isLoading, false, ""); + //subscribe interactive-mode command context.subscriptions.push( vscode.commands.registerCommand( @@ -88,7 +109,7 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.workspace.registerTextDocumentContentProvider( myScheme, - textDocumentProvider + textDocumentProvider(myStatusBarItem, g_isLoading) ) ); context.subscriptions.push( @@ -107,29 +128,16 @@ export async function activate(context: vscode.ExtensionContext) { ) ); - const statusBarItemCommandId = "codegeex.disable-enable"; - context.subscriptions.push( - vscode.commands.registerCommand("codegeex.disable-enable", () => { - disableEnable(myStatusBarItem, g_isLoading, originalColor); - }) - ); - // create a new status bar item that we can now manage - myStatusBarItem = vscode.window.createStatusBarItem( - vscode.StatusBarAlignment.Right, - 100 - ); - myStatusBarItem.command = statusBarItemCommandId; - context.subscriptions.push(myStatusBarItem); - //initialiser statusbar - changeIconColor(enableExtension, myStatusBarItem, originalColor); - updateStatusBarItem(myStatusBarItem, g_isLoading, false, ""); - //command after insert a suggestion in stealth mode context.subscriptions.push( vscode.commands.registerCommand( "verifyInsertion", - (id, completions, acceptItem) => { - getEndData(id, "", "Yes", acceptItem, completions); + async (id, completions, acceptItem) => { + try { + await getEndData(id, "", "Yes", acceptItem, completions); + } catch (err) { + console.log(err); + } } ) ); diff --git a/src/mode/generationWithTranslationMode.ts b/src/mode/generationWithTranslationMode.ts index c433083..0f44711 100644 --- a/src/mode/generationWithTranslationMode.ts +++ b/src/mode/generationWithTranslationMode.ts @@ -1,7 +1,6 @@ import * as vscode from "vscode"; import { hash, languageList } from "../param/constparams"; import { codegeexCodeTranslation } from "../utils/codegeexCodeTranslation"; -import { getCommentSignal } from "../utils/commentCode"; import { getCodeTranslation } from "../utils/getCodeTranslation"; import getDocumentLanguage from "../utils/getDocumentLanguage"; import { showQuickPick } from "../utils/showQuickPick"; @@ -40,18 +39,24 @@ export default async function generationWithTranslationMode( true, " Translating" ); - let commandid = getStartData( - text, - text, - `${srcLang}->${dstLang}`, - "translation" - ); + let commandid: string; + try{ + + commandid = await getStartData( + text, + text, + `${srcLang}->${dstLang}`, + "translation" + ); + }catch(err){ + commandid='' + } translation = await getCodeTranslation(text, srcLang, dstLang).then( async (res) => { await codegeexCodeTranslation( dstLang, res.translation[0].replaceAll("#", hash), - await commandid + commandid ).then(() => { updateStatusBarItem( myStatusBarItem, diff --git a/src/provider/textDocumentProvider.ts b/src/provider/textDocumentProvider.ts index cf7c3c4..c60808d 100644 --- a/src/provider/textDocumentProvider.ts +++ b/src/provider/textDocumentProvider.ts @@ -16,50 +16,21 @@ import getDocumentLanguage from "../utils/getDocumentLanguage"; import { getGPTCode } from "../utils/getGPTCode"; import { getCodeCompletions } from "../utils/getCodeCompletions"; -let myStatusBarItem: vscode.StatusBarItem; -let g_isLoading: boolean; - -export const textDocumentProvider = new (class { - async provideTextDocumentContent(uri: vscode.Uri) { - const params = new URLSearchParams(uri.query); - if (params.get("loading") === "true") { - return `/* CodeGeeX is generating ... */\n`; - } - const mode = params.get("mode"); - - if (mode === "translation") { - let transResult = params.get("translation_res") || ""; - transResult = transResult - .replaceAll(addSignal, "+") - .replaceAll(andSignal, "&"); - console.log("transResult", transResult); - const editor = vscode.window.activeTextEditor; - if (!editor) { - vscode.window.showInformationMessage( - "Please open a file first to use CodeGeeX." - ); - return; +export function textDocumentProvider(myStatusBarItem: vscode.StatusBarItem, g_isLoading: boolean) { + const textDocumentProvider = new (class { + async provideTextDocumentContent(uri: vscode.Uri) { + const params = new URLSearchParams(uri.query); + if (params.get("loading") === "true") { + return `/* CodeGeeX is generating ... */\n`; } - codelensProvider.clearEls(); - let commandid = params.get("commandid") || ""; - let commentSignal = getCommentSignal(editor.document.languageId); - transResult = transResult - .replaceAll(hash, "#") - .replaceAll(comment, commentSignal.line || "#"); - codelensProvider.addEl(0, transResult, commandid, "translation"); - return transResult; - } else { - let code_block = params.get("code_block") ?? ""; - - try { - code_block = code_block - .replaceAll(hash, "#") + const mode = params.get("mode"); + + if (mode === "translation") { + let transResult = params.get("translation_res") || ""; + transResult = transResult .replaceAll(addSignal, "+") .replaceAll(andSignal, "&"); - // 'lang': 'Python', - if (code_block.length > 1200) { - code_block = code_block.slice(code_block.length - 1200); - } + console.log("transResult", transResult); const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showInformationMessage( @@ -67,51 +38,80 @@ export const textDocumentProvider = new (class { ); return; } - let payload = {}; - const num = candidateNum; - let lang = getDocumentLanguage(editor); - if (lang.length == 0) { - payload = { - prompt: code_block, - n: num, - apikey: apiKey, - apisecret: apiSecret, - }; - } else { - payload = { - lang: lang, - prompt: code_block, - n: num, - apikey: apiKey, - apisecret: apiSecret, - }; - } - // } - const agent = new https.Agent({ - rejectUnauthorized: false, - }); - const { commandid, completions } = await getCodeCompletions( - code_block, - num, - lang, - apiKey, - apiSecret, - "interactive" - ); - if (completions.length > 0) { - return getGPTCode( - completions, - commandid, - myStatusBarItem, - g_isLoading + codelensProvider.clearEls(); + let commandid = params.get("commandid") || ""; + let commentSignal = getCommentSignal(editor.document.languageId); + transResult = transResult + .replaceAll(hash, "#") + .replaceAll(comment, commentSignal.line || "#"); + codelensProvider.addEl(0, transResult, commandid, "translation"); + return transResult; + } else { + let code_block = params.get("code_block") ?? ""; + + try { + code_block = code_block + .replaceAll(hash, "#") + .replaceAll(addSignal, "+") + .replaceAll(andSignal, "&"); + // 'lang': 'Python', + if (code_block.length > 1200) { + code_block = code_block.slice(code_block.length - 1200); + } + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX." + ); + return; + } + let payload = {}; + const num = candidateNum; + let lang = getDocumentLanguage(editor); + if (lang.length == 0) { + payload = { + prompt: code_block, + n: num, + apikey: apiKey, + apisecret: apiSecret, + }; + } else { + payload = { + lang: lang, + prompt: code_block, + n: num, + apikey: apiKey, + apisecret: apiSecret, + }; + } + // } + const agent = new https.Agent({ + rejectUnauthorized: false, + }); + const { commandid, completions } = await getCodeCompletions( + code_block, + num, + lang, + apiKey, + apiSecret, + "interactive" ); - } else { - return "No result to show"; + if (completions.length > 0) { + return getGPTCode( + completions, + commandid, + myStatusBarItem, + g_isLoading + ); + } else { + return "No result to show"; + } + } catch (err) { + console.log("Error sending request", err); + return "There was an error sending the request\n" + err; } - } catch (err) { - console.log("Error sending request", err); - return "There was an error sending the request\n" + err; } } - } -})(); + })(); + return textDocumentProvider; +} diff --git a/src/utils/chooseCandidate.ts b/src/utils/chooseCandidate.ts index c0965c8..1186f40 100644 --- a/src/utils/chooseCandidate.ts +++ b/src/utils/chooseCandidate.ts @@ -14,7 +14,7 @@ export default function chooseCandidate( if (!targetEditor) return; try { targetEditor - .edit((editBuilder) => { + .edit(async (editBuilder) => { var s = targetEditor.selection; if (s.start.character == 0 && fn.slice(0, 1) == "\n") { fn = fn.slice(1); @@ -38,7 +38,12 @@ export default function chooseCandidate( } else { editBuilder.replace(s, fn); } - getEndData(commandid, "", "Yes", fn); + try{ + + await getEndData(commandid, "", "Yes", fn); + }catch(err){ + console.log(err) + } }) .then((success) => { var postion = targetEditor.selection.end; diff --git a/src/utils/getCodeCompletions.ts b/src/utils/getCodeCompletions.ts index 129714c..1824388 100644 --- a/src/utils/getCodeCompletions.ts +++ b/src/utils/getCodeCompletions.ts @@ -95,7 +95,7 @@ export function getCodeCompletions( try { axios .post(API_URL, payload, { httpsAgent: agent, timeout: 120000 }) - .then((res) => { + .then(async (res) => { console.log(res); if (res?.data.status === 0) { let codeArray = res?.data.result.output.code; @@ -110,16 +110,20 @@ export function getCodeCompletions( resolve({ completions, commandid }); } else { console.log(res); - getEndData(commandid, res.data.message, "No"); + try{ + + await getEndData(commandid, res.data.message, "No"); + }catch(err){ + console.log(err); + } + reject(res.data.message); } }) .catch((err) => { - getEndData(commandid, err.message, "No"); reject(err); }); } catch (e) { - getEndData(commandid, "", "No"); reject(e); } }); diff --git a/src/utils/statisticFunc.ts b/src/utils/statisticFunc.ts index cead8eb..0ed3fe2 100644 --- a/src/utils/statisticFunc.ts +++ b/src/utils/statisticFunc.ts @@ -9,7 +9,7 @@ import { enableStats } from "../localconfig"; const privacy = vscode.workspace.getConfiguration("Codegeex").get("Privacy"); export function getOpenExtensionData(): Promise { - return new Promise((resolve) => { + return new Promise((resolve,reject) => { if(enableStats){ try { axios @@ -32,10 +32,10 @@ export function getOpenExtensionData(): Promise { resolve(res.data.msg); }) .catch((err) => { - resolve("error"); + reject("error"); }); } catch (e) { - resolve("error"); + reject("error"); } }else{ resolve("No stats"); @@ -49,7 +49,7 @@ export function getStartData( lang: string, mode?: string ): Promise { - return new Promise((resolve) => { + return new Promise((resolve,reject) => { if(enableStats){ const startParam = { @@ -70,10 +70,10 @@ export function getStartData( resolve(commandid); }); } catch (err) { - resolve(""); + reject(""); } }else{ - resolve(''); + reject(''); } }); } @@ -84,8 +84,11 @@ export function getEndData( acceptItem?: string | null, completions?: Array | string ): Promise { - return new Promise((resolve) => { + return new Promise((resolve,reject) => { if(enableStats){ + if (commandid === "") { + reject("No command id"); + } let endparam = { id: commandid, @@ -106,7 +109,7 @@ export function getEndData( resolve(""); }) }catch(err){ - resolve(""); + reject(""); } }else{ resolve("");