Skip to content

Commit

Permalink
feat: passing settings to lsp (#294)
Browse files Browse the repository at this point in the history
* feat: passing settings to lsp

allows passing settings to language servers via config.serverSettings.

* cleanup package.json
  • Loading branch information
farwyler authored Oct 14, 2022
1 parent a96c25f commit c898347
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ The following are tested so far:
}
```

Pass settings to the language server via `serverSettings`.
```jsonc
{
"nix.serverSettings": {
// settings for 'nil' LSP
"nil": {
"diagnostics": {
"ignored": ["unused_binding", "unused_with"]
},
"formatting": {
"command": ["nixpkgs-fmt"]
}
}
}
}
```

* When `Language Server` support is not enabled the following tools are used to
+ Formatting support. Set `nix.formatterPath` to any command which can accept file contents on stdin and return formatted text on stdout; e.g.,
```jsonc
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"type": "boolean",
"default": false,
"description": "Use LSP instead of nix-instantiate and nixpkgs-fmt."
},
"nix.serverSettings": {
"type": "object",
"default": {},
"description": "Settings passed to the language server on configuration requests."
}
}
},
Expand Down
28 changes: 26 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// from PR of https://github.com/nix-community/vscode-nix-ide/pull/16/

import { env, ExtensionContext, Uri, window, workspace } from "vscode";
import { LanguageClientOptions } from "vscode-languageclient";
import {
LanguageClientOptions,
LSPArray,
ConfigurationParams,
} from "vscode-languageclient";
import {
Executable,
LanguageClient,
Expand Down Expand Up @@ -41,13 +45,33 @@ export async function activate(context: ExtensionContext): Promise<void> {
documentSelector: nixDocumentSelector,
synchronize: {
fileEvents: workspace.createFileSystemWatcher("**/*.nix"),
configurationSection: [config.rootSection],
},
outputChannel: window.createOutputChannel("Nix"),
middleware: {
workspace: {
configuration: (params: ConfigurationParams): LSPArray[] => {
const items = params.items || [];
const res: LSPArray = [];
const settings = config.serverSettings;
for (const item of items) {
if (!item?.section) {
continue;
}
res.push(settings[item.section as keyof typeof settings] ?? null);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return res;
},
},
},
};

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

context.subscriptions.push(client);
}

export function deactivate(): Thenable<void> | undefined {
Expand Down
5 changes: 5 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WorkspaceConfiguration, workspace } from "vscode";
import { LSPObject } from "vscode-languageclient";

import { MessageItem, Uri } from "vscode";

Expand Down Expand Up @@ -28,5 +29,9 @@ export class Config {
get LSPEnabled(): boolean {
return this.get<boolean>("enableLanguageServer", false);
}

get serverSettings(): LSPObject {
return this.get<LSPObject>("serverSettings", {});
}
}
export const config = new Config();

0 comments on commit c898347

Please sign in to comment.