-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathnodeClientMain.ts
198 lines (172 loc) · 5.8 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { createLabsInfo } from '@volar/vscode';
import * as serverLib from '@vue/language-server';
import * as fs from 'fs';
import * as vscode from 'vscode';
import * as lsp from 'vscode-languageclient/node';
import { activate as commonActivate, deactivate as commonDeactivate } from './common';
import { config } from './config';
import { middleware } from './middleware';
export async function activate(context: vscode.ExtensionContext) {
let serverPathStatusItem: vscode.StatusBarItem | undefined;
await commonActivate(context, (
id,
name,
documentSelector,
initOptions,
port,
outputChannel
) => {
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 = 'vue.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,
markdown: {
isTrusted: true,
supportHtml: true,
},
outputChannel
};
const client = new _LanguageClient(
id,
name,
serverOptions,
clientOptions,
);
client.start();
volarLabs.addLanguageClient(client);
updateProviders(client);
return client;
});
const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features');
const vueTsPluginExtension = vscode.extensions.getExtension('Vue.vscode-typescript-vue-plugin');
if (tsExtension) {
await tsExtension.activate();
}
else {
vscode.window.showWarningMessage(
'Takeover mode is no longer needed in version 2.0. Please enable the "TypeScript and JavaScript Language Features" extension.',
'Show Extension'
).then((selected) => {
if (selected) {
vscode.commands.executeCommand('workbench.extensions.search', '@builtin TypeScript and JavaScript Language Features');
}
});
}
if (vueTsPluginExtension) {
vscode.window.showWarningMessage(
`The "${vueTsPluginExtension.packageJSON.displayName}" extension is no longer needed in version 2.0. Please uninstall it.`,
'Show Extension'
).then((selected) => {
if (selected) {
vscode.commands.executeCommand('workbench.extensions.search', vueTsPluginExtension.id);
}
});
}
const volarLabs = createLabsInfo(serverLib);
volarLabs.extensionExports.volarLabs.codegenStackSupport = true;
return volarLabs.extensionExports;
}
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);
};
}
try {
const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features')!;
const readFileSync = fs.readFileSync;
const extensionJsPath = require.resolve('./dist/extension.js', { paths: [tsExtension.extensionPath] });
// @ts-expect-error
fs.readFileSync = (...args) => {
if (args[0] === extensionJsPath) {
// @ts-expect-error
let text = readFileSync(...args) as string;
// patch jsTsLanguageModes
text = text.replace('t.$u=[t.$r,t.$s,t.$p,t.$q]', s => s + '.concat("vue")');
// patch isSupportedLanguageMode
text = text.replace('.languages.match([t.$p,t.$q,t.$r,t.$s]', s => s + '.concat("vue")');
return text;
}
// @ts-expect-error
return readFileSync(...args);
};
} catch { }