Skip to content

Commit fca83f4

Browse files
committed
获取多个题解
1 parent fc9b88c commit fca83f4

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# version 3.1.3 等待自测
2+
3+
- 重构代码,减少耦合
4+
- 中文选择 5 个题解,可能一个看不懂
5+
- 没搬的砖太多了,搬不完了,重置
6+
17
# version 3.1.2
28

39
- 重构 filebutton 代码, 减少代码耦合

src/childCall/childCallModule.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as os from "os";
1313
import * as path from "path";
1414
import { ExtensionContext, ProgressLocation, Progress } from "vscode";
1515
import { ConfigurationChangeEvent, Disposable, MessageItem, window, workspace } from "vscode";
16-
import { DialogOptions, OutPutType, Endpoint, leetcodeHasInited } from "../model/ConstDefind";
16+
import { DialogOptions, OutPutType, Endpoint, leetcodeHasInited, IQuickItemEx } from "../model/ConstDefind";
1717
import { getLeetCodeEndpoint, getNodePath } from "../utils/ConfigUtils";
1818
import { openUrl, ShowMessage } from "../utils/OutputUtils";
1919
import * as systemUtils from "../utils/SystemUtils";
@@ -175,13 +175,83 @@ class ExecuteService implements Disposable {
175175
if (!needTranslation) {
176176
cmd.push("-T");
177177
}
178+
let solution;
178179
if (cn_help) {
179180
cmd.push("-f");
181+
solution = await this.callWithMsg(
182+
"正在获取中文题解~~~",
183+
this.nodeExecutable,
184+
cmd,
185+
undefined,
186+
this.tryCnMulSolution,
187+
{}
188+
);
189+
} else {
190+
solution = await this.callWithMsg("正在获取题解~~~", this.nodeExecutable, cmd);
180191
}
181-
const solution: string = await this.callWithMsg("正在获取题解~~~", this.nodeExecutable, cmd);
192+
182193
return solution;
183194
}
184195

