Skip to content

Commit 9b7eb3a

Browse files
committed
refine the extension hint
1 parent 9effae3 commit 9b7eb3a

File tree

7 files changed

+85
-14
lines changed

7 files changed

+85
-14
lines changed

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"onCommand:leetcode.signin",
1515
"onCommand:leetcode.signout",
1616
"onCommand:leetcode.selectSessions",
17+
"onCommand:leetcode.createSession",
1718
"onCommand:leetcode.refreshExplorer",
1819
"onCommand:leetcode.showProblem",
1920
"onCommand:leetcode.searchProblem",
@@ -38,6 +39,11 @@
3839
"title": "Select session",
3940
"category": "LeetCode"
4041
},
42+
{
43+
"command": "leetcode.createSession",
44+
"title": "Create new session",
45+
"category": "LeetCode"
46+
},
4147
{
4248
"command": "leetcode.refreshExplorer",
4349
"title": "Refresh",
@@ -54,7 +60,7 @@
5460
},
5561
{
5662
"command": "leetcode.searchProblem",
57-
"title": "Search",
63+
"title": "Search Problem",
5864
"category": "LeetCode",
5965
"icon": "resources/search.png"
6066
},
@@ -112,6 +118,7 @@
112118
},
113119
"dependencies": {
114120
"fs-extra": "^5.0.0",
115-
"leetcode-cli": "2.5.1"
121+
"leetcode-cli": "2.5.1",
122+
"opn": "^5.2.0"
116123
}
117124
}

src/commands/session.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import * as vscode from "vscode";
44
import { leetCodeManager } from "../leetCodeManager";
55
import { IQuickItemEx, leetCodeBinaryPath } from "../shared";
66
import * as cp from "../utils/cpUtils";
7-
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
7+
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
88

99
export async function getSessionList(): Promise<ISession[]> {
1010
const signInStatus = leetCodeManager.getUser();
1111
if (!signInStatus) {
12+
promptForSignIn();
1213
return [];
1314
}
1415
const result: string = await cp.executeCommand("node", [leetCodeBinaryPath, "session"]);
@@ -32,7 +33,11 @@ export async function getSessionList(): Promise<ISession[]> {
3233

3334
export async function selectSession(): Promise<void> {
3435
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks(getSessionList()));
35-
if (!choice || choice.description) {
36+
if (!choice || choice.description === "Active") {
37+
return;
38+
}
39+
if (choice.value === ":createNewSession") {
40+
await vscode.commands.executeCommand("leetcode.createSession");
3641
return;
3742
}
3843
try {
@@ -51,10 +56,30 @@ async function parseSessionsToPicks(p: Promise<ISession[]>): Promise<Array<IQuic
5156
detail: `AC Questions: ${s.acQuestions}, AC Submits: ${s.acSubmits}`,
5257
value: s.id,
5358
}));
59+
picks.push({
60+
label: "$(plus) Create a new session",
61+
description: "",
62+
value: ":createNewSession",
63+
});
5464
resolve(picks);
5565
});
5666
}
5767

