-
Notifications
You must be signed in to change notification settings - Fork 672
Star Problems Extension && All Problems Category #188
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
Changes from 6 commits
8d5d6d6
8c7e53e
413564c
42b0856
95dea19
885cac7
a11f371
ed4b40c
62b9c5c
7480bc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 { IProblem } from "../shared"; | ||
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils"; | ||
|
||
export async function starProblem(provider: LeetCodeTreeDataProvider, node: LeetCodeNode): Promise<void> { | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
const problem: IProblem = Object.assign({}, node.nodeData, { | ||
isFavorite: await leetCodeExecutor.starProblem(node, !node.isFavorite), | ||
}); | ||
provider.updateProblem(problem); | ||
} catch (error) { | ||
await promptForOpenOutputChannel("Failed to star the problem. Please open the output channel for details.", DialogType.error); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ import { LeetCodeNode } from "./LeetCodeNode"; | |
|
||
export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCodeNode> { | ||
|
||
private allProblems: Map<string, IProblem>; // store reference of all problems. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now |
||
|
||
private treeData: { | ||
Difficulty: Map<string, IProblem[]>, | ||
Tag: Map<string, IProblem[]>, | ||
|
@@ -31,6 +33,14 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
this.onDidChangeTreeDataEvent.fire(); | ||
} | ||
|
||
public async updateProblem(problem: IProblem): Promise<void> { | ||
if (this.allProblems.has(problem.id)) { | ||
this.updateTreeDataByProblem(problem); // only modify the content of tree data, problem is not updated. | ||
Object.assign(this.allProblems.get(problem.id), problem); // update problem, where reference is preserved. | ||
this.onDidChangeTreeDataEvent.fire(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that the reason that explorer returns to uncollapsed state is that, we do not pass the node element into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some investigations I seems to get the hang of this API. Maybe we can keep another reference container here: allTreeNodes: Map<string, LeetcodeNode[]> // id -> relative nodes Then, in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Vigilans Cool. Noticed that now the same problem in the different category has different id. So if we refresh the That means, I'm thinking that if we should make the same problem has the same id in the whole explorer. Not sure the real behavior of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After further investigations, to accomplish this feature, there are some important changes which I think should be discussed in next PR. In this PR, we may temporarily accept the current behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So |
||
} | ||
} | ||
|
||
public getTreeItem(element: LeetCodeNode): vscode.TreeItem | Thenable<vscode.TreeItem> { | ||
if (element.id === "notSignIn") { | ||
return { | ||
|
@@ -46,7 +56,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
|
||
const idPrefix: number = Date.now(); | ||
return { | ||
label: element.isProblem ? `[${element.id}] ${element.name}` : element.name, | ||
label: element.isProblem ? `[${element.id}] ${element.name} ${element.isFavorite ? "♥" : ""}` : element.name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious about the |
||
tooltip: this.getSubCategoryTooltip(element), | ||
id: `${idPrefix}.${element.parentName}.${element.id}`, | ||
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed, | ||
|
@@ -66,6 +76,10 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
} | ||
if (!element) { // Root view | ||
return [ | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: Category.All, | ||
name: Category.All, | ||
}), "ROOT", false), | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: Category.Difficulty, | ||
name: Category.Difficulty, | ||
|
@@ -85,6 +99,9 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
]; | ||
} else { | ||
switch (element.name) { // First-level | ||
case Category.All: | ||
const all: IProblem[] = [...this.allProblems.values()]; | ||
return all.map((p: IProblem) => new LeetCodeNode(p, Category.All)); | ||
case Category.Favorite: | ||
const nodes: IProblem[] = this.treeData[Category.Favorite]; | ||
return nodes.map((p: IProblem) => new LeetCodeNode(p, Category.Favorite)); | ||
|
@@ -100,13 +117,15 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
|
||
private async getProblemData(): Promise<void> { | ||
// clear cache | ||
this.allProblems = new Map<string, IProblem>(); | ||
this.treeData = { | ||
Difficulty: new Map<string, IProblem[]>(), | ||
Tag: new Map<string, IProblem[]>(), | ||
Company: new Map<string, IProblem[]>(), | ||
Favorite: [], | ||
}; | ||
for (const problem of await list.listProblems()) { | ||
this.allProblems.set(problem.id, problem); | ||
// Add favorite problem, no matter whether it is solved. | ||
if (problem.isFavorite) { | ||
this.treeData[Category.Favorite].push(problem); | ||
|
@@ -168,12 +187,26 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
} | ||
|
||
private getSubCategoryTooltip(element: LeetCodeNode): string { | ||
// return '' unless it is a sub-category node | ||
if (element.isProblem || !this.treeData[element.parentName]) { | ||
// return '' if it does not directly hold problems. | ||
if (element.isProblem) { | ||
return ""; | ||
} | ||
|
||
const problems: IProblem[] = this.treeData[element.parentName].get(element.id); | ||
let problems: IProblem[]; | ||
switch (element.name) { | ||
case Category.Difficulty: | ||
case Category.Tag: | ||
case Category.Company: | ||
return ""; | ||
case Category.All: | ||
problems = [...this.allProblems.values()]; | ||
break; | ||
case Category.Favorite: | ||
problems = this.treeData[Category.Favorite]; | ||
break; | ||
default: | ||
problems = this.treeData[element.parentName].get(element.id); | ||
break; | ||
} | ||
|
||
let acceptedNum: number = 0; | ||
let failedNum: number = 0; | ||
|
@@ -197,6 +230,18 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
].join(os.EOL); | ||
} | ||
|
||
private updateTreeDataByProblem(problem: IProblem): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method name her is confusing. Cuz we only handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we might handle the case of updating other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure now other fields will be updated here in the near PRs, e.g. when a problem is accepted, there will be some code here to update other fields. |
||
const origin: IProblem | undefined = this.allProblems.get(problem.id); | ||
if (origin && origin.isFavorite !== problem.isFavorite) { | ||
const problemIndex: number = this.treeData.Favorite.findIndex((p: LeetCodeNode) => Number(p.id) >= Number(problem.id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we directly compare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problems are originally in number-ascending order, thus the comparation in index searching should also be done this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, the main problem here is that we store the Favorite problems as an array instead of a Map(id -> Problem). So you may observe that we have to use Can we change it to a Map? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it can be discussed in subsequent PRs, when updating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, can be solved in another PR. |
||
if (problem.isFavorite) { | ||
this.treeData.Favorite.splice(problemIndex, 0, origin); // insert original problem's reference as favorite | ||
} else { | ||
this.treeData.Favorite.splice(problemIndex, 1); // delete favorite | ||
} | ||
} | ||
} | ||
|
||
private addProblemToTreeData(problem: IProblem): void { | ||
this.putProblemToMap(this.treeData.Difficulty, problem.difficulty, problem); | ||
for (const tag of problem.tags) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.