/
tsVersion.ts
164 lines (145 loc) · 5.24 KB
/
tsVersion.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
import * as vscode from 'vscode';
import { BaseLanguageClient } from 'vscode-languageclient';
import * as shared from '@volar/shared';
import { userPick } from './splitEditors';
import { takeOverModeEnabled } from '../common';
const defaultTsdk = 'node_modules/typescript/lib';
export async function activate(context: vscode.ExtensionContext, clients: BaseLanguageClient[]) {
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
statusBar.command = 'volar.selectTypeScriptVersion';
const subscription = vscode.commands.registerCommand('volar.selectTypeScriptVersion', async () => {
const useWorkspaceTsdk = getCurrentTsPaths(context).isWorkspacePath;
const workspaceTsPaths = getWorkspaceTsPaths();
const workspaceTsVersion = workspaceTsPaths ? shared.getTypeScriptVersion(workspaceTsPaths.serverPath) : undefined;
const vscodeTsPaths = getVscodeTsPaths();
const vscodeTsVersion = shared.getTypeScriptVersion(vscodeTsPaths.serverPath);
const tsdk = getTsdk();
const defaultTsServer = shared.getWorkspaceTypescriptPath(defaultTsdk, (vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.fsPath));
const defaultTsVersion = defaultTsServer ? shared.getTypeScriptVersion(defaultTsServer) : undefined;
const options: Record<string, vscode.QuickPickItem> = {};
options[0] = {
label: (!useWorkspaceTsdk ? '• ' : '') + "Use VS Code's Version",
description: vscodeTsVersion,
};
if (tsdk) {
options[1] = {
label: (useWorkspaceTsdk ? '• ' : '') + 'Use Workspace Version',
description: workspaceTsVersion ?? 'Could not load the TypeScript version at this path',
detail: tsdk,
};
}
if (tsdk !== defaultTsdk) {
options[2] = {
label: (useWorkspaceTsdk ? '• ' : '') + 'Use Workspace Version',
description: defaultTsVersion ?? 'Could not load the TypeScript version at this path',
detail: defaultTsdk,
};
}
if (takeOverModeEnabled()) {
options[3] = {
label: 'What is Takeover Mode?',
};
}
const select = await userPick(options);
if (select === undefined)
return; // cancel
if (select === '3') {
vscode.env.openExternal(vscode.Uri.parse('https://vuejs.org/guide/typescript/overview.html#takeover-mode'));
return;
}
if (select === '2') {
vscode.workspace.getConfiguration('typescript').update('tsdk', defaultTsdk);
}
const nowUseWorkspaceTsdk = select !== '0';
if (nowUseWorkspaceTsdk !== isUseWorkspaceTsdk(context)) {
context.workspaceState.update('typescript.useWorkspaceTsdk', nowUseWorkspaceTsdk);
reloadServers();
}
updateStatusBar();
});
context.subscriptions.push(subscription);
let tsdk = getTsdk();
vscode.workspace.onDidChangeConfiguration(() => {
const newTsdk = getTsdk();
if (newTsdk !== tsdk) {
tsdk = newTsdk;
if (isUseWorkspaceTsdk(context)) {
reloadServers();
}
}
});
updateStatusBar();
vscode.window.onDidChangeActiveTextEditor(updateStatusBar, undefined, context.subscriptions);
function updateStatusBar() {
if (
vscode.window.activeTextEditor?.document.languageId !== 'vue'
&& vscode.window.activeTextEditor?.document.languageId !== 'markdown'
&& !(
takeOverModeEnabled()
&& vscode.window.activeTextEditor
&& ['javascript', 'typescript', 'javascriptreact', 'typescriptreact'].includes(vscode.window.activeTextEditor.document.languageId)
)
) {
statusBar.hide();
}
else {
const tsPaths = getCurrentTsPaths(context);
const tsVersion = shared.getTypeScriptVersion(tsPaths.serverPath);
statusBar.text = 'TS ' + tsVersion;
if (takeOverModeEnabled()) {
statusBar.text += ' (takeover)';
}
statusBar.show();
}
}
async function reloadServers() {
const tsPaths = getCurrentTsPaths(context);
for (const client of clients) {
const newInitOptions: shared.ServerInitializationOptions = {
...client.clientOptions.initializationOptions,
typescript: tsPaths,
};
client.clientOptions.initializationOptions = newInitOptions;
}
vscode.commands.executeCommand('volar.action.restartServer');
}
}
export function getCurrentTsPaths(context: vscode.ExtensionContext) {
if (isUseWorkspaceTsdk(context)) {
const workspaceTsPaths = getWorkspaceTsPaths(true);
if (workspaceTsPaths) {
return { ...workspaceTsPaths, isWorkspacePath: true };
}
}
return { ...getVscodeTsPaths(), isWorkspacePath: false };
}
function getWorkspaceTsPaths(useDefault = false) {
let tsdk = getTsdk();
if (!tsdk && useDefault) {
tsdk = defaultTsdk;
}
if (tsdk) {
const fsPaths = (vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.fsPath);
const tsPath = shared.getWorkspaceTypescriptPath(tsdk, fsPaths);
if (tsPath) {
return {
serverPath: tsPath,
localizedPath: shared.getWorkspaceTypescriptLocalizedPath(tsdk, vscode.env.language, fsPaths),
};
}
}
}
function getVscodeTsPaths() {
return {
serverPath: shared.getVscodeTypescriptPath(vscode.env.appRoot),
localizedPath: shared.getVscodeTypescriptLocalizedPath(vscode.env.appRoot, vscode.env.language),
};
}
function getTsdk() {
const tsConfigs = vscode.workspace.getConfiguration('typescript');
const tsdk = tsConfigs.get<string>('tsdk');
return tsdk;
}
function isUseWorkspaceTsdk(context: vscode.ExtensionContext) {
return context.workspaceState.get('typescript.useWorkspaceTsdk', false);
}