-
-
Notifications
You must be signed in to change notification settings - Fork 410
/
nodeClientMain.ts
169 lines (144 loc) · 5.02 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
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) {
const volarLabs = createLabsInfo(serverLib);
volarLabs.extensionExports.volarLabs.codegenStackSupport = true;
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');
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
},
};
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 since v2. Please enable the "TypeScript and JavaScript Language Features" extension.',
'Show Extension'
).then((selected) => {
if (selected) {
vscode.commands.executeCommand('workbench.extensions.search', '@builtin typescript-language-features');
}
});
}
if (vueTsPluginExtension) {
vscode.window.showWarningMessage(
`The "${vueTsPluginExtension.packageJSON.displayName}" extension is no longer needed since v2. Please uninstall it.`,
'Show Extension'
).then((selected) => {
if (selected) {
vscode.commands.executeCommand('workbench.extensions.search', vueTsPluginExtension.id);
}
});
}
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] });
const { hybridMode } = config.server;
// @ts-expect-error
fs.readFileSync = (...args) => {
if (args[0] === extensionJsPath) {
// @ts-expect-error
let text = readFileSync(...args) as string;
// VSCode < 1.87.0
text = text.replace('t.$u=[t.$r,t.$s,t.$p,t.$q]', s => s + '.concat("vue")'); // patch jsTsLanguageModes
text = text.replace('.languages.match([t.$p,t.$q,t.$r,t.$s]', s => s + '.concat("vue")'); // patch isSupportedLanguageMode
// VSCode >= 1.87.0
text = text.replace('t.jsTsLanguageModes=[t.javascript,t.javascriptreact,t.typescript,t.typescriptreact]', s => s + '.concat("vue")'); // patch jsTsLanguageModes
text = text.replace('.languages.match([t.typescript,t.typescriptreact,t.javascript,t.javascriptreact]', s => s + '.concat("vue")'); // patch isSupportedLanguageMode
if (!hybridMode) {
// patch readPlugins
text = text.replace(
'languages:Array.isArray(e.languages)',
[
'languages:',
`e.name==='typescript-vue-plugin-bundle'?[]:`,
'Array.isArray(e.languages)',
].join(''),
);
}
return text;
}
// @ts-expect-error
return readFileSync(...args);
};
} catch { }