Skip to content

Commit

Permalink
refactor: move loading of bound keys from package.json to configurati…
Browse files Browse the repository at this point in the history
…on class
  • Loading branch information
jpoon committed Jan 5, 2018
1 parent ce0e896 commit ca16f3b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
41 changes: 2 additions & 39 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ import { AngleBracketNotation } from './src/configuration/notation';
import { StatusBar } from './src/statusBar';
import { taskQueue } from './src/taskQueue';

interface VSCodeKeybinding {
key: string;
mac?: string;
linux?: string;
command: string;
when: string;
}

const packagejson: {
contributes: {
keybindings: VSCodeKeybinding[];
};
} = require('../package.json'); // out/../package.json

let extensionContext: vscode.ExtensionContext;

/**
Expand Down Expand Up @@ -284,33 +270,10 @@ export async function activate(context: vscode.ExtensionContext) {
toggleExtension(Configuration.disableExt);
});

// Clear boundKeyCombinations array incase there are any entries in it so
// that we have a clean list of keys with no duplicates
Configuration.boundKeyCombinations = [];

for (let keybinding of packagejson.contributes.keybindings) {
if (keybinding.when.indexOf('listFocus') !== -1) {
continue;
}
let keyToBeBound = '';
if (process.platform === 'darwin') {
keyToBeBound = keybinding.mac || keybinding.key;
} else if (process.platform === 'linux') {
keyToBeBound = keybinding.linux || keybinding.key;
} else {
keyToBeBound = keybinding.key;
}

// Store registered key bindings in bracket notation form
const bracketedKey = AngleBracketNotation.Normalize(keyToBeBound);
Configuration.boundKeyCombinations.push(bracketedKey);

registerCommand(context, keybinding.command, () => handleKeyEvent(`${bracketedKey}`));
for (const boundKey of Configuration.boundKeyCombinations) {
registerCommand(context, boundKey.command, () => handleKeyEvent(`${boundKey.key}`));
}

// Update configuration now that bound keys array is populated
Configuration.reload();

// Initialize mode handler for current active Text Editor at startup.
if (vscode.window.activeTextEditor) {
let mh = await getAndUpdateModeHandler();
Expand Down
70 changes: 55 additions & 15 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ import { Globals } from '../globals';
import { taskQueue } from '../taskQueue';
import { AngleBracketNotation } from './notation';

const packagejson: {
contributes: {
keybindings: VSCodeKeybinding[];
};
} = require('../../../package.json');

type OptionValue = number | string | boolean;
type ValueMapping = {
[key: number]: number | string | boolean;
[key: string]: number | string | boolean;
};

interface VSCodeKeybinding {
key: string;
mac?: string;
linux?: string;
command: string;
when: string;
}

interface IHandleKeys {
[key: string]: boolean;
}
Expand All @@ -24,7 +38,12 @@ interface IModeSpecificStrings {
replace: string | undefined;
}

export interface IKeybinding {
interface IKeyBinding {
key: string;
command: string;
}

export interface IKeyRemapping {
before: string[];
after?: string[];
commands?: { command: string; args: any[] }[];
Expand Down Expand Up @@ -55,6 +74,7 @@ class ConfigurationClass {
}

reload() {
// read configurations
let vimConfigs = getConfiguration('vim');
/* tslint:disable:forin */
// Disable forin rule here as we make accessors enumerable.`
Expand All @@ -70,8 +90,8 @@ class ConfigurationClass {
this.leader = ' ';
}

