Skip to content

Commit

Permalink
fix: fixes circular dependency between notation and configuration. cl…
Browse files Browse the repository at this point in the history
…oses #2275
  • Loading branch information
jpoon committed Jan 10, 2018
1 parent 63be67e commit 8d3504d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export async function activate(context: vscode.ExtensionContext) {
const mh = await getAndUpdateModeHandler();
if (args.after) {
for (const key of args.after) {
await mh.handleKeyEvent(Notation.NormalizeKey(key));
await mh.handleKeyEvent(Notation.NormalizeKey(key, Configuration.leader));
}
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/cmd_line/commands/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export class FileCommand extends node.CommandBase {
await vscode.commands.executeCommand('workbench.action.files.newUntitledFile');
await vscode.commands.executeCommand('workbench.action.closeOtherEditors');
}

return;
} else if (this._arguments.name === '') {
if (this._arguments.position === FilePosition.NewWindow) {
Expand Down
12 changes: 4 additions & 8 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ class ConfigurationClass {
}
}

// resolve and normalize leader key
this.leader =
this.leader.toLocaleLowerCase() === '<leader>'
? this.leaderDefault
: Notation.NormalizeKey(this.leader);
this.leader = Notation.NormalizeKey(this.leader, this.leaderDefault);

// normalize keys
const keybindingList: IKeyRemapping[][] = [
Expand All @@ -102,13 +98,13 @@ class ConfigurationClass {
for (let remapping of keybindings) {
if (remapping.before) {
remapping.before.forEach(
(key, idx) => (remapping.before[idx] = Notation.NormalizeKey(key))
(key, idx) => (remapping.before[idx] = Notation.NormalizeKey(key, this.leader))
);
}

if (remapping.after) {
remapping.after.forEach(
(key, idx) => (remapping.after![idx] = Notation.NormalizeKey(key))
(key, idx) => (remapping.after![idx] = Notation.NormalizeKey(key, this.leader))
);
}
}
Expand All @@ -129,7 +125,7 @@ class ConfigurationClass {
}

this.boundKeyCombinations.push({
key: Notation.NormalizeKey(key),
key: Notation.NormalizeKey(key, this.leader),
command: keybinding.command,
});
}
Expand Down
6 changes: 2 additions & 4 deletions src/configuration/notation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as _ from 'lodash';

import { Configuration } from './configuration';

export class Notation {
// Mapping from the nomalized string to regex strings that could match it.
private static _notationMap: { [key: string]: string[] } = {
Expand All @@ -17,7 +15,7 @@ export class Notation {
* (e.g. <ctrl+x>, Ctrl+x, <c-x> normalized to <C-x>)
* and resolves special cases such as '<leader>'
*/
public static NormalizeKey(key: string): string {
public static NormalizeKey(key: string, leaderKey: string): string {
if (!this.isSurroundedByAngleBrackets(key) && key.length > 1) {
key = `<${key.toLocaleLowerCase()}>`;
}
Expand All @@ -32,7 +30,7 @@ export class Notation {
}

if (key.toLocaleLowerCase() === '<leader>') {
return Configuration.leader;
return leaderKey;
}

if (_.includes(['<up>', '<down>', '<left>', '<right>'], key.toLocaleLowerCase())) {
Expand Down
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const ErrorMessage: IErrorMessage = {
208: 'Error writing to file',
348: 'No string under cursor',
444: 'Cannot close last window',
492: 'Not an editor command',
488: 'Trailing characters',
492: 'Not an editor command',
};

export class VimError extends Error {
Expand Down
6 changes: 5 additions & 1 deletion test/notation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import { Notation } from '../src/configuration/notation';

suite('Notation', () => {
test('Normalize', () => {
let leaderKey = '//';
let testCases = {
'<cTrL+w>': '<C-w>',
'cTrL+x': '<C-x>',
'CtRl+y': '<C-y>',
'c-z': '<C-z>',
'<CmD+a>': '<D-a>',
eScapE: '<Esc>',
'<LeAder>': '//',
};

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

let actual = Notation.NormalizeKey(test);
let actual = Notation.NormalizeKey(test, leaderKey);
assert.equal(actual, expected);
}
}
Expand Down

0 comments on commit 8d3504d

Please sign in to comment.