Skip to content

Commit

Permalink
Fix #716. Remapper options should be case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Sep 6, 2016
1 parent 45535ba commit 206122b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
5 changes: 2 additions & 3 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*/

import * as vscode from 'vscode';
import * as util from './src/util';
import * as _ from "lodash";
import { showCmdLine } from './src/cmd_line/main';
import { ModeHandler } from './src/mode/modeHandler';
import { TaskQueue } from './src/taskQueue';
import { Position } from './src/motion/position';
import { Globals } from './src/globals';

import { AngleBracketNotation } from './src/notation';

interface VSCodeKeybinding {
key: string;
Expand Down Expand Up @@ -206,7 +205,7 @@ export async function activate(context: vscode.ExtensionContext) {
});

for (let { key } of packagejson.contributes.keybindings) {
let bracketedKey = util.translateToAngleBracketNotation(key);
let bracketedKey = AngleBracketNotation.Normalize(key);
registerCommand(context, `extension.vim_${ key.toLowerCase() }`, () => handleKeyEvent(`${ bracketedKey }`));
}

Expand Down
4 changes: 2 additions & 2 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export class BaseAction {
* Can this action be paired with an operator (is it like w in dw)? All
* BaseMovements can be, and some more sophisticated commands also can be.
*/
isMotion = false;
public isMotion = false;

canBeRepeatedWithDot = false;
public canBeRepeatedWithDot = false;

/**
* Modes that this action can be run in.
Expand Down
6 changes: 3 additions & 3 deletions src/mode/remapper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as vscode from 'vscode';
import * as _ from 'lodash';
import * as util from '../util';
import { ModeName } from './mode';
import { ModeHandler, VimState } from './modeHandler';
import { AngleBracketNotation } from './../notation';

interface IKeybinding {
before: string[];
Expand All @@ -24,10 +24,10 @@ class Remapper {

for (let remapping of remappings) {
let before: string[] = [];
remapping.before.forEach(item => before.push(util.translateToAngleBracketNotation(item)));
remapping.before.forEach(item => before.push(AngleBracketNotation.Normalize(item)));

let after: string[] = [];
remapping.after.forEach(item => after.push(util.translateToAngleBracketNotation(item)));
remapping.after.forEach(item => after.push(AngleBracketNotation.Normalize(item)));

this._remappings.push(<IKeybinding> {
before: before,
Expand Down
34 changes: 34 additions & 0 deletions src/notation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class AngleBracketNotation {
private static _notationMap : { [key:string] : string[]; } = {
'C-': ['ctrl\\+', 'c\\-'],
'Esc': ['escape', 'esc'],
'BS': ['backspace', 'bs'],
'Del': ['delete', 'del'],
};

/**
* Normalizes key to AngleBracketNotation
* For instance, <ctrl+x>, Ctrl+x, <c-x> normalized to <C-x>
*/
public static Normalize(key: string): string {
if (!this.IsSurroundedByAngleBrackets(key) && key.length > 1) {
key = `<${ key.toLocaleLowerCase() }>`;
}

for (const notationMapKey in this._notationMap) {
if (this._notationMap.hasOwnProperty(notationMapKey)) {
const regex = new RegExp(this._notationMap[notationMapKey].join('|'), 'gi');
if (regex.test(key)) {
key = key.replace(regex, notationMapKey);
break;
}
}
}

return key;
}

private static IsSurroundedByAngleBrackets(key: string): boolean {
return key.startsWith('<') && key.endsWith('>');
}
}
21 changes: 0 additions & 21 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,3 @@ export async function showError(message : string): Promise<{}> {
return vscode.window.showErrorMessage("Vim: " + message);
}

export function translateToAngleBracketNotation(key: string): string {
const angleBracketNotationMap = {
'ctrl+' : 'C-',
'escape': 'Esc',
'backspace': 'BS',
'delete': 'Del',
};

key = key.toLowerCase();
if (!(key.startsWith('<') && key.endsWith('>')) && key.length > 1) {
key = `<${ key }>`;
}

for (const searchKey in angleBracketNotationMap) {
if (angleBracketNotationMap.hasOwnProperty(searchKey)) {
key = key.replace(searchKey, angleBracketNotationMap[searchKey]);
}
}

return key;
}
24 changes: 24 additions & 0 deletions test/notation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

import * as assert from 'assert';
import { AngleBracketNotation } from '../src/notation';

suite("Notation", () => {
test("Normalize", () => {
let testCases = {
'<CTRL+w>': '<C-w>',
'ctrl+x': '<C-x>',
'CTRL+y': '<C-y>',
'c-z': '<C-z>',
};

for (const test in testCases) {
if (testCases.hasOwnProperty(test)) {
let expected = testCases[test];

let actual = AngleBracketNotation.Normalize(test);
assert.equal(actual, expected);
}
}
});
});

0 comments on commit 206122b

Please sign in to comment.