forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetCodeExplorer.ts
181 lines (162 loc) · 6.16 KB
/
leetCodeExplorer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use strict";
import * as path from "path";
import * as vscode from "vscode";
import * as list from "./commands/list";
import { leetCodeManager } from "./leetCodeManager";
import { ProblemState } from "./shared";
// tslint:disable:max-classes-per-file
export class LeetCodeNode {
constructor(private data: list.IProblem, private isProblemNode = true) { }
public get locked(): boolean {
return this.data.locked;
}
public get name(): string {
return this.data.name;
}
public get state(): ProblemState {
return this.data.state;
}
public get id(): string {
return this.data.id;
}
public get passRate(): string {
return this.data.passRate;
}
public get isProblem(): boolean {
return this.isProblemNode;
}
}
export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCodeNode> {
private treeData: Map<string, list.IProblem[]> = new Map();
private onDidChangeTreeDataEvent: vscode.EventEmitter<any> = new vscode.EventEmitter<any>();
// tslint:disable-next-line:member-ordering
public readonly onDidChangeTreeData: vscode.Event<any> = this.onDidChangeTreeDataEvent.event;
constructor(private context: vscode.ExtensionContext, private channel: vscode.OutputChannel) { }
public async refresh(): Promise<void> {
await this.getProblemData();
this.onDidChangeTreeDataEvent.fire();
}
public getTreeItem(element: LeetCodeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {
if (element.id === "notSignIn") {
return {
label: element.name,
id: element.id,
collapsibleState: vscode.TreeItemCollapsibleState.None,
command: {
command: "leetcode.signin",
title: "Sign in to LeetCode",
},
};
}
const idPrefix: number = Date.now();
return {
label: element.isProblem ? `[${element.id}] ${element.name}` : element.name,
id: `${idPrefix}.${element.id}`,
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
contextValue: element.isProblem ? "problem" : "difficulty",
iconPath: this.parseIconPathFromProblemState(element),
};
}
public getChildren(element?: LeetCodeNode | undefined): vscode.ProviderResult<LeetCodeNode[]> {
if (!leetCodeManager.getUser()) {
return [
new LeetCodeNode(
{
favorite: false,
locked: false,
state: ProblemState.Unknown,
id: "notSignIn",
name: "Sign in to LeetCode",
difficulty: "",
passRate: "",
},
false,
),
];
}
if (!element) {
return new Promise(async (resolve: (res: LeetCodeNode[]) => void): Promise<void> => {
await this.getProblemData();
resolve(this.composeDifficultyNodes());
});
} else {
return element.isProblem ? [] : this.composeProblemNodes(element.name);
}
}
private async getProblemData(): Promise<void> {
const allProblems: list.IProblem[] = await list.listProblems(this.channel);
this.treeData.clear();
for (const problem of allProblems) {
const problems: list.IProblem[] | undefined = this.treeData.get(problem.difficulty);
if (problems) {
problems.push(problem);
} else {
this.treeData.set(problem.difficulty, [problem]);
}
}
}
private composeProblemNodes(difficulty: string): LeetCodeNode[] {
const problems: list.IProblem[] | undefined = this.treeData.get(difficulty);
if (!problems || problems.length === 0) {
return [];
}
const problemNodes: LeetCodeNode[] = [];
for (const problem of problems) {
problemNodes.push(new LeetCodeNode(problem));
}
return problemNodes;
}
private composeDifficultyNodes(): LeetCodeNode[] {
const difficultynodes: LeetCodeNode[] = [];
for (const difficulty of this.treeData.keys()) {
difficultynodes.push(
new LeetCodeNode(
{
favorite: false,
locked: false,
state: ProblemState.Unknown,
id: difficulty,
name: difficulty,
difficulty: "",
passRate: "",
},
false,
),
);
}
difficultynodes.sort((a: LeetCodeNode, b: LeetCodeNode): number => {
function getValue(input: string): number {
switch (input.toLowerCase()) {
case "easy":
return 1;
case "medium":
return 2;
case "hard":
return 3;
default:
return Number.MAX_SAFE_INTEGER;
}
}
return getValue(a.name) - getValue(b.name);
});
return difficultynodes;
}
private parseIconPathFromProblemState(element: LeetCodeNode): string {
if (!element.isProblem) {
return "";
}
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) {
return this.context.asAbsolutePath(path.join("resources", "lock.png"));
}
return this.context.asAbsolutePath(path.join("resources", "blank.png"));
default:
return "";
}
}
}