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 7374061
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
3 changes: 2 additions & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { StatusBar } from './src/statusBar';
import { VsCodeContext } from './src/util/vscode-context';
import { commandLine } from './src/cmd_line/commandLine';
import { configuration } from './src/configuration/configuration';
import { configurationValidator } from './src/configuration/configurationValidator';
import { logger } from './src/util/logger';
import { taskQueue } from './src/taskQueue';

Expand Down Expand Up @@ -311,8 +312,8 @@ export async function activate(context: vscode.ExtensionContext) {
}

await commandLine.load();
// Initialize the search history
await globalState.loadSearchHistory();
await configurationValidator.initialize();

// This is called last because getAndUpdateModeHandler() will change cursor
toggleExtension(configuration.disableExt, compositionState);
Expand Down
21 changes: 21 additions & 0 deletions src/configuration/configurationValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as vscode from 'vscode';

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

public async initialize(): Promise<void> {
this.map = new Map(
(await vscode.commands.getCommands(true)).map(x => [x, true] as [string, boolean])
);
}

public isCommandValid(command: string): boolean {
if (command.startsWith(':')) {
return true;
}

return this.map.get(command) || false;
}
}

export let configurationValidator = new ConfigurationValidator();
43 changes: 35 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 { 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 { configurationValidator } from './configurationValidator';
import { logger } from '../util/logger';
import { commandLine } from '../cmd_line/commandLine';

interface IRemapper {
/**
Expand Down Expand Up @@ -81,8 +82,11 @@ export class Remapper implements IRemapper {

const userDefinedRemappings = this._getRemappings();

logger.debug(
`Remapper: find matching remap. keys=${keys}. mode=${ModeName[vimState.currentMode]}.`
);
let remapping: IKeyRemapping | undefined = Remapper._findMatchingRemap(
userDefinedRemappings,
await userDefinedRemappings,
keys,
vimState.currentMode
);
Expand Down Expand Up @@ -177,19 +181,33 @@ export class Remapper implements IRemapper {
private _getRemappings(): { [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,13 +221,22 @@ 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}`);
continue;
}

logger.debug(`Remapper: ${this._configKey}. ${debugMsg}`);
logger.debug(`Remapper: ${this._configKey}. loaded: ${debugMsg}`);
remappings[keys] = remapping;
}

Expand Down

0 comments on commit 7374061

Please sign in to comment.