8000 feat: Support toggle favorite problem (#405) · whouey/vscode-leetcode@71d9ec9 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 71d9ec9

Browse files
authored
feat: Support toggle favorite problem (LeetCode-OpenSource#405)
1 parent 6ae2284 commit 71d9ec9

File tree

9 files changed

+84
-4
lines changed

9 files changed

+84
-4
lines changed

package.json

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@
124124
"title": "Submit to LeetCode",
125125
"category": "LeetCode"
126126
},
127+
{
128+
"command": "leetcode.addFavorite",
129+
"title": "Add to Favorite List",
130+
"category": "LeetCode",
131+
"icon": {
132+
"light": "resources/light/like.png",
133+
"dark": "resources/dark/like.png"
134+
}
135+
},
136+
{
137+
"command": "leetcode.removeFavorite",
138+
"title": "Remove from Favorite List",
139+
"category": "LeetCode",
140+
"icon": {
141+
"light": "resources/light/dislike.png",
142+
"dark": "resources/dark/dislike.png"
143+
}
144+
},
127145
{
128146
"command": "leetcode.switchDefaultLanguage",
129147
"title": "Switch Default Language",
@@ -178,18 +196,28 @@
178196
"view/item/context": [
179197
{
180198
"command": "leetcode.previewProblem",
181-
"when": "view == leetCodeExplorer && viewItem == problem",
199+
"when": "view == leetCodeExplorer && viewItem =~ /problem*/",
182200
"group": "leetcode@1"
183201
},
184202
{
185203
"command": "leetcode.showProblem",
186-
"when": "view == leetCodeExplorer && viewItem == problem",
204+
"when": "view == leetCodeExplorer && viewItem =~ /problem*/",
187205
"group": "leetcode@2"
188206
},
189207
{
190208
"command": "leetcode.showSolution",
191-
"when": "view == leetCodeExplorer && viewItem == problem",
209+
"when": "view == leetCodeExplorer && viewItem =~ /problem*/",
192210
"group": "leetcode@3"
211+
},
212+
{
213+
"command": "leetcode.addFavorite",
214+
"when": "view == leetCodeExplorer && viewItem == problem",
215+
"group": "inline"
216+
},
217+
{
218+
"command": "leetcode.removeFavorite",
219+
"when": "view == leetCodeExplorer && viewItem == problem-favorite",
220+
"group": "inline"
193221
}
194222
],
195223
"commandPalette": [
@@ -204,6 +232,14 @@
204232
{
205233
"command": "leetcode.previewProblem",
206234
"when": "never"
235+
},
236+
{
237+
"command": "leetcode.addFavorite",
238+
"when": "never"
239+
},
240+
{
241+
"command": "leetcode.removeFavorite",
242+
"when": "never"
207243
}
208244
67E6 ],
209245
"explorer/context": [

resources/dark/dislike.png

700 Bytes
Loading

resources/dark/like.png

472 Bytes
Loading

resources/light/dislike.png

599 Bytes
Loading

resources/light/like.png

418 Bytes
Loading

src/commands/star.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
// Copyright (c) jdneo. All rights reserved.
3+
// Licensed under the MIT license.
4+
5+
import { LeetCodeNode } from "../explorer/LeetCodeNode";
6+
import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider";
7+
import { leetCodeExecutor } from "../leetCodeExecutor";
8+
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
9+
10+
export async function addFavorite(node: LeetCodeNode): Promise<void> {
11+
try {
12+
await leetCodeExecutor.toggleFavorite(node, true);
13+
leetCodeTreeDataProvider.refresh();
14+
} catch (error) {
15+
await promptForOpenOutputChannel("Failed to add the problem to favorite. Please open the output channel for details.", DialogType.error);
16+
}
17+
}
18+
19+
export async function removeFavorite(node: LeetCodeNode): Promise<void> {
20+
try {
21+
await leetCodeExecutor.toggleFavorite(node, false);
22+
leetCodeTreeDataProvider.refresh();
23+
} catch (error) {
24+
await promptForOpenOutputChannel("Failed to remove the problem from favorite. Please open the output channel for details.", DialogType.error);
25+
}
26+
}

src/explorer/LeetCodeTreeDataProvider.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
3838
};
3939
}
4040

41+
let contextValue: string;
42+
if (element.isProblem) {
43+
contextValue = element.isFavorite ? "problem-favorite" : "problem";
44+
} else {
45+
contextValue = element.id.toLowerCase();
46+
}
47+
4148
return {
4249
label: element.isProblem ? `[${element.id}] ${element.name}` : element.name,
4350
tooltip: this.getSubCategoryTooltip(element),
4451
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
45-
contextValue: element.isProblem ? "problem" : element.id.toLowerCase(),
4652
iconPath: this.parseIconPathFromProblemState(element),
4753
command: element.isProblem ? element.previewCommand : undefined,
54+
contextValue,
4855
};
4956
}
5057

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { switchDefaultLanguage } from "./commands/language";
88
import * as plugin from "./commands/plugin";
99
import * as session from "./commands/session";
1010
import * as show from "./commands/show";
11+
import * as star from "./commands/star";
1112
import * as submit from "./commands/submit";
12< FDAC /td>13
import * as test from "./commands/test";
1314
import { explorerNodeManager } from "./explorer/explorerNodeManager";
@@ -61,6 +62,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
6162
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
6263
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
6364
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
65+
vscode.commands.registerCommand("leetcode.addFavorite", (node: LeetCodeNode) => star.addFavorite(node)),
66+
vscode.commands.registerCommand("leetcode.removeFavorite", (node: LeetCodeNode) => star.removeFavorite(node)),
6467
);
6568

6669
await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());

src/leetCodeExecutor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ class LeetCodeExecutor implements Disposable {
162162
}
163163
}
164164

165+
public async toggleFavorite(node: IProblem, addToFavorite: boolean): Promise<void> {
166+
const commandParams: string[] = [await this.getLeetCodeBinaryPath(), "star", node.id];
167+
if (!addToFavorite) {
168+
commandParams.push("-d");
169+
}
170+
await this.executeCommandWithProgressEx("Updating the favorite list...", "node", commandParams);
171+
}
172+
165173
public async getCompaniesAndTags(): Promise<{ companies: { [key: string]: string[] }, tags: { [key: string]: string[] } }> {
166174
// preprocess the plugin source
167175
const companiesTagsPath: string = path.join(await leetCodeExecutor.getLeetCodeRootPath(), "lib", "plugins", "company.js");

0 commit comments

Comments
 (0)
0