From b5aa594745d03a2607659fda35449c95a4cc70dd Mon Sep 17 00:00:00 2001 From: Ulugbek Abdullaev Date: Thu, 30 Mar 2023 17:27:18 +0200 Subject: [PATCH] keybindingService: make the schema holder non-singleton to see its lifecycle easier --- .../keybinding/browser/keybindingService.ts | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/vs/workbench/services/keybinding/browser/keybindingService.ts b/src/vs/workbench/services/keybinding/browser/keybindingService.ts index 0b1a362cb5b..f710af4a9a2 100644 --- a/src/vs/workbench/services/keybinding/browser/keybindingService.ts +++ b/src/vs/workbench/services/keybinding/browser/keybindingService.ts @@ -181,6 +181,7 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService { private userKeybindings: UserKeybindings; private isComposingGlobalContextKey: IContextKey; private readonly _contributions: KeybindingsSchemaContribution[] = []; + private readonly kbsJsonSchema: KeybindingsJsonSchema; constructor( @IContextKeyService contextKeyService: IContextKeyService, @@ -197,6 +198,8 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService { super(contextKeyService, commandService, telemetryService, notificationService, logService); this.isComposingGlobalContextKey = contextKeyService.createKey('isComposing', false); + + this.kbsJsonSchema = new KeybindingsJsonSchema(); this.updateKeybindingsJsonSchema(); this._keyboardMapper = this.keyboardLayoutService.getKeyboardMapper(); @@ -284,7 +287,7 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService { } private updateKeybindingsJsonSchema() { - KeybindingsJsonSchema.getInstance().updateSchema(this._contributions.flatMap(x => x.getSchemaAdditions())); + this.kbsJsonSchema.updateSchema(this._contributions.flatMap(x => x.getSchemaAdditions())); } private _printKeybinding(keybinding: Keybinding): string { @@ -770,17 +773,13 @@ class UserKeybindings extends Disposable { } } +/** + * Registers the `keybindings.json`'s schema with the JSON schema registry. Allows updating the schema, e.g., when new commands are registered (e.g., by extensions). + * + * Lifecycle owned by `WorkbenchKeybindingService`. Must be instantiated only once. + */ class KeybindingsJsonSchema { - private static instance: KeybindingsJsonSchema | undefined; - - static getInstance() { - if (!this.instance) { - this.instance = new KeybindingsJsonSchema(); - } - return this.instance; - } - private static readonly schemaId = 'vscode://schemas/keybindings'; private readonly commandsSchemas: IJSONSchema[] = []; @@ -880,10 +879,13 @@ class KeybindingsJsonSchema { private readonly schemaRegistry = Registry.as(Extensions.JSONContribution); - private constructor() { + constructor() { this.schemaRegistry.registerSchema(KeybindingsJsonSchema.schemaId, this.schema); } + // TODO@ulugbekna: can updates happen incrementally rather than rebuilding; concerns: + // - is just appending additional schemas enough for the registry to pick them up? + // - can `CommandsRegistry.getCommands` and `MenuRegistry.getCommands` return different values at different times? ie would just pushing new schemas from `additionalContributions` not be enough? updateSchema(additionalContributions: readonly IJSONSchema[]) { this.commandsSchemas.length = 0; this.commandsEnum.length = 0;