Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@
"default": true,
"scope": "window",
"description": "Show a hint to set the default language."
},
"leetcode.useWsl": {
"type": "boolean",
"default": false,
"scope": "window",
"description": "Use nodejs inside the Windows Subsystem for Linux."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodejs -> Node.js

}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IQuickItemEx, languages, leetCodeBinaryPath, ProblemState } from "../sh
import { executeCommand } from "../utils/cpUtils";
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
import * as wsl from "../utils/wslUtils";
import * as list from "./list";

export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise<void> {
Expand Down Expand Up @@ -53,7 +54,9 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
const reg: RegExp = /\* Source Code:\s*(.*)/;
const match: RegExpMatchArray | null = result.match(reg);
if (match && match.length >= 2) {
await vscode.window.showTextDocument(vscode.Uri.file(match[1].trim()), { preview: false });
const filePath = wsl.useWsl() ? wsl.toWinPath(match[1].trim()) : match[1].trim();

await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false });
} else {
throw new Error("Failed to fetch the problem information.");
}
Expand Down
7 changes: 6 additions & 1 deletion src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UserStatus } from "./shared";
import { leetCodeBinaryPath } from "./shared";
import { executeCommand } from "./utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";

export interface ILeetCodeManager extends EventEmitter {
getLoginStatus(channel: vscode.OutputChannel): void;
Expand Down Expand Up @@ -43,7 +44,11 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
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"], { shell: true });

const childProc: cp.ChildProcess = wsl.useWsl()
? cp.spawn("wsl", ["--", "node", leetCodeBinaryPath, "user", "-l"], { shell: true })
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });

childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
result = result.concat(data);
Expand Down
9 changes: 8 additions & 1 deletion src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import * as path from "path";
import * as vscode from "vscode";
import * as wsl from "./utils/wslUtils";

export const leetCodeBinaryPath: string = `"${path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode")}"`;
let binPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode");

if (wsl.useWsl()) {
binPath = wsl.toWslPath(binPath);
}

export const leetCodeBinaryPath: string = `"${binPath}"`;

export interface IQuickItemEx<T> extends vscode.QuickPickItem {
value: T;
Expand Down
6 changes: 5 additions & 1 deletion src/utils/cpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import * as cp from "child_process";
import * as vscode from "vscode";
import * as wsl from "./wslUtils";

export async function executeCommand(channel: vscode.OutputChannel, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
let result: string = "";
const childProc: cp.ChildProcess = cp.spawn(command, args, options);

const childProc: cp.ChildProcess = wsl.useWsl()
? cp.spawn("wsl", ["--", command].concat(args), options)
: cp.spawn(command, args, options);

childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
Expand Down
8 changes: 6 additions & 2 deletions src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import * as wsl from "./wslUtils";

export async function selectWorkspaceFolder(): Promise<string> {
let folder: vscode.WorkspaceFolder | undefined;
Expand All @@ -15,7 +16,10 @@ export async function selectWorkspaceFolder(): Promise<string> {
folder = vscode.workspace.workspaceFolders[0];
}
}
return folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");

const workFolder = folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");

return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder;
}

export async function getActivefilePath(uri?: vscode.Uri): Promise<string | undefined> {
Expand All @@ -33,5 +37,5 @@ export async function getActivefilePath(uri?: vscode.Uri): Promise<string | unde
vscode.window.showWarningMessage("Please save the solution file first.");
return undefined;
}
return textEditor.document.uri.fsPath;
return wsl.useWsl() ? wsl.toWslPath(textEditor.document.uri.fsPath) : textEditor.document.uri.fsPath;
}
18 changes: 18 additions & 0 deletions src/utils/wslUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

import * as cp from "child_process";
import * as vscode from "vscode";

export function useWsl(): boolean {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");

return process.platform === "win32" && leetCodeConfig.get<boolean>("useWsl") === true;
}

export function toWslPath(path: string): string {
return cp.execFileSync("wsl", ["--", "wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim();
}

export function toWinPath(path: string): string {
return cp.execFileSync("wsl", ["--", "wslpath", "-w", path]).toString().trim();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks the 1st param "--" doesn't work for me.

CMD>wsl wslpath -w /mnt/c/
C:\
CMD>wsl -- wslpath -w /mnt/c/
/bin/bash: --: invalid option
Usage:  /bin/bash [GNU long option] [option] ...
        /bin/bash [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --rcfile
        --restricted
        --verbose
        --version
Shell options:
        -ilrsD or -c command or -O shopt_option         (invocation only)
        -abefhkmnptuvxBCHP or -o option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the Windows version, maybe this param can be removed.

C:\>ver

Microsoft Windows [Version 10.0.17713.1000]

C:\>wsl --help
Usage: wsl.exe [option] ...
Options:
    -d, --distribution <DistributionName>
        Launch the specified distribition.

    -e, --exec <CommandLine>
        Execute the specified Linux command. The remainder of the arguments are
        used as the command line to execute.

    -u, --user <UserName>
        Run as the specified user.

    --help
        Display this usage information.

    --
        Stop parsing arguments and pass the remainder to the Linux process.

C:\>wsl -- wslpath -u "C:/"
/mnt/c/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attaching mine.

C:\>ver

Microsoft Windows [Version 10.0.17134.167]

C:\>wsl --help
/bin/bash: --: invalid option
Usage:  /bin/bash [GNU long option] [option] ...
        /bin/bash [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --rcfile
        --restricted
        --verbose
        --version
Shell options:
        -ilrsD or -c command or -O shopt_option         (invocation only)
        -abefhkmnptuvxBCHP or -o option

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think wsl.exe is a wrapper to run inner shell in your WSL. I suppose the parameters depend on what your inner shell is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@purocean If you remove the -- at your side, will this command still workable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}