Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Non-Recursive mapping capability. Fixes issue #408 #589

Merged
merged 1 commit into from
Aug 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
"type": "array",
"description": "Keybinding overrides to use for normal mode."
},
"vim.otherModesKeyBindingsNonRecursive": {
"type": "array",
"description": "Non-recursive keybinding overrides to use for normal mode."
},
"vim.useCtrlKeys": {
"type": "boolean",
"description": "Enable some vim ctrl key commands that override otherwise common operations, like ctrl+c"
Expand All @@ -163,6 +167,10 @@
"type": "array",
"description": "Keybinding overrides to use for insert mode."
},
"vim.insertModeKeyBindingsNonRecursive": {
"type": "array",
"description": "Non-recursive keybinding overrides to use for insert mode."
},
"vim.useSolidBlockCursor": {
"type": "boolean",
"description": "Use a non blinking block cursor."
Expand Down
25 changes: 20 additions & 5 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export class VimState {

public focusChanged = false;

/**
* Used to prevent non-recursive remappings from looping.
*/
public isCurrentlyPreformingRemapping = false;

/**
* The current full action we are building up.
*/
Expand Down Expand Up @@ -407,6 +412,9 @@ export class ModeHandler implements vscode.Disposable {
private _vimState: VimState;
private _insertModeRemapper: InsertModeRemapper;
private _otherModesRemapper: OtherModesRemapper;
private _otherModesNonRecursive: OtherModesRemapper;
private _insertModeNonRecursive: InsertModeRemapper;


public get vimState(): VimState {
return this._vimState;
Expand Down Expand Up @@ -451,8 +459,11 @@ export class ModeHandler implements vscode.Disposable {
this.filename = filename;

this._vimState = new VimState();
this._insertModeRemapper = new InsertModeRemapper();
this._otherModesRemapper = new OtherModesRemapper();
this._insertModeRemapper = new InsertModeRemapper(true);
this._otherModesRemapper = new OtherModesRemapper(true);
this._insertModeNonRecursive = new InsertModeRemapper(false);
this._otherModesNonRecursive = new OtherModesRemapper(false);

this._modes = [
new NormalMode(this),
new InsertMode(),
Expand Down Expand Up @@ -600,9 +611,13 @@ export class ModeHandler implements vscode.Disposable {

try {
let handled = false;

handled = handled || await this._insertModeRemapper.sendKey(key, this, this.vimState);
handled = handled || await this._otherModesRemapper.sendKey(key, this, this.vimState);
if (!this._vimState.isCurrentlyPreformingRemapping) {
// Non-recursive remapping do not allow for further mappings
handled = handled || await this._insertModeRemapper.sendKey(key, this, this.vimState);
handled = handled || await this._otherModesRemapper.sendKey(key, this, this.vimState);
handled = handled || await this._insertModeNonRecursive.sendKey(key, this, this.vimState);
handled = handled || await this._otherModesNonRecursive.sendKey(key, this, this.vimState);
}

if (!handled) {
this._vimState = await this.handleKeyEventHelper(key, this._vimState);
Expand Down
29 changes: 21 additions & 8 deletions src/mode/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class Remapper {

private _remappedModes: ModeName[];

constructor(configKey: string, remappedModes: ModeName[]) {
private _recursive: boolean;

constructor(configKey: string, remappedModes: ModeName[], recursive: boolean) {
this._recursive = recursive;
this._remappedModes = remappedModes;
this._remappings = vscode.workspace.getConfiguration('vim')
.get<IKeybinding[]>(configKey, []);
Expand Down Expand Up @@ -53,8 +56,15 @@ class Remapper {
vimState.historyTracker.undoAndRemoveChanges(this._mostRecentKeys.length);
}

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

await modeHandler.handleMultipleKeyEvents(remapping.after);

// Since this should always be false after remapping, save the cycles by skipping the check
vimState.isCurrentlyPreformingRemapping = false;

this._mostRecentKeys = [];

return true;
Expand All @@ -70,19 +80,22 @@ class Remapper {
}

export class InsertModeRemapper extends Remapper {
constructor() {
constructor(recursive: boolean) {
super(
"insertModeKeyBindings",
[ModeName.Insert]
"insertModeKeyBindings" + (recursive ? "" : "NonRecursive"),
[ModeName.Insert],
recursive
);
}
}

export class OtherModesRemapper extends Remapper {
constructor() {
constructor(recursive: boolean) {
super(
"otherModesKeyBindings",
[ModeName.Normal, ModeName.Visual, ModeName.VisualLine]
"otherModesKeyBindings" + (recursive ? "" : "NonRecursive"),
[ModeName.Normal, ModeName.Visual, ModeName.VisualLine],
recursive
);
}
}
}