Skip to content

Commit

Permalink
change the usage of outputchannel
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Feb 15, 2018
1 parent b7e3f22 commit dfbdd18
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 97 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Solve LeetCode problems in VS Code.

## Known Issues:
- This extension will infer the current target problem according to the active editing file. Please do not change the file name.
- Currently, only unlocked problems will be listed.

## Release Notes

Expand Down Expand Up @@ -72,6 +73,7 @@ This extension is based on [@skygragon](https://github.com/skygragon)'s [leetcod

## 已知问题
- 本插件会根据文件名称推测当前的目标题目,因此建议不要改变文件名。
- 本插件目前仅会显示左右已解锁的问题。

## 更新日志

Expand Down
7 changes: 4 additions & 3 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

import * as vscode from "vscode";
import { leetCodeManager } from "../leetCodeManager";
import { leetCodeBinaryPath } from "../shared";
import { UserStatus } from "../shared";
Expand All @@ -14,12 +15,12 @@ export interface IProblem {
passRate: string;
}

export async function listProblems(): Promise<IProblem[]> {
export async function listProblems(channel: vscode.OutputChannel): Promise<IProblem[]> {
try {
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
return [];
}
const result: string = await executeCommand("node", [leetCodeBinaryPath, "list", "-q", "L"]);
const result: string = await executeCommand(channel, "node", [leetCodeBinaryPath, "list", "-q", "L"]);
const problems: IProblem[] = [];
const lines: string[] = result.split("\n");
const reg: RegExp = /(.?)\s*\[\s*(\d*)\]\s*(.*)\s*(Easy|Medium|Hard)\s*\((\s*\d+\.\d+ %)\)/;
Expand All @@ -37,7 +38,7 @@ export async function listProblems(): Promise<IProblem[]> {
}
return problems.reverse();
} catch (error) {
await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details", DialogType.error);
await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details", DialogType.error, channel);
return [];
}

Expand Down
18 changes: 9 additions & 9 deletions src/commands/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { IQuickItemEx, leetCodeBinaryPath } from "../shared";
import * as cp from "../utils/cpUtils";
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";

export async function getSessionList(): Promise<ISession[]> {
export async function getSessionList(channel: vscode.OutputChannel): Promise<ISession[]> {
const signInStatus = leetCodeManager.getUser();
if (!signInStatus) {
promptForSignIn();
return [];
}
const result: string = await cp.executeCommand("node", [leetCodeBinaryPath, "session"]);
const result: string = await cp.executeCommand(channel, "node", [leetCodeBinaryPath, "session"]);
const lines: string[] = result.split("\n");
const sessions: ISession[] = [];
const reg: RegExp = /(.?)\s*(\d+)\s+(.*)\s+(\d+ \(\s*\d+\.\d+ %\))\s+(\d+ \(\s*\d+\.\d+ %\))/;
Expand All @@ -31,8 +31,8 @@ export async function getSessionList(): Promise<ISession[]> {
return sessions;
}

export async function selectSession(): Promise<void> {
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks(getSessionList()));
export async function selectSession(channel: vscode.OutputChannel): Promise<void> {
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks(getSessionList(channel)));
if (!choice || choice.description === "Active") {
return;
}
Expand All @@ -41,11 +41,11 @@ export async function selectSession(): Promise<void> {
return;
}
try {
await cp.executeCommand("node", [leetCodeBinaryPath, "session", "-e", choice.value]);
await cp.executeCommand(channel, "node", [leetCodeBinaryPath, "session", "-e", choice.value]);
vscode.window.showInformationMessage(`Successfully switched to session '${choice.label}'.`);
await vscode.commands.executeCommand("leetcode.refreshExplorer");
} catch (error) {
await promptForOpenOutputChannel("Failed to switch session. Please open the output channel for details", DialogType.error);
await promptForOpenOutputChannel("Failed to switch session. Please open the output channel for details", DialogType.error, channel);
}
}

Expand All @@ -67,7 +67,7 @@ async function parseSessionsToPicks(p: Promise<ISession[]>): Promise<Array<IQuic
});
}

