Skip to content

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

Closed
wants to merge 10 commits into from
Closed

Star Problems Extension && All Problems Category #188

wants to merge 10 commits into from

Conversation

Vigilans
Copy link
Contributor

@Vigilans Vigilans commented Mar 6, 2019

Introduction

Solved following issues:

Details

Feature

  • LeetCodeNode now exposes its inner IProblem data through nodeData property.
  • TreeDataProvider now preserves a allProblems field which holds the reference of all problems.
  • TreeDataProvider now provides ability to update one specific problem data with the help of allProblems field.
  • The starred problem will be appended with "♥" in the tree item name.
  • The hovering tooltip is enhanced to work for All Problems Category and Favorite Category.

Flaw

  • After star/unstar problems, the tree view return to uncollapsed state. It is because we do not store collapse state in the TreeDataProvider.

Demonstration

image
image
image

@jdneo
Copy link
Member

jdneo commented Mar 6, 2019

@Vigilans Thank you for your contribution, will take a look later~

@jdneo jdneo self-requested a review March 6, 2019 10:27
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();
Copy link
Member

Choose a reason for hiding this comment

The 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 this.onDidChangeTreeDataEvent.fire() (This API can accept a node element. Then all the data are cleared before the explorer rerendering.

Copy link
Contributor Author

@Vigilans Vigilans Mar 6, 2019

Choose a reason for hiding this comment

The 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 updateProblem function, we can fire events to request all relative nodes to be updated.

Copy link
Member

Choose a reason for hiding this comment

The 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 Two Sum in All Problem, will the Two Sum in Difficulty be refreshed?

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So updateProblem() is not used in this PR, right?

@@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about the appearance on different OS platforms, haha.

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we separate allProblems with treeData?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • allProblems is an id-IProblem dictionary primarily for fast lookup, whose structure is not compatible with treeData;
  • allProblems serves as a buffer which holds all the problems' ownership, where elements in treeData is just references to the problems;
  • with regard to this.onDidChangeTreeDataEvent.fire(problem) review below, we may need another lookup dictionary for fast retrieving relative nodes. allProblems seems more similar to it rather than treeData.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now allProblems is decoupled with treeData, and will be grouped with allTreeNodes as an object pool in next PR.

private updateTreeDataByProblem(problem: IProblem): void {
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we directly compare p.id with problem.id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
e.g. the favorite problems' ids are ["1", "4", "16", "23"], I want to delete the problem with id "23" and id is compared directly. Since "4" >= "23" === true, the search process stops at "4", thus the deletion is incorrect.

Copy link
Member

Choose a reason for hiding this comment

The 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 findIndex and splice to add/remove items, which I think is hard to read.

Can we change it to a Map?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it can be discussed in subsequent PRs, when updating treeData[Category.Company/Tag] field is necessary(delete problem from treeview after it is solved)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, can be solved in another PR.

@@ -197,6 +230,18 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
].join(os.EOL);
}

private updateTreeDataByProblem(problem: IProblem): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name her is confusing. Cuz we only handle isFavorite here, but the method name looks like we can update all the fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we might handle the case of updating other treeData fields the at a later time. Seems a overdesign here?
Should I leave a comment announcing modification of other categories is not needed yet here, or just change the method name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@Vigilans
Copy link
Contributor Author

Vigilans commented Mar 7, 2019

In order to hide solved problems in All Problems category, Category.All is added as a field of treeData.

Now, allProblems holds the ownership of every problem, whereas treeData[Category.All] may hide any problem that is solved.

return {
label: element.name,
tooltip: this.getSubCategoryTooltip(element),
id: `LeetCode.Category::${element.parentId}.${element.id}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have element.parentId, Data.Now() prefix seems no longer necessary.

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you will send out another PR to remove element.parentId & Data.Now(), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

@jdneo
Copy link
Member

jdneo commented Mar 7, 2019

I'll take a deep look at the weekend.

};

private onDidChangeTreeDataEvent: vscode.EventEmitter<any> = new vscode.EventEmitter<any>();
private onDidChangeTreeDataEvent: vscode.EventEmitter<LeetCodeNode> = new vscode.EventEmitter<LeetCodeNode>();
Copy link
Member

@jdneo jdneo Mar 8, 2019

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

Copy link
Contributor Author

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

@Vigilans
Copy link
Contributor Author

Vigilans commented Mar 8, 2019

There are so many interesting and important things about treeview...which will make our explorer far better than current form.

TreeItem.id & Collapse State

TreeItem.description

/**
 * A human readable string which is rendered less prominent.
 * When `true`, it is derived from [resourceUri](#TreeItem.resourceUri) and when `falsy`, it is not shown.
 */
description?: string | boolean;

This field is officially public since vscode engine 1.30, and gitlens has already adopted it:
image
May be we can show some message including difficulty and pass rate to solve #192.

TreeItem.highlight

Update: proposed API is only available in insider...pass.

Toggle Favorite through button

This is the solution adopted by gitlens, which I think perfectly suits the situation:
image
image

Reference Repo

vscode-sample
GitLens

I think this PR could be temporarily suspended for further discussion, or merge it for now and then progressively refactoring the TreeDataProvider.

@jdneo
Copy link
Member

jdneo commented Mar 10, 2019

@Vigilans looks like we have a lot of works to do with the explorer. Yes, my personal suggestion is to suspend the PR just for now and implement the improvement one by one.

You can separately create issues for each of them, and we can discuss the implementation in each issue before sending out the PR.

@Vigilans Vigilans mentioned this pull request Mar 11, 2019
@Allianzcortex
Copy link

If we can add more search options(e.g. from easy->difficult) when displaying the problems in one category ,it can be awesome . The order of problems up to now is based on number order.

@jdneo
Copy link
Member

jdneo commented May 10, 2019

@Vigilans

Seems this PR is out of date. Shall we close this and open another PR about toggle the favorite problem? (Show all problems have been resolved)

@Vigilans
Copy link
Contributor Author

@jdneo Yes, and there are some more improvements that could be done on toggleFavorite, like using a button to toggle.

@Vigilans Vigilans closed this May 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants