Skip to content

Commit

Permalink
handle vim modes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Nov 16, 2015
1 parent 1b117f2 commit 8953ae3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
12 changes: 11 additions & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ import * as vscode from 'vscode';

import {showCmdLine} from './src/cmd_line/main';
import * as cc from './src/cmd_line/lexer';
import {ModeHandler} from "./src/mode/mode_handler";
import {ModeName} from "./src/mode/mode";

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

var modeHandler = new ModeHandler();

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "vim" is now active!');

var cmdLineDisposable = vscode.commands.registerCommand('extension.showCmdLine', () => {
showCmdLine();
});


vscode.commands.registerCommand('extension.vimMode_esc', () => modeHandler.HandleKeyEvent("esc"));
vscode.commands.registerCommand('extension.vimMode_h', () => modeHandler.HandleKeyEvent("h"));

This comment has been minimized.

Copy link
@guillermooo

guillermooo Nov 16, 2015

Member

curious, does this prevent the the 'h' from being inserted in the text buffer?

This comment has been minimized.

Copy link
@jpoon

jpoon Nov 16, 2015

Author Member

Yeah, it does :(.

My thinking right now is to handle all the key bindings, check what mode you are in, and, if we don't care about it (eg. it's in insert mode), then pass it through.

This sucks cause:

  • We'll need to define all the keybindings in package.json and extension.ts
  • Need to figure out how to pass-through (not sure if VSCode will even allow).

This comment has been minimized.

Copy link
@guillermooo

guillermooo Nov 16, 2015

Member

Yeah, that's what I do in Sublime Text. It's pretty ugly. There must be an insert command or something like that, I suppose.

vscode.commands.registerCommand('extension.vimMode_j', () => modeHandler.HandleKeyEvent("j"));
vscode.commands.registerCommand('extension.vimMode_k', () => modeHandler.HandleKeyEvent("k"));
vscode.commands.registerCommand('extension.vimMode_l', () => modeHandler.HandleKeyEvent("l"));

context.subscriptions.push(cmdLineDisposable);
}
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
{ "command": "extension.showCmdLine", "title": "Vim: Show Command Line" }
],
"keybindings": [
{ "key": "Escape", "command": "extension.vimMode_esc", "when": "editorTextFocus" },
{ "key": "H", "command": "extension.vimMode_h", "when": "editorTextFocus" },
{ "key": "J", "command": "extension.vimMode_j", "when": "editorTextFocus" },
{ "key": "K", "command": "extension.vimMode_k", "when": "editorTextFocus" },
{ "key": "L", "command": "extension.vimMode_l", "when": "editorTextFocus" },

{ "key": "Ctrl+H", "command": "cursorLeft", "when": "editorTextFocus" },
{ "key": "Ctrl+J", "command": "cursorDown", "when": "editorTextFocus" },
{ "key": "Ctrl+K", "command": "cursorUp", "when": "editorTextFocus" },
Expand Down
29 changes: 29 additions & 0 deletions src/mode/mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export enum ModeName {
Normal,
Insert,
Visual,
}

export abstract class Mode {
private _isActive : boolean;
private _name : ModeName;

constructor(name: ModeName, isActive: boolean) {
this._name = name;
this._isActive = isActive || false;
}

get Name(): ModeName {
return this._name;
}

get IsActive():boolean {
return this._isActive;
}

set IsActive(val:boolean) {
this._isActive = val;
}

abstract HandleKeyEvent(key:string) : void;
}
31 changes: 31 additions & 0 deletions src/mode/mode_command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as baseMode from './mode';
import * as vscode from 'vscode';

export class CommandMode extends baseMode.Mode {
_keyHistory: string[];

constructor(isActive?: boolean) {
super(baseMode.ModeName.Normal, isActive);

this._keyHistory = [];
}

HandleKeyEvent(key:string) : void {
this._keyHistory.push(key);

switch(key) {
case 'h':
vscode.commands.executeCommand("cursorLeft");
break;
case 'j':
vscode.commands.executeCommand("cursorDown");
break;
case 'k':
vscode.commands.executeCommand("cursorUp");
break;
case 'l':
vscode.commands.executeCommand("cursorRight");
break;
}
}
}
36 changes: 36 additions & 0 deletions src/mode/mode_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//import * as vscode from 'vscode';
import {commands, window, workspace, ExtensionContext, TextEditorOptions, TextEditor, TextDocument, Disposable} from 'vscode';
import {Mode, ModeName} from './mode';
import {CommandMode} from './mode_command';

export class ModeHandler {
private _modes: Mode[];

constructor() {
this._modes = [
new CommandMode(true),
];

}

get CurrentMode(): Mode {
var currentMode = this._modes.find((mode, index) => {
return mode.IsActive;
});

return currentMode;
}

SetCurrentModeByName(modeName:ModeName) {
this._modes.forEach(mode => {
mode.IsActive = (mode.Name == modeName);
});
}

HandleKeyEvent(key:string) : void {
if (key == "esc") {

}
this.CurrentMode.HandleKeyEvent(key);
}
}

0 comments on commit 8953ae3

Please sign in to comment.