Skip to content

Commit 89e21b7

Browse files
committed
do
1 parent f5de250 commit 89e21b7

File tree

4 files changed

+53
-77
lines changed

4 files changed

+53
-77
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
]
100100
},
101101
"nix.serverPath": {
102-
"default": "nil",
102+
"default": "nixd",
103103
"description": "Location of the nix language server command.",
104104
"oneOf": [
105105
{

src/client.ts

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,15 @@
22

33
import { inspect } from "node:util";
44
import { sync as commandExistsSync } from "command-exists";
5-
import {
6-
type Disposable,
7-
type ExtensionContext,
8-
env,
9-
Uri,
10-
window,
11-
workspace,
12-
} from "vscode";
5+
import { type Disposable, type ExtensionContext, env, Uri, window, workspace } from "vscode";
136
import type {
147
CancellationToken,
158
ConfigurationParams,
169
LanguageClientOptions,
1710
LSPArray,
1811
MessageSignature,
1912
} from "vscode-languageclient";
20-
import {
21-
type Executable,
22-
LanguageClient,
23-
type ServerOptions,
24-
} from "vscode-languageclient/node";
13+
import { type Executable, LanguageClient, type ServerOptions } from "vscode-languageclient/node";
2514
import { config, type UriMessageItem } from "./configuration";
2615

2716
class Client extends LanguageClient {
@@ -35,18 +24,10 @@ class Client extends LanguageClient {
3524
showNotification?: boolean,
3625
): T {
3726
if (config.hiddenErrorKinds.includes(type.method)) {
38-
this.outputChannel.appendLine(
39-
`Suppressing failed ${inspect(type.method)} notification`,
40-
);
27+
this.outputChannel.appendLine(`Suppressing failed ${inspect(type.method)} notification`);
4128
return super.handleFailedRequest(type, token, error, defaultValue, false);
4229
}
43-
return super.handleFailedRequest(
44-
type,
45-
token,
46-
error,
47-
defaultValue,
48-
showNotification,
49-
);
30+
return super.handleFailedRequest(type, token, error, defaultValue, showNotification);
5031
}
5132

5233
override dispose(timeout?: number): Promise<void> {
@@ -70,25 +51,39 @@ class Client extends LanguageClient {
7051

7152
let client: Client;
7253

54+
const defaultServerCandidates = [["nixd"], ["nil"]];
55+
56+
function resolveServerPath(): Array<string> {
57+
const configured = config.serverPath;
58+
if (configured.length > 0) {
59+
return configured;
60+
}
61+
62+
for (const candidate of defaultServerCandidates) {
63+
if (commandExistsSync(candidate[0])) {
64+
return candidate;
65+
}
66+
}
67+
68+
return ["nixd"];
69+
}
70+
7371
export async function activate(context: ExtensionContext): Promise<void> {
74-
if (!commandExistsSync(config.serverPath[0])) {
75-
const selection = await window.showErrorMessage<UriMessageItem>(
76-
`Command ${config.serverPath} not found in $PATH`,
77-
{
78-
title: "Install language server",
79-
uri: Uri.parse(
80-
"https://github.com/nix-community/vscode-nix-ide?tab=readme-ov-file#language-servers",
81-
),
82-
},
83-
);
72+
const serverPath = resolveServerPath();
73+
74+
if (!commandExistsSync(serverPath[0])) {
75+
const selection = await window.showErrorMessage<UriMessageItem>(`Command ${serverPath} not found in $PATH`, {
76+
title: "Install language server",
77+
uri: Uri.parse("https://github.com/nix-community/vscode-nix-ide?tab=readme-ov-file#language-servers"),
78+
});
8479
if (selection?.uri !== undefined) {
8580
await env.openExternal(selection?.uri);
8681
return;
8782
}
8883
}
8984
const serverExecutable: Executable = {
90-
command: config.serverPath[0],
91-
args: config.serverPath.slice(1),
85+
command: serverPath[0],
86+
args: serverPath.slice(1),
9287
};
9388
const serverOptions: ServerOptions = serverExecutable;
9489

@@ -117,12 +112,9 @@ export async function activate(context: ExtensionContext): Promise<void> {
117112
if (!item?.section) {
118113
continue;
119114
}
120-
const sectionSettings =
121-
settings[item.section as keyof typeof settings];
115+
const sectionSettings = settings[item.section as keyof typeof settings];
122116
if (!sectionSettings) {
123-
client.warn(
124-
`failed to find "${item.section}" in "nix.serverSettings"`,
125-
);
117+
client.warn(`failed to find "${item.section}" in "nix.serverSettings"`);
126118
}
127119
res.push(sectionSettings ?? null);
128120
}
@@ -149,9 +141,7 @@ export async function deactivate(): Promise<void> {
149141
}
150142

151143
export async function restart(context: ExtensionContext): Promise<void> {
152-
const restartingMsg = window.setStatusBarMessage(
153-
"$(loading~spin) Restarting Nix language server",
154-
);
144+
const restartingMsg = window.setStatusBarMessage("$(loading~spin) Restarting Nix language server");
155145

156146
try {
157147
await deactivate();

src/configuration.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { MessageItem, Uri } from "vscode";
2-
import {
3-
type ConfigurationChangeEvent,
4-
type WorkspaceConfiguration,
5-
workspace,
6-
} from "vscode";
2+
import { type ConfigurationChangeEvent, type WorkspaceConfiguration, workspace } from "vscode";
73
import type { LSPObject } from "vscode-languageclient";
84
import { transformConfigValueByVscodeVariables } from "./utils";
95

@@ -18,13 +14,8 @@ export class Config {
1814
return workspace.getConfiguration(this.rootSection);
1915
}
2016

21-
private get<T extends string | boolean | LSPObject>(
22-
path: string,
23-
def_val: T,
24-
): T {
25-
return transformConfigValueByVscodeVariables(
26-
this.cfg.get<T>(path) ?? def_val,
27-
);
17+
private get<T extends string | boolean | LSPObject>(path: string, def_val: T): T {
18+
return transformConfigValueByVscodeVariables(this.cfg.get<T>(path) ?? def_val);
2819
}
2920

3021
get formatterPath(): Array<string> {
@@ -43,7 +34,7 @@ export class Config {
4334
}
4435

4536
get serverPath(): Array<string> {
46-
const path: Array<string> | string = this.get("serverPath", "nil");
37+
const path: Array<string> | string = this.get("serverPath", "nixd");
4738
if (typeof path === "string") {
4839
return [path];
4940
}
@@ -65,10 +56,7 @@ export class Config {
6556
requiresServerRestart(change: ConfigurationChangeEvent): boolean {
6657
// NOTE: this might be easier if all the settings were nested under
6758
// e.g. `"nix.languageServer" or something like that, to deduplicate keys
68-
return (
69-
change.affectsConfiguration("nix.serverPath") ||
70-
change.affectsConfiguration("nix.enableLanguageServer")
71-
);
59+
return change.affectsConfiguration("nix.serverPath") || change.affectsConfiguration("nix.enableLanguageServer");
7260
}
7361
}
7462
export const config = new Config();

src/extension.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { startLinting } from "./linter";
99
* Activate this extension.
1010
*
1111
* If LSP is enabled
12-
* then support IDE features with {@link https://github.com/oxalica/nil|nil}
12+
* then support IDE features with an available LSP
1313
* Else
1414
* Format with nixpkgs-format
1515
* validate with nix-instantiate
@@ -18,13 +18,10 @@ import { startLinting } from "./linter";
1818
* @return A promise for the initialization
1919
*/
2020
export async function activate(context: ExtensionContext): Promise<void> {
21-
if (config.LSPEnabled) {
22-
try {
23-
await client.activate(context);
24-
} catch (err) {
25-
console.error(err);
26-
}
27-
} else {
21+
try {
22+
await client.activate(context);
23+
} catch (err) {
24+
console.error(err);
2825
await startLinting(context);
2926
const subs = [
3027
vscode.languages.registerDocumentFormattingEditProvider,
@@ -34,14 +31,15 @@ export async function activate(context: ExtensionContext): Promise<void> {
3431
}
3532

3633
context.subscriptions.push(
37-
vscode.commands.registerCommand(
38-
"nix-ide.restartLanguageServer",
39-
async () => {
40-
if (config.LSPEnabled) {
41-
await client.restart(context);
42-
}
43-
},
44-
),
34+
vscode.commands.registerCommand("nix-ide.restartLanguageServer", async () => {
35+
if (config.LSPEnabled) {
36+
await client.restart(context);
37+
} else {
38+
await client.activate(context).catch(async () => {
39+
await startLinting(context);
40+
});
41+
}
42+
}),
4543
);
4644

4745
vscode.workspace.onDidChangeConfiguration(async (event) => {

0 commit comments

Comments
 (0)