diff --git a/.vscode/launch.json b/.vscode/launch.json index a211681..7db916f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,6 +26,7 @@ "request": "attach", "name": "attach process", "processId": "${command:PickProcess}", + "continueOnAttach": true }, { "name": "Launch Tests", diff --git a/CHANGELOG.md b/CHANGELOG.md index a1676e7..4d26ecf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## version 2.19.6 + +- 获取题目错误,则不生成文件 + ## version 2.19.5 - 子进程执行路径 diff --git a/package.json b/package.json index a608f0d..4296acb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/controller/TreeViewController.ts b/src/controller/TreeViewController.ts index dd18078..3f6ef6c 100644 --- a/src/controller/TreeViewController.ts +++ b/src/controller/TreeViewController.ts @@ -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 => 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 => 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); } @@ -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 { diff --git a/src/rpc/actionChain/chainNode/leetcode.cn.ts b/src/rpc/actionChain/chainNode/leetcode.cn.ts index 8ae05e3..d95b7e0 100644 --- a/src/rpc/actionChain/chainNode/leetcode.cn.ts +++ b/src/rpc/actionChain/chainNode/leetcode.cn.ts @@ -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"); diff --git a/src/rpc/factory/api/showApi.ts b/src/rpc/factory/api/showApi.ts index 8ee34a1..34c0bc5 100644 --- a/src/rpc/factory/api/showApi.ts +++ b/src/rpc/factory/api/showApi.ts @@ -8,7 +8,6 @@ */ // let util = require("util"); -let childProcess = require("child_process"); import { storageUtils } from "../../utils/storageUtils"; @@ -32,17 +31,6 @@ 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", @@ -50,12 +38,6 @@ class ShowApi extends ApiBase { 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", @@ -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; } @@ -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 = {}; @@ -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) { @@ -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 { diff --git a/src/service/ExecuteService.ts b/src/service/ExecuteService.ts index 3548796..434c466 100644 --- a/src/service/ExecuteService.ts +++ b/src/service/ExecuteService.ts @@ -146,7 +146,7 @@ class ExecuteService implements Disposable { filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean - ): Promise { + ): Promise { const templateType: string = showDescriptionInComment ? "-cx" : "-c"; const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.qid, templateType, "-l", language]; @@ -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 {