Skip to content

Commit

Permalink
Merge pull request #3314 from VSCodeVim/fix3307
Browse files Browse the repository at this point in the history
fix: log warning if remapped command does not exist. closes #3307
  • Loading branch information
jpoon authored Jan 1, 2019
2 parents 1ccb766 + 4c94ebb commit 3dd6b48
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 38 deletions.
19 changes: 10 additions & 9 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export async function activate(context: vscode.ExtensionContext) {
if (mh.vimState.currentMode !== ModeName.Insert) {
let text = compositionState.composingText;
compositionState.reset();
await mh.handleMultipleKeyEvents(text.split(''));
mh.handleMultipleKeyEvents(text.split(''));
}
});
});
Expand All @@ -287,9 +287,9 @@ export async function activate(context: vscode.ExtensionContext) {
// Check if this is a vim command by looking for :
if (command.command.slice(0, 1) === ':') {
await commandLine.Run(command.command.slice(1, command.command.length), mh.vimState);
await mh.updateView(mh.vimState);
mh.updateView(mh.vimState);
} else {
await vscode.commands.executeCommand(command.command, command.args);
vscode.commands.executeCommand(command.command, command.args);
}
}
}
Expand All @@ -311,12 +311,13 @@ export async function activate(context: vscode.ExtensionContext) {
mh.updateView(mh.vimState, { drawSelection: false, revealRange: false });
}

await commandLine.load();
await globalState.loadSearchHistory();
await configurationValidator.initialize();

