- Notifications
You must be signed in to change notification settings - Fork669
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
8d5d6d6
8c7e53e
413564c
42b0856
95dea19
885cac7
a11f371
ed4b40c
62b9c5c
7480bc6
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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: { | ||
[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<LeetCodeNode> = new vscode.EventEmitter<LeetCodeNode>(); | ||
Member 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. We can also support refresh the whole explorer, so the parameter type can be written as You can address it in the next PR 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 event fire's parameter is | ||
// tslint:disable-next-line:member-ordering | ||
public readonly onDidChangeTreeData: vscode.Event<LeetCodeNode> = this.onDidChangeTreeDataEvent.event; | ||
constructor(private context: vscode.ExtensionContext) { } | ||
public async refresh(): Promise<void> { | ||
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 ContributorAuthor 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 { | ||
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), | ||
}; | ||
} | ||
} | ||
public getChildren(element?: LeetCodeNode | undefined): vscode.ProviderResult<LeetCodeNode[]> { | ||
if (!leetCodeManager.getUser()) { | ||
return [ | ||
new LeetCodeNode(Object.assign({}, defaultProblem, { | ||
id: "NotSignIn", | ||
name: "Sign in to LeetCode", | ||
}), "Root", false), | ||
]; | ||
} | ||
if (!element) { // Root view | ||
return [ | ||
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 { | ||
// First-level | ||
switch (element.name) { | ||
case Category.All: | ||
case 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); | ||
} | ||
// Second and lower levels | ||
return element.isProblem ? [] : this.composeProblemNodes(element); | ||
} | ||
} | ||
private asyncgetFullProblemData(): Promise<void> { | ||
// clear cache | ||
this.allProblems = new Map<string, IProblem>(); | ||
this.treeData = { | ||
[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.parentId]; | ||
if (!map) { | ||
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.All || category === Category.Favorite) { | ||
leetCodeChannel.appendLine("No sub-level forAll orFavorite 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) { // 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 ''if itdoes not directly hold problems. | ||
if (element.isProblem) { // In fact will never be satisfied | ||
return ""; | ||
} | ||
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.treeData[Category.All].push(problem); | ||
this.putProblemToMap(this.treeData[Category.Difficulty], problem.difficulty, problem); | ||
for (const tag of problem.tags) { | ||
this.putProblemToMap(this.treeData[Category.Tag], this.beautifyCategoryName(tag), problem); | ||
} | ||
for (const company of problem.companies) { | ||
this.putProblemToMap(this.treeData[Category.Company], this.beautifyCategoryName(company), problem); | ||
} | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.