Skip to content

Commit

Permalink
feat: Allow specifying WSL distro by wslDistribution (#1693)
Browse files Browse the repository at this point in the history
* feat: Allow specifying WSL distro by wslDistribution

Add new config option `wslDistribution` to allow specifying the WSL distribution

* refactor: build spawn's arguments in order
  • Loading branch information
xiyaowong committed Dec 9, 2023
1 parent 240e8f9 commit 6c5dbcb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@
"default": false,
"markdownDescription": "Use neovim installed in [default WSL distro](https://learn.microsoft.com/en-us/windows/wsl/basic-commands#run-a-specific-linux-distribution-from-powershell-or-cmd). If you enable this setting, specify the path to the neovim executable installed in WSL `neovimExecutablePaths.linux` setting. "
},
"vscode-neovim.wslDistribution": {
"type": "string",
"default": "",
"markdownDescription": "Specify the WSL distribution to run neovim in. Use default WSL distro if empty. Use wsl.exe --list to see available distros"
},
"vscode-neovim.revealCursorScrollLine": {
"type": "boolean",
"default": false,
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export class Config implements Disposable {
const ext = extensions.getExtension(EXT_ID)!;
return ext.extensionKind !== ExtensionKind.Workspace && isWindows && this.cfg.get("useWSL", false);
}
get wslDistribution() {
return this.cfg.get("wslDistribution", "");
}
get revealCursorScrollLine() {
return this.cfg.get("revealCursorScrollLine", false);
}
Expand Down
48 changes: 27 additions & 21 deletions src/main_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ export class MainController implements vscode.Disposable {
public viewportManager!: ViewportManager;

public constructor(private extContext: ExtensionContext) {
const wslpath = (path: string) =>
const wslpath = (path: string) => {
// execSync returns a newline character at the end
execSync(`C:\\Windows\\system32\\wsl.exe wslpath '${path}'`).toString().trim();
const distro = config.wslDistribution.length ? `-d ${config.wslDistribution}` : "";
return execSync(`C:\\Windows\\system32\\wsl.exe ${distro} wslpath '${path}'`).toString().trim();
};

let extensionPath = extContext.extensionPath.replace(/\\/g, "\\\\");
if (config.useWsl) {
Expand All @@ -75,26 +77,33 @@ export class MainController implements vscode.Disposable {
const neovimPreScriptPath = path.posix.join(extensionPath, "vim", "vscode-neovim.vim");
const neovimPostScriptPath = path.posix.join(extensionPath, "runtime/lua", "vscode-neovim/force-options.lua");

const args = [
const args = [];

if (config.useWsl) {
args.push("C:\\Windows\\system32\\wsl.exe");
if (config.wslDistribution.length) {
args.push("-d", config.wslDistribution);
}
}

args.push(
config.neovimPath,
"-N",
"--embed",
// load options after user config
"-S",
neovimPostScriptPath,
// load support script before user config (to allow to rebind keybindings/commands)
"--cmd",
`source ${neovimPreScriptPath}`,
];
// load options after user config
"-S",
neovimPostScriptPath,
);

const workspaceFolder = vscode.workspace.workspaceFolders;
const cwd = workspaceFolder?.length ? workspaceFolder[0].uri.fsPath : undefined;
if (cwd && !vscode.env.remoteName) {
args.push("-c", `cd ${config.useWsl ? wslpath(cwd) : cwd}`);
}

if (config.useWsl) {
args.unshift(config.neovimPath);
}
if (parseInt(process.env.NEOVIM_DEBUG || "", 10) === 1) {
args.push(
"-u",
Expand All @@ -103,16 +112,14 @@ export class MainController implements vscode.Disposable {
`${process.env.NEOVIM_DEBUG_HOST || "127.0.0.1"}:${process.env.NEOVIM_DEBUG_PORT || 4000}`,
);
}

if (config.clean) {
args.push("--clean");
}
// #1162
if (!config.clean && config.neovimInitPath) {
args.push("-u", config.neovimInitPath);
}
logger.debug(
`Spawning nvim, path: ${config.neovimPath}, useWsl: ${config.useWsl}, args: ${JSON.stringify(args)}`,
);
if (config.NVIM_APPNAME) {
process.env.NVIM_APPNAME = config.NVIM_APPNAME;
if (config.useWsl) {
Expand All @@ -123,14 +130,13 @@ export class MainController implements vscode.Disposable {
process.env.WSLENV = "NVIM_APPNAME/u";
}
}
this.nvimProc = spawn(config.useWsl ? "C:\\Windows\\system32\\wsl.exe" : config.neovimPath, args, {});
this.nvimProc.on("close", (code) => {
logger.error(`Neovim exited with code: ${code}`);
});
this.nvimProc.on("error", (err) => {
logger.error(`Neovim spawn error: ${err.message}. Check if the path is correct.`);
vscode.window.showErrorMessage("Neovim: configure the path to neovim and restart the editor");
});

logger.debug(`Spawning nvim, ${args.join(" ")}`);
this.nvimProc = spawn(args[0], args.slice(1));
this.nvimProc.on("close", (code) => logger.error(`Neovim exited with code: ${code}`));
this.nvimProc.on("error", (err) =>
logger.error(`Neovim spawn error: ${err.message}. Check if the path is correct.`),
);
logger.debug(`Attaching to neovim`);
this.client = attach({
proc: this.nvimProc,
Expand Down

0 comments on commit 6c5dbcb

Please sign in to comment.