Skip to content

Commit

Permalink
Add context to keymaps.json when editing through the keybindings-widget
Browse files Browse the repository at this point in the history
Include the keybinding context to the keymaps.json for editing keybindings.
Fixes a bug where if a keybinding which already has a context and is edited, it loses it's context from the table.

Signed-off-by: Vincent Fugnitto <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Oct 22, 2018
1 parent ac580dd commit c2939ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
19 changes: 10 additions & 9 deletions packages/keymaps/src/browser/keybindings-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,18 @@ export class KeybindingWidget extends ReactWidget {
}

protected editKeybinding(item: KeybindingItem): void {
const rawCommand = this.getRawValue(item.command);
const rawId = this.getRawValue(item.id);
const rawKeybinding = (item.keybinding) ? this.getRawValue(item.keybinding) : '';
const command = this.getRawValue(item.command);
const id = this.getRawValue(item.id);
const keybinding = (item.keybinding) ? this.getRawValue(item.keybinding) : '';
const context = (item.context) ? this.getRawValue(item.context) : '';
const dialog = new SingleTextInputDialog({
title: `Edit Keybinding For ${rawCommand}`,
initialValue: rawKeybinding,
validate: keybinding => this.validateKeybinding(rawCommand, rawKeybinding, keybinding),
title: `Edit Keybinding For ${command}`,
initialValue: keybinding,
validate: newKeybinding => this.validateKeybinding(command, keybinding, newKeybinding),
});
dialog.open().then(async keybinding => {
if (keybinding) {
await this.keymapsService.setKeybinding(rawId, keybinding);
dialog.open().then(async newKeybinding => {
if (newKeybinding) {
await this.keymapsService.setKeybinding({ 'command': id, 'keybinding': newKeybinding, 'context': context });
}
});
}
Expand Down
9 changes: 5 additions & 4 deletions packages/keymaps/src/browser/keymaps-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Emitter } from '@theia/core/lib/common/';
export interface KeybindingJson {
command: string,
keybinding: string,
context: string,
}

@injectable()
Expand Down Expand Up @@ -79,21 +80,21 @@ export class KeymapsService {
open(this.opener, this.resource.uri);
}

async setKeybinding(command: string, keybinding: string): Promise<void> {
async setKeybinding(keybindingJson: KeybindingJson): Promise<void> {
if (!this.resource.saveContents) {
return;
}
const content = await this.resource.readContents();
const keybindings: KeybindingJson[] = content ? jsoncparser.parse(content) : [];
let updated = false;
for (let i = 0; i < keybindings.length; i++) {
if (keybindings[i].command === command) {
if (keybindings[i].command === keybindingJson.command) {
updated = true;
keybindings[i].keybinding = keybinding;
keybindings[i].keybinding = keybindingJson.keybinding;
}
}
if (!updated) {
const item: KeybindingJson = { 'command': command, 'keybinding': keybinding };
const item: KeybindingJson = { ...keybindingJson };
keybindings.push(item);
}
await this.resource.saveContents(JSON.stringify(keybindings, undefined, 4));
Expand Down

0 comments on commit c2939ab

Please sign in to comment.