// Normalize Keys
const keybindingList: IKeybinding[][] = [
// normalize keys
const keybindingList: IKeyRemapping[][] = [
this.insertModeKeyBindings,
this.insertModeKeyBindingsNonRecursive,
this.otherModesKeyBindings,
Expand All @@ -93,26 +113,46 @@ class ConfigurationClass {
}
}

// Enable/Disable certain key combinations
for (const bracketedKey of this.boundKeyCombinations) {
// read package.json for bound keys
this.boundKeyCombinations = [];
for (let keybinding of packagejson.contributes.keybindings) {
if (keybinding.when.indexOf('listFocus') !== -1) {
continue;
}

let key = keybinding.key;
if (process.platform === 'darwin') {
key = keybinding.mac || key;
} else if (process.platform === 'linux') {
key = keybinding.linux || key;
}

this.boundKeyCombinations.push({
key: AngleBracketNotation.Normalize(key),
command: keybinding.command,
});
}

// enable/disable certain key combinations
for (const boundKey of this.boundKeyCombinations) {
// By default, all key combinations are used
let useKey = true;

let handleKey = this.handleKeys[bracketedKey];
let handleKey = this.handleKeys[boundKey.key];
if (handleKey !== undefined) {
// enabled/disabled through `vim.handleKeys`
useKey = handleKey;
} else if (!this.useCtrlKeys && bracketedKey.slice(1, 3) === 'C-') {
} else if (!this.useCtrlKeys && boundKey.key.slice(1, 3) === 'C-') {
// user has disabled CtrlKeys and the current key is a CtrlKey
// <C-c>, still needs to be captured to overrideCopy
if (bracketedKey === '<C-c>' && this.overrideCopy) {
if (boundKey.key === '<C-c>' && this.overrideCopy) {
useKey = true;
} else {
useKey = false;
}
}

vscode.commands.executeCommand('setContext', `vim.use${bracketedKey}`, useKey);
vscode.commands.executeCommand('setContext', `vim.use${boundKey.key}`, useKey);
}
}

Expand Down Expand Up @@ -301,9 +341,9 @@ class ConfigurationClass {
iskeyword: string = '/\\()"\':,.;<>~!@#$%^&*|+=[]{}`?-';

/**
* Array of all key combinations that were registered in angle bracket notation
* Array of all bound key combinations in angle bracket notation
*/
boundKeyCombinations: string[] = [];
boundKeyCombinations: IKeyBinding[] = [];

/**
* In visual mode, start a search with * or # using the current selection
Expand Down Expand Up @@ -364,10 +404,10 @@ class ConfigurationClass {
/**
* Keybindings
*/
insertModeKeyBindings: IKeybinding[] = [];
insertModeKeyBindingsNonRecursive: IKeybinding[] = [];
otherModesKeyBindings: IKeybinding[] = [];
otherModesKeyBindingsNonRecursive: IKeybinding[] = [];
insertModeKeyBindings: IKeyRemapping[] = [];
insertModeKeyBindingsNonRecursive: IKeyRemapping[] = [];
otherModesKeyBindings: IKeyRemapping[] = [];
otherModesKeyBindingsNonRecursive: IKeyRemapping[] = [];
}

function getConfiguration(section: string): vscode.WorkspaceConfiguration {
Expand Down
8 changes: 4 additions & 4 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as _ from 'lodash';
import * as vscode from 'vscode';

import { CommandLine } from '../cmd_line/commandLine';
import { Configuration, IKeybinding } from '../configuration/configuration';
import { Configuration, IKeyRemapping } from '../configuration/configuration';
import { ModeName } from '../mode/mode';
import { ModeHandler } from '../mode/modeHandler';
import { VimState } from './../state/vimState';
Expand Down Expand Up @@ -44,7 +44,7 @@ interface IRemapper {
class Remapper implements IRemapper {
private readonly _remappedModes: ModeName[];
private readonly _recursive: boolean;
private readonly _remappings: IKeybinding[] = [];
private readonly _remappings: IKeyRemapping[] = [];

/**
* Have the keys pressed so far potentially be a remap
Expand All @@ -57,7 +57,7 @@ class Remapper implements IRemapper {
constructor(configKey: string, remappedModes: ModeName[], recursive: boolean) {
this._recursive = recursive;
this._remappedModes = remappedModes;
this._remappings = Configuration[configKey] as IKeybinding[];
this._remappings = Configuration[configKey] as IKeyRemapping[];
}

public async sendKey(
Expand All @@ -71,7 +71,7 @@ class Remapper implements IRemapper {
return false;
}

let remapping: IKeybinding | undefined;
let remapping: IKeyRemapping | undefined;
const longestKeySequence = this._longestKeySequence();

/**
Expand Down

0 comments on commit ca16f3b

Please sign in to comment.