Skip to content

获取题目错误,则不生成文件 #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"request": "attach",
"name": "attach process",
"processId": "${command:PickProcess}",
"continueOnAttach": true
},
{
"name": "Launch Tests",
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 2.19.6

- 获取题目错误,则不生成文件

## version 2.19.5

- 子进程执行路径
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-leetcode-problem-rating",
"displayName": "LeetCode",
"description": "%main.description%",
"version": "2.19.5",
"version": "2.19.6",
"author": "ccagml",
"publisher": "ccagml",
"license": "MIT",
Expand Down
65 changes: 42 additions & 23 deletions src/controller/TreeViewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,30 +730,38 @@ class TreeViewController implements Disposable {
const descriptionConfig: IDescriptionConfiguration = getDescriptionConfiguration();
const needTranslation: boolean = isUseEndpointTranslation();

await executeService.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
const promises: any[] = [
vscode.window.showTextDocument(vscode.Uri.file(finalPath), {
preview: false,
viewColumn: vscode.ViewColumn.One,
}),
promptHintMessage(
"hint.commentDescription",
'You can config how to show the problem description through "leetcode-problem-rating.showDescription".',
"Open settings",
(): Promise<any> => openSettingsEditor("leetcode-problem-rating.showDescription")
),
];
if (descriptionConfig.showInWebview) {
promises.push(this.showDescriptionView(node));
}
promises.push(
new Promise(async (resolve, _) => {
await eventService.emit("showProblemFinish", node);
resolve(1);
})
let show_code = await executeService.showProblem(
node,
language,
finalPath,
descriptionConfig.showInComment,
needTranslation
);
if (show_code == 100) {
const promises: any[] = [
vscode.window.showTextDocument(vscode.Uri.file(finalPath), {
preview: false,
viewColumn: vscode.ViewColumn.One,
}),
promptHintMessage(
"hint.commentDescription",
'You can config how to show the problem description through "leetcode-problem-rating.showDescription".',
"Open settings",
(): Promise<any> => openSettingsEditor("leetcode-problem-rating.showDescription")
),
];
if (descriptionConfig.showInWebview) {
promises.push(this.showDescriptionView(node));
}
promises.push(
new Promise(async (resolve, _) => {
await eventService.emit("showProblemFinish", node);
resolve(1);
})
);

await Promise.all(promises);
await Promise.all(promises);
}
} catch (error) {
await promptForOpenOutputChannel(`${error} 请查看控制台信息~`, OutPutType.error);
}
Expand Down Expand Up @@ -785,7 +793,18 @@ class TreeViewController implements Disposable {
}
const needTranslation: boolean = isUseEndpointTranslation();
const descString: string = await executeService.getDescription(node.qid, needTranslation);
previewService.show(descString, node, isSideMode);

let successResult;
try {
successResult = JSON.parse(descString);
} catch (e) {
successResult = {};
}
if (successResult.code == 100) {
previewService.show(JSON.stringify(successResult.msg), node, isSideMode);
} else {
await promptForOpenOutputChannel(`${descString} 请查看控制台信息~`, OutPutType.error);
}
}

public async searchScoreRange(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/actionChain/chainNode/leetcode.cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function getSolutionBySlug(question_slug: string, articles_slug: string, lang: s
// let bbb = body;
// console.log(bbb);
let solution = body.data.solutionArticle;
if (!solution) return reply.error("本题没有题解");
if (!solution) return reply.info(JSON.stringify({ code: -1, error: "本题没有题解" }));

let link = URL_DISCUSS.replace("$slug", question_slug).replace("$articles_slug", articles_slug);
// let content = solution.content.replace(/\\n/g, "\n").replace(/\\t/g, "\t");
Expand Down
83 changes: 6 additions & 77 deletions src/rpc/factory/api/showApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

// let util = require("util");
let childProcess = require("child_process");

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

Expand All @@ -32,30 +31,13 @@ class ShowApi extends ApiBase {
default: false,
describe: "Only show code template",
})
.option("e", {
alias: "editor",
type: "string",
describe: "Open source code in editor",
})
.option("g", {
alias: "gen",
type: "boolean",
default: false,
describe: "Generate source code",
})
.option("l", {
alias: "lang",
type: "string",
default: configUtils.code.lang,
describe: "Programming language of the source code",
choices: configUtils.sys.langs,
})
.option("o", {
alias: "outdir",
type: "string",
describe: "Where to save source code",
default: ".",
})
.option("q", {
alias: "query",
type: "string",
Expand Down Expand Up @@ -110,23 +92,12 @@ class ShowApi extends ApiBase {
}

showProblem(problem, argv) {
// const taglist = [problem.category]
// .concat(problem.companies || [])
// .concat(problem.tags || [])
// .map((x) => " " + x + " ")
// .join(" ");
// const langlist = problem.templates
// .map((x) => " " + x.value + " ")
// .sort()
// .join(" ");

let code;
const needcode = argv.gen || argv.codeonly;
if (needcode) {
const template = problem.templates.find((x) => x.value === argv.lang);
if (!template) {
reply.info('Not supported language "' + argv.lang + '"');
// reply.warn("Supported languages: " + langlist);
reply.info(JSON.stringify({ code: 101, error: `Not supported language ${argv.lang} ` }));
return;
}

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

let filename;
if (argv.gen) {
storageUtils.mkdir(argv.outdir);
filename = this.genFileName(problem, argv);
storageUtils.write(filename, code);

if (argv.editor !== undefined) {
childProcess.spawn(argv.editor || configUtils.code.editor, [filename], {
// in case your editor of choice is vim or emacs
stdio: "inherit",
});
}
} else {
if (argv.codeonly) {
reply.info(code);
return;
}
if (argv.codeonly) {
reply.info(JSON.stringify({ code: 100, msg: code }));
return;
}

let preview_data: any = {};
Expand All @@ -164,35 +121,7 @@ class ShowApi extends ApiBase {
preview_data.likes = `${problem.likes}`;
preview_data.dislikes = `${problem.dislikes}`;
preview_data.desc = problem.desc;
reply.info(JSON.stringify(preview_data));

// reply.info(`[${problem.fid}] ${problem.name}`);
// reply.info();
// reply.info(problem.link);
// if (argv.extra) {
// reply.info();
// reply.info("Tags: " + taglist);
// reply.info();
// reply.info("Langs: " + langlist);
// }

// reply.info();
// reply.info(`* ${problem.category}`);
// reply.info(`* ${problem.level} (${problem.percent.toFixed(2)}%)`);

// if (problem.likes) reply.info(`* Likes: ${problem.likes}`);
// if (problem.dislikes) reply.info(`* Dislikes: ${problem.dislikes}`);
// else reply.info(`* Dislikes: -`);
// if (problem.totalAC) reply.info(`* Total Accepted: ${problem.totalAC}`);
// if (problem.totalSubmit) reply.info(`* Total Submissions: ${problem.totalSubmit}`);
// if (problem.testable && problem.testcase) {
// let testcase_value = util.inspect(problem.testcase);
// reply.info(`* Testcase Example: ${testcase_value}`);
// }
// if (filename) reply.info(`* Source Code: ${filename}`);

// reply.info();
// reply.info(problem.desc);
reply.info(JSON.stringify({ code: 100, msg: preview_data }));
}

call(argv) {
Expand All @@ -201,7 +130,7 @@ class ShowApi extends ApiBase {
if (argv.keyword.length > 0) {
// show specific one
chainMgr.getChainHead().getProblem(argv.keyword, !argv.dontTranslate, function (e, problem) {
if (e) return reply.info(e);
if (e) return reply.info(JSON.stringify({ code: 102, error: e }));
that.showProblem(problem, argv);
});
} else {
Expand Down
20 changes: 17 additions & 3 deletions src/service/ExecuteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ExecuteService implements Disposable {
filePath: string,
showDescriptionInComment: boolean = false,
needTranslation: boolean
): Promise<void> {
): Promise<number> {
const templateType: string = showDescriptionInComment ? "-cx" : "-c";
const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.qid, templateType, "-l", language];

Expand All @@ -155,14 +155,28 @@ class ExecuteService implements Disposable {
}

if (!(await fse.pathExists(filePath))) {
await fse.createFile(filePath);
const codeTemplate: string = await this.executeCommandWithProgressEx(
"正在获取题目数据~",
this.nodeExecutable,
cmd
);
await fse.writeFile(filePath, codeTemplate);

let successResult;
try {
successResult = JSON.parse(codeTemplate);
} catch (e) {
successResult = { code: -1 };
}
if (successResult.code == 100) {
await fse.createFile(filePath);
await fse.writeFile(filePath, successResult.msg);
return successResult.code;
} else {
await promptForOpenOutputChannel(`${codeTemplate} 请查看控制台信息~`, OutPutType.error);
}
return successResult.code;
}
return 100;
}

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