Skip to content

Commit ca11f53

Browse files
committed
feat: don't update TS plugin automatically
1 parent f3bb2ef commit ca11f53

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"volar.codeLens.pugTools": {
8787
"type": "boolean",
8888
"default": true,
89-
"description": "[html ☑ | pug ☐] code lens."
89+
"description": "[pug ☐] code lens."
9090
},
9191
"volar.codeLens.scriptSetupTools": {
9292
"type": "boolean",
@@ -270,7 +270,7 @@
270270
"icon": "$(open-preview)"
271271
},
272272
{
273-
"command": "volar.action.switchTsPlugin",
273+
"command": "volar.action.toggleTsPlugin",
274274
"title": "Switch TS Plugin on / off",
275275
"category": "Volar"
276276
},

packages/vscode-client/src/extension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function createLanguageService(context: vscode.ExtensionContext, mode: 'api' | '
6666
mode: mode,
6767
appRoot: vscode.env.appRoot,
6868
language: vscode.env.language,
69-
tsPlugin: tsPlugin.isPluginEnabled(),
69+
tsPlugin: tsPlugin.isTsPluginEnabled(),
7070
};
7171
const clientOptions: lsp.LanguageClientOptions = {
7272
documentSelector: fileOnly ?

packages/vscode-client/src/features/tsPlugin.ts

+43-49
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,56 @@ import { userPick } from './splitEditors';
55

66
export async function activate(context: vscode.ExtensionContext) {
77

8-
const tsPluginEnabled = isPluginEnabled();
98
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
10-
statusBar.command = 'volar.action.switchTsPlugin';
11-
onConfigUpdated();
9+
statusBar.command = 'volar.action.toggleTsPlugin';
10+
updateTsPlugin();
11+
updateTsPluginStatus();
1212

13-
context.subscriptions.push(vscode.commands.registerCommand('volar.action.switchTsPlugin', async () => {
14-
const options = new Map<number, string>();
15-
const tsPluginStatus = getTsPluginStatus();
16-
options.set(0, (tsPluginStatus === null ? '• ' : '') + `Don't care (Don't reload VSCode)`);
17-
options.set(1, (tsPluginStatus === true ? '• ' : '') + 'Enable TS Plugin');
18-
options.set(2, (tsPluginStatus === false ? '• ' : '') + 'Disable TS Plugin');
19-
options.set(3, 'Hide TS Plugin Status (or config "volar.tsPluginStatus")');
13+
context.subscriptions.push(vscode.commands.registerCommand('volar.action.toggleTsPlugin', async () => {
14+
const options = new Map<boolean | number, string>();
15+
const _isTsPluginEnabled = isTsPluginEnabled();
16+
options.set(true, (_isTsPluginEnabled === true ? '• ' : '') + 'Enable TS Plugin');
17+
options.set(false, (_isTsPluginEnabled === false ? '• ' : '') + 'Disable TS Plugin');
2018

2119
const select = await userPick(options);
2220
if (select === undefined) return; // cancle
2321

24-
if (select === 0) {
25-
await vscode.workspace.getConfiguration('volar').update('tsPlugin', null);
26-
}
27-
if (select === 1) {
28-
await vscode.workspace.getConfiguration('volar').update('tsPlugin', true);
29-
}
30-
if (select === 2) {
31-
await vscode.workspace.getConfiguration('volar').update('tsPlugin', false);
32-
}
33-
if (select === 3) {
34-
await vscode.workspace.getConfiguration('volar').update('tsPluginStatus', false);
22+
if (select !== _isTsPluginEnabled) {
23+
toggleTsPlugin();
3524
}
3625
}));
37-
vscode.workspace.onDidChangeConfiguration(onConfigUpdated);
26+
vscode.workspace.onDidChangeConfiguration(updateTsPlugin);
27+
vscode.workspace.onDidChangeConfiguration(updateTsPluginStatus);
3828

39-
function onConfigUpdated() {
40-
const tsPluginStatus = getTsPluginStatus();
41-
if (tsPluginStatus !== null && tsPluginStatus !== tsPluginEnabled) {
42-
switchTsPlugin();
29+
async function updateTsPlugin() {
30+
const shouldTsPluginEnabled = getTsPluginConfig();
31+
const _isTsPluginEnabled = isTsPluginEnabled();
32+
if (shouldTsPluginEnabled !== null && shouldTsPluginEnabled !== _isTsPluginEnabled) {
33+
const msg = shouldTsPluginEnabled
34+
? `Workspace using TS plugin but it's disabled, do you want to turn it on?`
35+
: `Workspace unused TS plugin but it's enabled, do you want to turn it off?`;
36+
const btnText = shouldTsPluginEnabled ? 'Enable TS Plugin' : 'Disable TS Plugin';
37+
const toggle = await vscode.window.showInformationMessage(msg, btnText);
38+
if (toggle === btnText) {
39+
toggleTsPlugin();
40+
}
4341
}
44-
updateStatusBar();
45-
if (vscode.workspace.getConfiguration('volar').get<boolean>('tsPluginStatus')) {
42+
}
43+
function updateTsPluginStatus() {
44+
if (getTsPluginStatusConfig()) {
45+
if (isTsPluginEnabled()) {
46+
statusBar.text = 'Vue TS Plugin ☑';
47+
}
48+
else {
49+
statusBar.text = 'Vue TS Plugin ☐';
50+
}
4651
statusBar.show();
4752
}
4853
else {
4954
statusBar.hide();
5055
}
5156
}
52-
function switchTsPlugin() {
57+
function toggleTsPlugin() {
5358
const volar = vscode.extensions.getExtension('johnsoncodehk.volar');
5459
if (!volar) {
5560
vscode.window.showWarningMessage('Extension [Volar - johnsoncodehk.volar] not found.');
@@ -62,12 +67,12 @@ export async function activate(context: vscode.ExtensionContext) {
6267
if (packageText.indexOf(`"typescriptServerPlugins-off"`) >= 0) {
6368
const newText = packageText.replace(`"typescriptServerPlugins-off"`, `"typescriptServerPlugins"`);
6469
fs.writeFileSync(packageJson, newText, 'utf8');
65-
showReload(true);
70+
showReload();
6671
}
6772
else if (packageText.indexOf(`"typescriptServerPlugins"`) >= 0) {
6873
const newText = packageText.replace(`"typescriptServerPlugins"`, `"typescriptServerPlugins-off"`);
6974
fs.writeFileSync(packageJson, newText, 'utf8');
70-
showReload(false);
75+
showReload();
7176
}
7277
else {
7378
vscode.window.showWarningMessage('Unknow package.json status.');
@@ -77,28 +82,14 @@ export async function activate(context: vscode.ExtensionContext) {
7782
vscode.window.showWarningMessage('Volar package.json update failed.');
7883
}
7984
}
80-
async function showReload(enabled: boolean) {
81-
const reload = await vscode.window.showInformationMessage(`Volar TS Plugin ${enabled ? 'enabled' : 'disabled'}, please reload VSCode to refresh TS Server.`, 'Reload Window');
85+
async function showReload() {
86+
const reload = await vscode.window.showInformationMessage('Please reload VSCode to restart TS Server.', 'Reload Window');
8287
if (reload === undefined) return; // cancel
8388
vscode.commands.executeCommand('workbench.action.reloadWindow');
8489
}
85-
function updateStatusBar() {
86-
if (tsPluginEnabled) {
87-
statusBar.text = '[Volar] TS Plugin: On';
88-
statusBar.color = undefined;
89-
}
90-
else {
91-
statusBar.text = '[Volar] TS Plugin: Off';
92-
statusBar.color = new vscode.ThemeColor('titleBar.inactiveForeground');
93-
}
94-
const tsPluginStatus = getTsPluginStatus();
95-
if (tsPluginStatus !== null && tsPluginStatus !== tsPluginEnabled) {
96-
statusBar.text += ' -> ' + (tsPluginStatus ? 'On' : 'Off');
97-
}
98-
}
9990
}
10091

101-
export function isPluginEnabled() {
92+
export function isTsPluginEnabled() {
10293
const volar = vscode.extensions.getExtension('johnsoncodehk.volar');
10394
if (!volar) {
10495
return false;
@@ -114,6 +105,9 @@ export function isPluginEnabled() {
114105

115106
return false;
116107
}
117-
function getTsPluginStatus() {
108+
function getTsPluginConfig() {
118109
return vscode.workspace.getConfiguration('volar').get<boolean | null>('tsPlugin');
119110
}
111+
function getTsPluginStatusConfig() {
112+
return vscode.workspace.getConfiguration('volar').get<boolean>('tsPluginStatus');
113+
}

0 commit comments

Comments
 (0)