Skip to content

Commit 1004e1d

Browse files
authored
feat: Support delete a session (LeetCode-OpenSource#358)
1 parent 19af64f commit 1004e1d

File tree

8 files changed

+91
-37
lines changed

8 files changed

+91
-37
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
<img src="https://raw.githubusercontent.com/jdneo/vscode-leetcode/master/docs/imgs/session.png" alt="Manage Session" />
111111
</p>
112112

113-
- To manage your LeetCode sessions, just clicking the `LeetCode: ***` at the bottom of the status bar. You can **switch** between sessions or **create** a new session.
113+
- To manage your LeetCode sessions, just clicking the `LeetCode: ***` at the bottom of the status bar. You can **switch** between sessions or **create**, **delete** a session.
114114

115115

116116
## Settings

docs/README_zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
<img src="https://raw.githubusercontent.com/jdneo/vscode-leetcode/master/docs/imgs/session.png" alt="管理存档" />
111111
</p>
112112

113-
- 点击位于 VS Code 底部状态栏的 `LeetCode: ***` 管理 `LeetCode 存档`。你可以**切换**存档或者**创建**新的存档
113+
- 点击位于 VS Code 底部状态栏的 `LeetCode: ***` 管理 `LeetCode 存档`。你可以**切换**存档或者**创建****删除**存档
114114

115115

116116
## 插件配置项

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"onCommand:leetcode.toggleLeetCodeCn",
3030
"onCommand:leetcode.signin",
3131
"onCommand:leetcode.signout",
32-
"onCommand:leetcode.selectSessions",
33-
"onCommand:leetcode.createSession",
32+
"onCommand:leetcode.manageSessions",
3433
"onCommand:leetcode.refreshExplorer",
3534
"onCommand:leetcode.showProblem",
3635
"onCommand:leetcode.previewProblem",
@@ -72,13 +71,8 @@
7271
"category": "LeetCode"
7372
},
7473
{
75-
"command": "leetcode.selectSessions",
76-
"title": "Select Session",
77-
"category": "LeetCode"
78-
},
79-
{
80-
"command": "leetcode.createSession",
81-
"title": "Create New Session",
74+
"command": "leetcode.manageSessions",
75+
"title": "Manage Sessions",
8276
"category": "LeetCode"
8377
},
8478
{
@@ -394,6 +388,6 @@
394388
"markdown-it": "^8.4.2",
395389
"require-from-string": "^2.0.2",
396390
"unescape-js": "^1.1.1",
397-
"vsc-leetcode-cli": "2.6.7"
391+
"vsc-leetcode-cli": "2.6.8"
398392
}
399393
}

src/commands/session.ts

+74-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from "vscode";
55
import { leetCodeExecutor } from "../leetCodeExecutor";
66
import { leetCodeManager } from "../leetCodeManager";
77
import { IQuickItemEx } from "../shared";
8-
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
8+
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
99

