Skip to content

Commit

Permalink
feat: add rnix LSP support
Browse files Browse the repository at this point in the history
closes #10

adapted code from
#16
thanks to @JamieMagee
  • Loading branch information
jnoortheen committed Dec 23, 2020
1 parent 718deed commit ec3dee3
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 11 deletions.
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,22 @@
"language": "nix",
"path": "./snippets.json"
}
]
],
"configuration": {
"title": "NixIDE",
"properties": {
"nix.serverPath": {
"type": "string",
"default": "",
"description": "Location of the rnix language server. By default rnix from $PATH is used (if enableLanguageServer is true.)"
},
"nix.enableLanguageServer": {
"type": "boolean",
"default": false,
"description": "Setting this would disable nix-instantiate and nixpkgs-fmt"
}
}
}
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand Down Expand Up @@ -94,5 +109,8 @@
"release": "standard-version && git push --follow-tags",
"pkg-release": "vsce publish && ovsx publish --pat $OVS_PAT",
"lint": "eslint --ext ts src"
},
"dependencies": {
"vscode-languageclient": "^7.0.0"
}
}
53 changes: 53 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// from PR of https://github.com/nix-community/vscode-nix-ide/pull/16/

import { env, ExtensionContext, Uri, window, workspace } from "vscode";
import {
Executable,
LanguageClient,
LanguageClientOptions,
ServerOptions,
} from "vscode-languageclient";
import { config, UriMessageItem } from "./configuration";

let client: LanguageClient;

export async function activate(context: ExtensionContext): Promise<void> {
if (config.serverPath === undefined || config.serverPath === "") {
const selection = await window.showErrorMessage<UriMessageItem>(
"Unable to find Nix language server",
{
title: "Install language server",
uri: Uri.parse("https://github.com/nix-community/rnix-lsp"),
}
);
if (selection?.uri !== undefined) {
await env.openExternal(selection?.uri);
return;
}
}
const serverExecutable: Executable = {
command: config.serverPath,
};
const serverOptions: ServerOptions = serverExecutable;

const nixDocumentSelector: { scheme: string; language: string }[] = [
{ scheme: "file", language: "nix" },
{ scheme: "untitled", language: "nix" },
];

const clientOptions: LanguageClientOptions = {
documentSelector: nixDocumentSelector,
synchronize: {
fileEvents: workspace.createFileSystemWatcher("**/*.nix"),
},
outputChannel: window.createOutputChannel("Nix"),
};

client = new LanguageClient("nix", "Nix", serverOptions, clientOptions);
client.registerProposedFeatures();
context.subscriptions.push(client.start());
}

export function deactivate(): Thenable<void> | undefined {
return client ? client.stop() : undefined;
}
28 changes: 28 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as vscode from "vscode";

import { MessageItem, Uri } from "vscode";

export interface UriMessageItem extends MessageItem {
uri: Uri;
}

export class Config {
readonly rootSection = "nix";

private get cfg(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration(this.rootSection);
}

private get<T>(path: string): T {
return this.cfg.get<T>(path)!;
}

get serverPath(): string {
return this.get<string>("serverPath");
}

get LSPEnabled(): boolean {
return this.get<boolean>("enableLanguageServer");
}
}
export const config = new Config();
33 changes: 23 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@ import * as vscode from "vscode";
import { ExtensionContext } from "vscode";
import { formattingProviders } from "./formatter";
import { startLinting } from "./linter";
import { config } from "./configuration";
import * as client from "./client";

/**
* Activate this extension.
*
* Format with nixpkgs-format
* validate with nix-instantiate
* If LSP is enabled
* then support IDE features with {@link https://github.com/nix-community/rnix-lsp|rnix-lsp}
* Else
* Format with nixpkgs-format
* validate with nix-instantiate
*
* @param context The context for this extension
* @return A promise for the initialization
*/
export async function activate(context: ExtensionContext): Promise<void> {
await startLinting(context);
if (config.LSPEnabled) {
await client.activate(context);
} else {
await startLinting(context);
const subs = [
vscode.languages.registerDocumentFormattingEditProvider,
vscode.languages.registerDocumentRangeFormattingEditProvider,
].map((func) => func("nix", formattingProviders));
context.subscriptions.concat(subs);
}
}

const subs = [
vscode.languages.registerDocumentFormattingEditProvider,
vscode.languages.registerDocumentRangeFormattingEditProvider,
].map((func) => func("nix", formattingProviders));

context.subscriptions.concat(subs);
};
export async function deactivate(): Promise<void> {
if (config.LSPEnabled) {
await client.deactivate();
}
}
46 changes: 46 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,13 @@ loud-rejection@^1.0.0:
currently-unhandled "^0.4.1"
signal-exit "^3.0.0"

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==
dependencies:
yallist "^4.0.0"

map-obj@^1.0.0, map-obj@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d"
Expand Down Expand Up @@ -2884,6 +2891,13 @@ semver@^6.0.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.3.4:
version "7.3.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97"
integrity sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==
dependencies:
lru-cache "^6.0.0"

set-blocking@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
Expand Down Expand Up @@ -3371,6 +3385,33 @@ vsce@^1.83.0:
yauzl "^2.3.1"
yazl "^2.2.2"

[email protected]:
version "6.0.0"
resolved "https://registry.yarnpkg.com/vscode-jsonrpc/-/vscode-jsonrpc-6.0.0.tgz#108bdb09b4400705176b957ceca9e0880e9b6d4e"
integrity sha512-wnJA4BnEjOSyFMvjZdpiOwhSq9uDoK8e/kpRJDTaMYzwlkrhG1fwDIZI94CLsLzlCK5cIbMMtFlJlfR57Lavmg==

vscode-languageclient@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/vscode-languageclient/-/vscode-languageclient-7.0.0.tgz#b505c22c21ffcf96e167799757fca07a6bad0fb2"
integrity sha512-P9AXdAPlsCgslpP9pRxYPqkNYV7Xq8300/aZDpO35j1fJm/ncize8iGswzYlcvFw5DQUx4eVk+KvfXdL0rehNg==
dependencies:
minimatch "^3.0.4"
semver "^7.3.4"
vscode-languageserver-protocol "3.16.0"

[email protected]:
version "3.16.0"
resolved "https://registry.yarnpkg.com/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.16.0.tgz#34135b61a9091db972188a07d337406a3cdbe821"
integrity sha512-sdeUoAawceQdgIfTI+sdcwkiK2KU+2cbEYA0agzM2uqaUy2UpnnGHtWTHVEtS0ES4zHU0eMFRGN+oQgDxlD66A==
dependencies:
vscode-jsonrpc "6.0.0"
vscode-languageserver-types "3.16.0"

[email protected]:
version "3.16.0"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0.tgz#ecf393fc121ec6974b2da3efb3155644c514e247"
integrity sha512-k8luDIWJWyenLc5ToFQQMaSrqCHiLwyKPHKPQZ5zz21vM+vIVUSvsRpcbiECH4WR88K2XZqc4ScRcZ7nk/jbeA==

vscode-test@^0.4.1:
version "0.4.3"
resolved "https://registry.yarnpkg.com/vscode-test/-/vscode-test-0.4.3.tgz#461ebf25fc4bc93d77d982aed556658a2e2b90b8"
Expand Down Expand Up @@ -3446,6 +3487,11 @@ y18n@^4.0.0:
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==

yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==

yaml@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
Expand Down

0 comments on commit ec3dee3

Please sign in to comment.