Skip to content

Commit

Permalink
Improve the experience of switch endpoint (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Jan 21, 2019
1 parent 424ba0c commit b5d44ce
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 61 deletions.
28 changes: 17 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,40 @@
"commands": [
{
"command": "leetcode.deleteCache",
"title": "Delete cache",
"title": "Delete Cache",
"category": "LeetCode"
},
{
"command": "leetcode.toogleLeetCodeCn",
"title": "Switch endpoint",
"title": "Switch Endpoint",
"category": "LeetCode",
"icon": "resources/cn.png"
"icon": {
"light": "resources/light/endpoint.svg",
"dark": "resources/dark/endpoint.svg"
}
},
{
"command": "leetcode.signin",
"title": "Sign in",
"title": "Sign In",
"category": "LeetCode",
"icon": {
"light": "resources/light/signin.png",
"dark": "resources/dark/signin.png"
"light": "resources/light/signin.svg",
"dark": "resources/dark/signin.svg"
}
},
{
"command": "leetcode.signout",
"title": "Sign out",
"title": "Sign Out",
"category": "LeetCode"
},
{
"command": "leetcode.selectSessions",
"title": "Select session",
"title": "Select Session",
"category": "LeetCode"
},
{
"command": "leetcode.createSession",
"title": "Create new session",
"title": "Create New Session",
"category": "LeetCode"
},
{
Expand All @@ -87,14 +90,17 @@
},
{
"command": "leetcode.showProblem",
"title": "Show problem",
"title": "Show Problem",
"category": "LeetCode"
},
{
"command": "leetcode.searchProblem",
"title": "Search Problem",
"category": "LeetCode",
"icon": "resources/search.png"
"icon": {
"light": "resources/light/search.svg",
"dark": "resources/dark/search.svg"
}
},
{
"command": "leetcode.testSolution",
Expand Down
Binary file removed resources/cn.png
Binary file not shown.
6 changes: 6 additions & 0 deletions resources/dark/endpoint.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion resources/dark/refresh.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions resources/dark/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/dark/signin.png
Binary file not shown.
7 changes: 7 additions & 0 deletions resources/dark/signin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions resources/light/endpoint.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion resources/light/refresh.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions resources/light/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/light/signin.png
Binary file not shown.
7 changes: 7 additions & 0 deletions resources/light/signin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/search.png
Binary file not shown.
54 changes: 14 additions & 40 deletions src/commands/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import * as fse from "fs-extra";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { IQuickItemEx } from "../shared";
import { Endpoint } from "../shared";
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
import { deleteCache } from "./cache";

export async function toogleLeetCodeCn(): Promise<void> {
const isCnEnbaled: boolean = isLeetCodeCnEnabled();
export async function switchEndpoint(): Promise<void> {
const isCnEnbaled: boolean = getLeetCodeEndpoint() === Endpoint.LeetCodeCN;
const picks: Array<IQuickItemEx<string>> = [];
picks.push(
{
label: `${isCnEnbaled ? "$(check) " : ""}On`,
description: "",
detail: `Enable ${Endpoint.LeetCodeCN}.`,
value: "on",
label: `${isCnEnbaled ? "" : "$(check) "}LeetCode`,
description: "leetcode.com",
detail: `Enable LeetCode US`,
value: Endpoint.LeetCode,
},
{
label: `${isCnEnbaled ? "" : "$(check) "}Off`,
description: "",
detail: `Disable ${Endpoint.LeetCodeCN}.`,
value: "off",
label: `${isCnEnbaled ? "$(check) " : ""}力扣`,
description: "leetcode-cn.com",
detail: `启用中国版 LeetCode`,
value: Endpoint.LeetCodeCN,
},
);
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
Expand All @@ -34,9 +31,8 @@ export async function toogleLeetCodeCn(): Promise<void> {
}
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
try {
const enabled: boolean = choice.value === "on";
const endpoint: string = enabled ? Endpoint.LeetCodeCN : Endpoint.LeetCode;
await leetCodeExecutor.toggleLeetCodeCn(enabled);
const endpoint: string = choice.value;
await leetCodeExecutor.switchEndpoint(endpoint);
await leetCodeConfig.update("endpoint", endpoint, true /* UserSetting */);
vscode.window.showInformationMessage(`Switched the endpoint to ${endpoint}`);
} catch (error) {
Expand All @@ -52,29 +48,7 @@ export async function toogleLeetCodeCn(): Promise<void> {
}
}

export async function initializeEndpoint(): Promise<void> {
const isCnEnabledInExtension: boolean = isLeetCodeCnEnabled();
const isCnEnabledInCli: boolean = await isLeetCodeCnEnabledInCli();
await leetCodeExecutor.toggleLeetCodeCn(isCnEnabledInExtension);
if (isCnEnabledInCli !== isCnEnabledInExtension) {
await deleteCache();
}
}

export function isLeetCodeCnEnabled(): boolean {
export function getLeetCodeEndpoint(): string {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
const endpoint: string | undefined = leetCodeConfig.get<string>("endpoint");
if (endpoint && endpoint === Endpoint.LeetCodeCN) {
return true;
}
return false;
}

async function isLeetCodeCnEnabledInCli(): Promise<boolean> {
const pluginsStatusFile: string = path.join(os.homedir(), ".lc", "plugins.json");
if (!await fse.pathExists(pluginsStatusFile)) {
return false;
}
const pluginsObj: {} = await fse.readJson(pluginsStatusFile);
return pluginsObj["leetcode.cn"];
return leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
}
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.window.registerTreeDataProvider("leetCodeExplorer", leetCodeTreeDataProvider),
vscode.languages.registerCodeLensProvider({ scheme: "file" }, codeLensProvider),
vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()),
vscode.commands.registerCommand("leetcode.toogleLeetCodeCn", () => plugin.toogleLeetCodeCn()),
vscode.commands.registerCommand("leetcode.toogleLeetCodeCn", () => plugin.switchEndpoint()),
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
Expand All @@ -48,7 +48,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
);

await plugin.initializeEndpoint();
await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());
leetCodeManager.getLoginStatus();
}

Expand Down
12 changes: 8 additions & 4 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as cp from "child_process";
import * as path from "path";
import * as vscode from "vscode";
import { Endpoint } from "./shared";
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils";
import { DialogOptions, openUrl } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";
Expand Down Expand Up @@ -89,11 +90,14 @@ class LeetCodeExecutor {
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]);
}

public async toggleLeetCodeCn(isEnable: boolean): Promise<string> {
if (isEnable) {
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-e", "leetcode.cn"]);
public async switchEndpoint(endpoint: string): Promise<string> {
switch (endpoint) {
case Endpoint.LeetCodeCN:
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-e", "leetcode.cn"]);
case Endpoint.LeetCode:
default:
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-d", "leetcode.cn"]);
}
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-d", "leetcode.cn"]);
}

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

0 comments on commit b5d44ce

Please sign in to comment.