-
Notifications
You must be signed in to change notification settings - Fork 665
/
Copy pathLeetCodeTreeDataProvider.ts
154 lines (138 loc) · 5.78 KB
/
LeetCodeTreeDataProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { leetCodeManager } from "../leetCodeManager";
import { Category, defaultProblem, ProblemState } from "../shared";
import { explorerNodeManager } from "./explorerNodeManager";
import { LeetCodeNode } from "./LeetCodeNode";
import { globalState } from "../globalState";
export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCodeNode> {
private context: vscode.ExtensionContext;
private onDidChangeTreeDataEvent: vscode.EventEmitter<LeetCodeNode | undefined | null> = new vscode.EventEmitter<
LeetCodeNode | undefined | null
>();
// tslint:disable-next-line:member-ordering
public readonly onDidChangeTreeData: vscode.Event<any> = this.onDidChangeTreeDataEvent.event;
public initialize(context: vscode.ExtensionContext): void {
this.context = context;
}
public async refresh(): Promise<void> {
await explorerNodeManager.refreshCache();
this.onDidChangeTreeDataEvent.fire(null);
}
public getTreeItem(element: LeetCodeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {
if (element.id === "notSignIn") {
return {
label: element.name,
collapsibleState: vscode.TreeItemCollapsibleState.None,
command: {
command: "leetcode.signin",
title: "Sign in to LeetCode",
},
};
}
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}` + this.parsePremiumUnLockIconPath(element) : element.name,
tooltip: this.getSubCategoryTooltip(element),
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
iconPath: this.parseIconPathFromProblemState(element),
command: element.isProblem ? element.previewCommand : undefined,
resourceUri: element.uri,
contextValue,
};
}
public getChildren(element?: LeetCodeNode | undefined): vscode.ProviderResult<LeetCodeNode[]> {
if (!leetCodeManager.getUser()) {
return [
new LeetCodeNode(
Object.assign({}, defaultProblem, {
id: "notSignIn",
name: "Sign in to LeetCode",
}),
false
),
];
}
if (!element) {
// Root view
return explorerNodeManager.getRootNodes();
} else {
switch (element.id) {
case Category.All:
return explorerNodeManager.getAllNodes();
case Category.Favorite:
return explorerNodeManager.getFavoriteNodes();
case Category.Difficulty:
return explorerNodeManager.getAllDifficultyNodes();
case Category.Tag:
return explorerNodeManager.getAllTagNodes();
case Category.Company:
return explorerNodeManager.getAllCompanyNodes();
case Category.Daily:
return explorerNodeManager.getDailyChallengeNode();
default:
if (element.isProblem) {
return [];
}
return explorerNodeManager.getChildrenNodesById(element.id);
}
}
}
private parseIconPathFromProblemState(element: LeetCodeNode): string {
if (!element.isProblem) {
return "";
}
const { isPremium } = globalState.getUserStatus() ?? {};
switch (element.state) {
case ProblemState.AC:
return this.context.asAbsolutePath(path.join("resources", "check.png"));
case ProblemState.NotAC:
return this.context.asAbsolutePath(path.join("resources", "x.png"));
case ProblemState.Unknown:
if (element.locked && !isPremium) {
return this.context.asAbsolutePath(path.join("resources", "lock.png"));
}
return this.context.asAbsolutePath(path.join("resources", "blank.png"));
default:
return "";
}
}
private parsePremiumUnLockIconPath(element: LeetCodeNode): string {
const { isPremium } = globalState.getUserStatus() ?? {};
if (isPremium && element.locked) {
return " 🔓";
}
return "";
}
private getSubCategoryTooltip(element: LeetCodeNode): string {
// return '' unless it is a sub-category node
if (element.isProblem || element.id === "ROOT" || element.id in Category) {
return "";
}
const childernNodes: LeetCodeNode[] = explorerNodeManager.getChildrenNodesById(element.id);
let acceptedNum: number = 0;
let failedNum: number = 0;
for (const node of childernNodes) {
switch (node.state) {
case ProblemState.AC:
acceptedNum++;
break;
case ProblemState.NotAC:
failedNum++;
break;
default:
break;
}
}
return [`AC: ${acceptedNum}`, `Failed: ${failedNum}`, `Total: ${childernNodes.length}`].join(os.EOL);
}
}
export const leetCodeTreeDataProvider: LeetCodeTreeDataProvider = new LeetCodeTreeDataProvider();