196+
public async tryCnMulSolution(_, child_process, resolve, reject) {
197+
child_process.stdout?.on("data", async (data: string | Buffer) => {
198+
data = data.toString();
199+
BABA.getProxy(BabaStr.LogOutputProxy).get_log().append(data);
200+
let successMatch: any;
201+
try {
202+
successMatch = JSON.parse(data);
203+
} catch (e) {
204+
successMatch = {};
205+
}
206+
207+
if (successMatch.oper == "requireOper") {
208+
let cookie = successMatch.cookie;
209+
let arg = successMatch.arg;
210+
if (arg.oper == "need_select") {
211+
let canSelect = arg.canSelect || [];
212+
const picks: Array<IQuickItemEx<string>> = [];
213+
214+
canSelect.forEach((element) => {
215+
if (element?.node?.slug) {
216+
picks.push({
217+
label: `${element?.node.title}`,
218+
description: `作者:${element?.node?.author?.username}`,
219+
detail: "",
220+
value: element?.node?.slug,
221+
});
222+
}
223+
});
224+
225+
const choice: IQuickItemEx<string> | undefined = await window.showQuickPick(picks, {
226+
ignoreFocusOut: true,
227+
});
228+
if (!choice) {
229+
child_process.stdin?.end();
230+
return reject(new Error(successMatch.msg));
231+
}
232+
233+
let select_result = {
234+
c: cookie,
235+
slug: choice.value,
236+
};
237+
child_process.stdin?.write(JSON.stringify(select_result));
238+
}
239+
return;
240+
}
241+
if (successMatch.code == 100) {
242+
child_process.stdin?.end();
243+
return resolve(data);
244+
} else if (successMatch.code < 0) {
245+
child_process.stdin?.end();
246+
return reject(new Error(successMatch.msg));
247+
}
248+
});
249+
child_process.stderr?.on("data", (data: string | Buffer) => {
250+
BABA.getProxy(BabaStr.LogOutputProxy).get_log().append(data.toString());
251+
});
252+
child_process.on("error", reject);
253+
}
254+
185255
public async getUserContest(needTranslation: boolean, username: string): Promise<string> {
186256
// solution don't support translation
187257
const cmd: string[] = [await this.getLeetCodeBinaryPath(), "query", "-b", username];

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ function getSolutionArticlesSlugList(question_slug: string, lang: string, cb) {
299299
opts.json = true;
300300
opts.body = {
301301
operationName: "questionSolutionArticles",
302-
variables: { questionSlug: question_slug, first: 1, skip: 0, orderBy: "DEFAULT", tagSlugs: [lang] },
302+
variables: { questionSlug: question_slug, first: 5, skip: 0, orderBy: "DEFAULT", tagSlugs: [lang] },
303303
query: [
304304
"query questionSolutionArticles($questionSlug: String!, $skip: Int, $first: Int, $orderBy: SolutionArticleOrderBy, $userInput: String, $tagSlugs: [String!]) {",
305305
"questionSolutionArticles(questionSlug: $questionSlug, skip: $skip, first: $first, orderBy: $orderBy, userInput: $userInput, tagSlugs: $tagSlugs) {",
@@ -317,6 +317,10 @@ function getSolutionArticlesSlugList(question_slug: string, lang: string, cb) {
317317
"fragment solutionArticle on SolutionArticleNode {",
318318
" uuid",
319319
" slug",
320+
" title",
321+
" author {",
322+
" username",
323+
" }",
320324
" byLeetcode",
321325
" __typename",
322326
"}",
@@ -333,7 +337,17 @@ function getSolutionArticlesSlugList(question_slug: string, lang: string, cb) {
333337
}
334338
});
335339

336-
cb(e, temp_result);
340+
if (edges.length > 0) {
341+
cnSelectSolution(edges, cb);
342+
} else {
343+
cb(e, temp_result);
344+
}
345+
});
346+
}
347+
348+
function cnSelectSolution(edges, cb) {
349+
reply.remote_post({ canSelect: edges, oper: "need_select" }, function (_, back_arg) {
350+
cb(undefined, back_arg.slug);
337351
});
338352
}
339353

src/rpc/utils/ReplyUtils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
let _ = require("underscore");
1111

1212
class Reply {
13+
init_recv_flag = false;
1314
output = _.bind(console.log, console);
1415
level: any;
1516
levels = new Map([
1617
["INFO", { value: 2 }],
1718
["WARN", { value: 3 }],
1819
["ERROR", { value: 4 }],
1920
]);
21+
22+
operCookie = 0;
23+
24+
operWaitMap: Map<number, any> = new Map<number, any>();
25+
2026
setLevel(name: any) {
2127
this.level = this.levels.get(name) || this.levels.get("INFO");
2228
}
@@ -57,6 +63,39 @@ class Reply {
5763
let s = args.map((x) => x.toString()).join(" ");
5864
this.output(s);
5965
}
66+
67+
initRecv() {
68+
// 监听父进程的输入
69+
this.init_recv_flag = true;
70+
process.stdin.on("data", (data) => {
71+
reply.recvCallback(data.toString());
72+
});
73+
}
74+
75+
recvCallback(data) {
76+
let data_ob = JSON.parse(data);
77+
let c = data_ob.c;
78+
let need_call = this.operWaitMap.get(c);
79+
if (need_call) {
80+
need_call.callback(need_call.arg, data_ob);
81+
}
82+
}
83+
84+
getOperCookie() {
85+
this.operCookie = this.operCookie + 1;
86+
return this.operCookie;
87+
}
88+
89+
remote_post(oper_data, cb) {
90+
if (!this.init_recv_flag) {
91+
this.initRecv();
92+
}
93+
let c = this.getOperCookie();
94+
this.operWaitMap.set(c, { arg: oper_data, callback: cb });
95+
96+
let msg = { oper: "requireOper", cookie: c, arg: oper_data };
97+
this.output(JSON.stringify(msg));
98+
}
6099
}
61100

62101
export const reply: Reply = new Reply();

0 commit comments

Comments
 (0)