Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): support using new language server implementations #162

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
88 changes: 31 additions & 57 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as path from "path";
// import { workspace, ExtensionContext } from "vscode";
import * as vscode from "vscode";
import * as which from "which";

import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
} from "vscode-languageclient/node";

let client: LanguageClient;
activate as activateExtension,
deactivate as deactivateExtension,
} from "./nu-ide";
import {
activate as activateNuLsp,
deactivate as deactivateNuLsp,
} from "./nu-lsp";
import { activate as activateNuls, deactivate as deactivateNuls } from "./nuls";
import { LanguageClientOptions } from "vscode-languageclient";

export function activate(context: vscode.ExtensionContext) {
export function activate(context: vscode.ExtensionContext): void {
console.log("Terminals: " + (<any>vscode.window).terminals.length);
context.subscriptions.push(
vscode.window.registerTerminalProfileProvider("nushell_default", {
provideTerminalProfile(
token: vscode.CancellationToken
token: vscode.CancellationToken,
): vscode.ProviderResult<vscode.TerminalProfile> {
const which = require("which");
const path = require("path");

const PATH_FROM_ENV = process.env["PATH"];
const pathsToCheck = [
PATH_FROM_ENV,
Expand Down Expand Up @@ -76,21 +69,21 @@ export function activate(context: vscode.ExtensionContext) {

if (found_nushell_path == null) {
console.log(
"Nushell not found in env:PATH or any of the heuristic locations."
"Nushell not found in env:PATH or any of the heuristic locations.",
);
// use an async arrow funciton to use `await` inside
return (async () => {
if (
(await vscode.window.showErrorMessage(
"We cannot find a nushell executable in your path or pre-defined locations",
"install from website"
"install from website",
)) &&
(await vscode.env.openExternal(
vscode.Uri.parse("https://www.nushell.sh/")
vscode.Uri.parse("https://www.nushell.sh/"),
)) &&
(await vscode.window.showInformationMessage(
"after you install nushell, you might need to reload vscode",
"reload now"
"reload now",
))
) {
vscode.commands.executeCommand("workbench.action.reloadWindow");
Expand All @@ -107,34 +100,14 @@ export function activate(context: vscode.ExtensionContext) {
shellPath: found_nushell_path,
iconPath: vscode.Uri.joinPath(
context.extensionUri,
"assets/nu.svg"
"assets/nu.svg",
),
},
};
},
})
);

// The server is implemented in node
const serverModule = context.asAbsolutePath(
path.join("out", "server", "src", "server.js")
}),
);

// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
const debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: { module: serverModule, transport: TransportKind.ipc },
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions,
},
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for plain text documents
Expand All @@ -145,21 +118,22 @@ export function activate(context: vscode.ExtensionContext) {
},
};

// Create the language client and start the client.
client = new LanguageClient(
const configuration = vscode.workspace.getConfiguration(
"nushellLanguageServer",
"Nushell Language Server",
serverOptions,
clientOptions
null,
);

// Start the client. This will also launch the server
client.start();
if (configuration.implementation == "nu --lsp") {
activateNuLsp(context, clientOptions);
} else if (configuration.implementation == "nuls") {
activateNuls(context, clientOptions);
} else {
activateExtension(context, clientOptions);
}
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
export function deactivate(): void {
deactivateExtension();
deactivateNuLsp();
deactivateNuls();
}
68 changes: 68 additions & 0 deletions client/src/nu-ide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as path from "path";
import { ExtensionContext, window } from "vscode";
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
} from "vscode-languageclient/node";

let client: LanguageClient | null = null;

async function startClient(
context: ExtensionContext,
clientOptions: LanguageClientOptions,
) {
// The server is implemented in node
const serverModule = context.asAbsolutePath(
path.join("out", "server", "src", "server.js"),
);

// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
const debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: { module: serverModule, transport: TransportKind.ipc },
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions,
},
};

// Create the language client and start the client.
client = new LanguageClient(
"nushellLanguageServer",
"Nushell Language Server",
serverOptions,
clientOptions,
);

return client.start().catch((reason) => {
window.showWarningMessage(
`Failed to run Nushell Language Server (extension): ${reason}`,
);
client = null;
});
}

async function stopClient(): Promise<void> {
if (client) {
client.stop();
}
client = null;
}

export async function activate(
context: ExtensionContext,
clientOptions: LanguageClientOptions,
) {
await startClient(context, clientOptions);
}

export function deactivate(): Thenable<void> {
return stopClient();
}
60 changes: 60 additions & 0 deletions client/src/nu-lsp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ExtensionContext, window } from "vscode";
import * as vscode from "vscode";
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
} from "vscode-languageclient/node";

