Skip to content

Commit

Permalink
[vscode] fix #4203: support os specific keybindings
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <[email protected]>
  • Loading branch information
akosyakov committed Jan 31, 2019
1 parent d552d2a commit c9da297
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
10 changes: 8 additions & 2 deletions packages/plugin-ext/src/common/plugin-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ export interface PluginPackageMenu {
}

export interface PluginPackageKeybinding {
key: string;
key?: string;
command: string;
when?: string;
mac?: string;
linux?: string;
win?: string;
}

export interface PluginPackageGrammarsContribution {
Expand Down Expand Up @@ -480,9 +483,12 @@ export interface Menu {
* Keybinding contribution
*/
export interface Keybinding {
keybinding: string;
keybinding?: string;
command: string;
when?: string;
mac?: string;
linux?: string;
win?: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ export class TheiaPluginScanner implements PluginScanner {
return {
keybinding: rawKeybinding.key,
command: rawKeybinding.command,
when: rawKeybinding.when
when: rawKeybinding.when,
mac: rawKeybinding.mac,
linux: rawKeybinding.linux,
win: rawKeybinding.win
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
********************************************************************************/

import { injectable, inject } from 'inversify';
import { PluginContribution } from '../../../common';
import { PluginContribution, Keybinding as PluginKeybinding } from '../../../common';
import { Keybinding, KeybindingRegistry, KeybindingScope } from '@theia/core/lib/browser/keybinding';
import { KeySequence } from '@theia/core/lib/browser';
import { ILogger } from '@theia/core/lib/common/logger';
import { OS } from '@theia/core/lib/common/os';

@injectable()
export class KeybindingsContributionPointHandler {
Expand All @@ -33,19 +34,43 @@ export class KeybindingsContributionPointHandler {
if (!contributions || !contributions.keybindings) {
return;
}

const keybindings = contributions.keybindings;
keybindings.forEach(keybinding => {
try {
const keybindingResult = this.keybindingRegistry.getKeybindingsForKeySequence(KeySequence.parse(keybinding.keybinding));
this.handleShadingKeybindings(keybinding, keybindingResult.shadow);
this.handlePartialKeybindings(keybinding, keybindingResult.partial);
} catch (e) {
this.logger.error(e.message || e);
const keybindings: Keybinding[] = [];
for (const raw of contributions.keybindings) {
const keybinding = this.toKeybinding(raw);
if (keybinding) {
try {
const keybindingResult = this.keybindingRegistry.getKeybindingsForKeySequence(KeySequence.parse(keybinding.keybinding));
this.handleShadingKeybindings(keybinding, keybindingResult.shadow);
this.handlePartialKeybindings(keybinding, keybindingResult.partial);
keybindings.push(keybinding);
} catch (e) {
this.logger.error(e.message || e);
}
}
});
}
this.keybindingRegistry.setKeymap(KeybindingScope.USER, keybindings);
}

this.keybindingRegistry.setKeymap(KeybindingScope.USER, contributions.keybindings);
protected toKeybinding(pluginKeybinding: PluginKeybinding): Keybinding | undefined {
const keybinding = this.toOSKeybinding(pluginKeybinding);
if (!keybinding) {
return undefined;
}
const { command, when } = pluginKeybinding;
return { keybinding, command, when };
}

protected toOSKeybinding(pluginKeybinding: PluginKeybinding): string | undefined {
let keybinding: string | undefined;
const os = OS.type();
if (os === OS.Type.Windows) {
keybinding = pluginKeybinding.win;
} else if (os === OS.Type.OSX) {
keybinding = pluginKeybinding.mac;
} else {
keybinding = pluginKeybinding.linux;
}
return keybinding || pluginKeybinding.keybinding;
}

private handlePartialKeybindings(keybinding: Keybinding, partialKeybindings: Keybinding[]) {
Expand Down

0 comments on commit c9da297

Please sign in to comment.