@@ -9,6 +9,8 @@ import { OperatingSystem, OS } from 'vs/base/common/platform';
9
9
import { CommandsRegistry , ICommandHandler , ICommandHandlerDescription } from 'vs/platform/commands/common/commands' ;
10
10
import { ContextKeyExpression } from 'vs/platform/contextkey/common/contextkey' ;
11
11
import { Registry } from 'vs/platform/registry/common/platform' ;
12
+ import { combinedDisposable , DisposableStore , IDisposable , toDisposable } from 'vs/base/common/lifecycle' ;
13
+ import { LinkedList } from 'vs/base/common/linkedList' ;
12
14
13
15
export interface IKeybindingItem {
14
16
keybinding : ( SimpleKeybinding | ScanCodeBinding ) [ ] ;
@@ -69,20 +71,20 @@ export interface ICommandAndKeybindingRule extends IKeybindingRule {
69
71
}
70
72
71
73
export interface IKeybindingsRegistry {
72
- registerKeybindingRule ( rule : IKeybindingRule ) : void ;
74
+ registerKeybindingRule ( rule : IKeybindingRule ) : IDisposable ;
73
75
setExtensionKeybindings ( rules : IExtensionKeybindingRule [ ] ) : void ;
74
- registerCommandAndKeybindingRule ( desc : ICommandAndKeybindingRule ) : void ;
76
+ registerCommandAndKeybindingRule ( desc : ICommandAndKeybindingRule ) : IDisposable ;
75
77
getDefaultKeybindings ( ) : IKeybindingItem [ ] ;
76
78
}
77
79
78
80
class KeybindingsRegistryImpl implements IKeybindingsRegistry {
79
81
80
- private _coreKeybindings : IKeybindingItem [ ] ;
82
+ private _coreKeybindings : LinkedList < IKeybindingItem > ;
81
83
private _extensionKeybindings : IKeybindingItem [ ] ;
82
84
private _cachedMergedKeybindings : IKeybindingItem [ ] | null ;
83
85
84
86
constructor ( ) {
85
- this . _coreKeybindings = [ ] ;
87
+ this . _coreKeybindings = new LinkedList ( ) ;
86
88
this . _extensionKeybindings = [ ] ;
87
89
this . _cachedMergedKeybindings = null ;
88
90
}
@@ -108,13 +110,14 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
108
110
return kb ;
109
111
}
110
112
111
- public registerKeybindingRule ( rule : IKeybindingRule ) : void {
113
+ public registerKeybindingRule ( rule : IKeybindingRule ) : IDisposable {
112
114
const actualKb = KeybindingsRegistryImpl . bindToCurrentPlatform ( rule ) ;
115
+ const result = new DisposableStore ( ) ;
113
116
114
117
if ( actualKb && actualKb . primary ) {
115
118
const kk = createKeybinding ( actualKb . primary , OS ) ;
116
119
if ( kk ) {
117
- this . _registerDefaultKeybinding ( kk , rule . id , rule . args , rule . weight , 0 , rule . when ) ;
120
+ result . add ( this . _registerDefaultKeybinding ( kk , rule . id , rule . args , rule . weight , 0 , rule . when ) ) ;
118
121
}
119
122
}
120
123
@@ -123,10 +126,11 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
123
126
const k = actualKb . secondary [ i ] ;
124
127
const kk = createKeybinding ( k , OS ) ;
125
128
if ( kk ) {
126
- this . _registerDefaultKeybinding ( kk , rule . id , rule . args , rule . weight , - i - 1 , rule . when ) ;
129
+ result . add ( this . _registerDefaultKeybinding ( kk , rule . id , rule . args , rule . weight , - i - 1 , rule . when ) ) ;
127
130
}
128
131
}
129
132
}
133
+ return result ;
130
134
}
131
135
132
136
public setExtensionKeybindings ( rules : IExtensionKeybindingRule [ ] ) : void {
@@ -151,9 +155,11 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
151
155
this . _cachedMergedKeybindings = null ;
152
156
}
153
157
154
- public registerCommandAndKeybindingRule ( desc : ICommandAndKeybindingRule ) : void {
155
- this . registerKeybindingRule ( desc ) ;
156
- CommandsRegistry . registerCommand ( desc ) ;
158
+ public registerCommandAndKeybindingRule ( desc : ICommandAndKeybindingRule ) : IDisposable {
159
+ return combinedDisposable (
160
+ this . registerKeybindingRule ( desc ) ,
161
+ CommandsRegistry . registerCommand ( desc )
162
+ ) ;
157
163
}
158
164
159
165
private static _mightProduceChar ( keyCode : KeyCode ) : boolean {
@@ -190,11 +196,11 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
190
196
}
191
197
}
192
198
193
- private _registerDefaultKeybinding ( keybinding : Keybinding , commandId : string , commandArgs : any , weight1 : number , weight2 : number , when : ContextKeyExpression | null | undefined ) : void {
199
+ private _registerDefaultKeybinding ( keybinding : Keybinding , commandId : string , commandArgs : any , weight1 : number , weight2 : number , when : ContextKeyExpression | null | undefined ) : IDisposable {
194
200
if ( OS === OperatingSystem . Windows ) {
195
201
this . _assertNoCtrlAlt ( keybinding . parts [ 0 ] , commandId ) ;
196
202
}
197
- this . _coreKeybindings . push ( {
203
+ const remove = this . _coreKeybindings . push ( {
198
204
keybinding : keybinding . parts ,
199
205
command : commandId ,
200
206
commandArgs : commandArgs ,
@@ -205,11 +211,16 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
205
211
isBuiltinExtension : false
206
212
} ) ;
207
213
this . _cachedMergedKeybindings = null ;
214
+
215
+ return toDisposable ( ( ) => {
216
+ remove ( ) ;
217
+ this . _cachedMergedKeybindings = null ;
218
+ } ) ;
208
219
}
209
220
210
221
public getDefaultKeybindings ( ) : IKeybindingItem [ ] {
211
222
if ( ! this . _cachedMergedKeybindings ) {
212
- this . _cachedMergedKeybindings = ( < IKeybindingItem [ ] > [ ] ) . concat ( this . _coreKeybindings ) . concat ( this . _extensionKeybindings ) ;
223
+ this . _cachedMergedKeybindings = Array . from ( this . _coreKeybindings ) . concat ( this . _extensionKeybindings ) ;
213
224
this . _cachedMergedKeybindings . sort ( sorter ) ;
214
225
}
215
226
return this . _cachedMergedKeybindings . slice ( 0 ) ;
0 commit comments