let client: LanguageClient | null = null;

async function startClient(
_context: ExtensionContext,
clientOptions: LanguageClientOptions,
) {
const configuration = vscode.workspace.getConfiguration(
"nushellLanguageServer",
null,
);

const serverOptions: ServerOptions = {
command: configuration.nushellExecutablePath,
args: ["--lsp"],
};

// Create the language client and start the client.
client = new LanguageClient(
"nushellLanguageServer",
"Nushell Language Server",
serverOptions,
clientOptions,
);

return client.start().catch((reason) => {
window.showWarningMessage(
`Failed to run Nushell Language Server (nu --lsp): ${reason}`,
);
client = null;
});
}

async function stopClient(): Promise<void> {
if (client) {
client.stop();
}
client = null;
}

export async function activate(
context: ExtensionContext,
clientOptions: LanguageClientOptions,
) {
// TODO: use configuration
// const configuration = workspace.getConfiguration("nushellLanguageServer", null);

await startClient(context, clientOptions);
}

export function deactivate(): Thenable<void> {
return stopClient();
}
51 changes: 51 additions & 0 deletions client/src/nuls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ExtensionContext, window } from "vscode";
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
} from "vscode-languageclient/node";

let client: LanguageClient | null = null;

async function startClient(
_context: ExtensionContext,
clientOptions: LanguageClientOptions,
) {
const serverOptions: ServerOptions = {
command: "nuls",
args: [],
};

// Create the language client and start the client.
client = new LanguageClient(
"nushellLanguageServer",
"Nushell Language Server",
serverOptions,
clientOptions,
);

return client.start().catch((reason) => {
window.showWarningMessage(
`Failed to run Nushell Language Server (nuls): ${reason}`,
);
client = null;
});
}

async function stopClient(): Promise<void> {
if (client) {
client.stop();
}
client = null;
}

export async function activate(
context: ExtensionContext,
clientOptions: LanguageClientOptions,
) {
await startClient(context, clientOptions);
}

export function deactivate(): Thenable<void> {
return stopClient();
}
38 changes: 13 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,15 @@
"vscode": "^1.75.0"
},
"icon": "assets/nushell.ico",
"categories": [
"Programming Languages",
"Snippets"
],
"activationEvents": [
"onTerminalProfile:nushell_default"
],
"categories": ["Programming Languages", "Snippets"],
"activationEvents": ["onTerminalProfile:nushell_default"],
"main": "out/client/src/extension.js",
"contributes": {
"languages": [
{
"id": "nushell",
"aliases": [
"nushell",
"nu"
],
"extensions": [
".nu"
],
"aliases": ["nushell", "nu"],
"extensions": [".nu"],
"icon": {
"light": "assets/nu.svg",
"dark": "assets/nu.svg"
Expand Down Expand Up @@ -85,6 +75,13 @@
"type": "object",
"title": "Nushell IDE Support",
"properties": {
"nushellLanguageServer.implementation": {
"scope": "window",
"type": "string",
"enum": ["extension", "nu --lsp", "nuls"],
"default": "extension",
"description": "Switch between different language server implementations (requires restarting Visual Studio Code to take effect)."
},
Copy link
Author

Choose a reason for hiding this comment

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

Bah, this is the only deliberate change here

The rest is auto-prettier, which I can revert if necessary

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you're using vscode to edit these files, one thing that I've just learned is you can do ctrl/cmd-shift-p and choose File:Save without formatting and it stops things like prettier or other formatters from running on save.

"nushellLanguageServer.maxNumberOfProblems": {
"scope": "resource",
"type": "number",
Expand All @@ -94,11 +91,7 @@
"nushellLanguageServer.trace.server": {
"scope": "window",
"type": "string",
"enum": [
"off",
"messages",
"verbose"
],
"enum": ["off", "messages", "verbose"],
"default": "off",
"description": "Traces the communication between VS Code and the language server."
},
Expand Down Expand Up @@ -163,12 +156,7 @@
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
},
"keywords": [
"nushell",
"nu",
"shell",
"scripting"
],
"keywords": ["nushell", "nu", "shell", "scripting"],
"galleryBanner": {
"color": "#008000",
"theme": "light"
Expand Down