export async function createSession(): Promise<void> {
export async function createSession(channel: vscode.OutputChannel): Promise<void> {
const session: string | undefined = await vscode.window.showInputBox({
prompt: "Enter the new session name.",
validateInput: (s: string) => s.trim() ? undefined : "Session name must not be empty",
Expand All @@ -76,10 +76,10 @@ export async function createSession(): Promise<void> {
return;
}
try {
await cp.executeCommand("node", [leetCodeBinaryPath, "session", "-c", session]);
await cp.executeCommand(channel, "node", [leetCodeBinaryPath, "session", "-c", session]);
vscode.window.showInformationMessage("New session created, you can switch to it by clicking the status bar.");
} catch (error) {
await promptForOpenOutputChannel("Failed to create session. Please open the output channel for details", DialogType.error);
await promptForOpenOutputChannel("Failed to create session. Please open the output channel for details", DialogType.error, channel);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../util
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
import * as list from "./list";

export async function showProblem(node?: LeetCodeNode): Promise<void> {
export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise<void> {
if (!node) {
return;
}
await showProblemInternal(node.id);
await showProblemInternal(channel, node.id);
}

export async function searchProblem(): Promise<void> {
export async function searchProblem(channel: vscode.OutputChannel): Promise<void> {
if (!leetCodeManager.getUser()) {
promptForSignIn();
return;
}
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(
parseProblemsToPicks(list.listProblems()),
parseProblemsToPicks(list.listProblems(channel)),
{
matchOnDetail: true,
placeHolder: "Select one problem",
Expand All @@ -32,18 +32,18 @@ export async function searchProblem(): Promise<void> {
if (!choice) {
return;
}
await showProblemInternal(choice.value);
await showProblemInternal(channel, choice.value);
}

async function showProblemInternal(id: string): Promise<void> {
async function showProblemInternal(channel: vscode.OutputChannel, id: string): Promise<void> {
try {
const language: string | undefined = await vscode.window.showQuickPick(languages, { placeHolder: "Select the language you want to use" });
if (!language) {
return;
}
const outdir: string = await selectWorkspaceFolder();
await fse.ensureDir(outdir);
const result: string = await executeCommand("node", [leetCodeBinaryPath, "show", id, "-gx", "-l", language, "-o", outdir]);
const result: string = await executeCommand(channel, "node", [leetCodeBinaryPath, "show", id, "-gx", "-l", language, "-o", outdir]);
const reg: RegExp = /\* Source Code:\s*(.*)/;
const match: RegExpMatchArray | null = result.match(reg);
if (match && match.length >= 2) {
Expand All @@ -52,7 +52,7 @@ async function showProblemInternal(id: string): Promise<void> {
throw new Error("Failed to fetch the problem information");
}
} catch (error) {
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details", DialogType.error);
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details", DialogType.error, channel);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { leetCodeBinaryPath } from "../shared";
import { executeCommand } from "../utils/cpUtils";
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";

export async function submitSolution(): Promise<void> {
export async function submitSolution(channel: vscode.OutputChannel): Promise<void> {
if (!leetCodeManager.getUser()) {
promptForSignIn();
return;
Expand All @@ -24,12 +24,12 @@ export async function submitSolution(): Promise<void> {
}
const filePath: string = textEditor.document.uri.fsPath;
try {
const result: string = await executeCommand("node", [leetCodeBinaryPath, "submit", filePath]);
const result: string = await executeCommand(channel, "node", [leetCodeBinaryPath, "submit", filePath]);
const resultPath: string = path.join(os.homedir(), ".leetcode", "Result");
await fse.ensureFile(resultPath);
await fse.writeFile(resultPath, result);
await vscode.window.showTextDocument(vscode.Uri.file(resultPath));
} catch (error) {
await promptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details", DialogType.error);
await promptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details", DialogType.error, channel);
}
}
24 changes: 11 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ import * as vscode from "vscode";
import * as session from "./commands/session";
import * as show from "./commands/show";
import * as submit from "./commands/submit";
import { leetcodeChannel } from "./leetCodeChannel";
import { LeetCodeNode, LeetCodeTreeDataProvider } from "./leetCodeExplorer";
import { leetCodeManager } from "./leetCodeManager";
import { leetCodeStatusBarItem } from "./leetCodeStatusBarItem";
import { isNodeInstalled } from "./utils/nodeUtils";

export async function activate(context: vscode.ExtensionContext) {
if (!await isNodeInstalled()) {
const channel: vscode.OutputChannel = vscode.window.createOutputChannel("LeetCode");
if (!await isNodeInstalled(channel)) {
return;
}

leetCodeManager.getLoginStatus();
const leetCodeTreeDataProvider: LeetCodeTreeDataProvider = new LeetCodeTreeDataProvider(context);
leetCodeManager.getLoginStatus(channel);
const leetCodeTreeDataProvider: LeetCodeTreeDataProvider = new LeetCodeTreeDataProvider(context, channel);

context.subscriptions.push(
vscode.window.registerTreeDataProvider("leetCodeExplorer", leetCodeTreeDataProvider),
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession()),
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn(channel)),
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut(channel)),
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession(channel)),
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession(channel)),
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(channel, node)),
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem(channel)),
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
vscode.commands.registerCommand("leetcode.submitSolution", () => submit.submitSolution()),
vscode.commands.registerCommand("leetcode.submitSolution", () => submit.submitSolution(channel)),
);

leetCodeManager.on("statusChanged", () => {
Expand All @@ -37,6 +36,5 @@ export async function activate(context: vscode.ExtensionContext) {
}

export function deactivate() {
leetcodeChannel.dispose();
leetCodeStatusBarItem.dispose();
}
37 changes: 0 additions & 37 deletions src/leetCodeChannel.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/leetCodeExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
// tslint:disable-next-line:member-ordering
public readonly onDidChangeTreeData: vscode.Event<any> = this.onDidChangeTreeDataEvent.event;

constructor(private context: vscode.ExtensionContext) { }
constructor(private context: vscode.ExtensionContext, private channel: vscode.OutputChannel) { }

public async refresh(): Promise<void> {
this.treeData.clear();
Expand Down Expand Up @@ -97,7 +97,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
}

private async getProblemData(): Promise<void> {
const allProblems: list.IProblem[] = await list.listProblems();
const allProblems: list.IProblem[] = await list.listProblems(this.channel);
for (const problem of allProblems) {
const problems: list.IProblem[] | undefined = this.treeData.get(problem.difficulty);
if (problems) {
Expand Down
25 changes: 12 additions & 13 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
import * as cp from "child_process";
import { EventEmitter } from "events";
import * as vscode from "vscode";
import { leetcodeChannel } from "./leetCodeChannel";
import { UserStatus } from "./shared";
import { leetCodeBinaryPath } from "./shared";
import { executeCommand } from "./utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";

export interface ILeetCodeManager extends EventEmitter {
getLoginStatus(): void;
getLoginStatus(channel: vscode.OutputChannel): void;
getStatus(): UserStatus;
getUser(): string | undefined;
signIn(): void;
signOut(): void;
signIn(channel: vscode.OutputChannel): void;
signOut(channel: vscode.OutputChannel): void;
}

class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
Expand All @@ -27,9 +26,9 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
this.userStatus = UserStatus.SignedOut;
}

public async getLoginStatus(): Promise<void> {
public async getLoginStatus(channel: vscode.OutputChannel): Promise<void> {
try {
const result = await executeCommand("node", [leetCodeBinaryPath, "user"]);
const result = await executeCommand(channel, "node", [leetCodeBinaryPath, "user"]);
this.currentUser = result.slice("You are now login as".length).trim();
this.userStatus = UserStatus.SignedIn;
} catch (error) {
Expand All @@ -40,18 +39,18 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
}
}

public async signIn(): Promise<void> {
public async signIn(channel: vscode.OutputChannel): Promise<void> {
try {
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
let result: string = "";
const childProc: cp.ChildProcess = cp.spawn("node", [leetCodeBinaryPath, "user", "-l"]);
childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
result = result.concat(data);
leetcodeChannel.append(data);
channel.append(data);
});

childProc.stderr.on("data", (data: string | Buffer) => leetcodeChannel.append(data.toString()));
childProc.stderr.on("data", (data: string | Buffer) => channel.append(data.toString()));

childProc.on("error", reject);
const name: string | undefined = await vscode.window.showInputBox({
Expand Down Expand Up @@ -90,20 +89,20 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
this.emit("statusChanged");
}
} catch (error) {
promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details", DialogType.error);
promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details", DialogType.error, channel);
}

}

public async signOut(): Promise<void> {
public async signOut(channel: vscode.OutputChannel): Promise<void> {
try {
await executeCommand("node", [leetCodeBinaryPath, "user", "-L"]);
await executeCommand(channel, "node", [leetCodeBinaryPath, "user", "-L"]);
vscode.window.showInformationMessage("Successfully signed out.");
this.currentUser = undefined;
this.userStatus = UserStatus.SignedOut;
this.emit("statusChanged");
} catch (error) {
promptForOpenOutputChannel("Failed to sign out. Please open the output channel for details", DialogType.error);
promptForOpenOutputChannel("Failed to sign out. Please open the output channel for details", DialogType.error, channel);
}
}

Expand Down
Loading

0 comments on commit dfbdd18

Please sign in to comment.