Skip to content

Commit ac20b6c

Browse files
committed
using a global output channel to avoid passing outputchannel through functions
1 parent 6e920d5 commit ac20b6c

12 files changed

+102
-66
lines changed

src/commands/list.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export interface IProblem {
1616
passRate: string;
1717
}
1818

19-
export async function listProblems(channel: vscode.OutputChannel): Promise<IProblem[]> {
19+
export async function listProblems(): Promise<IProblem[]> {
2020
try {
2121
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
2222
return [];
2323
}
2424
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
2525
const showLocked: boolean | undefined = leetCodeConfig.get<boolean>("showLocked");
26-
const result: string = await executeCommand(channel, "node", showLocked ? [leetCodeBinaryPath, "list"] : [leetCodeBinaryPath, "list", "-q", "L"]);
26+
const result: string = await executeCommand("node", showLocked ? [leetCodeBinaryPath, "list"] : [leetCodeBinaryPath, "list", "-q", "L"]);
2727
const problems: IProblem[] = [];
2828
const lines: string[] = result.split("\n");
2929
const reg: RegExp = /^(.)\s(.{1,2})\s(.)\s\[\s*(\d*)\]\s*(.*)\s*(Easy|Medium|Hard)\s*\((\s*\d+\.\d+ %)\)/;
@@ -43,7 +43,7 @@ export async function listProblems(channel: vscode.OutputChannel): Promise<IProb
4343
}
4444
return problems.reverse();
4545
} catch (error) {
46-
await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details.", DialogType.error, channel);
46+
await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details.", DialogType.error);
4747
return [];
4848
}
4949
}

src/commands/session.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { IQuickItemEx, leetCodeBinaryPath } from "../shared";
66
import { executeCommand } from "../utils/cpUtils";
77
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
88

9-
export async function getSessionList(channel: vscode.OutputChannel): Promise<ISession[]> {
9+
export async function getSessionList(): Promise<ISession[]> {
1010
const signInStatus: string | undefined = leetCodeManager.getUser();
1111
if (!signInStatus) {
1212
promptForSignIn();
1313
return [];
1414
}
15-
const result: string = await executeCommand(channel, "node", [leetCodeBinaryPath, "session"]);
15+
const result: string = await executeCommand("node", [leetCodeBinaryPath, "session"]);
1616
const lines: string[] = result.split("\n");
1717
const sessions: ISession[] = [];
1818
const reg: RegExp = /(.?)\s*(\d+)\s+(.*)\s+(\d+ \(\s*\d+\.\d+ %\))\s+(\d+ \(\s*\d+\.\d+ %\))/;
@@ -31,8 +31,8 @@ export async function getSessionList(channel: vscode.OutputChannel): Promise<ISe
3131
return sessions;
3232
}
3333

34-
export async function selectSession(channel: vscode.OutputChannel): Promise<void> {
35-
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks(channel));
34+
export async function selectSession(): Promise<void> {
35+
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(parseSessionsToPicks());
3636
if (!choice || choice.description === "Active") {
3737
return;
3838
}
@@ -41,18 +41,18 @@ export async function selectSession(channel: vscode.OutputChannel): Promise<void
4141
return;
4242
}
4343
try {
44-
await executeCommand(channel, "node", [leetCodeBinaryPath, "session", "-e", choice.value]);
44+
await executeCommand("node", [leetCodeBinaryPath, "session", "-e", choice.value]);
4545
vscode.window.showInformationMessage(`Successfully switched to session '${choice.label}'.`);
4646
await vscode.commands.executeCommand("leetcode.refreshExplorer");
4747
} catch (error) {
48-
await promptForOpenOutputChannel("Failed to switch session. Please open the output channel for details.", DialogType.error, channel);
48+
await promptForOpenOutputChannel("Failed to switch session. Please open the output channel for details.", DialogType.error);
4949
}
5050
}
5151

