Skip to content

Commit 7f61fa2

Browse files
committed
fix some pbs
1 parent ae8f53b commit 7f61fa2

File tree

6 files changed

+153
-128
lines changed

6 files changed

+153
-128
lines changed

src/extension.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ let myStatusBarItem: vscode.StatusBarItem;
2222

2323
export async function activate(context: vscode.ExtensionContext) {
2424
console.log('Congratulations, your extension "CodeGeeX" is now active!');
25-
await getOpenExtensionData();
25+
try {
26+
await getOpenExtensionData();
27+
} catch (err) {
28+
console.error(err);
29+
}
2630
context.subscriptions.push(
2731
vscode.commands.registerCommand("codegeex.welcome-page", async () => {
2832
await welcomePage(context);
@@ -34,6 +38,23 @@ export async function activate(context: vscode.ExtensionContext) {
3438
checkPrivacy();
3539
let targetEditor: vscode.TextEditor;
3640

41+
const statusBarItemCommandId = "codegeex.disable-enable";
42+
context.subscriptions.push(
43+
vscode.commands.registerCommand("codegeex.disable-enable", () => {
44+
disableEnable(myStatusBarItem, g_isLoading, originalColor);
45+
})
46+
);
47+
// create a new status bar item that we can now manage
48+
myStatusBarItem = vscode.window.createStatusBarItem(
49+
vscode.StatusBarAlignment.Right,
50+
100
51+
);
52+
myStatusBarItem.command = statusBarItemCommandId;
53+
context.subscriptions.push(myStatusBarItem);
54+
//initialiser statusbar
55+
changeIconColor(enableExtension, myStatusBarItem, originalColor);
56+
updateStatusBarItem(myStatusBarItem, g_isLoading, false, "");
57+
3758
//subscribe interactive-mode command
3859
context.subscriptions.push(
3960
vscode.commands.registerCommand(
@@ -88,7 +109,7 @@ export async function activate(context: vscode.ExtensionContext) {
88109
context.subscriptions.push(
89110
vscode.workspace.registerTextDocumentContentProvider(
90111
myScheme,
91-
textDocumentProvider
112+
textDocumentProvider(myStatusBarItem, g_isLoading)
92113
)
93114
);
94115
context.subscriptions.push(
@@ -107,29 +128,16 @@ export async function activate(context: vscode.ExtensionContext) {
107128
)
108129
);
109130

110-
const statusBarItemCommandId = "codegeex.disable-enable";
111-
context.subscriptions.push(
112-
vscode.commands.registerCommand("codegeex.disable-enable", () => {
113-
disableEnable(myStatusBarItem, g_isLoading, originalColor);
114-
})
115-
);
116-
// create a new status bar item that we can now manage
117-
myStatusBarItem = vscode.window.createStatusBarItem(
118-
vscode.StatusBarAlignment.Right,
119-
100
120-
);
121-
myStatusBarItem.command = statusBarItemCommandId;
122-
context.subscriptions.push(myStatusBarItem);
123-
//initialiser statusbar
124-
changeIconColor(enableExtension, myStatusBarItem, originalColor);
125-
updateStatusBarItem(myStatusBarItem, g_isLoading, false, "");
126-
127131
//command after insert a suggestion in stealth mode
128132
context.subscriptions.push(
129133
vscode.commands.registerCommand(
130134
"verifyInsertion",
131-
(id, completions, acceptItem) => {
132-
getEndData(id, "", "Yes", acceptItem, completions);
135+
async (id, completions, acceptItem) => {
136+
try {
137+
await getEndData(id, "", "Yes", acceptItem, completions);
138+
} catch (err) {
139+
console.log(err);
140+
}
133141
}
134142
)
135143
);

src/mode/generationWithTranslationMode.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from "vscode";
22
import { hash, languageList } from "../param/constparams";
33
import { codegeexCodeTranslation } from "../utils/codegeexCodeTranslation";
4-
import { getCommentSignal } from "../utils/commentCode";
54
import { getCodeTranslation } from "../utils/getCodeTranslation";
65
import getDocumentLanguage from "../utils/getDocumentLanguage";
76
import { showQuickPick } from "../utils/showQuickPick";
@@ -40,18 +39,24 @@ export default async function generationWithTranslationMode(
4039
true,
4140
" Translating"
4241
);
43-
let commandid = getStartData(
44-
text,
45-
text,
46-
`${srcLang}->${dstLang}`,
47-
"translation"
48-
);
42+
let commandid: string;
43+
try{
44+
45+
commandid = await getStartData(
46+
text,
47+
text,
48+
`${srcLang}->${dstLang}`,
49+
"translation"
50+
);
51+
}catch(err){
52+
commandid=''
53+
}
4954
translation = await getCodeTranslation(text, srcLang, dstLang).then(
5055
async (res) => {
5156
await codegeexCodeTranslation(
5257
dstLang,
5358
res.translation[0].replaceAll("#", hash),
54-
await commandid
59+
commandid
5560
).then(() => {
5661
updateStatusBarItem(
5762
myStatusBarItem,

src/provider/textDocumentProvider.ts

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -16,102 +16,102 @@ import getDocumentLanguage from "../utils/getDocumentLanguage";
1616
import { getGPTCode } from "../utils/getGPTCode";
1717
import { getCodeCompletions } from "../utils/getCodeCompletions";
1818

19-
let myStatusBarItem: vscode.StatusBarItem;
20-
let g_isLoading: boolean;
21-
22-
export const textDocumentProvider = new (class {
23-
async provideTextDocumentContent(uri: vscode.Uri) {
24-
const params = new URLSearchParams(uri.query);
25-
if (params.get("loading") === "true") {
26-
return `/* CodeGeeX is generating ... */\n`;
27-
}
28-
const mode = params.get("mode");
29-
30-
if (mode === "translation") {
31-
let transResult = params.get("translation_res") || "";
32-
transResult = transResult
33-
.replaceAll(addSignal, "+")
34-
.replaceAll(andSignal, "&");
35-
console.log("transResult", transResult);
36-
const editor = vscode.window.activeTextEditor;
37-
if (!editor) {
38-
vscode.window.showInformationMessage(
39-
"Please open a file first to use CodeGeeX."
40-
);
41-
return;
19+
export function textDocumentProvider(myStatusBarItem: vscode.StatusBarItem, g_isLoading: boolean) {
20+
const textDocumentProvider = new (class {
21+
async provideTextDocumentContent(uri: vscode.Uri) {
22+
const params = new URLSearchParams(uri.query);
23+
if (params.get("loading") === "true") {
24+
return `/* CodeGeeX is generating ... */\n`;
4225
}
43-
codelensProvider.clearEls();
44-
let commandid = params.get("commandid") || "";
45-
let commentSignal = getCommentSignal(editor.document.languageId);
46-
transResult = transResult
47-
.replaceAll(hash, "#")
48-
.replaceAll(comment, commentSignal.line || "#");
49-
codelensProvider.addEl(0, transResult, commandid, "translation");
50-
return transResult;
51-
} else {
52-
let code_block = params.get("code_block") ?? "";
53-
54-
try {
55-
code_block = code_block
56-
.replaceAll(hash, "#")
26+
const mode = params.get("mode");
27+
28+
if (mode === "translation") {
29+
let transResult = params.get("translation_res") || "";
30+
transResult = transResult
5731
.replaceAll(addSignal, "+")
5832
.replaceAll(andSignal, "&");
59-
// 'lang': 'Python',
60-
if (code_block.length > 1200) {
61-
code_block = code_block.slice(code_block.length - 1200);
62-
}
33+
console.log("transResult", transResult);
6334
const editor = vscode.window.activeTextEditor;
6435
if (!editor) {
6536
vscode.window.showInformationMessage(
6637
"Please open a file first to use CodeGeeX."
6738
);
6839
return;
6940
}
70-
let payload = {};
71-
const num = candidateNum;
72-
let lang = getDocumentLanguage(editor);
73-
if (lang.length == 0) {
74-
payload = {
75-
prompt: code_block,
76-
n: num,
77-
apikey: apiKey,
78-
apisecret: apiSecret,
79-
};
80-
} else {
81-
payload = {
82-
lang: lang,
83-
prompt: code_block,
84-
n: num,
85-
apikey: apiKey,
86-
apisecret: apiSecret,
87-
};
88-
}
89-
// }
90-
const agent = new https.Agent({
91-
rejectUnauthorized: false,
92-
});
93-
const { commandid, completions } = await getCodeCompletions(
94-
code_block,
95-
num,
96-
lang,
97-
apiKey,
98-
apiSecret,
99-
"interactive"
100-
);
101-
if (completions.length > 0) {
102-
return getGPTCode(
103-
completions,
104-
commandid,
105-
myStatusBarItem,
106-
g_isLoading
41+
codelensProvider.clearEls();
42+
let commandid = params.get("commandid") || "";
43+
let commentSignal = getCommentSignal(editor.document.languageId);
44+
transResult = transResult
45+
.replaceAll(hash, "#")
46+
.replaceAll(comment, commentSignal.line || "#");
47+
codelensProvider.addEl(0, transResult, commandid, "translation");
48+
return transResult;
49+
} else {
50+
let code_block = params.get("code_block") ?? "";
51+
52+
try {
53+
code_block = code_block
54+
.replaceAll(hash, "#")
55+
.replaceAll(addSignal, "+")
56+
.replaceAll(andSignal, "&");
57+
// 'lang': 'Python',
58+
if (code_block.length > 1200) {
59+
code_block = code_block.slice(code_block.length - 1200);
60+
}
61+
const editor = vscode.window.activeTextEditor;
62+
if (!editor) {
63+
vscode.window.showInformationMessage(
64+
"Please open a file first to use CodeGeeX."
65+
);
66+
return;
67+
}
68+
let payload = {};
69+
const num = candidateNum;
70+
let lang = getDocumentLanguage(editor);
71+
if (lang.length == 0) {
72+
payload = {
73+
prompt: code_block,
74+
n: num,
75+
apikey: apiKey,
76+
apisecret: apiSecret,
77+
};
78+
} else {
79+
payload = {
80+
lang: lang,
81+
prompt: code_block,
82+
n: num,
83+
apikey: apiKey,
84+
apisecret: apiSecret,
85+
};
86+
}
87+
// }
88+
const agent = new https.Agent({
89+
rejectUnauthorized: false,
90+
});
91+
const { commandid, completions } = await getCodeCompletions(
92+
code_block,
93+
num,
94+
lang,
95+
apiKey,
96+
apiSecret,
97+
"interactive"
10798
);
108-
} else {
109-
return "No result to show";
99+
if (completions.length > 0) {
100+
return getGPTCode(
101+
completions,
102+
commandid,
103+
myStatusBarItem,
104+
g_isLoading
105+
);
106+
} else {
107+
return "No result to show";
108+
}
109+
} catch (err) {
110+
console.log("Error sending request", err);
111+
return "There was an error sending the request\n" + err;
110112
}
111-
} catch (err) {
112-
console.log("Error sending request", err);
113-
return "There was an error sending the request\n" + err;
114113
}
115114
}
116-
}
117-
})();
115+
})();
116+
return textDocumentProvider;
117+
}

src/utils/chooseCandidate.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function chooseCandidate(
1414
if (!targetEditor) return;
1515
try {
1616
targetEditor
17-
.edit((editBuilder) => {
17+
.edit(async (editBuilder) => {
1818
var s = targetEditor.selection;
1919
if (s.start.character == 0 && fn.slice(0, 1) == "\n") {
2020
fn = fn.slice(1);
@@ -38,7 +38,12 @@ export default function chooseCandidate(
3838
} else {
3939
editBuilder.replace(s, fn);
4040
}
41-
getEndData(commandid, "", "Yes", fn);
41+
try{
42+
43+
await getEndData(commandid, "", "Yes", fn);
44+
}catch(err){
45+
console.log(err)
46+
}
4247
})
4348
.then((success) => {
4449
var postion = targetEditor.selection.end;

src/utils/getCodeCompletions.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function getCodeCompletions(
9595
try {
9696
axios
9797
.post(API_URL, payload, { httpsAgent: agent, timeout: 120000 })
98-
.then((res) => {
98+
.then(async (res) => {
9999
console.log(res);
100100
if (res?.data.status === 0) {
101101
let codeArray = res?.data.result.output.code;
@@ -110,16 +110,20 @@ export function getCodeCompletions(
110110
resolve({ completions, commandid });
111111
} else {
112112
console.log(res);
113-
getEndData(commandid, res.data.message, "No");
113+
try{
114+
115+
await getEndData(commandid, res.data.message, "No");
116+
}catch(err){
117+
console.log(err);
118+
}
119+
114120
reject(res.data.message);
115121
}
116122
})
117123
.catch((err) => {
118-
getEndData(commandid, err.message, "No");
119124
reject(err);
120125
});
121126
} catch (e) {
122-
getEndData(commandid, "", "No");
123127
reject(e);
124128
}
125129
});

0 commit comments

Comments
 (0)