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

fix: validate configurations once, instead of every key press #3418

Merged
merged 8 commits into from
Jan 30, 2019
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
35 changes: 26 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,40 @@ 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} errors 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.');
// before we do anything else,
// we need to load the configuration first
await loadConfiguration();

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

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

// workspace events
registerEventListener(
context,
vscode.workspace.onDidChangeConfiguration,
() => {
logger.debug('onDidChangeConfiguration: reloading configuration');
configuration.reload();
async () => {
await loadConfiguration();
},
false
);
Expand Down Expand Up @@ -344,7 +362,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
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@
"neovim": "4.2.1",
"untildify": "3.0.3",
"winston": "3.1.0",
"winston-console-for-electron": "0.0.5"
"winston-console-for-electron": "0.0.6"
},
"devDependencies": {
"@types/diff": "3.5.2",
Expand Down
35 changes: 7 additions & 28 deletions src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,14 +663,8 @@ class MoveFindForward extends BaseMovement {
!this.isRepeat &&
(!vimState.recordedState.operator || !(isIMovement(result) && result.failed))
) {
vimState.lastSemicolonRepeatableMovement = new MoveFindForward(
this.keysPressed,
true
);
vimState.lastCommaRepeatableMovement = new MoveFindBackward(
this.keysPressed,
true
);
vimState.lastSemicolonRepeatableMovement = new MoveFindForward(this.keysPressed, true);
vimState.lastCommaRepeatableMovement = new MoveFindBackward(this.keysPressed, true);
}

return result;
Expand Down Expand Up @@ -698,14 +692,8 @@ class MoveFindBackward extends BaseMovement {
!this.isRepeat &&
(!vimState.recordedState.operator || !(isIMovement(result) && result.failed))
) {
vimState.lastSemicolonRepeatableMovement = new MoveFindBackward(
this.keysPressed,
true
);
vimState.lastCommaRepeatableMovement = new MoveFindForward(
this.keysPressed,
true
);
vimState.lastSemicolonRepeatableMovement = new MoveFindBackward(this.keysPressed, true);
vimState.lastCommaRepeatableMovement = new MoveFindForward(this.keysPressed, true);
}

return result;
Expand Down Expand Up @@ -742,14 +730,8 @@ class MoveTilForward extends BaseMovement {
!this.isRepeat &&
(!vimState.recordedState.operator || !(isIMovement(result) && result.failed))
) {
vimState.lastSemicolonRepeatableMovement = new MoveTilForward(
this.keysPressed,
true
);
vimState.lastCommaRepeatableMovement = new MoveTilBackward(
this.keysPressed,
true
);
vimState.lastSemicolonRepeatableMovement = new MoveTilForward(this.keysPressed, true);
vimState.lastCommaRepeatableMovement = new MoveTilBackward(this.keysPressed, true);
}

return result;
Expand Down Expand Up @@ -782,10 +764,7 @@ class MoveTilBackward extends BaseMovement {
!this.isRepeat &&
(!vimState.recordedState.operator || !(isIMovement(result) && result.failed))
) {
vimState.lastSemicolonRepeatableMovement = new MoveTilBackward(
this.keysPressed,
true
);
vimState.lastSemicolonRepeatableMovement = new MoveTilBackward(this.keysPressed, true);
vimState.lastCommaRepeatableMovement = new MoveTilForward(this.keysPressed, true);
}

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
10 changes: 2 additions & 8 deletions src/actions/plugins/sneak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ class SneakForward extends BaseMovement {

public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
if (!this.isRepeat) {
vimState.lastSemicolonRepeatableMovement = new SneakForward(
this.keysPressed,
true
);
vimState.lastSemicolonRepeatableMovement = new SneakForward(this.keysPressed, true);
vimState.lastCommaRepeatableMovement = new SneakBackward(this.keysPressed, true);
}

Expand Down Expand Up @@ -79,10 +76,7 @@ class SneakBackward extends BaseMovement {

public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
if (!this.isRepeat) {
vimState.lastSemicolonRepeatableMovement = new SneakBackward(
this.keysPressed,
true
);
vimState.lastSemicolonRepeatableMovement = new SneakBackward(this.keysPressed, true);
vimState.lastCommaRepeatableMovement = new SneakForward(this.keysPressed, true);
}

Expand Down
Loading