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 31, 2019
1 parent a44bb51 commit b158b87
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 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
5 changes: 3 additions & 2 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Remapper implements IRemapper {
ModeName[vimState.currentMode]
}. keybindings=${this._configKey}.`
);
let remapping: IKeyRemapping | undefined = Remapper.findMatchingRemap(
let remapping: IKeyRemapping | undefined = this.findMatchingRemap(
userDefinedRemappings,
keys,
vimState.currentMode
Expand Down Expand Up @@ -181,7 +181,7 @@ export class Remapper implements IRemapper {
}
}

protected static findMatchingRemap(
protected findMatchingRemap(
userDefinedRemappings: Map<string, IKeyRemapping>,
inputtedKeys: string[],
currentMode: ModeName
Expand All @@ -197,6 +197,7 @@ export class Remapper implements IRemapper {
for (let sliceLength = startingSliceLength; sliceLength >= range[0]; sliceLength--) {
const keySlice = inputtedKeys.slice(-sliceLength).join('');

this._logger.verbose(`trying to find matching remap for keySlice=${keySlice}.`);
if (userDefinedRemappings.has(keySlice)) {
// In Insert mode, we allow users to precede remapped commands
// with extraneous keystrokes (eg. "hello world jj")
Expand Down
31 changes: 17 additions & 14 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +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;
const isWithinTimeout = now - this.vimState.lastKeyPressedTimestamp < configuration.timeout;
if (!isWithinTimeout) {
// sufficient time has elapsed since the prior keypress,
// only consider the last keypress for remapping
this.vimState.recordedState.commandList = [
this.vimState.recordedState.commandList[
this.vimState.recordedState.commandList.length - 1
],
];
}

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 operator
// 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,
Expand Down
43 changes: 42 additions & 1 deletion 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 @@ -77,7 +81,7 @@ suite('Remapper', () => {
inputtedKeys: string[],
currentMode: ModeName
) {
return TestRemapper.findMatchingRemap(userDefinedRemappings, inputtedKeys, currentMode);
return super.findMatchingRemap(userDefinedRemappings, inputtedKeys, currentMode);
}

public getRemappedKeySequenceLengthRange(
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 b158b87

Please sign in to comment.