Skip to content

Commit

Permalink
keybindingService: make the schema holder non-singleton to see its li…
Browse files Browse the repository at this point in the history
…fecycle easier
  • Loading branch information
ulugbekna committed Mar 30, 2023
1 parent 8d83139 commit b5aa594
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/vs/workbench/services/keybinding/browser/keybindingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService {
private userKeybindings: UserKeybindings;
private isComposingGlobalContextKey: IContextKey<boolean>;
private readonly _contributions: KeybindingsSchemaContribution[] = [];
private readonly kbsJsonSchema: KeybindingsJsonSchema;

constructor(
@IContextKeyService contextKeyService: IContextKeyService,
Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -880,10 +879,13 @@ class KeybindingsJsonSchema {

private readonly schemaRegistry = Registry.as<IJSONContributionRegistry>(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;
Expand Down

0 comments on commit b5aa594

Please sign in to comment.