Skip to content

Commit

Permalink
fix some pbs
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwang1993 committed Dec 1, 2022
1 parent ae8f53b commit 7f61fa2
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 128 deletions.
50 changes: 29 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}
}
)
);
Expand Down
21 changes: 13 additions & 8 deletions src/mode/generationWithTranslationMode.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
170 changes: 85 additions & 85 deletions src/provider/textDocumentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,102 +16,102 @@ 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(
"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"
);
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;
}
9 changes: 7 additions & 2 deletions src/utils/chooseCandidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/utils/getCodeCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
});
Expand Down
Loading

0 comments on commit 7f61fa2

Please sign in to comment.