1010
export async function getSessionList(): Promise<ISession[]> {
1111
const signInStatus: string | undefined = leetCodeManager.getUser();
@@ -32,48 +32,64 @@ export async function getSessionList(): Promise<ISession[]> {
3232
return sessions;
3333
}
3434

35-
export async function selectSession(): Promise<void> {
36-
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks());
35+
export async function manageSessions(): Promise<void> {
36+
const choice: IQuickItemEx<ISession | string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks(true /* includeOperation */));
3737
if (!choice || choice.description === "Active") {
3838
return;
3939
}
40-
if (choice.value === ":createNewSession") {
41-
await vscode.commands.executeCommand("leetcode.createSession");
40+
if (choice.value === ":createSession") {
41+
await createSession();
42+
return;
43+
}
44+
if (choice.value === ":deleteSession") {
45+
await deleteSession();
4246
return;
4347
}
4448
try {
45-
await leetCodeExecutor.enableSession(choice.value);
49+
await leetCodeExecutor.enableSession((choice.value as ISession).id);
4650
vscode.window.showInformationMessage(`Successfully switched to session '${choice.label}'.`);
4751
await vscode.commands.executeCommand("leetcode.refreshExplorer");
4852
} catch (error) {
4953
await promptForOpenOutputChannel("Failed to switch session. Please open the output channel for details.", DialogType.error);
5054
}
5155
}
5256

53-
async function parseSessionsToPicks(): Promise<Array<IQuickItemEx<string>>> {
54-
return new Promise(async (resolve: (res: Array<IQuickItemEx<string>>) => void): Promise<void> => {
57+
async function parseSessionsToPicks(includeOperations: boolean = false): Promise<Array<IQuickItemEx<ISession | string>>> {
58+
return new Promise(async (resolve: (res: Array<IQuickItemEx<ISession | string>>) => void): Promise<void> => {
5559
try {
5660
const sessions: ISession[] = await getSessionList();
57-
const picks: Array<IQuickItemEx<string>> = sessions.map((s: ISession) => Object.assign({}, {
61+
const picks: Array<IQuickItemEx<ISession | string>> = sessions.map((s: ISession) => Object.assign({}, {
5862
label: `${s.active ? "$(check) " : ""}${s.name}`,
5963
description: s.active ? "Active" : "",
6064
detail: `AC Questions: ${s.acQuestions}, AC Submits: ${s.acSubmits}`,
61-
value: s.id,
65+
value: s,
6266
}));
63-
picks.push({
64-
label: "$(plus) Create a new session",
65-
description: "",
66-
detail: "Click this item to create a new session",
67-
value: ":createNewSession",
68-
});
67+
68+
if (includeOperations) {
69+
picks.push(...parseSessionManagementOperations());
70+
}
6971
resolve(picks);
7072
} catch (error) {
7173
return await promptForOpenOutputChannel("Failed to list sessions. Please open the output channel for details.", DialogType.error);
7274
}
7375
});
7476
}
7577

76-
export async function createSession(): Promise<void> {
78+
function parseSessionManagementOperations(): Array<IQuickItemEx<string>> {
79+
return [{
80+
label: "$(plus) Create a session",
81+
description: "",
82+
detail: "Click this item to create a session",
83+
value: ":createSession",
84+
}, {
85+
label: "$(trashcan) Delete a session",
86+
description: "",
87+
detail: "Click this item to DELETE a session",
88+
value: ":deleteSession",
89+
}];
90+
}
91+
92+
async function createSession(): Promise<void> {
7793
const session: string | undefined = await vscode.window.showInputBox({
7894
prompt: "Enter the new session name.",
7995
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "Session name must not be empty",
@@ -89,6 +105,47 @@ export async function createSession(): Promise<void> {
89105
}
90106
}
91107

108+
async function deleteSession(): Promise<void> {
109+
const choice: IQuickItemEx<ISession | string> | undefined = await vscode.window.showQuickPick(
110+
parseSessionsToPicks(false /* includeOperation */),
111+
{ placeHolder: "Please select the session you want to delete" },
112+
);
113+
if (!choice) {
114+
return;
115+
}
116+
117+
const selectedSession: ISession = choice.value as ISession;
118+
if (selectedSession.active) {
119+
vscode.window.showInformationMessage("Cannot delete an active session.");
120+
return;
121+
}
122+
123+
const action: vscode.MessageItem | undefined = await vscode.window.showWarningMessage(
124+
`This operation cannot be reverted. Are you sure to delete the session: ${selectedSession.name}?`,
125+
DialogOptions.yes,
126+
DialogOptions.no,
127+
);
128+
if (action !== DialogOptions.yes) {
129+
return;
130+
}
131+
132+
const confirm: string | undefined = await vscode.window.showInputBox({
133+
prompt: "Enter 'yes' to confirm deleting the session",
134+
validateInput: (value: string): string => {
135+
if (value === "yes") {
136+
return "";
137+
} else {
138+
return "Enter 'yes' to confirm";
139+
}
140+
},
141+
});
142+
143+
if (confirm === "yes") {
144+
await leetCodeExecutor.deleteSession(selectedSession.id);
145+
vscode.window.showInformationMessage("The session has been successfully deleted.");
146+
}
147+
}
148+
92149
export interface ISession {
93150
active: boolean;
94151
id: string;

src/extension.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
5151
vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()),
5252
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
5353
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
54-
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
55-
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession()),
54+
vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()),
5655
vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)),
5756
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
5857
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),

src/leetCodeExecutor.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ class LeetCodeExecutor implements Disposable {
124124
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session", "-e", name]);
125125
}
126126

127-
public async createSession(name: string): Promise<string> {
128-
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session", "-c", name]);
127+
public async createSession(id: string): Promise<string> {
128+
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session", "-c", id]);
129+
}
130+
131+
public async deleteSession(id: string): Promise<string> {
132+
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session", "-d", id]);
129133
}
130134

131135
public async submitSolution(filePath: string): Promise<string> {

src/statusbar/LeetCodeStatusBarItem.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class LeetCodeStatusBarItem implements vscode.Disposable {
99

1010
constructor() {
1111
this.statusBarItem = vscode.window.createStatusBarItem();
12-
this.statusBarItem.command = "leetcode.selectSessions";
12+
this.statusBarItem.command = "leetcode.manageSessions";
1313
}
1414

1515
public updateStatusBar(status: UserStatus, user?: string): void {

0 commit comments

Comments
 (0)