Skip to content

feat: Support toggle favorite problem #405

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 2 commits into from
Sep 3, 2019
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
42 changes: 39 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@
"title": "Submit to LeetCode",
"category": "LeetCode"
},
{
"command": "leetcode.addFavorite",
"title": "Add to Favorite List",
"category": "LeetCode",
"icon": {
"light": "resources/light/like.png",
"dark": "resources/dark/like.png"
}
},
{
"command": "leetcode.removeFavorite",
"title": "Remove from Favorite List",
"category": "LeetCode",
"icon": {
"light": "resources/light/dislike.png",
"dark": "resources/dark/dislike.png"
}
},
{
"command": "leetcode.switchDefaultLanguage",
"title": "Switch Default Language",
Expand Down Expand Up @@ -178,18 +196,28 @@
"view/item/context": [
{
"command": "leetcode.previewProblem",
"when": "view == leetCodeExplorer && viewItem == problem",
"when": "view == leetCodeExplorer && viewItem =~ /problem*/",
"group": "leetcode@1"
},
{
"command": "leetcode.showProblem",
"when": "view == leetCodeExplorer && viewItem == problem",
"when": "view == leetCodeExplorer && viewItem =~ /problem*/",
"group": "leetcode@2"
},
{
"command": "leetcode.showSolution",
"when": "view == leetCodeExplorer && viewItem == problem",
"when": "view == leetCodeExplorer && viewItem =~ /problem*/",
"group": "leetcode@3"
},
{
"command": "leetcode.addFavorite",
"when": "view == leetCodeExplorer && viewItem == problem",
"group": "inline"
},
{
"command": "leetcode.removeFavorite",
"when": "view == leetCodeExplorer && viewItem == problem-favorite",
"group": "inline"
}
],
"commandPalette": [
Expand All @@ -204,6 +232,14 @@
{
"command": "leetcode.previewProblem",
"when": "never"
},
{
"command": "leetcode.addFavorite",
"when": "never"
},
{
"command": "leetcode.removeFavorite",
"when": "never"
}
],
"explorer/context": [
Expand Down
Binary file added resources/dark/dislike.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/dark/like.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/light/dislike.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/light/like.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/commands/star.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import { LeetCodeNode } from "../explorer/LeetCodeNode";
import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";

export async function addFavorite(node: LeetCodeNode): Promise<void> {
try {
await leetCodeExecutor.toggleFavorite(node, true);
leetCodeTreeDataProvider.refresh();
} catch (error) {
await promptForOpenOutputChannel("Failed to add the problem to favorite. Please open the output channel for details.", DialogType.error);
}
}

export async function removeFavorite(node: LeetCodeNode): Promise<void> {
try {
await leetCodeExecutor.toggleFavorite(node, false);
leetCodeTreeDataProvider.refresh();
} catch (error) {
await promptForOpenOutputChannel("Failed to remove the problem from favorite. Please open the output channel for details.", DialogType.error);
}
}
9 changes: 8 additions & 1 deletion src/explorer/LeetCodeTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
};
}

let contextValue: string;
if (element.isProblem) {
contextValue = element.isFavorite ? "problem-favorite" : "problem";
} else {
contextValue = element.id.toLowerCase();
}

return {
label: element.isProblem ? `[${element.id}] ${element.name}` : element.name,
tooltip: this.getSubCategoryTooltip(element),
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
contextValue: element.isProblem ? "problem" : element.id.toLowerCase(),
iconPath: this.parseIconPathFromProblemState(element),
command: element.isProblem ? element.previewCommand : undefined,
contextValue,
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { switchDefaultLanguage } from "./commands/language";
import * as plugin from "./commands/plugin";
import * as session from "./commands/session";
import * as show from "./commands/show";
import * as star from "./commands/star";
import * as submit from "./commands/submit";
import * as test from "./commands/test";
import { explorerNodeManager } from "./explorer/explorerNodeManager";
Expand Down Expand Up @@ -61,6 +62,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
vscode.commands.registerCommand("leetcode.addFavorite", (node: LeetCodeNode) => star.addFavorite(node)),
vscode.commands.registerCommand("leetcode.removeFavorite", (node: LeetCodeNode) => star.removeFavorite(node)),
);

await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());
Expand Down
8 changes: 8 additions & 0 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ class LeetCodeExecutor implements Disposable {
}
}

public async toggleFavorite(node: IProblem, addToFavorite: boolean): Promise<void> {
const commandParams: string[] = [await this.getLeetCodeBinaryPath(), "star", node.id];
if (!addToFavorite) {
commandParams.push("-d");
}
await this.executeCommandWithProgressEx("Updating the favorite list...", "node", commandParams);
}

public async getCompaniesAndTags(): Promise<{ companies: { [key: string]: string[] }, tags: { [key: string]: string[] } }> {
// preprocess the plugin source
const companiesTagsPath: string = path.join(await leetCodeExecutor.getLeetCodeRootPath(), "lib", "plugins", "company.js");
Expand Down