@@ -5,51 +5,56 @@ import { userPick } from './splitEditors';
5
5
6
6
export async function activate ( context : vscode . ExtensionContext ) {
7
7
8
- const tsPluginEnabled = isPluginEnabled ( ) ;
9
8
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 ( ) ;
12
12
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' ) ;
20
18
21
19
const select = await userPick ( options ) ;
22
20
if ( select === undefined ) return ; // cancle
23
21
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 ( ) ;
35
24
}
36
25
} ) ) ;
37
- vscode . workspace . onDidChangeConfiguration ( onConfigUpdated ) ;
26
+ vscode . workspace . onDidChangeConfiguration ( updateTsPlugin ) ;
27
+ vscode . workspace . onDidChangeConfiguration ( updateTsPluginStatus ) ;
38
28
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
+ }
43
41
}
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
+ }
46
51
statusBar . show ( ) ;
47
52
}
48
53
else {
49
54
statusBar . hide ( ) ;
50
55
}
51
56
}
52
- function switchTsPlugin ( ) {
57
+ function toggleTsPlugin ( ) {
53
58
const volar = vscode . extensions . getExtension ( 'johnsoncodehk.volar' ) ;
54
59
if ( ! volar ) {
55
60
vscode . window . showWarningMessage ( 'Extension [Volar - johnsoncodehk.volar] not found.' ) ;
@@ -62,12 +67,12 @@ export async function activate(context: vscode.ExtensionContext) {
62
67
if ( packageText . indexOf ( `"typescriptServerPlugins-off"` ) >= 0 ) {
63
68
const newText = packageText . replace ( `"typescriptServerPlugins-off"` , `"typescriptServerPlugins"` ) ;
64
69
fs . writeFileSync ( packageJson , newText , 'utf8' ) ;
65
- showReload ( true ) ;
70
+ showReload ( ) ;
66
71
}
67
72
else if ( packageText . indexOf ( `"typescriptServerPlugins"` ) >= 0 ) {
68
73
const newText = packageText . replace ( `"typescriptServerPlugins"` , `"typescriptServerPlugins-off"` ) ;
69
74
fs . writeFileSync ( packageJson , newText , 'utf8' ) ;
70
- showReload ( false ) ;
75
+ showReload ( ) ;
71
76
}
72
77
else {
73
78
vscode . window . showWarningMessage ( 'Unknow package.json status.' ) ;
@@ -77,28 +82,14 @@ export async function activate(context: vscode.ExtensionContext) {
77
82
vscode . window . showWarningMessage ( 'Volar package.json update failed.' ) ;
78
83
}
79
84
}
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' ) ;
82
87
if ( reload === undefined ) return ; // cancel
83
88
vscode . commands . executeCommand ( 'workbench.action.reloadWindow' ) ;
84
89
}
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
- }
99
90
}
100
91
101
- export function isPluginEnabled ( ) {
92
+ export function isTsPluginEnabled ( ) {
102
93
const volar = vscode . extensions . getExtension ( 'johnsoncodehk.volar' ) ;
103
94
if ( ! volar ) {
104
95
return false ;
@@ -114,6 +105,9 @@ export function isPluginEnabled() {
114
105
115
106
return false ;
116
107
}
117
- function getTsPluginStatus ( ) {
108
+ function getTsPluginConfig ( ) {
118
109
return vscode . workspace . getConfiguration ( 'volar' ) . get < boolean | null > ( 'tsPlugin' ) ;
119
110
}
111
+ function getTsPluginStatusConfig ( ) {
112
+ return vscode . workspace . getConfiguration ( 'volar' ) . get < boolean > ( 'tsPluginStatus' ) ;
113
+ }
0 commit comments