2016-05-28 01:49:35 +03:00
|
|
|
"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().
|
|
|
|
*/
|
2016-02-07 09:37:46 +03:00
|
|
|
|
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';
|
2016-02-07 09:37:46 +03:00
|
|
|
import {ModeHandler} from "./src/mode/modeHandler";
|
2015-11-12 22:51:40 +03:00
|
|
|
|
2016-05-28 01:49:35 +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.
|
|
|
|
*/
|
2016-05-28 01:49:35 +03:00
|
|
|
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) {
|
2016-01-02 10:02:42 +03:00
|
|
|
extensionContext = context;
|
2015-11-17 23:41:38 +03:00
|
|
|
|
2016-03-24 10:28:55 +03:00
|
|
|
registerCommand(context, 'type', async (args) => {
|
2016-05-28 01:49:35 +03:00
|
|
|
if (!vscode.window.activeTextEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:28:55 +03:00
|
|
|
var isHandled = await handleKeyEvent(args.text);
|
|
|
|
|
2016-05-28 01:49:35 +03:00
|
|
|
if (!isHandled) {
|
2016-03-24 10:28:55 +03:00
|
|
|
vscode.commands.executeCommand('default:type', {
|
|
|
|
text: args.text
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-05-28 01:49:35 +03:00
|
|
|
|
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);
|
2015-11-29 10:11:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerCommand(context: vscode.ExtensionContext, command: string, callback: (...args: any[]) => any) {
|
2016-02-07 09:37:46 +03:00
|
|
|
let disposable = vscode.commands.registerCommand(command, callback);
|
2015-11-29 10:11:55 +03:00
|
|
|
context.subscriptions.push(disposable);
|
2015-11-17 09:14:48 +03:00
|
|
|
}
|
|
|
|
|
2016-06-19 09:20:19 +03:00
|
|
|
async function handleKeyEvent(key: string): Promise<Boolean> {
|
2016-01-02 10:02:42 +03:00
|
|
|
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-02 10:02:42 +03:00
|
|
|
}
|
2016-01-30 22:41:53 +03:00
|
|
|
|
2016-03-24 10:28:55 +03:00
|
|
|
return modeHandler.handleKeyEvent(key);
|
2016-06-18 22:13:07 +03:00
|
|
|
}
|