/
common.ts
256 lines (229 loc) · 8.11 KB
/
common.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import * as shared from '@volar/shared';
import * as vscode from 'vscode';
import * as lsp from 'vscode-languageclient';
import * as activeSelection from './features/activeSelection';
import * as attrNameCase from './features/attrNameCase';
import * as callGraph from './features/callGraph';
import * as createWorkspaceSnippets from './features/createWorkspaceSnippets';
import * as documentVersion from './features/documentVersion';
import * as documentContent from './features/documentContent';
import * as preview from './features/preview';
import * as showReferences from './features/showReferences';
import * as splitEditors from './features/splitEditors';
import * as autoInsertion from './features/autoInsertion';
import * as tagNameCase from './features/tagNameCase';
import * as tsVersion from './features/tsVersion';
import * as verifyAll from './features/verifyAll';
import * as virtualFiles from './features/virtualFiles';
import * as tsconfig from './features/tsconfig';
let apiClient: lsp.CommonLanguageClient;
let docClient: lsp.CommonLanguageClient | undefined;
let htmlClient: lsp.CommonLanguageClient;
type CreateLanguageClient = (
id: string,
name: string,
documentSelector: lsp.DocumentSelector,
initOptions: shared.ServerInitializationOptions,
port: number,
) => lsp.CommonLanguageClient;
export async function activate(context: vscode.ExtensionContext, createLc: CreateLanguageClient) {
const stopCheck = vscode.window.onDidChangeActiveTextEditor(tryActivate);
tryActivate();
function tryActivate() {
if (!vscode.window.activeTextEditor) {
// onWebviewPanel:preview
doActivate(context, createLc);
stopCheck.dispose();
return;
}
const currentlangId = vscode.window.activeTextEditor.document.languageId;
if (currentlangId === 'vue') {
doActivate(context, createLc);
stopCheck.dispose();
}
const takeOverMode = takeOverModeEnabled();
if (takeOverMode && ['javascript', 'typescript', 'javascriptreact', 'typescriptreact'].includes(currentlangId)) {
doActivate(context, createLc);
stopCheck.dispose();
}
}
}
async function doActivate(context: vscode.ExtensionContext, createLc: CreateLanguageClient) {
vscode.commands.executeCommand('setContext', 'volar.activated', true);
const lowPowerMode = lowPowerModeEnabled();
if (lowPowerMode) {
vscode.window
.showInformationMessage('Low Power Mode Enabled.', 'Disable')
.then(option => {
if (option !== undefined) {
vscode.commands.executeCommand('workbench.action.openSettings', 'volar.lowPowerMode');
}
});
}
const takeOverMode = takeOverModeEnabled();
if (takeOverMode) {
vscode.window
.showInformationMessage('Take Over Mode enabled.', 'What is Take Over Mode?')
.then(option => {
if (option !== undefined) {
vscode.env.openExternal(vscode.Uri.parse('https://github.com/johnsoncodehk/volar/discussions/471'));
}
});
}
const languageFeaturesDocumentSelector: lsp.DocumentSelector = takeOverMode ?
[
{ scheme: 'file', language: 'vue' },
{ scheme: 'file', language: 'javascript' },
{ scheme: 'file', language: 'typescript' },
{ scheme: 'file', language: 'javascriptreact' },
{ scheme: 'file', language: 'typescriptreact' },
{ scheme: 'file', language: 'json' },
] : [
{ scheme: 'file', language: 'vue' },
];
const documentFeaturesDocumentSelector: lsp.DocumentSelector = takeOverMode ?
[
{ language: 'vue' },
{ language: 'javascript' },
{ language: 'typescript' },
{ language: 'javascriptreact' },
{ language: 'typescriptreact' },
] : [
{ language: 'vue' },
];
apiClient = createLc(
'volar-api',
'Volar - API',
languageFeaturesDocumentSelector,
getInitializationOptions(context, 'api', undefined, lowPowerMode),
6009,
);
docClient = !lowPowerMode ? createLc(
'volar-document',
'Volar - Document',
languageFeaturesDocumentSelector,
getInitializationOptions(context, 'doc', undefined, lowPowerMode),
6010,
) : undefined;
htmlClient = createLc(
'volar-html',
'Volar - HTML',
documentFeaturesDocumentSelector,
getInitializationOptions(context, 'html', undefined, lowPowerMode),
6011,
);
const clients = [apiClient, docClient, htmlClient].filter(shared.notEmpty);
registarLowPowerModeChange();
registarRestartRequest();
registarClientRequests();
splitEditors.activate(context);
preview.activate(context);
createWorkspaceSnippets.activate(context);
callGraph.activate(context, apiClient);
verifyAll.activate(context, docClient ?? apiClient);
virtualFiles.activate(context, docClient ?? apiClient);
autoInsertion.activate(context, htmlClient, apiClient);
tsVersion.activate(context, [apiClient, docClient].filter(shared.notEmpty));
tsconfig.activate(context, docClient ?? apiClient);
async function registarLowPowerModeChange() {
vscode.workspace.onDidChangeConfiguration(async () => {
const nowIsLowPowerMode = lowPowerModeEnabled();
if (lowPowerMode !== nowIsLowPowerMode) {
const reload = await vscode.window.showInformationMessage('Please reload VSCode to switch low power mode.', 'Reload Window');
if (reload === undefined) return; // cancel
vscode.commands.executeCommand('workbench.action.reloadWindow');
}
});
}
async function registarRestartRequest() {
await Promise.all(clients.map(client => client.onReady()));
context.subscriptions.push(vscode.commands.registerCommand('volar.action.restartServer', async () => {
await Promise.all(clients.map(client => client.stop()));
await Promise.all(clients.map(client => client.start()));
registarClientRequests();
}));
}
function registarClientRequests() {
for (const client of clients) {
showReferences.activate(context, client);
documentVersion.activate(context, client);
documentContent.activate(context, client);
activeSelection.activate(context, client);
}
(async () => {
const getTagNameCase = await tagNameCase.activate(context, apiClient);
const getAttrNameCase = await attrNameCase.activate(context, apiClient);
apiClient.onRequest(shared.GetDocumentNameCasesRequest.type, async handler => ({
tagNameCase: getTagNameCase(handler.uri),
attrNameCase: getAttrNameCase(handler.uri),
}));
})();
}
}
export function deactivate(): Thenable<any> | undefined {
return Promise.all([
apiClient?.stop(),
docClient?.stop(),
htmlClient?.stop(),
].filter(shared.notEmpty));
}
export function takeOverModeEnabled() {
const status = vscode.workspace.getConfiguration('volar').get<boolean | 'auto'>('takeOverMode.enabled');
if (status === 'auto') {
return !vscode.extensions.getExtension('vscode.typescript-language-features');
}
return status;
}
function lowPowerModeEnabled() {
return !!vscode.workspace.getConfiguration('volar').get<boolean>('lowPowerMode');
}
function getInitializationOptions(
context: vscode.ExtensionContext,
mode: 'api' | 'doc' | 'html',
initMessage: string | undefined,
lowPowerMode: boolean,
) {
const initializationOptions: shared.ServerInitializationOptions = {
typescript: tsVersion.getCurrentTsPaths(context),
languageFeatures: (mode === 'api' || mode === 'doc') ? {
...(mode === 'api' ? {
references: true,
implementation: true,
definition: true,
typeDefinition: true,
callHierarchy: true,
hover: true,
rename: true,
renameFileRefactoring: true,
signatureHelp: true,
codeAction: true,
workspaceSymbol: true,
completion: {
defaultTagNameCase: 'both',
defaultAttrNameCase: 'kebabCase',
getDocumentNameCasesRequest: true,
getDocumentSelectionRequest: true,
},
schemaRequestService: { getDocumentContentRequest: true },
} : {}),
...((mode === 'doc' || (mode === 'api' && lowPowerMode)) ? {
documentHighlight: true,
documentLink: true,
codeLens: { showReferencesNotification: true },
semanticTokens: true,
diagnostics: { getDocumentVersionRequest: true },
schemaRequestService: { getDocumentContentRequest: true },
} : {}),
} : undefined,
documentFeatures: mode === 'html' ? {
selectionRange: true,
foldingRange: true,
linkedEditingRange: true,
documentSymbol: true,
documentColor: true,
documentFormatting: true,
} : undefined,
initializationMessage: initMessage,
};
return initializationOptions;
}