-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
client.ts
144 lines (125 loc) · 4.39 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { DiagnosticModel, InitializationOptions } from '@volar/language-server';
import * as protocol from '@volar/language-server/protocol';
import {
activateAutoInsertion,
activateFindFileReferences,
activateReloadProjects,
activateTsConfigStatusItem,
activateTsVersionStatusItem,
supportLabsVersion,
type ExportsInfoForLabs,
} from '@volar/vscode';
import * as path from 'node:path';
import * as vscode from 'vscode';
import * as lsp from 'vscode-languageclient/node';
let client: lsp.BaseLanguageClient;
export async function activate(context: vscode.ExtensionContext): Promise<ExportsInfoForLabs> {
const runtimeConfig = vscode.workspace.getConfiguration('astro.language-server');
const { workspaceFolders } = vscode.workspace;
const rootPath = workspaceFolders?.[0].uri.fsPath;
let lsPath = await getConfiguredServerPath(context.workspaceState);
if (typeof lsPath === 'string' && lsPath.trim() !== '' && typeof rootPath === 'string') {
lsPath = path.isAbsolute(lsPath) ? lsPath : path.join(rootPath, lsPath);
console.info(`Using language server at ${lsPath}`);
} else {
lsPath = undefined;
}
const serverModule = lsPath
? require.resolve(lsPath)
: vscode.Uri.joinPath(context.extensionUri, 'dist/node/server.js').fsPath;
const runOptions = { execArgv: [] };
const debugOptions = { execArgv: ['--nolazy', '--inspect=' + 6009] };
const serverOptions: lsp.ServerOptions = {
run: {
module: serverModule,
transport: lsp.TransportKind.ipc,
options: runOptions,
},
debug: {
module: serverModule,
transport: lsp.TransportKind.ipc,
options: debugOptions,
},
};
const serverRuntime = runtimeConfig.get<string>('runtime');
if (serverRuntime) {
serverOptions.run.runtime = serverRuntime;
serverOptions.debug.runtime = serverRuntime;
console.info(`Using ${serverRuntime} as runtime`);
}
const initializationOptions: InitializationOptions = {
typescript: {
tsdk: require('path').join(vscode.env.appRoot, 'extensions/node_modules/typescript/lib'),
},
diagnosticModel: DiagnosticModel.Pull,
};
const clientOptions: lsp.LanguageClientOptions = {
documentSelector: [{ language: 'astro' }],
initializationOptions,
};
client = new lsp.LanguageClient('astro', 'Astro Language Server', serverOptions, clientOptions);
await client.start();
// support for auto close tag
activateAutoInsertion([client], (document) => document.languageId === 'astro');
activateFindFileReferences('astro.findFileReferences', client);
activateReloadProjects('astro.reloadProjects', [client]);
activateTsConfigStatusItem('astro.openTsConfig', client, () => false);
activateTsVersionStatusItem(
'astro.selectTypescriptVersion',
context,
client,
(document) => document.languageId === 'astro',
(text) => text,
true
);
return {
volarLabs: {
version: supportLabsVersion,
languageClients: [client],
languageServerProtocol: protocol,
},
};
}
export function deactivate(): Thenable<any> | undefined {
return client?.stop();
}
async function getConfiguredServerPath(workspaceState: vscode.Memento) {
const scope = 'astro.language-server';
const detailedLSPath = vscode.workspace.getConfiguration(scope).inspect<string>('ls-path');
const lsPath =
detailedLSPath?.globalLanguageValue ||
detailedLSPath?.defaultLanguageValue ||
detailedLSPath?.globalValue ||
detailedLSPath?.defaultValue;
const workspaceLSPath =
detailedLSPath?.workspaceFolderLanguageValue ||
detailedLSPath?.workspaceLanguageValue ||
detailedLSPath?.workspaceFolderValue ||
detailedLSPath?.workspaceValue;
const useLocalLanguageServerKey = `${scope}.useLocalLS`;
let useWorkspaceServer = workspaceState.get<boolean>(useLocalLanguageServerKey);
if (useWorkspaceServer === undefined && workspaceLSPath !== undefined) {
const msg =
'This workspace contains an Astro Language Server version. Would you like to use the workplace version?';
const allowPrompt = 'Allow';
const dismissPrompt = 'Dismiss';
const neverPrompt = 'Never in This Workspace';
const result = await vscode.window.showInformationMessage(
msg,
allowPrompt,
dismissPrompt,
neverPrompt
);
if (result === allowPrompt) {
await workspaceState.update(useLocalLanguageServerKey, true);
useWorkspaceServer = true;
} else if (result === neverPrompt) {
await workspaceState.update(useLocalLanguageServerKey, false);
}
}
if (useWorkspaceServer === true) {
return workspaceLSPath || lsPath;
} else {
return lsPath;
}
}