68+
export async function createSession(): Promise<void> {
69+
const session: string | undefined = await vscode.window.showInputBox({
70+
prompt: "Enter the new session name.",
71+
validateInput: (s: string) => s.trim() ? undefined : "Session name must not be empty",
72+
});
73+
if (!session) {
74+
return;
75+
}
76+
try {
77+
await cp.executeCommand("node", [leetCodeBinaryPath, "session", "-c", session]);
78+
} catch (error) {
79+
await promptForOpenOutputChannel("Failed to create session. Please open the output channel for details", DialogType.error);
80+
}
81+
}
82+
5883
export interface ISession {
5984
active: boolean;
6085
id: string;

src/commands/show.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { LeetCodeNode } from "../leetCodeExplorer";
55
import { leetCodeManager } from "../leetCodeManager";
66
import { IQuickItemEx, languages, leetCodeBinaryPath } from "../shared";
77
import { executeCommand } from "../utils/cpUtils";
8-
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
8+
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
99
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
1010
import * as list from "./list";
1111

@@ -18,6 +18,7 @@ export async function showProblem(node?: LeetCodeNode): Promise<void> {
1818

1919
export async function searchProblem(): Promise<void> {
2020
if (!leetCodeManager.getUser()) {
21+
promptForSignIn();
2122
return;
2223
}
2324
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(

src/commands/submit.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import * as vscode from "vscode";
77
import { leetCodeManager } from "../leetCodeManager";
88
import { leetCodeBinaryPath } from "../shared";
99
import { executeCommand } from "../utils/cpUtils";
10-
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
10+
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
1111

1212
export async function submitSolution(): Promise<void> {
1313
if (!leetCodeManager.getUser()) {
14+
promptForSignIn();
1415
return;
1516
}
1617
const textEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;

src/extension.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ import { leetcodeChannel } from "./leetCodeChannel";
88
import { LeetCodeNode, LeetCodeTreeDataProvider } from "./leetCodeExplorer";
99
import { leetCodeManager } from "./leetCodeManager";
1010
import { leetCodeStatusBarItem } from "./leetCodeStatusBarItem";
11+
import { isNodeInstalled } from "./utils/nodeUtils";
12+
13+
export async function activate(context: vscode.ExtensionContext) {
14+
if (!await isNodeInstalled()) {
15+
return;
16+
}
1117

12-
export function activate(context: vscode.ExtensionContext) {
1318
leetCodeManager.getLoginStatus();
1419
const leetCodeTreeDataProvider: LeetCodeTreeDataProvider = new LeetCodeTreeDataProvider(context);
20+
1521
context.subscriptions.push(
1622
vscode.window.registerTreeDataProvider("leetCodeExplorer", leetCodeTreeDataProvider),
1723
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
1824
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
1925
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
26+
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession()),
2027
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
2128
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
2229
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
2330
vscode.commands.registerCommand("leetcode.submitSolution", () => submit.submitSolution()),
2431
);
32+
2533
leetCodeManager.on("statusChanged", () => {
2634
leetCodeStatusBarItem.updateStatusBar(leetCodeManager.getStatus(), leetCodeManager.getUser());
2735
leetCodeTreeDataProvider.refresh();

src/utils/nodeUtils.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
"use strict";
22

3+
import * as opn from "opn";
4+
import * as vscode from "vscode";
35
import { executeCommand } from "./cpUtils";
6+
import { DialogOptions } from "./uiUtils";
47

5-
export namespace mavenUtils {
6-
const nodeCommand: string = "node";
7-
export async function validateMavenInstalled(): Promise<void> {
8-
try {
9-
await executeCommand(nodeCommand, ["-v"]);
10-
} catch (error) {
11-
throw new Error('Failed to find "maven" on path.');
8+
export async function isNodeInstalled(): Promise<boolean> {
9+
try {
10+
await executeCommand("node", ["-v"]);
11+
return true;
12+
} catch (error) {
13+
const choice = await vscode.window.showErrorMessage(
14+
"LeetCode extension need Node.js installed in environment path",
15+
DialogOptions.open,
16+
);
17+
if (choice === DialogOptions.open) {
18+
opn("https://nodejs.org");
1219
}
20+
return false;
1321
}
1422
}

src/utils/uiUtils.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"use strict";
22

3+
import * as opn from "opn";
34
import * as vscode from "vscode";
45
import { leetcodeChannel } from "../leetCodeChannel";
56

67
export namespace DialogOptions {
78
export const open: vscode.MessageItem = { title: "Open" };
89
export const yes: vscode.MessageItem = { title: "Yes" };
910
export const no: vscode.MessageItem = { title: "No", isCloseAffordance: true };
11+
export const register: vscode.MessageItem = { title: "Register" };
1012
}
1113

1214
export async function promptForOpenOutputChannel(message: string, type: DialogType): Promise<void> {
@@ -30,6 +32,25 @@ export async function promptForOpenOutputChannel(message: string, type: DialogTy
3032
}
3133
}
3234

35+
export async function promptForSignIn(): Promise<void> {
36+
const choice = await vscode.window.showInformationMessage(
37+
"Please sign in to LeetCode.",
38+
DialogOptions.yes,
39+
DialogOptions.no,
40+
DialogOptions.register,
41+
);
42+
switch (choice) {
43+
case DialogOptions.yes:
44+
await vscode.commands.executeCommand("leetcode.signin");
45+
break;
46+
case DialogOptions.register:
47+
opn("https://leetcode.com");
48+
break;
49+
default:
50+
break;
51+
}
52+
}
53+
3354
export enum DialogType {
3455
info = "info",
3556
warning = "warning",

0 commit comments

Comments
 (0)