Vim/extension.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

"use strict";
/**
* Extension.ts is a lightweight wrapper around ModeHandler. It converts key
* events to their string names and passes them on to ModeHandler via
* handleKeyEvent().
*/
2015-11-13 10:38:13 +03:00
import * as vscode from 'vscode';
2015-11-17 23:41:38 +03:00
import {showCmdLine} from './src/cmd_line/main';
import {ModeHandler} from "./src/mode/modeHandler";
2015-11-12 22:51:40 +03:00
var extensionContext: vscode.ExtensionContext;
2016-06-18 10:21:17 +03:00
/**
* Note: We can't initialize modeHandler here, or even inside activate(), because some people
* see a bug where VSC hasn't fully initialized yet, which pretty much breaks VSCodeVim entirely.
*/
var modeHandler: ModeHandler;
2015-11-17 09:14:48 +03:00
2015-11-12 22:51:40 +03:00
export function activate(context: vscode.ExtensionContext) {
extensionContext = context;
2015-11-17 23:41:38 +03:00
registerCommand(context, 'type', async (args) => {
if (!vscode.window.activeTextEditor) {
return;
}
var isHandled = await handleKeyEvent(args.text);
if (!isHandled) {
vscode.commands.executeCommand('default:type', {
text: args.text
});
}
});
2016-05-29 07:13:31 +03:00
registerCommand(context, 'extension.vim_esc', () => handleKeyEvent("<esc>"));
2016-06-09 12:24:02 +03:00
registerCommand(context, 'extension.vim_backspace', () => handleKeyEvent("<backspace>"));
2016-06-27 10:40:35 +03:00
registerCommand(context, 'extension.vim_switchWindow', () => handleKeyEvent("ctrl+w"));
2016-06-09 12:24:02 +03:00
2016-03-04 17:11:57 +03:00
registerCommand(context, 'extension.showCmdLine', () => {
2016-06-18 10:21:17 +03:00
if (!modeHandler) {
modeHandler = new ModeHandler(false);
extensionContext.subscriptions.push(modeHandler);
}
2016-03-04 17:11:57 +03:00
showCmdLine("", modeHandler);
});
2015-11-17 23:41:38 +03:00
2016-06-27 10:51:21 +03:00
'rfb'.split('').forEach(key=> {
registerCommand(context, `extension.vim_ctrl+${key}`, () => handleKeyEvent(`ctrl+${key}`));
});
2016-01-30 22:41:53 +03:00
context.subscriptions.push(modeHandler);
}
function registerCommand(context: vscode.ExtensionContext, command: string, callback: (...args: any[]) => any) {
let disposable = vscode.commands.registerCommand(command, callback);
context.subscriptions.push(disposable);
2015-11-17 09:14:48 +03:00
}
async function handleKeyEvent(key: string): Promise<Boolean> {
if (!modeHandler) {
2016-06-18 10:21:17 +03:00
modeHandler = new ModeHandler(false);
2016-01-30 22:41:53 +03:00
extensionContext.subscriptions.push(modeHandler);
}
2016-01-30 22:41:53 +03:00
return modeHandler.handleKeyEvent(key);
2016-06-18 22:13:07 +03:00
}