-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
nodeClientMain.ts
168 lines (147 loc) · 4.98 KB
/
nodeClientMain.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as vscode from 'vscode';
import * as lsp from 'vscode-languageclient/node';
import { activate as commonActivate, deactivate as commonDeactivate, getDocumentSelector } from './common';
import { middleware } from './middleware';
import * as serverLib from '@vue/language-server';
import { config } from './config';
import { ExportsInfoForLabs, supportLabsVersion } from '@volar/vscode';
export async function activate(context: vscode.ExtensionContext) {
const cancellationPipeName = path.join(os.tmpdir(), `vscode-${context.extension.id}-cancellation-pipe.tmp`);
const documentSelector = getDocumentSelector(context, serverLib.ServerMode.Semantic);
const languageClients: lsp.LanguageClient[] = [];
let cancellationPipeUpdateKey: string | undefined;
let serverPathStatusItem: vscode.StatusBarItem | undefined;
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument((e) => {
let key = e.document.uri.toString() + '|' + e.document.version;
if (cancellationPipeUpdateKey === undefined) {
cancellationPipeUpdateKey = key;
return;
}
if (documentSelector.some(filter => filter.language === e.document.languageId) && cancellationPipeUpdateKey !== key) {
cancellationPipeUpdateKey = key;
fs.writeFileSync(cancellationPipeName, '');
}
}));
await commonActivate(context, (
id,
name,
documentSelector,
initOptions,
port,
outputChannel
) => {
initOptions.cancellationPipeName = cancellationPipeName;
class _LanguageClient extends lsp.LanguageClient {
fillInitializeParams(params: lsp.InitializeParams) {
// fix https://github.com/vuejs/language-tools/issues/1959
params.locale = vscode.env.language;
}
}
let serverModule = vscode.Uri.joinPath(context.extensionUri, 'server.js');
if (config.server.path) {
try {
const roots = (vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.fsPath);
const serverPath = require.resolve(config.server.path, { paths: roots });
serverModule = vscode.Uri.file(serverPath);
if (!serverPathStatusItem) {
serverPathStatusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
serverPathStatusItem.text = '[vue] configured server path';
serverPathStatusItem.command = 'volar.action.gotoServerFile';
serverPathStatusItem.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
serverPathStatusItem.show();
vscode.commands.registerCommand(serverPathStatusItem.command, () => {
vscode.window.showTextDocument(serverModule);
});
}
} catch (err) {
vscode.window.showWarningMessage(`Cannot find vue language server path: ${config.server.path}`);
}
}
const runOptions: lsp.ForkOptions = {};
if (config.server.maxOldSpaceSize) {
runOptions.execArgv ??= [];
runOptions.execArgv.push("--max-old-space-size=" + config.server.maxOldSpaceSize);
}
const debugOptions: lsp.ForkOptions = { execArgv: ['--nolazy', '--inspect=' + port] };
let serverOptions: lsp.ServerOptions = {
run: {
module: serverModule.fsPath,
transport: lsp.TransportKind.ipc,
options: runOptions
},
debug: {
module: serverModule.fsPath,
transport: lsp.TransportKind.ipc,
options: debugOptions
},
};
if (config.server.runtime === 'bun') {
serverOptions = {
run: {
transport: {
kind: lsp.TransportKind.socket,
port,
},
options: runOptions,
command: 'bun',
args: ['--bun', 'run', serverModule.fsPath],
},
debug: {
transport: {
kind: lsp.TransportKind.socket,
port,
},
options: debugOptions,
command: 'bun',
args: ['--bun', 'run', serverModule.fsPath],
},
};
}
const clientOptions: lsp.LanguageClientOptions = {
middleware,
documentSelector: documentSelector,
initializationOptions: initOptions,
outputChannel
};
const client = new _LanguageClient(
id,
name,
serverOptions,
clientOptions,
);
client.start();
languageClients.push(client);
updateProviders(client);
return client;
});
return {
volarLabs: {
version: supportLabsVersion,
codegenStackSupport: true,
languageClients,
languageServerProtocol: serverLib,
},
} satisfies ExportsInfoForLabs;
}
export function deactivate(): Thenable<any> | undefined {
return commonDeactivate();
}
function updateProviders(client: lsp.LanguageClient) {
const initializeFeatures = (client as any).initializeFeatures;
(client as any).initializeFeatures = (...args: any) => {
const capabilities = (client as any)._capabilities as lsp.ServerCapabilities;
if (!config.codeActions.enabled) {
capabilities.codeActionProvider = undefined;
}
if (!config.codeLens.enabled) {
capabilities.codeLensProvider = undefined;
}
if (!config.updateImportsOnFileMove.enabled && capabilities.workspace?.fileOperations?.willRename) {
capabilities.workspace.fileOperations.willRename = undefined;
}
return initializeFeatures.call(client, ...args);
};
}