Skip to content

Commit b5d44ce

Browse files
authored
Improve the experience of switch endpoint (#92)
1 parent 424ba0c commit b5d44ce

17 files changed

+93
-61
lines changed

package.json

+17-11
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,40 @@
4343
"commands": [
4444
{
4545
"command": "leetcode.deleteCache",
46-
"title": "Delete cache",
46+
"title": "Delete Cache",
4747
"category": "LeetCode"
4848
},
4949
{
5050
"command": "leetcode.toogleLeetCodeCn",
51-
"title": "Switch endpoint",
51+
"title": "Switch Endpoint",
5252
"category": "LeetCode",
53-
"icon": "resources/cn.png"
53+
"icon": {
54+
"light": "resources/light/endpoint.svg",
55+
"dark": "resources/dark/endpoint.svg"
56+
}
5457
},
5558
{
5659
"command": "leetcode.signin",
57-
"title": "Sign in",
60+
"title": "Sign In",
5861
"category": "LeetCode",
5962
"icon": {
60-
"light": "resources/light/signin.png",
61-
"dark": "resources/dark/signin.png"
63+
"light": "resources/light/signin.svg",
64+
"dark": "resources/dark/signin.svg"
6265
}
6366
},
6467
{
6568
"command": "leetcode.signout",
66-
"title": "Sign out",
69+
"title": "Sign Out",
6770
"category": "LeetCode"
6871
},
6972
{
7073
"command": "leetcode.selectSessions",
71-
"title": "Select session",
74+
"title": "Select Session",
7275
"category": "LeetCode"
7376
},
7477
{
7578
"command": "leetcode.createSession",
76-
"title": "Create new session",
79+
"title": "Create New Session",
7780
"category": "LeetCode"
7881
},
7982
{
@@ -87,14 +90,17 @@
8790
},
8891
{
8992
"command": "leetcode.showProblem",
90-
"title": "Show problem",
93+
"title": "Show Problem",
9194
"category": "LeetCode"
9295
},
9396
{
9497
"command": "leetcode.searchProblem",
9598
"title": "Search Problem",
9699
"category": "LeetCode",
97-
"icon": "resources/search.png"
100+
"icon": {
101+
"light": "resources/light/search.svg",
102+
"dark": "resources/dark/search.svg"
103+
}
98104
},
99105
{
100106
"command": "leetcode.testSolution",

resources/cn.png

-4.8 KB
Binary file not shown.

resources/dark/endpoint.svg

+6
Loading

resources/dark/refresh.svg

+6-1
Loading

resources/dark/search.svg

+6
Loading

resources/dark/signin.png

-395 Bytes
Binary file not shown.

resources/dark/signin.svg

+7
Loading

resources/light/endpoint.svg

+6
Loading

resources/light/refresh.svg

+6-1
Loading

resources/light/search.svg

+6
Loading

resources/light/signin.png

-427 Bytes
Binary file not shown.

resources/light/signin.svg

+7
Loading

resources/search.png

-541 Bytes
Binary file not shown.

src/commands/plugin.ts

+14-40
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
// Copyright (c) jdneo. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import * as fse from "fs-extra";
5-
import * as os from "os";
6-
import * as path from "path";
74
import * as vscode from "vscode";
85
import { leetCodeExecutor } from "../leetCodeExecutor";
96
import { IQuickItemEx } from "../shared";
107
import { Endpoint } from "../shared";
118
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
129
import { deleteCache } from "./cache";
1310

14-
export async function toogleLeetCodeCn(): Promise<void> {
15-
const isCnEnbaled: boolean = isLeetCodeCnEnabled();
11+
export async function switchEndpoint(): Promise<void> {
12+
const isCnEnbaled: boolean = getLeetCodeEndpoint() === Endpoint.LeetCodeCN;
1613
const picks: Array<IQuickItemEx<string>> = [];
1714
picks.push(
1815
{
19-
label: `${isCnEnbaled ? "$(check) " : ""}On`,
20-
description: "",
21-
detail: `Enable ${Endpoint.LeetCodeCN}.`,
22-
value: "on",
16+
label: `${isCnEnbaled ? "" : "$(check) "}LeetCode`,
17+
description: "leetcode.com",
18+
detail: `Enable LeetCode US`,
19+
value: Endpoint.LeetCode,
2320
},
2421
{
25-
label: `${isCnEnbaled ? "" : "$(check) "}Off`,
26-
description: "",
27-
detail: `Disable ${Endpoint.LeetCodeCN}.`,
28-
value: "off",
22+
label: `${isCnEnbaled ? "$(check) " : ""}力扣`,
23+
description: "leetcode-cn.com",
24+
detail: `启用中国版 LeetCode`,
25+
value: Endpoint.LeetCodeCN,
2926
},
3027
);
3128
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
@@ -34,9 +31,8 @@ export async function toogleLeetCodeCn(): Promise<void> {
3431
}
3532
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
3633
try {
37-
const enabled: boolean = choice.value === "on";
38-
const endpoint: string = enabled ? Endpoint.LeetCodeCN : Endpoint.LeetCode;
39-
await leetCodeExecutor.toggleLeetCodeCn(enabled);
34+
const endpoint: string = choice.value;
35+
await leetCodeExecutor.switchEndpoint(endpoint);
4036
await leetCodeConfig.update("endpoint", endpoint, true /* UserSetting */);
4137
vscode.window.showInformationMessage(`Switched the endpoint to ${endpoint}`);
4238
} catch (error) {
@@ -52,29 +48,7 @@ export async function toogleLeetCodeCn(): Promise<void> {
5248
}
5349
}
5450

55-
export async function initializeEndpoint(): Promise<void> {
56-
const isCnEnabledInExtension: boolean = isLeetCodeCnEnabled();
57-
const isCnEnabledInCli: boolean = await isLeetCodeCnEnabledInCli();
58-
await leetCodeExecutor.toggleLeetCodeCn(isCnEnabledInExtension);
59-
if (isCnEnabledInCli !== isCnEnabledInExtension) {
60-
await deleteCache();
61-
}
62-
}
63-
64-
export function isLeetCodeCnEnabled(): boolean {
51+
export function getLeetCodeEndpoint(): string {
6552
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
66-
const endpoint: string | undefined = leetCodeConfig.get<string>("endpoint");
67-
if (endpoint && endpoint === Endpoint.LeetCodeCN) {
68-
return true;
69-
}
70-
return false;
71-
}
72-
73-
async function isLeetCodeCnEnabledInCli(): Promise<boolean> {
74-
const pluginsStatusFile: string = path.join(os.homedir(), ".lc", "plugins.json");
75-
if (!await fse.pathExists(pluginsStatusFile)) {
76-
return false;
77-
}
78-
const pluginsObj: {} = await fse.readJson(pluginsStatusFile);
79-
return pluginsObj["leetcode.cn"];
53+
return leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
8054
}

src/extension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
3636
vscode.window.registerTreeDataProvider("leetCodeExplorer", leetCodeTreeDataProvider),
3737
vscode.languages.registerCodeLensProvider({ scheme: "file" }, codeLensProvider),
3838
vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()),
39-
vscode.commands.registerCommand("leetcode.toogleLeetCodeCn", () => plugin.toogleLeetCodeCn()),
39+
vscode.commands.registerCommand("leetcode.toogleLeetCodeCn", () => plugin.switchEndpoint()),
4040
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
4141
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
4242
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
@@ -48,7 +48,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4848
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
4949
);
5050

51-
await plugin.initializeEndpoint();
51+
await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());
5252
leetCodeManager.getLoginStatus();
5353
}
5454

src/leetCodeExecutor.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as cp from "child_process";
55
import * as path from "path";
66
import * as vscode from "vscode";
7+
import { Endpoint } from "./shared";
78
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils";
89
import { DialogOptions, openUrl } from "./utils/uiUtils";
910
import * as wsl from "./utils/wslUtils";
@@ -89,11 +90,14 @@ class LeetCodeExecutor {
8990
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]);
9091
}
9192

92-
public async toggleLeetCodeCn(isEnable: boolean): Promise<string> {
93-
if (isEnable) {
94-
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-e", "leetcode.cn"]);
93+
public async switchEndpoint(endpoint: string): Promise<string> {
94+
switch (endpoint) {
95+
case Endpoint.LeetCodeCN:
96+
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-e", "leetcode.cn"]);
97+
case Endpoint.LeetCode:
98+
default:
99+
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-d", "leetcode.cn"]);
95100
}
96-
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-d", "leetcode.cn"]);
97101
}
98102

99103
private async executeCommandEx(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {

0 commit comments

Comments
 (0)