8000 master · SeanTong11/vscode-leetcode@32c79bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 32c79bd

Browse files
committed
master
1 parent b4447f4 commit 32c79bd

12 files changed

+289
-344
lines changed

package-lock.json

Lines changed: 168 additions & 289 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode",
33
"displayName": "LeetCode",
44
"description": "Solve LeetCode problems in VS Code",
5-
"version": "0.17.0",
5+
"version": "0.18.0",
66
"author": "Sheng Chen",
77
"publisher": "LeetCode",
88
"license": "MIT",
@@ -671,11 +671,11 @@
671671
"@types/lodash": "^4.14.123",
672672
"@types/markdown-it": "0.0.7",
673673
"@types/mocha": "^2.2.42",
674-
"@types/node": "^7.0.43",
674+
"@types/node": "14.14.22",
675675
"@types/vscode": "1.42.0",
676676
"@types/require-from-string": "^1.2.0",
677677
"tslint": "^5.9.1",
678-
"typescript": "^2.6.1"
678+
"typescript": "4.1.3"
679679
},
680680
"dependencies": {
681681
"fs-extra": "^6.0.1",

src/commands/list.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as vscode from "vscode";
55
import { leetCodeExecutor } from "../leetCodeExecutor";
66
import { leetCodeManager } from "../leetCodeManager";
7-
import { IProblem, ProblemState, UserStatus } from "../shared";
7+
import { IProblem, ProblemCategory, ProblemState, UserStatus } from "../shared";
88
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
99

1010
export async function listProblems(): Promise<IProblem[]> {
@@ -14,26 +14,29 @@ export async function listProblems(): Promise<IProblem[]> {
1414
}
1515
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
1616
const showLocked: boolean = !!leetCodeConfig.get<boolean>("showLocked");
17-
const result: string = await leetCodeExecutor.listProblems(showLocked);
1817
const problems: IProblem[] = [];
19-
const lines: string[] = result.split("\n");
20-
const reg: RegExp = /^(.)\s(.{1,2})\s(.)\s\[\s*(\d*)\s*\]\s*(.*)\s*(Easy|Medium|Hard)\s*\((\s*\d+\.\d+ %)\)/;
21-
const { companies, tags } = await leetCodeExecutor.getCompaniesAndTags();
22-
for (const line of lines) {
23-
const match: RegExpMatchArray | null = line.match(reg);
24-
if (match && match.length === 8) {
25-
const id: string = match[4].trim();
26-
problems.push({
27-
id,
28-
isFavorite: match[1].trim().length > 0,
29-
locked: match[2].trim().length > 0,
30-
state: parseProblemState(match[3]),
31-
name: match[5].trim(),
32-
difficulty: match[6].trim(),
33-
passRate: match[7].trim(),
34-
companies: companies[id] || ["Unknown"],
35-
tags: tags[id] || ["Unknown&qu 10000 ot;],
36-
});
18+
for (let pc in ProblemCategory) {
19+
const result: string = await leetCodeExecutor.listProblems(showLocked, ProblemCategory[pc]);
20+
const lines: string[] = result.split("\n");
21+
const reg: RegExp = /^(.)\s(.{1,2})\s(.)\s\[(.*)\]\s*(.*)\s*(Easy|Medium|Hard)\s*\((\s*\d+\.\d+ %)\)/;
22+
const { companies, tags } = await leetCodeExecutor.getCompaniesAndTags();
23+
for (const line of lines) {
24+
const match: RegExpMatchArray | null = line.match(reg);
25+
if (match && match.length === 8) {
26+
const id: string = match[4].trim();
27+
problems.push({
28+
id,
29+
isFavorite: match[1].trim().length > 0,
30+
locked: match[2].trim().length > 0,
31+
state: parseProblemState(match[3]),
32+
name: match[5].trim(),
33+
difficulty: match[6].trim(),
34+
passRate: match[7].trim(),
35+
companies: companies[id] || ["Unknown"],
36+
tags: tags[id] || ["Unknown"],
37+
category: pc,
38+
});
39+
}
3740
}
3841
}
3942
return problems.reverse();

src/explorer/LeetCodeNode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class LeetCodeNode {
1515
return this.data.name;
1616
}
1717

18+
public get category(): string {
19+
return this.data.category;
20+
}
21+
1822
public get state(): ProblemState {
1923
return this.data.state;
2024
}

src/explorer/explorerNodeManager.ts

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@ class ExplorerNodeManager implements Disposable {
5757

5858
public getAllNodes(): LeetCodeNode[] {
5959
return Array.from(this.explorerNodeMap.values());
60+
// const res: LeetCodeNode[] = [];
61+
// res.push(
62+
// new LeetCodeNode(Object.assign({}, defaultProblem, {
63+
// id: ``,
64+
// name: "算法",
65+
// }), false),
66+
// new LeetCodeNode(Object.assign({}, defaultProblem, {
67+
// id: ``,
68+
// name: "数据库",
69+
// }), false),
70+
// new LeetCodeNode(Object.assign({}, defaultProblem, {
71+
// id: ``,
72+
// name: "Shell",
73+
// }), false),
74+
// new LeetCodeNode(Object.assign({}, defaultProblem, {
75+
// id: ``,
76+
// name: "多线程",
77+
// }), false),
78+
// new LeetCodeNode(Object.assign({}, defaultProblem, {
79+
// id: ``,
80+
// name: "程序员面试金典",
81+
// }), false),
82+
// new LeetCodeNode(Object.assign({}, defaultProblem, {
83+
// id: ``,
84+
// name: "剑指Offer",
85+
// }), false),
86+
// );
87+
// this.sortSubCategoryNodes(res, Category.All);
88+
// return res;
6089
}
6190

6291
public getAllDifficultyNodes(): LeetCodeNode[] {
@@ -153,22 +182,41 @@ class ExplorerNodeManager implements Disposable {
153182

154183
private sortSubCategoryNodes(subCategoryNodes: LeetCodeNode[], category: Category): void {
155184
switch (category) {
185+
case Category.All:
186+
function getCategoryValue(input: LeetCodeNode): number {
187+
switch (input.category.toLowerCase()) {
188+
case "algorithms":
189+
return 1;
190+
case "database":
191+
return 2;
192+
case "shell":
193+
return 3;
194+
case "concurrency":
195+
return 4;
196+
case "lcci":
197+
return 4;
198+
case "lcof":
199+
return 4;
200+
default:
201+
return Number.MAX_SAFE_INTEGER;
202+
}
203+
}
204+
subCategoryNodes.sort((a: LeetCodeNode, b: LeetCodeNode): number => getCategoryValue(a) - getCategoryValue(b));
205+
break;
156206
case Category.Difficulty:
157-
subCategoryNodes.sort((a: LeetCodeNode, b: LeetCodeNode): number => {
158-
function getValue(input: LeetCodeNode): number {
159-
switch (input.name.toLowerCase()) {
160-
case "easy":
161-
return 1;
162-
case "medium":
163-
return 2;
164-
case "hard":
165-
return 3;
166-
default:
167-
return Number.MAX_SAFE_INTEGER;
168-
}
207+
function getDifficultyValue(input: LeetCodeNode): number {
208+
switch (input.name.toLowerCase()) {
209+
case "easy":
210+
return 1;
211+
case "medium":
212+
return 2;
213+
case "hard":
214+
return 3;
215+
default:
216+
return Number.MAX_SAFE_INTEGER;
169217
}
170-
return getValue(a) - getValue(b);
171-
});
218+
}
219+
subCategoryNodes.sort((a: LeetCodeNode, b: LeetCodeNode): number => getDifficultyValue(a) - getDifficultyValue(b));
172220
break;
173221
case Category.Tag:
174222
case Category.Company:

src/leetCodeExecutor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ class LeetCodeExecutor implements Disposable {
8888
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user", "-L"]);
8989
}
9090

91-
public async listProblems(showLocked: boolean): Promise<string> {
91+
public async listProblems(showLocked: boolean, category: string): Promise<string> {
9292
return await this.executeCommandEx(this.nodeExecutable, showLocked ?
93-
[await this.getLeetCodeBinaryPath(), "list"] :
94-
[await this.getLeetCodeBinaryPath(), "list", "-q", "L"],
93+
[await this.getLeetCodeBinaryPath(), "list", "-t", category] :
94+
[await this.getLeetCodeBinaryPath(), "list", "-t", category, "-q", "L"],
9595
);
9696
}
9797

src/leetCodeManager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class LeetCodeManager extends EventEmitter {
8383
env: createEnvOption(),
8484
});
8585

86-
childProc.stdout.on("data", async (data: string | Buffer) => {
86+
childProc.stdout?.on("data", async (data: string | Buffer) => {
8787
data = data.toString();
8888
leetCodeChannel.append(data);
8989
if (data.includes("twoFactorCode")) {
@@ -96,19 +96,19 @@ class LeetCodeManager extends EventEmitter {
9696
childProc.kill();
9797
return resolve(undefined);
9898
}
99-
childProc.stdin.write(`${twoFactor}\n`);
99+
childProc.stdin?.write(`${twoFactor}\n`);
100100
}
101101
const successMatch: RegExpMatchArray | null = data.match(this.successRegex);
102102
if (successMatch && successMatch[1]) {
103-
childProc.stdin.end();
103+
childProc.stdin?.end();
104104
return resolve(successMatch[1]);
105105
} else if (data.match(this.failRegex)) {
106-
childProc.stdin.end();
106+
childProc.stdin?.end();
107107
return reject(new Error("Faile to login"));
108108
}
109109
});
110110

111-
childProc.stderr.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString()));
111+
childProc.stderr?.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString()));
112112

113113
childProc.on("error", reject);
114114
const name: string | undefined = await vscode.window.showInputBox({
@@ -120,7 +120,7 @@ class LeetCodeManager extends EventEmitter {
120120
childProc.kill();
121121
return resolve(undefined);
122122
}
123-
childProc.stdin.write(`${name}\n`);
123+
childProc.stdin?.write(`${name}\n`);
124124
const pwd: string | undefined = await vscode.window.showInputBox({
125125
prompt: isByCookie ? "Enter cookie" : "Enter password.",
126126
password: true,
@@ -131,7 +131,7 @@ class LeetCodeManager extends EventEmitter {
131131
childProc.kill();
132132
return resolve(undefined);
133133
}
134-
childProc.stdin.write(`${pwd}\n`);
134+
childProc.stdin?.write(`${pwd}\n`);
135135
});
136136
if (userName) {
137137
vscode.window.showInformationMessage(`Successfully ${inMessage}.`);

src/shared.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export interface IProblem {
8080
passRate: string;
8181
companies: string[];
8282
tags: string[];
83+
category: string;
8384
}
8485

8586
export const defaultProblem: IProblem = {
@@ -92,6 +93,7 @@ export const defaultProblem: IProblem = {
9293
passRate: "",
9394
companies: [] as string[],
9495
tags: [] as string[],
96+
category: "",
9597
};
9698

9799
export enum Category {
@@ -102,6 +104,15 @@ export enum Category {
102104
Favorite = "Favorite",
103105
}
104106

107+
export enum ProblemCategory {
108+
ALGORITHMS = "algorithms",
109+
DATABASE = "database",
110+
SHELL = "shell",
111+
CONCURRENCY = "concurrency",
112+
LCCI = "lcci",
113+
LCOF = "lcof",
114+
}
115+
105116
export const supportedPlugins: string[] = [
106117
"company",
107118
"solution.discuss",

src/utils/cpUtils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export async function executeCommand(command: string, args: string[], options: c
1515

1616
const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() });
1717

18-
childProc.stdout.on("data", (data: string | Buffer) => {
18+
childProc.stdout?.on("data", (data: string | Buffer) => {
1919
data = data.toString();
2020
result = result.concat(data);
2121
leetCodeChannel.append(data);
2222
});
2323

24-
childProc.stderr.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString()));
24+
childProc.stderr?.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString()));
2525

2626
childProc.on("error", reject);
2727

@@ -42,11 +42,11 @@ export async function executeCommand(command: string, args: string[], options: c
4242
export async function executeCommandWithProgress(message: string, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
4343
let result: string = "";
4444
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (p: vscode.Progress<{}>) => {
45-
return new Promise(async (resolve: () => void, reject: (e: Error) => void): Promise<void> => {
45+
return new Promise(async (resolve, reject): Promise<void> => {
4646
p.report({ message });
4747
try {
4848
result = await executeCommand(command, args, options);
49-
resolve();
49+
resolve(undefined);
5050
} catch (e) {
5151
reject(e);
5252
}

src/webview/leetCodePreviewProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
130130

131131
protected onDidDisposeWebview(): void {
132132
super.onDidDisposeWebview();
133-
delete this.node;
134-
delete this.description;
133+
// delete this.node;
134+
// delete this.description;
135135
this.sideMode = false;
136136
}
137137

src/webview/leetCodeSolutionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class LeetCodeSolutionProvider extends LeetCodeWebview {
6464

6565
protected onDidDisposeWebview(): void {
6666
super.onDidDisposeWebview();
67-
delete this.solution;
67+
// delete this.solution;
6868
}
6969

7070
private parseSolution(raw: string): Solution {

src/webview/leetCodeSubmissionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
5959

6060
protected onDidDisposeWebview(): void {
6161
super.onDidDisposeWebview();
62-
delete this.result;
62+
// delete this.result;
6363
}
6464

6565
private async showKeybindingsHint(): Promise<void> {

0 commit comments

Comments
 (0)
0