Skip to content

Commit

Permalink
fix: single-key remappings in insert mode were being ignored. closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Jan 30, 2019
1 parent e7dd011 commit 798763a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IAutoSwitchInputMethod {
switchIMCmd: string;
obtainIMCmd: string;
}

export interface IDebugConfiguration {
/**
* Boolean indicating whether all logs should be suppressed
Expand Down
37 changes: 17 additions & 20 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,31 +269,28 @@ export class ModeHandler implements vscode.Disposable {
this.vimState.recordedState.commandList.push(key);

try {
// Take the count prefix out to perform the correct remapping.
const withinTimeout = now - this.vimState.lastKeyPressedTimestamp < configuration.timeout;
const isOperatorCombination = this.vimState.recordedState.operator;

let handled = false;
const isOperatorCombination = this.vimState.recordedState.operator;

/**
* Check that
*
* 1) We are not already performing a nonrecursive remapping.
* 2) We aren't in normal mode performing on an operator
* Note: ciwjj should be remapped if jj -> <Esc> in insert mode
* dd should not remap the second "d", if d -> "_d in normal mode
* 3) We haven't timed out of our previous remapping.
*/
// Check for remapped keys if:
// 1. We are not currently performing a non-recursive remapping
// 2. We are not in normal mode performing on an oeprator
// Example: ciwjj should be remapped if jj -> <Esc> in insert mode
// dd should not remap the second "d", if d -> "_d in normal mode
if (
!this.vimState.isCurrentlyPerformingRemapping &&
(!isOperatorCombination || this.vimState.currentMode !== ModeName.Normal) &&
(withinTimeout || this.vimState.recordedState.commandList.length === 1)
(!isOperatorCombination || this.vimState.currentMode !== ModeName.Normal)
) {
handled = await this._remappers.sendKey(
this.vimState.recordedState.commandList,
this,
this.vimState
);
const isWithinTimeout = now - this.vimState.lastKeyPressedTimestamp < configuration.timeout;

let keysToRemap = this.vimState.recordedState.commandList;
if (!isWithinTimeout) {
// sufficient time has elapsed since the last keypress,
// only consider the last keypress for remapping
keysToRemap = [keysToRemap[keysToRemap.length - 1]];
}

handled = await this._remappers.sendKey(keysToRemap, this, this.vimState);
}

if (handled) {
Expand Down
41 changes: 41 additions & 0 deletions test/configuration/remapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ suite('Remapper', () => {
before: ['j', 'j'],
after: ['<Esc>'],
},
{
before: ['<c-e>'],
after: ['<Esc>'],
}
];
const defaultNormalModeKeyBindings: IKeyRemapping[] = [
{
Expand Down Expand Up @@ -297,6 +301,43 @@ suite('Remapper', () => {
assert.equal(vscode.window.visibleTextEditors.length, 0);
});

test('<c-e> -> <esc> in insert mode should go to normal mode', async () => {
const expectedDocumentContent = 'lorem ipsum';

// setup
await setupWithBindings({
insertModeKeyBindings: defaultInsertModeKeyBindings,
normalModeKeyBindings: defaultNormalModeKeyBindings,
visualModeKeyBindings: defaultVisualModeKeyBindings,
});

let remapper = new Remappers();

const edit = new vscode.WorkspaceEdit();
edit.insert(
vscode.window.activeTextEditor!.document.uri,
new vscode.Position(0, 0),
expectedDocumentContent
);
vscode.workspace.applyEdit(edit);

await modeHandler.handleKeyEvent('i');
assertEqual(modeHandler.currentMode.name, ModeName.Insert);

// act
let actual = false;
try {
actual = await remapper.sendKey(['<c-e>'], modeHandler, modeHandler.vimState);
} catch (e) {
assert.fail(e);
}

// assert
assert.equal(actual, true);
assertEqual(modeHandler.currentMode.name, ModeName.Normal);
assert.equal(vscode.window.activeTextEditor!.document.getText(), expectedDocumentContent);
});

test('leader, w -> closeActiveEditor in normal mode through modehandler', async () => {
// setup
await setupWithBindings({
Expand Down

0 comments on commit 798763a

Please sign in to comment.