Skip to content

Commit 4ab37cf

Browse files
authored
Merge pull request #218 from ccagml/main
获取题目错误,则不生成文件
2 parents 27c8b28 + f4e1655 commit 4ab37cf

File tree

7 files changed

+72
-105
lines changed

7 files changed

+72
-105
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"request": "attach",
2727
"name": "attach process",
2828
"processId": "${command:PickProcess}",
29+
"continueOnAttach": true
2930
},
3031
{
3132
"name": "Launch Tests",

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## version 2.19.6
2+
3+
- 获取题目错误,则不生成文件
4+
15
## version 2.19.5
26

37
- 子进程执行路径

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode",
44
"description": "%main.description%",
5-
"version": "2.19.5",
5+
"version": "2.19.6",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",

src/controller/TreeViewController.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -730,30 +730,38 @@ class TreeViewController implements Disposable {
730730
const descriptionConfig: IDescriptionConfiguration = getDescriptionConfiguration();
731731
const needTranslation: boolean = isUseEndpointTranslation();
732732

733-
await executeService.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
734-
const promises: any[] = [
735-
vscode.window.showTextDocument(vscode.Uri.file(finalPath), {
736-
preview: false,
737-
viewColumn: vscode.ViewColumn.One,
738-
}),
739-
promptHintMessage(
740-
"hint.commentDescription",
741-
'You can config how to show the problem description through "leetcode-problem-rating.showDescription".',
742-
"Open settings",
743-
(): Promise<any> => openSettingsEditor("leetcode-problem-rating.showDescription")
744-
),
745-
];
746-
if (descriptionConfig.showInWebview) {
747-
promises.push(this.showDescriptionView(node));
748-
}
749-
promises.push(
750-
new Promise(async (resolve, _) => {
751-
await eventService.emit("showProblemFinish", node);
752-
resolve(1);
753-
})
733+
let show_code = await executeService.showProblem(
734+
node,
735+
language,
736+
finalPath,
737+
descriptionConfig.showInComment,
738+
needTranslation
754739
);
740+
if (show_code == 100) {
741+
const promises: any[] = [
742+
vscode.window.showTextDocument(vscode.Uri.file(finalPath), {
743+
preview: false,
744+
viewColumn: vscode.ViewColumn.One,
745+
}),
746+
promptHintMessage(
747+
"hint.commentDescription",
748+
'You can config how to show the problem description through "leetcode-problem-rating.showDescription".',
749+
"Open settings",
750+
(): Promise<any> => openSettingsEditor("leetcode-problem-rating.showDescription")
751+
),
752+
];
753+
if (descriptionConfig.showInWebview) {
754+
promises.push(this.showDescriptionView(node));
755+
}
756+
promises.push(
757+
new Promise(async (resolve, _) => {
758+
await eventService.emit("showProblemFinish", node);
759+
resolve(1);
760+
})
761+
);
755762

756-
await Promise.all(promises);
763+
await Promise.all(promises);
764+
}
757765
} catch (error) {
758766
await promptForOpenOutputChannel(`${error} 请查看控制台信息~`, OutPutType.error);
759767
}
@@ -785,7 +793,18 @@ class TreeViewController implements Disposable {
785793
}
786794
const needTranslation: boolean = isUseEndpointTranslation();
787795
const descString: string = await executeService.getDescription(node.qid, needTranslation);
788-
previewService.show(descString, node, isSideMode);
796+
797+
let successResult;
798+
try {
799+
successResult = JSON.parse(descString);
800+
} catch (e) {
801+
successResult = {};
802+
}
803+
if (successResult.code == 100) {
804+
previewService.show(JSON.stringify(successResult.msg), node, isSideMode);
805+
} else {
806+
await promptForOpenOutputChannel(`${descString} 请查看控制台信息~`, OutPutType.error);
807+
}
789808
}
790809

791810
public async searchScoreRange(): Promise<void> {

src/rpc/actionChain/chainNode/leetcode.cn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function getSolutionBySlug(question_slug: string, articles_slug: string, lang: s
259259
// let bbb = body;
260260
// console.log(bbb);
261261
let solution = body.data.solutionArticle;
262-
if (!solution) return reply.error("本题没有题解");
262+
if (!solution) return reply.info(JSON.stringify({ code: -1, error: "本题没有题解" }));
263263

264264
let link = URL_DISCUSS.replace("$slug", question_slug).replace("$articles_slug", articles_slug);
265265
// let content = solution.content.replace(/\\n/g, "\n").replace(/\\t/g, "\t");

src/rpc/factory/api/showApi.ts

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
// let util = require("util");
11-
let childProcess = require("child_process");
1211

1312
import { storageUtils } from "../../utils/storageUtils";
1413

@@ -32,30 +31,13 @@ class ShowApi extends ApiBase {
3231
default: false,
3332
describe: "Only show code template",
3433
})
35-
.option("e", {
36-
alias: "editor",
37-
type: "string",
38-
describe: "Open source code in editor",
39-
})
40-
.option("g", {
41-
alias: "gen",
42-
type: "boolean",
43-
default: false,
44-
describe: "Generate source code",
45-
})
4634
.option("l", {
4735
alias: "lang",
4836
type: "string",
4937
default: configUtils.code.lang,
5038
describe: "Programming language of the source code",
5139
choices: configUtils.sys.langs,
5240
})
53-
.option("o", {
54-
alias: "outdir",
55-
type: "string",
56-
describe: "Where to save source code",
57-
default: ".",
58-
})
5941
.option("q", {
6042
alias: "query",
6143
type: "string",
@@ -110,23 +92,12 @@ class ShowApi extends ApiBase {
11092
}
11193

11294
showProblem(problem, argv) {
113-
// const taglist = [problem.category]
114-
// .concat(problem.companies || [])
115-
// .concat(problem.tags || [])
116-
// .map((x) => " " + x + " ")
117-
// .join(" ");
118-
// const langlist = problem.templates
119-
// .map((x) => " " + x.value + " ")
120-
// .sort()
121-
// .join(" ");
122-
12395
let code;
12496
const needcode = argv.gen || argv.codeonly;
12597
if (needcode) {
12698
const template = problem.templates.find((x) => x.value === argv.lang);
12799
if (!template) {
128-
reply.info('Not supported language "' + argv.lang + '"');
129-
// reply.warn("Supported languages: " + langlist);
100+
reply.info(JSON.stringify({ code: 101, error: `Not supported language ${argv.lang} ` }));
130101
return;
131102
}
132103

@@ -138,23 +109,9 @@ class ShowApi extends ApiBase {
138109
code = chainMgr.getChainHead().exportProblem(problem, opts);
139110
}
140111

141-
let filename;
142-
if (argv.gen) {
143-
storageUtils.mkdir(argv.outdir);
144-
filename = this.genFileName(problem, argv);
145-
storageUtils.write(filename, code);
146-
147-
if (argv.editor !== undefined) {
148-
childProcess.spawn(argv.editor || configUtils.code.editor, [filename], {
149-
// in case your editor of choice is vim or emacs
150-
stdio: "inherit",
151-
});
152-
}
153-
} else {
154-
if (argv.codeonly) {
155-
reply.info(code);
156-
return;
157-
}
112+
if (argv.codeonly) {
113+
reply.info(JSON.stringify({ code: 100, msg: code }));
114+
return;
158115
}
159116

160117
let preview_data: any = {};
@@ -164,35 +121,7 @@ class ShowApi extends ApiBase {
164121
preview_data.likes = `${problem.likes}`;
165122
preview_data.dislikes = `${problem.dislikes}`;
166123
preview_data.desc = problem.desc;
167-
reply.info(JSON.stringify(preview_data));
168-
169-
// reply.info(`[${problem.fid}] ${problem.name}`);
170-
// reply.info();
171-
// reply.info(problem.link);
172-
// if (argv.extra) {
173-
// reply.info();
174-
// reply.info("Tags: " + taglist);
175-
// reply.info();
176-
// reply.info("Langs: " + langlist);
177-
// }
178-
179-
// reply.info();
180-
// reply.info(`* ${problem.category}`);
181-
// reply.info(`* ${problem.level} (${problem.percent.toFixed(2)}%)`);
182-
183-
// if (problem.likes) reply.info(`* Likes: ${problem.likes}`);
184-
// if (problem.dislikes) reply.info(`* Dislikes: ${problem.dislikes}`);
185-
// else reply.info(`* Dislikes: -`);
186-
// if (problem.totalAC) reply.info(`* Total Accepted: ${problem.totalAC}`);
187-
// if (problem.totalSubmit) reply.info(`* Total Submissions: ${problem.totalSubmit}`);
188-
// if (problem.testable && problem.testcase) {
189-
// let testcase_value = util.inspect(problem.testcase);
190-
// reply.info(`* Testcase Example: ${testcase_value}`);
191-
// }
192-
// if (filename) reply.info(`* Source Code: ${filename}`);
193-
194-
// reply.info();
195-
// reply.info(problem.desc);
124+
reply.info(JSON.stringify({ code: 100, msg: preview_data }));
196125
}
197126

198127
call(argv) {
@@ -201,7 +130,7 @@ class ShowApi extends ApiBase {
201130
if (argv.keyword.length > 0) {
202131
// show specific one
203132
chainMgr.getChainHead().getProblem(argv.keyword, !argv.dontTranslate, function (e, problem) {
204-
if (e) return reply.info(e);
133+
if (e) return reply.info(JSON.stringify({ code: 102, error: e }));
205134
that.showProblem(problem, argv);
206135
});
207136
} else {

src/service/ExecuteService.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class ExecuteService implements Disposable {
146146
filePath: string,
147147
showDescriptionInComment: boolean = false,
148148
needTranslation: boolean
149-
): Promise<void> {
149+
): Promise<number> {
150150
const templateType: string = showDescriptionInComment ? "-cx" : "-c";
151151
const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.qid, templateType, "-l", language];
152152

@@ -155,14 +155,28 @@ class ExecuteService implements Disposable {
155155
}
156156

157157
if (!(await fse.pathExists(filePath))) {
158-
await fse.createFile(filePath);
159158
const codeTemplate: string = await this.executeCommandWithProgressEx(
160159
"正在获取题目数据~",
161160
this.nodeExecutable,
162161
cmd
163162
);
164-
await fse.writeFile(filePath, codeTemplate);
163+
164+
let successResult;
165+
try {
166+
successResult = JSON.parse(codeTemplate);
167+
} catch (e) {
168+
successResult = { code: -1 };
169+
}
170+
if (successResult.code == 100) {
171+
await fse.createFile(filePath);
172+
await fse.writeFile(filePath, successResult.msg);
173+
return successResult.code;
174+
} else {
175+
await promptForOpenOutputChannel(`${codeTemplate} 请查看控制台信息~`, OutPutType.error);
176+
}
177+
return successResult.code;
165178
}
179+
return 100;
166180
}
167181

168182
public async getHelp(input: string, language: string, needTranslation: boolean, cn_help?: boolean): Promise<string> {

0 commit comments

Comments
 (0)