Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

support WSL#46

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jdneo merged 5 commits intoLeetCode-OpenSource:masterfrompurocean:master
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackage.json
View file
Open in desktop
Original file line numberDiff line numberDiff 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 Node.js inside the Windows Subsystem for Linux."
}
}
}
Expand Down
5 changes: 4 additions & 1 deletionsrc/commands/show.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsrc/leetCodeManager.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsrc/shared.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsrc/utils/cpUtils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/utils/workspaceUtils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
returnwsl.useWsl() ? wsl.toWslPath(textEditor.document.uri.fsPath) :textEditor.document.uri.fsPath;
}
18 changes: 18 additions & 0 deletionssrc/utils/wslUtils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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();
Copy link
Member

Choose a reason for hiding this comment

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

If we use" to wrap the path instead of replace\, will it be workable?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Not Work :(

functiontoWslPath(path){returncp.execFileSync("wsl",["wslpath","-u",`"${path}"`]).toString().trim();}
module.js:549    throw err;    ^Error: Cannot find module '/mnt/y/env/VSCode-win32-x64/Y:/env/VSCode-win32-x64/.vscode/extensions/shengchen.vscode-leetcode-0.7.0/node_modules/leetcode-cli/bin/leetcode'    at Function.Module._resolveFilename (module.js:547:15)    at Function.Module._load (module.js:474:25)    at Function.Module.runMain (module.js:693:10)    at startup (bootstrap_node.js:191:16)    at bootstrap_node.js:612:3

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

WithpathUtils my point of view is not unnecessary at this time.

For this program, we just need do good job with file system and shell command for cross-platform. I think thecpUtils andworkspaceUtils is sufficiently abstract.

We can try to change these next time if needed.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

We could focus oncpUtils andworkspaceUtils provide service for others.

Copy link
Member

Choose a reason for hiding this comment

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

@purocean That is because by default the optionshell ofexecFileSync is false. If add{shell: true} to the argument list, then I think it should work.

Another thing I'm concerning is that here we useexecFileSync(). Personally, I prefer useasync/await wherever possible. Take a look at this:https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
So can we use executeCommand in the cpUtils?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

{shell: true} work perfectly.

https://github.com/purocean/vscode-leetcode/blob/d330913ad843179e12d0248a155f8a1f570e9f6f/src/shared.ts#L7

We need a lot of works if we use async/await. Abstractleetcode command is a better option.

But we could improve step by step. Half a loaf is better than no bread.

Copy link
Member

Choose a reason for hiding this comment

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

OK, make sense. Tracking issue:#48

}

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

[8]ページ先頭

©2009-2025 Movatter.jp