-
Notifications
You must be signed in to change notification settings - Fork 664
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 all 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 toggleFavorite(provider: LeetCodeTreeDataProvider, node: LeetCodeNode): Promise<void> { | ||
try { | ||
const problem: IProblem = Object.assign({}, node.nodeData, { | ||
isFavorite: await leetCodeExecutor.toggleFavorite(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,26 +13,37 @@ import { LeetCodeNode } from "./LeetCodeNode"; | |
|
||
export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCodeNode> { | ||
|
||
private allProblems: Map<string, IProblem>; // maintains the ownership of all problems. | ||
|
||
private treeData: { | ||
Difficulty: Map<string, IProblem[]>, | ||
Tag: Map<string, IProblem[]>, | ||
Company: Map<string, IProblem[]>, | ||
Favorite: IProblem[], | ||
[Category.All]: IProblem[], | ||
[Category.Difficulty]: Map<string, IProblem[]>, | ||
[Category.Tag]: Map<string, IProblem[]>, | ||
[Category.Company]: Map<string, IProblem[]>, | ||
[Category.Favorite]: IProblem[], | ||
}; | ||
|
||
private onDidChangeTreeDataEvent: vscode.EventEmitter<any> = new vscode.EventEmitter<any>(); | ||
private onDidChangeTreeDataEvent: vscode.EventEmitter<LeetCodeNode> = new vscode.EventEmitter<LeetCodeNode>(); | ||
// tslint:disable-next-line:member-ordering | ||
public readonly onDidChangeTreeData: vscode.Event<any> = this.onDidChangeTreeDataEvent.event; | ||
public readonly onDidChangeTreeData: vscode.Event<LeetCodeNode> = this.onDidChangeTreeDataEvent.event; | ||
|
||
constructor(private context: vscode.ExtensionContext) { } | ||
|
||
public async refresh(): Promise<void> { | ||
await this.getProblemData(); | ||
await this.getFullProblemData(); | ||
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") { | ||
if (element.id === "NotSignIn") { | ||
return { | ||
label: element.name, | ||
id: element.id, | ||
|
@@ -42,71 +53,75 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
title: "Sign in to LeetCode", | ||
}, | ||
}; | ||
} else if (!element.isProblem) { // category | ||
return { | ||
label: element.name, | ||
tooltip: this.getSubCategoryTooltip(element), | ||
id: `LeetCode.Category::${element.parentId}.${element.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. Now that we have Btw, the TreeItem Generation code here is separated here for better readability, as well as convenience for next PR(store generated TreeItem somewhere in TreeProvider or LeetCodeNode) 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 you will send out another PR to remove 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. Data.Now() has already been removed here, which is taken place by element.parentId |
||
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed, | ||
contextValue: `${element.parentId}.${element.id}`.toLowerCase(), | ||
}; | ||
} else { // problem | ||
return { | ||
label: `[${element.id}] ${element.name} ${element.isFavorite ? "♥" : ""}`, | ||
tooltip: "", // TODO: Add Problem Tooltip | ||
id: `LeetCode.Problem::${element.parentId}.${element.id}`, | ||
collapsibleState: vscode.TreeItemCollapsibleState.None, | ||
contextValue: "problem", | ||
iconPath: this.parseIconPathFromProblemState(element), | ||
}; | ||
} | ||
|
||
const idPrefix: number = Date.now(); | ||
return { | ||
label: element.isProblem ? `[${element.id}] ${element.name}` : element.name, | ||
tooltip: this.getSubCategoryTooltip(element), | ||
id: `${idPrefix}.${element.parentName}.${element.id}`, | ||
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed, | ||
contextValue: element.isProblem ? "problem" : element.id.toLowerCase(), | ||
iconPath: this.parseIconPathFromProblemState(element), | ||
}; | ||
} | ||
|
||
public getChildren(element?: LeetCodeNode | undefined): vscode.ProviderResult<LeetCodeNode[]> { | ||
if (!leetCodeManager.getUser()) { | ||
return [ | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: "notSignIn", | ||
id: "NotSignIn", | ||
name: "Sign in to LeetCode", | ||
}), "ROOT", false), | ||
}), "Root", false), | ||
]; | ||
} | ||
if (!element) { // Root view | ||
return [ | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: Category.Difficulty, | ||
name: Category.Difficulty, | ||
}), "ROOT", false), | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: Category.Tag, | ||
name: Category.Tag, | ||
}), "ROOT", false), | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: Category.Company, | ||
name: Category.Company, | ||
}), "ROOT", false), | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: Category.Favorite, | ||
name: Category.Favorite, | ||
}), "ROOT", false), | ||
]; | ||
Category.All, | ||
Category.Difficulty, | ||
Category.Tag, | ||
Category.Company, | ||
Category.Favorite, | ||
].map((c: Category) => new LeetCodeNode( | ||
Object.assign({}, defaultProblem, { id: c, name: c }), "Root", false, | ||
)); | ||
} else { | ||
switch (element.name) { // First-level | ||
// First-level | ||
switch (element.name) { | ||
case Category.All: | ||
case Category.Favorite: | ||
const nodes: IProblem[] = this.treeData[Category.Favorite]; | ||
return nodes.map((p: IProblem) => new LeetCodeNode(p, Category.Favorite)); | ||
const nodes: IProblem[] = this.treeData[element.name]; | ||
return nodes.map((p: IProblem) => new LeetCodeNode(p, element.name)); | ||
case Category.Difficulty: | ||
case Category.Tag: | ||
case Category.Company: | ||
return this.composeSubCategoryNodes(element); | ||
default: // Second and lower levels | ||
return element.isProblem ? [] : this.composeProblemNodes(element); | ||
} | ||
// Second and lower levels | ||
return element.isProblem ? [] : this.composeProblemNodes(element); | ||
} | ||
} | ||
|
||
private async getProblemData(): Promise<void> { | ||
private async getFullProblemData(): 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: [], | ||
[Category.All]: [], | ||
[Category.Difficulty]: new Map<string, IProblem[]>(), | ||
[Category.Tag]: new Map<string, IProblem[]>(), | ||
[Category.Company]: new Map<string, IProblem[]>(), | ||
[Category.Favorite]: [], | ||
}; | ||
for (const problem of await list.listProblems()) { | ||
// Add every problem to problem pool | ||
this.allProblems.set(problem.id, problem); | ||
// Add favorite problem, no matter whether it is solved. | ||
if (problem.isFavorite) { | ||
this.treeData[Category.Favorite].push(problem); | ||
|
@@ -121,9 +136,9 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
} | ||
|
||
private composeProblemNodes(node: LeetCodeNode): LeetCodeNode[] { | ||
const map: Map<string, IProblem[]> | undefined = this.treeData[node.parentName]; | ||
const map: Map<string, IProblem[]> | undefined = this.treeData[node.parentId]; | ||
if (!map) { | ||
leetCodeChannel.appendLine(`Category: ${node.parentName} is not available.`); | ||
leetCodeChannel.appendLine(`Category: ${node.parentId} is not available.`); | ||
return []; | ||
} | ||
const problems: IProblem[] = map.get(node.name) || []; | ||
|
@@ -136,8 +151,8 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
|
||
private composeSubCategoryNodes(node: LeetCodeNode): LeetCodeNode[] { | ||
const category: Category = node.name as Category; | ||
if (category === Category.Favorite) { | ||
leetCodeChannel.appendLine("No sub-level for Favorite nodes"); | ||
if (category === Category.All || category === Category.Favorite) { | ||
leetCodeChannel.appendLine("No sub-level for All or Favorite nodes"); | ||
return []; | ||
} | ||
const map: Map<string, IProblem[]> | undefined = this.treeData[category]; | ||
|
@@ -149,7 +164,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod | |
} | ||
|
||
private parseIconPathFromProblemState(element: LeetCodeNode): string { | ||
if (!element.isProblem) { | ||
if (!element.isProblem) { // In fact will never be satisfied | ||
return ""; | ||
} | ||
switch (element.state) { | ||
|
@@ -168,12 +183,23 @@ 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) { // In fact will never be satisfied | ||
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: | ||
case Category.Favorite: | ||
problems = this.treeData[element.name]; | ||
break; | ||
default: | ||
problems = this.treeData[element.parentId].get(element.id); | ||
} | ||
|
||
let acceptedNum: number = 0; | ||
let failedNum: number = 0; | ||
|
@@ -197,13 +223,27 @@ 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) { | ||
// Find appropriate index to insert/delete a problem | ||
const problemIndex: number = this.treeData[Category.Favorite].findIndex((p: LeetCodeNode) => Number(p.id) >= Number(problem.id)); | ||
if (problem.isFavorite) { | ||
this.treeData[Category.Favorite].splice(problemIndex, 0, origin); // insert original problem's reference as favorite | ||
} else { | ||
this.treeData[Category.Favorite].splice(problemIndex, 1); // delete favorite | ||
} | ||
} | ||
} | ||
|
||
private addProblemToTreeData(problem: IProblem): void { | ||
this.putProblemToMap(this.treeData.Difficulty, problem.difficulty, problem); | ||
this.treeData[Category.All].push(problem); | ||
this.putProblemToMap(this.treeData[Category.Difficulty], problem.difficulty, problem); | ||
for (const tag of problem.tags) { | ||
this.putProblemToMap(this.treeData.Tag, this.beautifyCategoryName(tag), problem); | ||
this.putProblemToMap(this.treeData[Category.Tag], this.beautifyCategoryName(tag), problem); | ||
} | ||
for (const company of problem.companies) { | ||
this.putProblemToMap(this.treeData.Company, this.beautifyCategoryName(company), problem); | ||
this.putProblemToMap(this.treeData[Category.Company], this.beautifyCategoryName(company), problem); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also support refresh the whole explorer, so the parameter type can be written as
LeetCodeNode | null | undefined
You can address it in the next PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event fire's parameter is
fire(data: T?)
where T=LeetCodeNode, so no need to append null and undefined here