Skip to content

Commit

Permalink
fix: properly update keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Nov 16, 2023
1 parent cc28aa5 commit e4104c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
10 changes: 8 additions & 2 deletions scripts/vscode.patch
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ index 23d547570e9..31dfb4fd8d4 100644

export * from 'vs/editor/editor.api';
diff --git a/src/vs/editor/standalone/browser/standaloneServices.ts b/src/vs/editor/standalone/browser/standaloneServices.ts
index 01f2c6987ac..a4853357178 100644
index 01f2c6987ac..e3bf9b1e9e4 100644
--- a/src/vs/editor/standalone/browser/standaloneServices.ts
+++ b/src/vs/editor/standalone/browser/standaloneServices.ts
@@ -89,8 +89,6 @@ import { DefaultConfiguration } from 'vs/platform/configuration/common/configura
Expand All @@ -409,7 +409,13 @@ index 01f2c6987ac..a4853357178 100644
import { ExtensionKind, IEnvironmentService, IExtensionHostDebugParams } from 'vs/platform/environment/common/environment';

class SimpleModel implements IResolvedTextEditorModel {
@@ -517,10 +515,14 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
@@ -512,15 +510,19 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
});
}

- private updateResolver(): void {
+ protected updateResolver(): void {
this._cachedResolver = null;
this._onDidUpdateKeybindings.fire();
}

Expand Down
23 changes: 21 additions & 2 deletions src/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ import { IKeybindingService, IKeyboardEvent } from 'vs/platform/keybinding/commo
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver'
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem'
import { Keybinding, ResolvedKeybinding } from 'vs/base/common/keybindings'
import { Event } from 'vs/base/common/event'
import { Emitter } from 'monaco-editor'
import { createInjectedClass } from './tools/injection'
import { checkServicesReady } from './services'

Check warning on line 49 in src/monaco.ts

View workflow job for this annotation

GitHub Actions / Check build

'checkServicesReady' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 49 in src/monaco.ts

View workflow job for this annotation

GitHub Actions / Check build

'checkServicesReady' is defined but never used
// Selectively comes from vs/workbench/contrib/codeEditor/browser/codeEditor.contribution.ts
import 'vs/workbench/contrib/codeEditor/browser/workbenchReferenceSearch'
import 'vs/workbench/contrib/codeEditor/browser/menuPreventer'
Expand Down Expand Up @@ -182,8 +185,13 @@ export async function createModelReference (resource: URI, content?: string): Pr
return (await StandaloneServices.get(ITextModelService).createModelReference(resource)) as IReference<ITextFileEditorModel>
}

export interface KeybindingProvider {
provideKeybindings (): ResolvedKeybindingItem[]
onDidChangeKeybindings: Event<void>
}

export interface DynamicKeybindingService extends IKeybindingService {
registerKeybindingProvider (provider: () => ResolvedKeybindingItem[]): IDisposable
registerKeybindingProvider (provider: KeybindingProvider): IDisposable
_getResolver(): KeybindingResolver
}

Expand All @@ -195,6 +203,7 @@ function isDynamicKeybindingService (keybindingService: IKeybindingService) {
// This class use useful so editor.addAction and editor.addCommand still work
// Monaco do an `instanceof` on the KeybindingService so we need it to extends `StandaloneKeybindingService`
class DelegateStandaloneKeybindingService extends StandaloneKeybindingService {
private _onDidChangeKeybindings = new Emitter<void>()
constructor (
private delegate: DynamicKeybindingService,
@IContextKeyService contextKeyService: IContextKeyService,
Expand All @@ -206,13 +215,23 @@ class DelegateStandaloneKeybindingService extends StandaloneKeybindingService {
) {
super(contextKeyService, commandService, telemetryService, notificationService, logService, codeEditorService)

this._register(delegate.registerKeybindingProvider(() => this.getUserKeybindingItems()))
this._register(delegate.registerKeybindingProvider({
provideKeybindings: () => {
return this.getUserKeybindingItems()
},
onDidChangeKeybindings: this._onDidChangeKeybindings.event
}))
}

protected override _getResolver (): KeybindingResolver {
return this.delegate._getResolver()
}

protected override updateResolver (): void {
super.updateResolver()
this._onDidChangeKeybindings.fire()
}

override resolveKeyboardEvent (keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
return this.delegate.resolveKeyboardEvent(keyboardEvent)
}
Expand Down
20 changes: 13 additions & 7 deletions src/service-override/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { BrowserKeyboardLayoutService } from 'vs/workbench/services/keybinding/b
import { IFileService, IFileWriteOptions } from 'vs/platform/files/common/files'
import { ICommandService } from 'vs/platform/commands/common/commands'
import { CommandService } from 'vs/workbench/services/commands/common/commandService'
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem'
import { toDisposable } from 'vs/base/common/lifecycle'
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver'
import { IContextKeyService, IContextKeyServiceTarget } from 'vs/platform/contextkey/common/contextkey'
Expand All @@ -24,7 +23,7 @@ import { WorkbenchContextKeysHandler } from 'vs/workbench/browser/contextkeys'
import { Schemas } from 'vs/base/common/network'
import { URI } from 'vs/base/common/uri'
import getFileServiceOverride, { initFile } from './files'
import { DynamicKeybindingService } from '../monaco'
import { DisposableStore, DynamicKeybindingService, KeybindingProvider } from '../monaco'
import { onRenderWorkbench } from '../lifecycle'
import { IInstantiationService } from '../services'
import 'vs/workbench/browser/workbench.contribution'
Expand All @@ -50,7 +49,7 @@ async function updateUserKeybindings (keybindingsJson: string): Promise<void> {
}

class DynamicWorkbenchKeybindingService extends WorkbenchKeybindingService implements DynamicKeybindingService {
private keybindingProviders: (() => ResolvedKeybindingItem[])[] = []
private keybindingProviders: KeybindingProvider[] = []

constructor (
private shouldUseGlobalKeybindings: () => boolean,
Expand All @@ -69,17 +68,24 @@ class DynamicWorkbenchKeybindingService extends WorkbenchKeybindingService imple
super(contextKeyService, commandService, telemetryService, notificationService, userDataProfileService, hostService, extensionService, fileService, uriIdentityService, logService, keyboardLayoutService)
}

public registerKeybindingProvider (provider: () => ResolvedKeybindingItem[]) {
public registerKeybindingProvider (provider: KeybindingProvider) {
this.keybindingProviders.push(provider)
this.updateResolver()

return toDisposable(() => {
const store = new DisposableStore()
store.add(provider.onDidChangeKeybindings(() => {
this.updateResolver()
}))

store.add(toDisposable(() => {
const idx = this.keybindingProviders.indexOf(provider)
if (idx >= 0) {
this.keybindingProviders.splice(idx, 1)
this.updateResolver()
}
})
}))

return store
}

public override _getResolver (): KeybindingResolver {
Expand All @@ -94,7 +100,7 @@ class DynamicWorkbenchKeybindingService extends WorkbenchKeybindingService imple
}

protected override getUserKeybindingItems () {
return [...super.getUserKeybindingItems(), ...this.keybindingProviders.flatMap(provider => provider())]
return [...super.getUserKeybindingItems(), ...this.keybindingProviders.flatMap(provider => provider.provideKeybindings())]
}
}

Expand Down

0 comments on commit e4104c7

Please sign in to comment.