Skip to content

support leetcode cn #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 30 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"preview": true,
"activationEvents": [
"onCommand:leetcode.toogleLeetCodeCn",
"onCommand:leetcode.signin",
"onCommand:leetcode.signout",
"onCommand:leetcode.selectSessions",
Expand All @@ -39,6 +40,12 @@
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "leetcode.toogleLeetCodeCn",
"title": "Switch endpoint",
"category": "LeetCode",
"icon": "resources/cn.png"
},
{
"command": "leetcode.signin",
"title": "Sign in",
Expand Down Expand Up @@ -114,19 +121,24 @@
"menus": {
"view/title": [
{
"command": "leetcode.signin",
"command": "leetcode.toogleLeetCodeCn",
"when": "view == leetCodeExplorer",
"group": "navigation@0"
},
{
"command": "leetcode.searchProblem",
"command": "leetcode.signin",
"when": "view == leetCodeExplorer",
"group": "navigation@1"
},
{
"command": "leetcode.refreshExplorer",
"command": "leetcode.searchProblem",
"when": "view == leetCodeExplorer",
"group": "navigation@2"
},
{
"command": "leetcode.refreshExplorer",
"when": "view == leetCodeExplorer",
"group": "navigation@3"
}
],
"view/item/context": [
Expand Down Expand Up @@ -172,7 +184,7 @@
"leetcode.showLocked": {
"type": "boolean",
"default": false,
"scope": "window",
"scope": "application",
"description": "Show locked problems."
},
"leetcode.defaultLanguage": {
Expand All @@ -193,20 +205,30 @@
"scala",
"swift"
],
"scope": "window",
"scope": "application",
"description": "Default language for solving the problems."
},
"leetcode.showSetDefaultLanguageHint": {
"type": "boolean",
"default": true,
"scope": "window",
"scope": "application",
"description": "Show a hint to set the default language."
},
"leetcode.useWsl": {
"type": "boolean",
"default": false,
"scope": "window",
"scope": "application",
"description": "Use Node.js inside the Windows Subsystem for Linux."
},
"leetcode.endpoint": {
"type": "string",
"default": "leetcode",
"scope": "application",
"enum": [
"leetcode",
"leetcode-cn"
],
"description": "Endpoint of the user account."
}
}
}
Expand All @@ -216,7 +238,7 @@
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"postinstall": "node ./node_modules/vscode/bin/install && node ./node_modules/leetcode-cli/bin/leetcode plugin -i leetcode.cn",
"test": "npm run compile && node ./node_modules/vscode/bin/test",
"lint": "tslint --project tsconfig.json -e src/*.d.ts -t verbose"
},
Expand Down
Binary file added resources/cn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions src/commands/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";

import * as vscode from "vscode";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { IQuickItemEx } from "../shared";
import { Endpoint } from "../shared";
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";

export async function toogleLeetCodeCn(): Promise<void> {
const isCnEnbaled: boolean = isLeetCodeCnEnabled();
const picks: Array<IQuickItemEx<string>> = [];
picks.push(
{
label: `${isCnEnbaled ? "$(check) " : ""}On`,
description: "",
detail: `Enable ${Endpoint.LeetCodeCN}.`,
value: "on",
},
{
label: `${isCnEnbaled ? "" : "$(check) "}Off`,
description: "",
detail: `Disable ${Endpoint.LeetCodeCN}.`,
value: "off",
},
);
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
if (!choice) {
return;
}
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);
await leetCodeConfig.update("endpoint", endpoint, true /* UserSetting */);
vscode.window.showInformationMessage(`Switched the endpoint to ${endpoint}`);
} catch (error) {
await promptForOpenOutputChannel("Failed to switch endpoint. Please open the output channel for details.", DialogType.error);
}

try {
await vscode.commands.executeCommand("leetcode.signout");
await promptForSignIn();
} catch (error) {
await promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details.", DialogType.error);
}
}

export async function initializeEndpoint(): Promise<void> {
await leetCodeExecutor.toggleLeetCodeCn(isLeetCodeCnEnabled());
}

export function isLeetCodeCnEnabled(): boolean {
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;
}
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import * as vscode from "vscode";
import * as plugin from "./commands/plugin";
import * as session from "./commands/session";
import * as show from "./commands/show";
import * as submit from "./commands/submit";
Expand All @@ -20,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

context.subscriptions.push(
vscode.window.registerTreeDataProvider("leetCodeExplorer", leetCodeTreeDataProvider),
vscode.commands.registerCommand("leetcode.toogleLeetCodeCn", () => plugin.toogleLeetCodeCn()),
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
Expand All @@ -31,6 +33,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
);

await plugin.initializeEndpoint();

leetCodeManager.on("statusChanged", () => {
leetCodeStatusBarItem.updateStatusBar(leetCodeManager.getStatus(), leetCodeManager.getUser());
leetCodeTreeDataProvider.refresh();
Expand Down
10 changes: 10 additions & 0 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export interface ILeetCodeExecutor {
/* section for solution command */
submitSolution(filePath: string): Promise<string>;
testSolution(filePath: string, testString?: string): Promise<string>;

/* section for plugin command */
toggleLeetCodeCn(isEnable: boolean): Promise<string>;
}

class LeetCodeExecutor implements ILeetCodeExecutor {
Expand Down Expand Up @@ -109,6 +112,13 @@ class LeetCodeExecutor implements ILeetCodeExecutor {
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"]);
}
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> {
if (wsl.useWsl()) {
return await executeCommand("wsl", [command].concat(args), options);
Expand Down
2 changes: 1 addition & 1 deletion src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
this.userStatus = UserStatus.SignedOut;
this.emit("statusChanged");
} catch (error) {
promptForOpenOutputChannel("Failed to sign out. Please open the output channel for details", DialogType.error);
// swallow the error when sign out.
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ export enum ProblemState {
NotAC = 2,
Unknown = 3,
}

export enum Endpoint {
LeetCode = "leetcode",
LeetCodeCN = "leetcode-cn",
}
7 changes: 6 additions & 1 deletion src/utils/uiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as opn from "opn";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { isLeetCodeCnEnabled } from "../commands/plugin";
import { leetCodeChannel } from "../leetCodeChannel";

export namespace DialogOptions {
Expand Down Expand Up @@ -48,7 +49,11 @@ export async function promptForSignIn(): Promise<void> {
await vscode.commands.executeCommand("leetcode.signin");
break;
case DialogOptions.singUp:
opn("https://leetcode.com");
if (isLeetCodeCnEnabled()) {
opn("https://leetcode-cn.com");
} else {
opn("https://leetcode.com");
}
break;
default:
break;
Expand Down