// This is called last because getAndUpdateModeHandler() will change cursor
toggleExtension(configuration.disableExt, compositionState);
await Promise.all([
commandLine.load(),
globalState.load(),
configurationValidator.initialize(),
// This is called last because getAndUpdateModeHandler() will change cursor
toggleExtension(configuration.disableExt, compositionState),
]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/cmd_line/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CommandLine {
this._history = new CommandLineHistory();
}

public load(): Promise<void> {
public async load(): Promise<void> {
return this._history.load();
}

Expand All @@ -56,7 +56,7 @@ class CommandLine {
const useNeovim = configuration.enableNeovim && cmd.command && cmd.command.neovimCapable;

if (useNeovim) {
var statusBarText = await vimState.nvim.run(vimState, command);
const statusBarText = await vimState.nvim.run(vimState, command);
StatusBar.Set(statusBarText, vimState.currentMode, vimState.isRecordingMacro, true);
} else {
await cmd.execute(vimState.editor, vimState);
Expand All @@ -74,7 +74,7 @@ class CommandLine {
);
}
} else {
logger.error(`commandLine : error executing cmd=${command}. err=${e}.`);
logger.error(`commandLine: error executing cmd=${command}. err=${e}.`);
Message.ShowError(e.toString());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_line/commands/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class WriteCommand extends node.CommandBase {
Message.ShowError('Not implemented.');
return;
}

// defer saving the file to vscode if file is new (to present file explorer) or if file is a remote file
if (vimState.editor.document.isUntitled || vimState.editor.document.uri.scheme !== 'file') {
await vscode.commands.executeCommand('workbench.action.files.save');
Expand Down
28 changes: 14 additions & 14 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { IKeyRemapping } from './iconfiguration';
import { ModeHandler } from '../mode/modeHandler';
import { ModeName } from '../mode/mode';
import { VimState } from './../state/vimState';
import { commandLine } from '../cmd_line/commandLine';
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 @@ -85,7 +85,7 @@ export class Remapper implements IRemapper {
logger.debug(
`Remapper: find matching remap. keys=${keys}. mode=${ModeName[vimState.currentMode]}.`
);
let remapping: IKeyRemapping | undefined = Remapper._findMatchingRemap(
let remapping: IKeyRemapping | undefined = Remapper.findMatchingRemap(
userDefinedRemappings,
keys,
vimState.currentMode
Expand Down Expand Up @@ -206,6 +206,7 @@ export class Remapper implements IRemapper {
debugMsg += `command=${cmd}. args=${args}.`;

if (!configurationValidator.isCommandValid(cmd)) {
debugMsg += ` Command does not exist.`;
isCommandValid = false;
break;
}
Expand All @@ -221,36 +222,35 @@ 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;
}

if (!isCommandValid) {
logger.warn(
`Remapper: ${this._configKey}.
${debugMsg}
If you are referencing a command from an extension that has not yet been activated, this message can be ignored.`
);
}

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

return remappings;
}

protected static _findMatchingRemap(
protected static findMatchingRemap(
userDefinedRemappings: { [key: string]: IKeyRemapping },
inputtedKeys: string[],
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('');
Expand Down Expand Up @@ -281,7 +281,7 @@ export class Remapper implements IRemapper {
* Given list of remappings, returns the length of the shortest and longest remapped keys
* @param remappings
*/
protected static _getRemappedKeysLengthRange(remappings: {
protected static getRemappedKeysLengthRange(remappings: {
[key: string]: IKeyRemapping;
}): [number, number] {
const keys = Object.keys(remappings);
Expand Down
2 changes: 1 addition & 1 deletion src/history/historyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class HistoryFile {
try {
let parsedData = JSON.parse(data);
if (!Array.isArray(parsedData)) {
throw Error('Expected JSON');
throw Error('Unexpected format in history file. Expected JSON.');
}
this._history = parsedData;
} catch (e) {
Expand Down
16 changes: 10 additions & 6 deletions src/state/globalState.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { JumpTracker } from '../jumps/jumpTracker';
import { ModeName } from '../mode/mode';
import { Position } from '../common/motion/position';
import { RecordedState } from './../state/recordedState';
import { SubstituteState } from './substituteState';
import { SearchState, SearchDirection } from './searchState';
import { SearchHistory } from '../history/historyFile';
import { Position } from '../common/motion/position';
import { ModeName } from '../mode/mode';
import { SearchState, SearchDirection } from './searchState';
import { SubstituteState } from './substituteState';

/**
* State which stores global state (across editors)
Expand Down Expand Up @@ -46,6 +46,11 @@ export class GlobalState {
*/
private static _jumpTracker: JumpTracker = new JumpTracker();

/**
* Tracks search history
*/
private static _searchHistory: SearchHistory | undefined;

/**
* Getters and setters for changing global state
*/
Expand All @@ -57,8 +62,7 @@ export class GlobalState {
GlobalState._searchStatePrevious = GlobalState._searchStatePrevious.concat(states);
}

private static _searchHistory: SearchHistory | undefined;
public async loadSearchHistory() {
public async load() {
if (GlobalState._searchHistory === undefined) {
GlobalState._searchHistory = new SearchHistory();
await GlobalState._searchHistory.load();
Expand Down
2 changes: 1 addition & 1 deletion src/util/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { configuration } from '../configuration/configuration';
import * as winston from 'winston';
import { ConsoleForElectron } from 'winston-console-for-electron';
import { configuration } from '../configuration/configuration';

export const logger = winston.createLogger({
level: configuration.debug.loggingLevel,
Expand Down
4 changes: 3 additions & 1 deletion src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export async function getCursorsAfterSync(timeoutInMilliseconds: number = 0): Pr
try {
await waitForCursorSync(timeoutInMilliseconds, true);
} catch (e) {
logger.warn(`getCursorsAfterSync: selection not updated within ${timeoutInMilliseconds}ms. error=${e}.`);
logger.warn(
`getCursorsAfterSync: selection not updated within ${timeoutInMilliseconds}ms. error=${e}.`
);
}

return vscode.window.activeTextEditor!.selections.map(
Expand Down
4 changes: 2 additions & 2 deletions test/configuration/remapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ suite('Remapper', () => {
inputtedKeys: string[],
currentMode: ModeName
) {
return TestRemapper._findMatchingRemap(userDefinedRemappings, inputtedKeys, currentMode);
return TestRemapper.findMatchingRemap(userDefinedRemappings, inputtedKeys, currentMode);
}

public getRemappedKeySequenceLengthRange(remappings: {
[key: string]: IKeyRemapping;
}): [number, number] {
return TestRemapper._getRemappedKeysLengthRange(remappings);
return TestRemapper.getRemappedKeysLengthRange(remappings);
}
}

Expand Down

0 comments on commit 3dd6b48

Please sign in to comment.