52-
async function parseSessionsToPicks(channel: vscode.OutputChannel): Promise<Array<IQuickItemEx<string>>> {
52+
async function parseSessionsToPicks(): Promise<Array<IQuickItemEx<string>>> {
5353
return new Promise(async (resolve: (res: Array<IQuickItemEx<string>>) => void): Promise<void> => {
5454
try {
55-
const sessions: ISession[] = await getSessionList(channel);
55+
const sessions: ISession[] = await getSessionList();
5656
const picks: Array<IQuickItemEx<string>> = sessions.map((s: ISession) => Object.assign({}, {
5757
label: `${s.active ? "$(check) " : ""}${s.name}`,
5858
description: s.active ? "Active" : "",
@@ -67,12 +67,12 @@ async function parseSessionsToPicks(channel: vscode.OutputChannel): Promise<Arra
6767
});
6868
resolve(picks);
6969
} catch (error) {
70-
return await promptForOpenOutputChannel("Failed to list sessions. Please open the output channel for details.", DialogType.error, channel);
70+
return await promptForOpenOutputChannel("Failed to list sessions. Please open the output channel for details.", DialogType.error);
7171
}
7272
});
7373
}
7474

75-
export async function createSession(channel: vscode.OutputChannel): Promise<void> {
75+
export async function createSession(): Promise<void> {
7676
const session: string | undefined = await vscode.window.showInputBox({
7777
prompt: "Enter the new session name.",
7878
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "Session name must not be empty",
@@ -81,10 +81,10 @@ export async function createSession(channel: vscode.OutputChannel): Promise<void
8181
return;
8282
}
8383
try {
84-
await executeCommand(channel, "node", [leetCodeBinaryPath, "session", "-c", session]);
84+
await executeCommand("node", [leetCodeBinaryPath, "session", "-c", session]);
8585
vscode.window.showInformationMessage("New session created, you can switch to it by clicking the status bar.");
8686
} catch (error) {
87-
await promptForOpenOutputChannel("Failed to create session. Please open the output channel for details.", DialogType.error, channel);
87+
await promptForOpenOutputChannel("Failed to create session. Please open the output channel for details.", DialogType.error);
8888
}
8989
}
9090

src/commands/show.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import { selectWorkspaceFolder } from "../utils/workspaceUtils";
1111
import * as wsl from "../utils/wslUtils";
1212
import * as list from "./list";
1313

14-
export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise<void> {
14+
export async function showProblem(node?: LeetCodeNode): Promise<void> {
1515
if (!node) {
1616
return;
1717
}
18-
await showProblemInternal(channel, node.id);
18+
await showProblemInternal(node.id);
1919
}
2020

21-
export async function searchProblem(channel: vscode.OutputChannel): Promise<void> {
21+
export async function searchProblem(): Promise<void> {
2222
if (!leetCodeManager.getUser()) {
2323
promptForSignIn();
2424
return;
2525
}
2626
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(
27-
parseProblemsToPicks(list.listProblems(channel)),
27+
parseProblemsToPicks(list.listProblems()),
2828
{
2929
matchOnDetail: true,
3030
placeHolder: "Select one problem",
@@ -33,10 +33,10 @@ export async function searchProblem(channel: vscode.OutputChannel): Promise<void
3333
if (!choice) {
3434
return;
3535
}
36-
await showProblemInternal(channel, choice.value);
36+
await showProblemInternal(choice.value);
3737
}
3838

39-
async function showProblemInternal(channel: vscode.OutputChannel, id: string): Promise<void> {
39+
async function showProblemInternal(id: string): Promise<void> {
4040
try {
4141
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
4242
let defaultLanguage: string | undefined = leetCodeConfig.get<string>("defaultLanguage");
@@ -50,7 +50,7 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
5050

5151
const outdir: string = await selectWorkspaceFolder();
5252
await fse.ensureDir(outdir);
53-
const result: string = await executeCommand(channel, "node", [leetCodeBinaryPath, "show", id, "-gx", "-l", language, "-o", `"${outdir}"`]);
53+
const result: string = await executeCommand("node", [leetCodeBinaryPath, "show", id, "-gx", "-l", language, "-o", `"${outdir}"`]);
5454
const reg: RegExp = /\* Source Code:\s*(.*)/;
5555
const match: RegExpMatchArray | null = result.match(reg);
5656
if (match && match.length >= 2) {
@@ -75,7 +75,7 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
7575
}
7676
}
7777
} catch (error) {
78-
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details.", DialogType.error, channel);
78+
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details.", DialogType.error);
7979
}
8080
}
8181

src/commands/submit.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { executeCommandWithProgress } from "../utils/cpUtils";
77
import { DialogType, promptForOpenOutputChannel, promptForSignIn, showResultFile } from "../utils/uiUtils";
88
import { getActivefilePath } from "../utils/workspaceUtils";
99

10-
export async function submitSolution(channel: vscode.OutputChannel, uri?: vscode.Uri): Promise<void> {
10+
export async function submitSolution(uri?: vscode.Uri): Promise<void> {
1111
if (!leetCodeManager.getUser()) {
1212
promptForSignIn();
1313
return;
@@ -19,9 +19,9 @@ export async function submitSolution(channel: vscode.OutputChannel, uri?: vscode
1919
}
2020

2121
try {
22-
const result: string = await executeCommandWithProgress("Submitting to LeetCode...", channel, "node", [leetCodeBinaryPath, "submit", `"${filePath}"`]);
22+
const result: string = await executeCommandWithProgress("Submitting to LeetCode...", "node", [leetCodeBinaryPath, "submit", `"${filePath}"`]);
2323
await showResultFile(result);
2424
} catch (error) {
25-
await promptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details.", DialogType.error, channel);
25+
await promptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details.", DialogType.error);
2626
}
2727
}

src/commands/test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { executeCommandWithProgress } from "../utils/cpUtils";
88
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog, showResultFile } from "../utils/uiUtils";
99
import { getActivefilePath } from "../utils/workspaceUtils";
1010

11-
export async function testSolution(channel: vscode.OutputChannel, uri?: vscode.Uri): Promise<void> {
11+
export async function testSolution(uri?: vscode.Uri): Promise<void> {
1212
try {
1313
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
1414
return;
@@ -47,7 +47,7 @@ export async function testSolution(channel: vscode.OutputChannel, uri?: vscode.U
4747
let result: string | undefined;
4848
switch (choice.value) {
4949
case ":default":
50-
result = await executeCommandWithProgress("Submitting to LeetCode...", channel, "node", [leetCodeBinaryPath, "test", `"${filePath}"`]);
50+
result = await executeCommandWithProgress("Submitting to LeetCode...", "node", [leetCodeBinaryPath, "test", `"${filePath}"`]);
5151
break;
5252
case ":direct":
5353
const testString: string | undefined = await vscode.window.showInputBox({
@@ -57,15 +57,15 @@ export async function testSolution(channel: vscode.OutputChannel, uri?: vscode.U
5757
ignoreFocusOut: true,
5858
});
5959
if (testString) {
60-
result = await executeCommandWithProgress("Submitting to LeetCode...", channel, "node", [leetCodeBinaryPath, "test", `"${filePath}"`, "-t", `"${testString.replace(/"/g, "")}"`]);
60+
result = await executeCommandWithProgress("Submitting to LeetCode...", "node", [leetCodeBinaryPath, "test", `"${filePath}"`, "-t", `"${testString.replace(/"/g, "")}"`]);
6161
}
6262
break;
6363
case ":file":
6464
const testFile: vscode.Uri[] | undefined = await showFileSelectDialog();
6565
if (testFile && testFile.length) {
6666
const input: string = await fse.readFile(testFile[0].fsPath, "utf-8");
6767
if (input.trim()) {
68-
result = await executeCommandWithProgress("Submitting to LeetCode...", channel, "node", [leetCodeBinaryPath, "test", `"${filePath}"`, "-t", `"${input.replace(/"/g, "").replace(/\r?\n/g, "\\n")}"`]);
68+
result = await executeCommandWithProgress("Submitting to LeetCode...", "node", [leetCodeBinaryPath, "test", `"${filePath}"`, "-t", `"${input.replace(/"/g, "").replace(/\r?\n/g, "\\n")}"`]);
6969
} else {
7070
vscode.window.showErrorMessage("The selected test file must not be empty.");
7171
}
@@ -79,6 +79,6 @@ export async function testSolution(channel: vscode.OutputChannel, uri?: vscode.U
7979
}
8080
await showResultFile(result);
8181
} catch (error) {
82-
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error, channel);
82+
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);
8383
}
8484
}

src/extension.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@ import * as session from "./commands/session";
55
import * as show from "./commands/show";
66
import * as submit from "./commands/submit";
77
import * as test from "./commands/test";
8+
import { leetCodeChannel } from "./leetCodeChannel";
89
import { LeetCodeNode, LeetCodeTreeDataProvider } from "./leetCodeExplorer";
910
import { leetCodeManager } from "./leetCodeManager";
1011
import { leetCodeStatusBarItem } from "./leetCodeStatusBarItem";
1112
import { isNodeInstalled } from "./utils/nodeUtils";
1213

1314
export async function activate(context: vscode.ExtensionContext): Promise<void> {
14-
const channel: vscode.OutputChannel = vscode.window.createOutputChannel("LeetCode");
15-
if (!await isNodeInstalled(channel)) {
15+
if (!await isNodeInstalled()) {
1616
return;
1717
}
18-
leetCodeManager.getLoginStatus(channel);
19-
const leetCodeTreeDataProvider: LeetCodeTreeDataProvider = new LeetCodeTreeDataProvider(context, channel);
18+
leetCodeManager.getLoginStatus();
19+
const leetCodeTreeDataProvider: LeetCodeTreeDataProvider = new LeetCodeTreeDataProvider(context);
2020

2121
context.subscriptions.push(
2222
vscode.window.registerTreeDataProvider("leetCodeExplorer", leetCodeTreeDataProvider),
23-
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn(channel)),
24-
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut(channel)),
25-
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession(channel)),
26-
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession(channel)),
27-
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(channel, node)),
28-
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem(channel)),
23+
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
24+
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
25+
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
26+
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession()),
27+
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
28+
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
2929
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
30-
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(channel, uri)),
31-
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(channel, uri)),
30+
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
31+
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
3232
);
3333

3434
leetCodeManager.on("statusChanged", () => {
@@ -39,4 +39,5 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
3939

4040
export function deactivate(): void {
4141
leetCodeStatusBarItem.dispose();
42+
leetCodeChannel.dispose();
4243
}

src/leetCodeChannel.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
import * as vscode from "vscode";
4+
5+
export interface ILeetCodeChannel {
6+
appendLine(message: any, title?: string): void;
7+
append(message: any): void;
8+
show(): void;
9+
dispose(): void;
10+
}
11+
12+
class LeetCodeChannel implements ILeetCodeChannel {
13+
private readonly channel: vscode.OutputChannel = vscode.window.createOutputChannel("LeetCode");
14+
15+
public appendLine(message: string): void {
16+
this.channel.appendLine(message);
17+
}
18+
19+
public append(message: string): void {
20+
this.channel.append(message);
21+
}
22+
23+
public show(): void {
24+
this.channel.show();
25+
}
26+
27+
public dispose(): void {
28+
this.channel.dispose();
29+
}
30+
}
31+
32+
export const leetCodeChannel: ILeetCodeChannel = new LeetCodeChannel();

src/leetCodeExplorer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
4242
// tslint:disable-next-line:member-ordering
4343
public readonly onDidChangeTreeData: vscode.Event<any> = this.onDidChangeTreeDataEvent.event;
4444

45-
constructor(private context: vscode.ExtensionContext, private channel: vscode.OutputChannel) { }
45+
constructor(private context: vscode.ExtensionContext) { }
4646

4747
public async refresh(): Promise<void> {
4848
await this.getProblemData();
@@ -100,7 +100,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
100100
}
101101

102102
private async getProblemData(): Promise<void> {
103-
const allProblems: list.IProblem[] = await list.listProblems(this.channel);
103+
const allProblems: list.IProblem[] = await list.listProblems();
104104
this.treeData.clear();
105105
for (const problem of allProblems) {
106106
const problems: list.IProblem[] | undefined = this.treeData.get(problem.difficulty);

0 commit comments

Comments
 (0)