forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplorerNodeManager.ts
231 lines (217 loc) · 8.79 KB
/
explorerNodeManager.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import * as _ from "lodash";
import { Disposable, WorkspaceConfiguration, workspace } from "vscode";
import * as list from "../commands/list";
import { getSortingStrategy } from "../commands/plugin";
import { Category, defaultProblem, ProblemState, SortingStrategy } from "../shared";
import { shouldHideSolvedProblem } from "../utils/settingUtils";
import { LeetCodeNode } from "./LeetCodeNode";
interface CustomListConfig {
name: string,
problems: string[]
}
class ExplorerNodeManager implements Disposable {
private explorerNodeMap: Map<string, LeetCodeNode> = new Map<string, LeetCodeNode>();
private companySet: Set<string> = new Set<string>();
private tagSet: Set<string> = new Set<string>();
public async refreshCache(): Promise<void> {
this.dispose();
const shouldHideSolved: boolean = shouldHideSolvedProblem();
for (const problem of await list.listProblems()) {
if (shouldHideSolved && problem.state === ProblemState.AC) {
continue;
}
this.explorerNodeMap.set(problem.id, new LeetCodeNode(problem));
for (const company of problem.companies) {
this.companySet.add(company);
}
for (const tag of problem.tags) {
this.tagSet.add(tag);
}
}
}
public getRootNodes(): LeetCodeNode[] {
return [
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: Category.All,
name: Category.All,
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: Category.Difficulty,
name: Category.Difficulty,
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: Category.Tag,
name: Category.Tag,
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: Category.Company,
name: Category.Company,
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: Category.Favorite,
name: Category.Favorite,
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: Category.Custom,
name: Category.Custom,
}), false),
];
}
public getAllNodes(): LeetCodeNode[] {
return this.applySortingStrategy(
Array.from(this.explorerNodeMap.values()),
);
}
public getAllDifficultyNodes(): LeetCodeNode[] {
const res: LeetCodeNode[] = [];
res.push(
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: `${Category.Difficulty}.Easy`,
name: "Easy",
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: `${Category.Difficulty}.Medium`,
name: "Medium",
}), false),
new LeetCodeNode(Object.assign({}, defaultProblem, {
id: `${Category.Difficulty}.Hard`,
name: "Hard",
}), false),
);
this.sortSubCategoryNodes(res, Category.Difficulty);
return res;
}
public getAllCompanyNodes(): LeetCodeNode[] {
const res: LeetCodeNode[] = [];
for (const company of this.companySet.values()) {
res.push(new LeetCodeNode(Object.assign({}, defaultProblem, {
id: `${Category.Company}.${company}`,
name: _.startCase(company),
}), false));
}
this.sortSubCategoryNodes(res, Category.Company);
return res;
}
public getAllCustomNodes(): LeetCodeNode[] {
const leetCodeConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode");
const customlist: CustomListConfig[] = leetCodeConfig.get<CustomListConfig[]>("customlist") ?? [];
const res: LeetCodeNode[] = [];
for (const customConfig of customlist) {
res.push(new LeetCodeNode(Object.assign({}, defaultProblem, {
id: `${Category.Custom}.${customConfig.name}`,
name: `${customConfig.name}`,
}), false));
}
this.sortSubCategoryNodes(res, Category.Custom);
return res;
}
public getAllTagNodes(): LeetCodeNode[] {
const res: LeetCodeNode[] = [];
for (const tag of this.tagSet.values()) {
res.push(new LeetCodeNode(Object.assign({}, defaultProblem, {
id: `${Category.Tag}.${tag}`,
name: _.startCase(tag),
}), false));
}
this.sortSubCategoryNodes(res, Category.Tag);
return res;
}
public getNodeById(id: string): LeetCodeNode | undefined {
return this.explorerNodeMap.get(id);
}
public getFavoriteNodes(): LeetCodeNode[] {
const res: LeetCodeNode[] = [];
for (const node of this.explorerNodeMap.values()) {
if (node.isFavorite) {
res.push(node);
}
}
return this.applySortingStrategy(res);
}
public getChildrenNodesById(id: string): LeetCodeNode[] {
// The sub-category node's id is named as {Category.SubName}
const metaInfo: string[] = id.split(".");
const res: LeetCodeNode[] = [];
const leetCodeConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode");
const customlist: CustomListConfig[] = leetCodeConfig.get<CustomListConfig[]>("customlist") ?? [];
const customProblems = customlist.find((item) => item.name == metaInfo[1])?.problems ?? []
for (const node of this.explorerNodeMap.values()) {
switch (metaInfo[0]) {
case Category.Company:
if (node.companies.indexOf(metaInfo[1]) >= 0) {
res.push(node);
}
break;
case Category.Difficulty:
if (node.difficulty === metaInfo[1]) {
res.push(node);
}
break;
case Category.Tag:
if (node.tags.indexOf(metaInfo[1]) >= 0) {
res.push(node);
}
break;
case Category.Custom:
if (customProblems.indexOf(_.kebabCase(node.name)) >= 0) {
res.push(node);
}
break;
default:
break;
}
}
return this.applySortingStrategy(res);
}
public dispose(): void {
this.explorerNodeMap.clear();
this.companySet.clear();
this.tagSet.clear();
}
private sortSubCategoryNodes(subCategoryNodes: LeetCodeNode[], category: Category): void {
switch (category) {
case Category.Difficulty:
subCategoryNodes.sort((a: LeetCodeNode, b: LeetCodeNode): number => {
function getValue(input: LeetCodeNode): number {
switch (input.name.toLowerCase()) {
case "easy":
return 1;
case "medium":
return 2;
case "hard":
return 3;
default:
return Number.MAX_SAFE_INTEGER;
}
}
return getValue(a) - getValue(b);
});
break;
case Category.Tag:
case Category.Company:
subCategoryNodes.sort((a: LeetCodeNode, b: LeetCodeNode): number => {
if (a.name === "Unknown") {
return 1;
} else if (b.name === "Unknown") {
return -1;
} else {
return Number(a.name > b.name) - Number(a.name < b.name);
}
});
break;
default:
break;
}
}
private applySortingStrategy(nodes: LeetCodeNode[]): LeetCodeNode[] {
const strategy: SortingStrategy = getSortingStrategy();
switch (strategy) {
case SortingStrategy.AcceptanceRateAsc: return nodes.sort((x: LeetCodeNode, y: LeetCodeNode) => Number(x.acceptanceRate) - Number(y.acceptanceRate));
case SortingStrategy.AcceptanceRateDesc: return nodes.sort((x: LeetCodeNode, y: LeetCodeNode) => Number(y.acceptanceRate) - Number(x.acceptanceRate));
default: return nodes;
}
}
}
export const explorerNodeManager: ExplorerNodeManager = new ExplorerNodeManager();