3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
+ import { localize } from 'vs/nls' ;
7
+ import { TPromise } from 'vs/base/common/winjs.base' ;
6
8
import { distinct } from 'vs/base/common/arrays' ;
7
9
import { IWorkspaceContextService , IWorkspace } from 'vs/platform/workspace/common/workspace' ;
8
10
import { IExtensionsRuntimeService } from 'vs/platform/extensions/common/extensions' ;
9
11
import { IStorageService , StorageScope } from 'vs/platform/storage/common/storage' ;
10
- import { IChoiceService } from 'vs/platform/message/common/message' ;
12
+ import { IChoiceService , Severity } from 'vs/platform/message/common/message' ;
11
13
12
14
const DISABLED_EXTENSIONS_STORAGE_PATH = 'extensions/disabled' ;
13
15
@@ -28,18 +30,67 @@ export class ExtensionsRuntimeService implements IExtensionsRuntimeService {
28
30
this . workspace = contextService . getWorkspace ( ) ;
29
31
}
30
32
31
- public getDisabledExtensions ( scope ?: StorageScope ) : string [ ] {
33
+ public setEnablement ( identifier : string , enable : boolean , displayName : string ) : TPromise < boolean > {
34
+ const disabled = this . getDisabledExtensionsFromStorage ( ) . indexOf ( identifier ) !== - 1 ;
35
+
36
+ if ( ! enable === disabled ) {
37
+ return TPromise . wrap ( true ) ;
38
+ }
39
+
40
+ if ( ! this . workspace ) {
41
+ return this . setGlobalEnablement ( identifier , enable , displayName ) ;
42
+ }
43
+
44
+ if ( enable ) {
45
+ if ( this . getDisabledExtensionsFromStorage ( StorageScope . GLOBAL ) . indexOf ( identifier ) !== - 1 ) {
46
+ return this . choiceService . choose ( Severity . Info , localize ( 'enableExtensionGlobally' , "Would you like to enable '{0}' extension globally?" , displayName ) ,
47
+ [ localize ( 'yes' , "Yes" ) , localize ( 'no' , "No" ) ] )
48
+ . then ( ( option ) => {
49
+ if ( option === 0 ) {
50
+ return TPromise . join ( [ this . enableExtension ( identifier , StorageScope . GLOBAL ) , this . enableExtension ( identifier , StorageScope . WORKSPACE ) ] ) . then ( ( ) => true ) ;
51
+ }
52
+ return TPromise . wrap ( false ) ;
53
+ } ) ;
54
+ }
55
+ return this . choiceService . choose ( Severity . Info , localize ( 'enableExtensionForWorkspace' , "Would you like to enable '{0}' extension for this workspace?" , displayName ) ,
56
+ [ localize ( 'yes' , "Yes" ) , localize ( 'no' , "No" ) ] )
57
+ . then ( ( option ) => {
58
+ if ( option === 0 ) {
59
+ return this . enableExtension ( identifier , StorageScope . WORKSPACE ) . then ( ( ) => true ) ;
60
+ }
61
+ return TPromise . wrap ( false ) ;
62
+ } ) ;
63
+ } else {
64
+ return this . choiceService . choose ( Severity . Info , localize ( 'disableExtension' , "Would you like to disable '{0}' extension for this workspace or globally?" , displayName ) ,
65
+ [ localize ( 'workspace' , "Workspace" ) , localize ( 'globally' , "Globally" ) , localize ( 'cancel' , "Cancel" ) ] )
66
+ . then ( ( option ) => {
67
+ switch ( option ) {
68
+ case 0 :
69
+ return this . disableExtension ( identifier , StorageScope . WORKSPACE ) ;
70
+ case 1 :
71
+ return this . disableExtension ( identifier , StorageScope . GLOBAL ) ;
72
+ default : return TPromise . wrap ( false ) ;
73
+ }
74
+ } ) ;
75
+ }
76
+ }
77
+
78
+ public getDisabledExtensions ( workspace ?: boolean ) : string [ ] {
32
79
if ( ! this . allDisabledExtensions ) {
33
80
this . globalDisabledExtensions = this . getDisabledExtensionsFromStorage ( StorageScope . GLOBAL ) ;
34
81
this . workspaceDisabledExtensions = this . getDisabledExtensionsFromStorage ( StorageScope . WORKSPACE ) ;
35
82
this . allDisabledExtensions = distinct ( [ ...this . globalDisabledExtensions , ...this . workspaceDisabledExtensions ] ) ;
36
83
}
37
84
38
- switch ( scope ) {
39
- case StorageScope . GLOBAL : return this . globalDisabledExtensions ;
40
- case StorageScope . WORKSPACE : return this . workspaceDisabledExtensions ;
85
+ if ( workspace === void 0 ) {
86
+ return this . allDisabledExtensions ;
41
87
}
42
- return this . allDisabledExtensions ;
88
+
89
+ if ( workspace ) {
90
+ return this . workspaceDisabledExtensions ;
91
+ }
92
+
93
+ return this . globalDisabledExtensions ;
43
94
}
44
95
45
96
private getDisabledExtensionsFromStorage ( scope ?: StorageScope ) : string [ ] {
@@ -52,8 +103,56 @@ export class ExtensionsRuntimeService implements IExtensionsRuntimeService {
52
103
return [ ...globallyDisabled , ...workspaceDisabled ] ;
53
104
}
54
105
106
+ private setGlobalEnablement ( identifier : string , enable : boolean , displayName : string ) : TPromise < boolean > {
107
+ if ( enable ) {
108
+ return this . choiceService . choose ( Severity . Info , localize ( 'enableExtensionGloballyNoWorkspace' , "Would you like to enable '{0}' extension globally?" , displayName ) ,
109
+ [ localize ( 'yes' , "Yes" ) , localize ( 'no' , "No" ) ] )
110
+ . then ( ( option ) => {
111
+ if ( option === 0 ) {
112
+ return this . enableExtension ( identifier , StorageScope . GLOBAL ) . then ( ( ) => true ) ;
113
+ }
114
+ return TPromise . wrap ( false ) ;
115
+ } ) ;
116
+ } else {
117
+ return this . choiceService . choose ( Severity . Info , localize ( 'disableExtensionGlobally' , "Would you like to disable '{0}' extension globally?" , displayName ) ,
118
+ [ localize ( 'yes' , "Yes" ) , localize ( 'no' , "No" ) ] )
119
+ . then ( ( option ) => {
120
+ if ( option === 0 ) {
121
+ return this . disableExtension ( identifier , StorageScope . GLOBAL ) . then ( ( ) => true ) ;
122
+ }
123
+ return TPromise . wrap ( false ) ;
124
+ } ) ;
125
+ }
126
+ }
127
+
128
+ private disableExtension ( identifier : string , scope : StorageScope ) : TPromise < boolean > {
129
+ let disabledExtensions = this . _getDisabledExtensions ( scope ) ;
130
+ disabledExtensions . push ( identifier ) ;
131
+ this . _setDisabledExtensions ( disabledExtensions , scope ) ;
132
+ return TPromise . wrap ( true ) ;
133
+ }
134
+
135
+ private enableExtension ( identifier : string , scope : StorageScope ) : TPromise < boolean > {
136
+ let disabledExtensions = this . _getDisabledExtensions ( scope ) ;
137
+ const index = disabledExtensions . indexOf ( identifier ) ;
138
+ if ( index !== - 1 ) {
139
+ disabledExtensions . splice ( index , 1 ) ;
140
+ this . _setDisabledExtensions ( disabledExtensions , scope ) ;
141
+ return TPromise . wrap ( true ) ;
142
+ }
143
+ return TPromise . wrap ( false ) ;
144
+ }
145
+
55
146
private _getDisabledExtensions ( scope : StorageScope ) : string [ ] {
56
147
const value = this . storageService . get ( DISABLED_EXTENSIONS_STORAGE_PATH , scope , '' ) ;
57
148
return value ? distinct ( value . split ( ',' ) ) : [ ] ;
58
149
}
150
+
151
+ private _setDisabledExtensions ( disabledExtensions : string [ ] , scope : StorageScope ) : void {
152
+ if ( disabledExtensions . length ) {
153
+ this . storageService . store ( DISABLED_EXTENSIONS_STORAGE_PATH , disabledExtensions . join ( ',' ) , scope ) ;
154
+ } else {
155
+ this . storageService . remove ( DISABLED_EXTENSIONS_STORAGE_PATH , scope ) ;
156
+ }
157
+ }
59
158
}
0 commit comments