From d27544d823f81cad8cac7d7b791c80560d579238 Mon Sep 17 00:00:00 2001 From: Bill Chen Date: Tue, 24 Aug 2021 21:55:28 +0800 Subject: [PATCH 1/2] feat: Select problem of today. Change-Id: Iea363f956d1f2fed5bbb9af900cd78defb346308 --- package.json | 12 ++++++++++++ src/commands/show.ts | 22 ++++++++++++++++++++++ src/extension.ts | 1 + src/leetCodeExecutor.ts | 5 +++++ 4 files changed, 40 insertions(+) diff --git a/package.json b/package.json index cad81253..4d1d0174 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "onCommand:leetcode.manageSessions", "onCommand:leetcode.refreshExplorer", "onCommand:leetcode.pickOne", + "onCommand:leetcode.problemOfToday", "onCommand:leetcode.showProblem", "onCommand:leetcode.previewProblem", "onCommand:leetcode.searchProblem", @@ -81,6 +82,12 @@ "title": "Pick One", "category": "LeetCode" }, + { + "command": "leetcode.problemOfToday", + "title": "Problem Of Today", + "category": "LeetCode", + "icon": "$(calendar)" + }, { "command": "leetcode.showProblem", "title": "Show Problem", @@ -175,6 +182,11 @@ "when": "view == leetCodeExplorer", "group": "navigation@3" }, + { + "command": "leetcode.problemOfToday", + "when": "view == leetCodeExplorer", + "group": "navigation@4" + }, { "command": "leetcode.pickOne", "when": "view == leetCodeExplorer", diff --git a/src/commands/show.ts b/src/commands/show.ts index 3aebce8f..60afcdca 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -52,6 +52,28 @@ export async function pickOne(): Promise { await showProblemInternal(randomProblem); } +export async function problemOfToday(): Promise { + if (!leetCodeManager.getUser()) { + promptForSignIn(); + return; + } + try { + const nodes: LeetCodeNode[] = await explorerNodeManager.getAllNodes() + const problemOfTodayStr: string = await leetCodeExecutor.problemOfToday(); + const lines: string[] = problemOfTodayStr.split("\n"); + const reg: RegExp = /^\[(.+)\]\s.*/ + const match: RegExpMatchArray | null = lines[0].match(reg); + if (match != null) { + const id = match[1] + const problemOfToday: IProblem = nodes.filter(one => one.id === id)[0]; + await showProblemInternal(problemOfToday); + } + } + catch { + await promptForOpenOutputChannel("Fail to load problem of today. You may need to refresh the problem list. Open the output channel for details.", DialogType.error); + } +} + export async function showProblem(node?: LeetCodeNode): Promise { if (!node) { return; diff --git a/src/extension.ts b/src/extension.ts index 5bd026f1..452e65af 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -56,6 +56,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)), vscode.commands.registerCommand("leetcode.pickOne", () => show.pickOne()), + vscode.commands.registerCommand("leetcode.problemOfToday", () => show.problemOfToday()), vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()), vscode.commands.registerCommand("leetcode.showSolution", (input: LeetCodeNode | vscode.Uri) => show.showSolution(input)), vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()), diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index d2332c7a..d8b611dc 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -100,6 +100,11 @@ class LeetCodeExecutor implements Disposable { return await this.executeCommandEx(this.nodeExecutable, cmd); } + public async problemOfToday(): Promise { + const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", "-d"]; + return await this.executeCommandWithProgressEx("Loading problem of today...", this.nodeExecutable, cmd); + } + public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise { const templateType: string = showDescriptionInComment ? "-cx" : "-c"; const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.id, templateType, "-l", language]; From ab6bce87878a72dd858cacc61bed2ec52c801b7a Mon Sep 17 00:00:00 2001 From: Bill Chen Date: Sat, 9 Oct 2021 10:23:39 +0800 Subject: [PATCH 2/2] fix: corner case --- src/commands/show.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/commands/show.ts b/src/commands/show.ts index 60afcdca..d060cf1c 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -58,19 +58,24 @@ export async function problemOfToday(): Promise { return; } try { - const nodes: LeetCodeNode[] = await explorerNodeManager.getAllNodes() + const nodes: LeetCodeNode[] = explorerNodeManager.getAllNodes() const problemOfTodayStr: string = await leetCodeExecutor.problemOfToday(); const lines: string[] = problemOfTodayStr.split("\n"); const reg: RegExp = /^\[(.+)\]\s.*/ const match: RegExpMatchArray | null = lines[0].match(reg); if (match != null) { const id = match[1] - const problemOfToday: IProblem = nodes.filter(one => one.id === id)[0]; - await showProblemInternal(problemOfToday); + const problemOfToday: IProblem[] = nodes.filter(one => one.id === id); + if (problemOfToday.length != 1) { + await promptForOpenOutputChannel("Fail to load problem of today. You may need to refresh the problem list.", DialogType.error); + } + else { + await showProblemInternal(problemOfToday[0]); + } } } catch { - await promptForOpenOutputChannel("Fail to load problem of today. You may need to refresh the problem list. Open the output channel for details.", DialogType.error); + await promptForOpenOutputChannel("Fail to load problem of today. Open the output channel for details.", DialogType.error); } }