@@ -19,7 +19,7 @@ import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/commo
1919import { IEnvironmentService } from 'vs/platform/environment/common/environment' ;
2020import { Extensions , IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry' ;
2121import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService' ;
22- import { IKeyboardEvent , IUserFriendlyKeybinding , KeybindingSource , IKeybindingService , IKeybindingEvent } from 'vs/platform/keybinding/common/keybinding' ;
22+ import { IKeyboardEvent , IUserFriendlyKeybinding , KeybindingSource , IKeybindingService , IKeybindingEvent , USER_KEYBINDINGS_KEY } from 'vs/platform/keybinding/common/keybinding' ;
2323import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver' ;
2424import { IKeybindingItem , IKeybindingRule2 , KeybindingWeight , KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry' ;
2525import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem' ;
@@ -36,17 +36,15 @@ import { MenuRegistry } from 'vs/platform/actions/common/actions';
3636import { registerSingleton } from 'vs/platform/instantiation/common/extensions' ;
3737// tslint:disable-next-line: import-patterns
3838import { commandsExtensionPoint } from 'vs/workbench/api/common/menusExtensionPoint' ;
39- import { Disposable , IDisposable , toDisposable } from 'vs/base/common/lifecycle' ;
39+ import { Disposable } from 'vs/base/common/lifecycle' ;
4040import { RunOnceScheduler } from 'vs/base/common/async' ;
41- import { URI } from 'vs/base/common/uri' ;
42- import { IFileService , FileChangesEvent , FileChangeType } from 'vs/platform/files/common/files' ;
43- import { dirname , isEqual } from 'vs/base/common/resources' ;
4441import { parse } from 'vs/base/common/json' ;
4542import * as objects from 'vs/base/common/objects' ;
4643import { IKeymapService } from 'vs/workbench/services/keybinding/common/keymapInfo' ;
4744import { getDispatchConfig } from 'vs/workbench/services/keybinding/common/dispatchConfig' ;
4845import { isArray } from 'vs/base/common/types' ;
4946import { INavigatorWithKeyboard } from 'vs/workbench/services/keybinding/common/navigatorKeyboard' ;
47+ import { IUserDataService } from 'vs/workbench/services/userData/common/userData' ;
5048
5149interface ContributedKeyBinding {
5250 command : string ;
@@ -158,8 +156,8 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService {
158156 @IConfigurationService configurationService : IConfigurationService ,
159157 @IWindowService private readonly windowService : IWindowService ,
160158 @IExtensionService extensionService : IExtensionService ,
161- @IFileService fileService : IFileService ,
162- @IKeymapService private readonly keymapService : IKeymapService
159+ @IKeymapService private readonly keymapService : IKeymapService ,
160+ @IUserDataService userDataService : IUserDataService ,
163161 ) {
164162 super ( contextKeyService , commandService , telemetryService , notificationService ) ;
165163
@@ -185,7 +183,7 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService {
185183
186184 this . _cachedResolver = null ;
187185
188- this . userKeybindings = this . _register ( new UserKeybindings ( environmentService . keybindingsResource , fileService ) ) ;
186+ this . userKeybindings = this . _register ( new UserKeybindings ( userDataService ) ) ;
189187 this . userKeybindings . initialize ( ) . then ( ( ) => {
190188 if ( this . userKeybindings . keybindings . length ) {
191189 this . updateResolver ( { source : KeybindingSource . User } ) ;
@@ -552,100 +550,40 @@ class UserKeybindings extends Disposable {
552550
553551 private _keybindings : IUserFriendlyKeybinding [ ] = [ ] ;
554552 get keybindings ( ) : IUserFriendlyKeybinding [ ] { return this . _keybindings ; }
553+
555554 private readonly reloadConfigurationScheduler : RunOnceScheduler ;
555+
556556 protected readonly _onDidChange : Emitter < void > = this . _register ( new Emitter < void > ( ) ) ;
557557 readonly onDidChange : Event < void > = this . _onDidChange . event ;
558558
559- private fileWatcherDisposable : IDisposable = Disposable . None ;
560- private directoryWatcherDisposable : IDisposable = Disposable . None ;
561-
562559 constructor (
563- private readonly keybindingsResource : URI ,
564- private readonly fileService : IFileService
560+ private readonly userDataService : IUserDataService
565561 ) {
566562 super ( ) ;
567563
568- this . _register ( fileService . onFileChanges ( e => this . handleFileEvents ( e ) ) ) ;
564+ this . _register ( Event . filter ( this . userDataService . onDidChange , e => e . contains ( USER_KEYBINDINGS_KEY ) ) ( ( ) => this . reloadConfigurationScheduler . schedule ( ) ) ) ;
569565 this . reloadConfigurationScheduler = this . _register ( new RunOnceScheduler ( ( ) => this . reload ( ) . then ( changed => {
570566 if ( changed ) {
571567 this . _onDidChange . fire ( ) ;
572568 }
573569 } ) , 50 ) ) ;
574- this . _register ( toDisposable ( ( ) => {
575- this . stopWatchingResource ( ) ;
576- this . stopWatchingDirectory ( ) ;
577- } ) ) ;
578- }
579-
580- private watchResource ( ) : void {
581- this . fileWatcherDisposable = this . fileService . watch ( this . keybindingsResource ) ;
582- }
583-
584- private stopWatchingResource ( ) : void {
585- this . fileWatcherDisposable . dispose ( ) ;
586- this . fileWatcherDisposable = Disposable . None ;
587- }
588-
589- private watchDirectory ( ) : void {
590- const directory = dirname ( this . keybindingsResource ) ;
591- this . directoryWatcherDisposable = this . fileService . watch ( directory ) ;
592- }
593-
594- private stopWatchingDirectory ( ) : void {
595- this . directoryWatcherDisposable . dispose ( ) ;
596- this . directoryWatcherDisposable = Disposable . None ;
597570 }
598571
599572 async initialize ( ) : Promise < void > {
600- const exists = await this . fileService . exists ( this . keybindingsResource ) ;
601- this . onResourceExists ( exists ) ;
602573 await this . reload ( ) ;
603574 }
604575
605576 private async reload ( ) : Promise < boolean > {
606577 const existing = this . _keybindings ;
607578 try {
608- const content = await this . fileService . readFile ( this . keybindingsResource ) ;
609- const value = parse ( content . value . toString ( ) ) ;
579+ const content = ( await this . userDataService . read ( USER_KEYBINDINGS_KEY ) ) || '[]' ;
580+ const value = parse ( content ) ;
610581 this . _keybindings = isArray ( value ) ? value : [ ] ;
611582 } catch ( e ) {
612583 this . _keybindings = [ ] ;
613584 }
614585 return existing ? ! objects . equals ( existing , this . _keybindings ) : true ;
615586 }
616-
617- private async handleFileEvents ( event : FileChangesEvent ) : Promise < void > {
618- const events = event . changes ;
619-
620- let affectedByChanges = false ;
621-
622- // Find changes that affect the resource
623- for ( const event of events ) {
624- affectedByChanges = isEqual ( this . keybindingsResource , event . resource ) ;
625- if ( affectedByChanges ) {
626- if ( event . type === FileChangeType . ADDED ) {
627- this . onResourceExists ( true ) ;
628- } else if ( event . type === FileChangeType . DELETED ) {
629- this . onResourceExists ( false ) ;
630- }
631- break ;
632- }
633- }
634-
635- if ( affectedByChanges ) {
636- this . reloadConfigurationScheduler . schedule ( ) ;
637- }
638- }
639-
640- private onResourceExists ( exists : boolean ) : void {
641- if ( exists ) {
642- this . stopWatchingDirectory ( ) ;
643- this . watchResource ( ) ;
644- } else {
645- this . stopWatchingResource ( ) ;
646- this . watchDirectory ( ) ;
647- }
648- }
649587}
650588
651589let schemaId = 'vscode://schemas/keybindings' ;
0 commit comments