Skip to content

Commit

Permalink
Rework finding of matching remaps and disable recursive mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
xmbhasin committed Jul 31, 2018
1 parent d03a473 commit f3e4311
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export class Remapper implements IRemapper {
}. command=${remapping.commands}.`
);

if (!this._recursive) {
vimState.isCurrentlyPerformingRemapping = true;
}
// if (!this._recursive) {
vimState.isCurrentlyPerformingRemapping = true;
// }

const numCharsToRemove = remapping.before.length - 1;
// Revert previously inserted characters
Expand Down Expand Up @@ -220,32 +220,53 @@ export class Remapper implements IRemapper {
currentMode: ModeName
) {
let remapping: IKeyRemapping | undefined;
// let range = Remapper._getRemappedKeysLengthRange(userDefinedRemappings);

let range = Remapper._getRemappedKeysLengthRange(userDefinedRemappings);
const startingSliceLength = Math.max(range[1], inputtedKeys.length);
for (let sliceLength = startingSliceLength; sliceLength >= range[0]; sliceLength--) {
const keySlice = inputtedKeys.slice(-sliceLength).join('');

if (keySlice in userDefinedRemappings) {
// In Insert mode, we allow users to precede remapped commands
// with extraneous keystrokes (eg. "hello world jj")
// In other modes, we have to precisely match the keysequence
// unless the preceding keys are numbers
if (currentMode !== ModeName.Insert) {
const precedingKeys = inputtedKeys
.slice(0, inputtedKeys.length - keySlice.length)
.join('');
if (precedingKeys.length > 0 && !/^[0-9]+$/.test(precedingKeys)) {
break;
}
const keysString = inputtedKeys.join(' ');

const mostRecentMatchingKeysIndex = Object.keys(userDefinedRemappings).reduce(
(acc, remappingKey) => {
const max = Math.max(acc[0], keysString.lastIndexOf(remappingKey));
if (acc[0] !== max) {
return [max, remappingKey.length];
} else {
return acc;
}
},
[-1, 0]
);
const mostRecentMatchingKeys = keysString.slice(
mostRecentMatchingKeysIndex[0],
mostRecentMatchingKeysIndex[0] + mostRecentMatchingKeysIndex[1]
);

remapping = userDefinedRemappings[keySlice];
break;
}
}
remapping = userDefinedRemappings[mostRecentMatchingKeys];

return remapping;

// for (let sliceLength = startingSliceLength; sliceLength >= range[0]; sliceLength--) {
// const keySlice = inputtedKeys.slice(-sliceLength).join('');

// if (keySlice in userDefinedRemappings) {
// // In Insert mode, we allow users to precede remapped commands
// // with extraneous keystrokes (eg. "hello world jj")
// // In other modes, we have to precisely match the keysequence
// // unless the preceding keys are numbers
// if (currentMode !== ModeName.Insert) {
// const precedingKeys = inputtedKeys
// .slice(0, inputtedKeys.length - keySlice.length)
// .join('');
// if (precedingKeys.length > 0 && !/^[0-9]+$/.test(precedingKeys)) {
// break;
// }
// }

// remapping = userDefinedRemappings[keySlice];
// break;
// }
// }

// return remapping;
}

/**
Expand Down

0 comments on commit f3e4311

Please sign in to comment.