Skip to content

Commit

Permalink
fix: validate configurations once, instead of every key press
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Jan 29, 2019
1 parent fbbc1c9 commit ccbb35b
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 160 deletions.
33 changes: 24 additions & 9 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ import { ModeHandler } from './src/mode/modeHandler';
import { ModeHandlerMap } from './src/mode/modeHandlerMap';
import { ModeName } from './src/mode/mode';
import { Notation } from './src/configuration/notation';
import { Logger } from './src/util/logger';
import { Position } from './src/common/motion/position';
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 { globalState } from './src/state/globalState';
import { logger } from './src/util/logger';
import { taskQueue } from './src/taskQueue';

let extensionContext: vscode.ExtensionContext;
Expand Down Expand Up @@ -68,21 +67,38 @@ export async function getAndUpdateModeHandler(forceSyncAndUpdate = false): Promi
return curHandler;
}

async function loadConfiguration() {
const configurationErrors = await configuration.load();
const logger = Logger.get('Configuration');

const numErrors = configurationErrors.filter(e => e.level === 'error').length;
logger.debug(`${numErrors} found with vim configuration`);

if (numErrors > 0) {
for (let configurationError of configurationErrors) {
switch (configurationError.level) {
case 'error':
logger.error(configurationError.message);
break;
case 'warning':
logger.warn(configurationError.message);
break;
}
}
}
}
export async function activate(context: vscode.ExtensionContext) {
logger.debug('Extension: activating vscodevim.');

extensionContext = context;
extensionContext.subscriptions.push(StatusBar);

logger.debug('Extension: registering event handlers.');
await loadConfiguration();

// workspace events
registerEventListener(
context,
vscode.workspace.onDidChangeConfiguration,
() => {
logger.debug('onDidChangeConfiguration: reloading configuration');
configuration.reload();
async () => {
await loadConfiguration();
},
false
);
Expand Down Expand Up @@ -344,7 +360,6 @@ export async function activate(context: vscode.ExtensionContext) {
await Promise.all([
commandLine.load(),
globalState.load(),
configurationValidator.initialize(),
// This is called last because getAndUpdateModeHandler() will change cursor
toggleExtension(configuration.disableExtension, compositionState),
]);
Expand Down
27 changes: 14 additions & 13 deletions src/actions/plugins/imswitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Globals } from '../../globals';
import { ModeName } from '../../mode/mode';
import { configuration } from '../../configuration/configuration';
import { exists } from 'fs';
import { logger } from '../../util/logger';
import { Logger } from '../../util/logger';
import { promisify } from 'util';

/**
Expand All @@ -14,8 +14,9 @@ export class InputMethodSwitcher {
this.execute = execute;
}

private savedIMKey = '';
private execute: (cmd: string) => Promise<string>;
private readonly logger = Logger.get('IMSwitcher');
private savedIMKey = '';

public async switchInputMethod(prevMode: ModeName, newMode: ModeName) {
if (configuration.autoSwitchInputMethod.enable !== true) {
Expand Down Expand Up @@ -48,11 +49,11 @@ export class InputMethodSwitcher {
this.savedIMKey = insertIMKey.trim();
}
} catch (e) {
logger.error(`IMSwitcher: Error switching to default IM. err=${e}`);
this.logger.error(`Error switching to default IM. err=${e}`);
}
} else {
logger.error(
`IMSwitcher: Unable to find ${rawObtainIMCmd}. Check your 'vim.autoSwitchInputMethod.obtainIMCmd' in VSCode setting.`
this.logger.error(
`Unable to find ${rawObtainIMCmd}. Check your 'vim.autoSwitchInputMethod.obtainIMCmd' in VSCode setting.`
);
this.disableIMSwitch();
}
Expand All @@ -79,12 +80,12 @@ export class InputMethodSwitcher {
try {
await this.execute(switchIMCmd);
} catch (e) {
logger.error(`IMSwitcher: Error switching to IM. err=${e}`);
this.logger.error(`Error switching to IM. err=${e}`);
}
}
} else {
logger.error(
`IMSwitcher: Unable to find ${rawSwitchIMCmd}. Check your 'vim.autoSwitchInputMethod.switchIMCmd' in VSCode setting.`
this.logger.error(
`Unable to find ${rawSwitchIMCmd}. Check your 'vim.autoSwitchInputMethod.switchIMCmd' in VSCode setting.`
);
this.disableIMSwitch();
}
Expand All @@ -104,27 +105,27 @@ export class InputMethodSwitcher {
}

private disableIMSwitch() {
logger.warn('IMSwitcher: disabling IMSwitch');
this.logger.warn('disabling IMSwitch');
configuration.autoSwitchInputMethod.enable = false;
}

private isConfigurationValid(): boolean {
let switchIMCmd = configuration.autoSwitchInputMethod.switchIMCmd;
if (!switchIMCmd.includes('{im}')) {
logger.error(
'IMSwitcher: vim.autoSwitchInputMethod.switchIMCmd is incorrect, \
this.logger.error(
'vim.autoSwitchInputMethod.switchIMCmd is incorrect, \
it should contain the placeholder {im}'
);
return false;
}
let obtainIMCmd = configuration.autoSwitchInputMethod.obtainIMCmd;
if (obtainIMCmd === undefined || obtainIMCmd === '') {
logger.error('IMSwitcher: vim.autoSwitchInputMethod.obtainIMCmd is empty');
this.logger.error('vim.autoSwitchInputMethod.obtainIMCmd is empty');
return false;
}
let defaultIMKey = configuration.autoSwitchInputMethod.defaultIM;
if (defaultIMKey === undefined || defaultIMKey === '') {
logger.error('IMSwitcher: vim.autoSwitchInputMethod.defaultIM is empty');
this.logger.error('vim.autoSwitchInputMethod.defaultIM is empty');
return false;
}
return true;
Expand Down
9 changes: 5 additions & 4 deletions src/cmd_line/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as parser from './parser';
import * as vscode from 'vscode';
import { CommandLineHistory } from '../history/historyFile';
import { ModeName } from './../mode/mode';
import { Logger } from '../util/logger';
import { StatusBar } from '../statusBar';
import { VimError, ErrorCode } from '../error';
import { VimState } from '../state/vimState';
import { configuration } from '../configuration/configuration';
import { logger } from '../util/logger';

class CommandLine {
private _history: CommandLineHistory;
private readonly _logger = Logger.get('CommandLine');

/**
* Index used for navigating commandline history with <up> and <down>
Expand Down Expand Up @@ -78,14 +79,14 @@ class CommandLine {
);
}
} else {
logger.error(`commandLine: Error executing cmd=${command}. err=${e}.`);
this._logger.error(`Error executing cmd=${command}. err=${e}.`);
}
}
}

public async PromptAndRun(initialText: string, vimState: VimState): Promise<void> {
if (!vscode.window.activeTextEditor) {
logger.debug('commandLine: No active document');
this._logger.debug('No active document');
return;
}
let cmd = await vscode.window.showInputBox(this.getInputBoxOptions(initialText));
Expand All @@ -103,7 +104,7 @@ class CommandLine {

public async ShowHistory(initialText: string, vimState: VimState): Promise<string | undefined> {
if (!vscode.window.activeTextEditor) {
logger.debug('commandLine: No active document.');
this._logger.debug('No active document.');
return '';
}

Expand Down
7 changes: 4 additions & 3 deletions src/cmd_line/commands/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as node from '../node';
import * as path from 'path';
import * as util from 'util';
import * as vscode from 'vscode';
import { logger } from '../../util/logger';
import untildify = require('untildify');
import { Logger } from '../../util/logger';

const untildify = require('untildify');
const doesFileExist = util.promisify(fs.exists);

export enum FilePosition {
Expand All @@ -23,6 +23,7 @@ export interface IFileCommandArguments extends node.ICommandArgs {

export class FileCommand extends node.CommandBase {
protected _arguments: IFileCommandArguments;
private readonly _logger = Logger.get('File');

constructor(args: IFileCommandArguments) {
super();
Expand Down Expand Up @@ -111,7 +112,7 @@ export class FileCommand extends node.CommandBase {
if (this._arguments.createFileIfNotExists) {
await util.promisify(fs.close)(await util.promisify(fs.open)(filePath, 'w'));
} else {
logger.error(`file: ${filePath} does not exist.`);
this._logger.error(`${filePath} does not exist.`);
return;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/cmd_line/commands/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as node from '../node';
import * as path from 'path';
import * as util from 'util';
import * as vscode from 'vscode';
import { Logger } from '../../util/logger';
import { StatusBar } from '../../statusBar';
import { VimState } from '../../state/vimState';
import { logger } from '../../util/logger';

export interface IWriteCommandArguments extends node.ICommandArgs {
opt?: string;
Expand All @@ -23,6 +23,7 @@ export interface IWriteCommandArguments extends node.ICommandArgs {
//
export class WriteCommand extends node.CommandBase {
protected _arguments: IWriteCommandArguments;
private readonly _logger = Logger.get('Write');

constructor(args: IWriteCommandArguments) {
super();
Expand All @@ -36,16 +37,16 @@ export class WriteCommand extends node.CommandBase {

async execute(vimState: VimState): Promise<void> {
if (this.arguments.opt) {
logger.warn('write: not implemented');
this._logger.warn('not implemented');
return;
} else if (this.arguments.file) {
logger.warn('write: not implemented');
this._logger.warn('not implemented');
return;
} else if (this.arguments.append) {
logger.warn('write: not implemented');
this._logger.warn('not implemented');
return;
} else if (this.arguments.cmd) {
logger.warn('write: not implemented');
this._logger.warn('not implemented');
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/cmd_line/parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as lexer from './lexer';
import * as node from './node';
import * as token from './token';
import { Logger } from '../util/logger';
import { VimError, ErrorCode } from '../error';
import { commandParsers } from './subparser';
import { logger } from '../util/logger';

interface IParseFunction {
(state: ParserState, command: node.CommandLine): IParseFunction | null;
Expand All @@ -20,6 +20,8 @@ export function parse(input: string): node.CommandLine {
}

function parseLineRange(state: ParserState, commandLine: node.CommandLine): IParseFunction | null {
const logger = Logger.get('Parser');

while (true) {
let tok = state.next();
switch (tok.type) {
Expand Down
Loading

0 comments on commit ccbb35b

Please sign in to comment.