Skip to content

Commit

Permalink
fix: ignore remappings with non-existent commands. fixes #3295
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Dec 28, 2018
1 parent d4d63ec commit 24631fa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/configuration/configurationValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as vscode from 'vscode';

export class ConfigurationValidator {
private static map: Map<string, boolean>;

public static async IsCommandValid(command: string): Promise<boolean> {
if (command.startsWith(':')) {
return true;
}

if (!this.map) {
this.map = new Map(
(await vscode.commands.getCommands(true)).map(x => [x, true] as [string, boolean])
);
}

return this.map.get(command) || false;
}
}
40 changes: 32 additions & 8 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as _ from 'lodash';
import * as vscode from 'vscode';

import { commandLine } from '../cmd_line/commandLine';
import { configuration } from '../configuration/configuration';
import { ModeName } from '../mode/mode';
import { ConfigurationValidator } from './configurationValidator';
import { IKeyRemapping } from './iconfiguration';
import { ModeHandler } from '../mode/modeHandler';
import { ModeName } from '../mode/mode';
import { VimState } from './../state/vimState';
import { IKeyRemapping } from './iconfiguration';
import { configuration } from '../configuration/configuration';
import { logger } from '../util/logger';
import { commandLine } from '../cmd_line/commandLine';

interface IRemapper {
/**
Expand Down Expand Up @@ -82,7 +83,7 @@ export class Remapper implements IRemapper {
const userDefinedRemappings = this._getRemappings();

let remapping: IKeyRemapping | undefined = Remapper._findMatchingRemap(
userDefinedRemappings,
await userDefinedRemappings,
keys,
vimState.currentMode
);
Expand Down Expand Up @@ -174,22 +175,36 @@ export class Remapper implements IRemapper {
return false;
}

private _getRemappings(): { [key: string]: IKeyRemapping } {
private async _getRemappings(): Promise<{ [key: string]: IKeyRemapping }> {
// Create a null object so that there is no __proto__
let remappings: { [key: string]: IKeyRemapping } = Object.create(null);

for (let remapping of configuration[this._configKey] as IKeyRemapping[]) {
let debugMsg = `before=${remapping.before}. `;

if (remapping.after) {
debugMsg += `after=${remapping.after}. `;
}

let isCommandValid = true;
if (remapping.commands) {
for (const command of remapping.commands) {
let cmd: string;
let args: any[];

if (typeof command === 'string') {
debugMsg += `command=${command}. args=.`;
cmd = command;
args = [];
} else {
debugMsg += `command=${command.command}. args=${command.args}.`;
cmd = command.command;
args = command.args;
}

debugMsg += `command=${cmd}. args=${args}.`;

if (!ConfigurationValidator.IsCommandValid(cmd)) {
isCommandValid = false;
break;
}
}
}
Expand All @@ -203,6 +218,15 @@ export class Remapper implements IRemapper {
continue;
}

if (!isCommandValid) {
logger.error(
`Remapper: ${
this._configKey
}. Invalid configuration. Command does not exist. ${debugMsg}.`
);
continue;
}

const keys = remapping.before.join('');
if (keys in remappings) {
logger.error(`Remapper: ${this._configKey}. Duplicate configuration. ${debugMsg}`);
Expand Down

0 comments on commit 24631fa

Please sign in to comment.