Skip to content

Commit

Permalink
Implements basic key bindings in the various modes.
Browse files Browse the repository at this point in the history
* Toggling between modes using 'esc' and insert keys

Supported Commands:
Insert Mode - i, I, a, A, o, O
Command Mode - h, j, k, l
  • Loading branch information
jpoon committed Nov 20, 2015
1 parent 37145b3 commit d30450c
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 115 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ language: node_js
node_js:
- "0.12"
install:
- npm install
- npm install
before_script:
- npm install -g gulp
- gulp init
- npm install -g gulp
- gulp init
script:
- gulp
- gulp
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
environment:
node_js_version: "4.1.1"
TSD_GITHUB_TOKEN: "4c7f278997af83ba584aaa9c5722d5ecbbcb1dd9"

install:
- ps: install-product node $env:node_js_version
Expand Down
87 changes: 45 additions & 42 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,55 @@ export function activate(context: vscode.ExtensionContext) {
showCmdLine();
});

vscode.commands.registerCommand('extension.vimMode_esc', () => handleKeyEvent("esc"));
vscode.commands.registerCommand('extension.vimMode_colon', () => handleKeyEvent(":"));
vscode.commands.registerCommand('extension.vimMode_i', () => handleKeyEvent("i"));
vscode.commands.registerCommand('extension.vimMode_I', () => handleKeyEvent("I"));
vscode.commands.registerCommand('extension.vimMode_h', () => handleKeyEvent("h"));
vscode.commands.registerCommand('extension.vimMode_j', () => handleKeyEvent("j"));
vscode.commands.registerCommand('extension.vimMode_k', () => handleKeyEvent("k"));
vscode.commands.registerCommand('extension.vimMode_l', () => handleKeyEvent("l"));

vscode.commands.registerCommand('extension.vimMode_a', () => handleKeyEvent("a"));
vscode.commands.registerCommand('extension.vimMode_b', () => handleKeyEvent("b"));
vscode.commands.registerCommand('extension.vimMode_c', () => handleKeyEvent("c"));
vscode.commands.registerCommand('extension.vimMode_d', () => handleKeyEvent("d"));
vscode.commands.registerCommand('extension.vimMode_e', () => handleKeyEvent("e"));
vscode.commands.registerCommand('extension.vimMode_f', () => handleKeyEvent("f"));
vscode.commands.registerCommand('extension.vimMode_g', () => handleKeyEvent("g"));
vscode.commands.registerCommand('extension.vimMode_m', () => handleKeyEvent("m"));
vscode.commands.registerCommand('extension.vimMode_n', () => handleKeyEvent("n"));
vscode.commands.registerCommand('extension.vimMode_o', () => handleKeyEvent("o"));
vscode.commands.registerCommand('extension.vimMode_p', () => handleKeyEvent("p"));
vscode.commands.registerCommand('extension.vimMode_q', () => handleKeyEvent("q"));
vscode.commands.registerCommand('extension.vimMode_r', () => handleKeyEvent("r"));
vscode.commands.registerCommand('extension.vimMode_s', () => handleKeyEvent("s"));
vscode.commands.registerCommand('extension.vimMode_t', () => handleKeyEvent("t"));
vscode.commands.registerCommand('extension.vimMode_u', () => handleKeyEvent("u"));
vscode.commands.registerCommand('extension.vimMode_v', () => handleKeyEvent("v"));
vscode.commands.registerCommand('extension.vimMode_w', () => handleKeyEvent("w"));
vscode.commands.registerCommand('extension.vimMode_x', () => handleKeyEvent("x"));
vscode.commands.registerCommand('extension.vimMode_y', () => handleKeyEvent("y"));
vscode.commands.registerCommand('extension.vimMode_z', () => handleKeyEvent("z"));
vscode.commands.registerCommand('extension.vimMode_space', () => handleKeyEvent("space"));
vscode.commands.registerCommand('extension.vim_esc', () => handleKeyEvent("esc"));
vscode.commands.registerCommand('extension.vim_colon', () => handleKeyEvent(":"));
vscode.commands.registerCommand('extension.vim_space', () => handleKeyEvent("space"));

vscode.commands.registerCommand('extension.vim_a', () => handleKeyEvent("a"));
vscode.commands.registerCommand('extension.vim_b', () => handleKeyEvent("b"));
vscode.commands.registerCommand('extension.vim_c', () => handleKeyEvent("c"));
vscode.commands.registerCommand('extension.vim_d', () => handleKeyEvent("d"));
vscode.commands.registerCommand('extension.vim_e', () => handleKeyEvent("e"));
vscode.commands.registerCommand('extension.vim_f', () => handleKeyEvent("f"));
vscode.commands.registerCommand('extension.vim_g', () => handleKeyEvent("g"));
vscode.commands.registerCommand('extension.vim_h', () => handleKeyEvent("h"));
vscode.commands.registerCommand('extension.vim_i', () => handleKeyEvent("i"));
vscode.commands.registerCommand('extension.vim_j', () => handleKeyEvent("j"));
vscode.commands.registerCommand('extension.vim_k', () => handleKeyEvent("k"));
vscode.commands.registerCommand('extension.vim_l', () => handleKeyEvent("l"));
vscode.commands.registerCommand('extension.vim_m', () => handleKeyEvent("m"));
vscode.commands.registerCommand('extension.vim_n', () => handleKeyEvent("n"));
vscode.commands.registerCommand('extension.vim_o', () => handleKeyEvent("o"));
vscode.commands.registerCommand('extension.vim_p', () => handleKeyEvent("p"));
vscode.commands.registerCommand('extension.vim_q', () => handleKeyEvent("q"));
vscode.commands.registerCommand('extension.vim_r', () => handleKeyEvent("r"));
vscode.commands.registerCommand('extension.vim_s', () => handleKeyEvent("s"));
vscode.commands.registerCommand('extension.vim_t', () => handleKeyEvent("t"));
vscode.commands.registerCommand('extension.vim_u', () => handleKeyEvent("u"));
vscode.commands.registerCommand('extension.vim_v', () => handleKeyEvent("v"));
vscode.commands.registerCommand('extension.vim_w', () => handleKeyEvent("w"));
vscode.commands.registerCommand('extension.vim_x', () => handleKeyEvent("x"));
vscode.commands.registerCommand('extension.vim_y', () => handleKeyEvent("y"));
vscode.commands.registerCommand('extension.vim_z', () => handleKeyEvent("z"));

vscode.commands.registerCommand('extension.vim_A', () => handleKeyEvent("A"));
vscode.commands.registerCommand('extension.vim_I', () => handleKeyEvent("I"));
vscode.commands.registerCommand('extension.vim_O', () => handleKeyEvent("O"));

vscode.commands.registerCommand('extension.vimMode_0', () => handleKeyEvent("0"));
vscode.commands.registerCommand('extension.vimMode_1', () => handleKeyEvent("1"));
vscode.commands.registerCommand('extension.vimMode_2', () => handleKeyEvent("2"));
vscode.commands.registerCommand('extension.vimMode_3', () => handleKeyEvent("3"));
vscode.commands.registerCommand('extension.vimMode_4', () => handleKeyEvent("4"));
vscode.commands.registerCommand('extension.vimMode_5', () => handleKeyEvent("5"));
vscode.commands.registerCommand('extension.vimMode_6', () => handleKeyEvent("6"));
vscode.commands.registerCommand('extension.vimMode_7', () => handleKeyEvent("7"));
vscode.commands.registerCommand('extension.vimMode_8', () => handleKeyEvent("8"));
vscode.commands.registerCommand('extension.vimMode_9', () => handleKeyEvent("9"));
vscode.commands.registerCommand('extension.vim_0', () => handleKeyEvent("0"));
vscode.commands.registerCommand('extension.vim_1', () => handleKeyEvent("1"));
vscode.commands.registerCommand('extension.vim_2', () => handleKeyEvent("2"));
vscode.commands.registerCommand('extension.vim_3', () => handleKeyEvent("3"));
vscode.commands.registerCommand('extension.vim_4', () => handleKeyEvent("4"));
vscode.commands.registerCommand('extension.vim_5', () => handleKeyEvent("5"));
vscode.commands.registerCommand('extension.vim_6', () => handleKeyEvent("6"));
vscode.commands.registerCommand('extension.vim_7', () => handleKeyEvent("7"));
vscode.commands.registerCommand('extension.vim_8', () => handleKeyEvent("8"));
vscode.commands.registerCommand('extension.vim_9', () => handleKeyEvent("9"));

context.subscriptions.push(cmdLineDisposable);
}

function handleKeyEvent(key:string) {
modeHandler.HandleKeyEvent(key);
modeHandler.handleKeyEvent(key);
}
83 changes: 43 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,51 @@
}
],
"keybindings": [
{ "key": "Escape", "command": "extension.vimMode_esc", "when": "editorTextFocus" },
{ "key": ":", "command": "extension.vimMode_colon", "when": "editorTextFocus" },
{ "key": "i", "command": "extension.vimMode_i", "when": "editorTextFocus" },
{ "key": "Shift+i", "command": "extension.vimMode_I", "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": "Escape", "command": "extension.vim_esc", "when": "editorTextFocus" },
{ "key": "Shift+;", "command": "extension.vim_colon", "when": "editorTextFocus" },
{ "key": "space", "command": "extension.vim_space", "when": "editorTextFocus" },

{ "key": "a", "command": "extension.vimMode_a", "when": "editorTextFocus" },
{ "key": "b", "command": "extension.vimMode_b", "when": "editorTextFocus" },
{ "key": "c", "command": "extension.vimMode_c", "when": "editorTextFocus" },
{ "key": "d", "command": "extension.vimMode_d", "when": "editorTextFocus" },
{ "key": "e", "command": "extension.vimMode_e", "when": "editorTextFocus" },
{ "key": "f", "command": "extension.vimMode_f", "when": "editorTextFocus" },
{ "key": "g", "command": "extension.vimMode_g", "when": "editorTextFocus" },
{ "key": "m", "command": "extension.vimMode_m", "when": "editorTextFocus" },
{ "key": "n", "command": "extension.vimMode_n", "when": "editorTextFocus" },
{ "key": "o", "command": "extension.vimMode_o", "when": "editorTextFocus" },
{ "key": "p", "command": "extension.vimMode_p", "when": "editorTextFocus" },
{ "key": "q", "command": "extension.vimMode_q", "when": "editorTextFocus" },
{ "key": "r", "command": "extension.vimMode_r", "when": "editorTextFocus" },
{ "key": "s", "command": "extension.vimMode_s", "when": "editorTextFocus" },
{ "key": "t", "command": "extension.vimMode_t", "when": "editorTextFocus" },
{ "key": "u", "command": "extension.vimMode_u", "when": "editorTextFocus" },
{ "key": "v", "command": "extension.vimMode_v", "when": "editorTextFocus" },
{ "key": "w", "command": "extension.vimMode_w", "when": "editorTextFocus" },
{ "key": "x", "command": "extension.vimMode_x", "when": "editorTextFocus" },
{ "key": "y", "command": "extension.vimMode_y", "when": "editorTextFocus" },
{ "key": "z", "command": "extension.vimMode_z", "when": "editorTextFocus" },
{ "key": "space", "command": "extension.vimMode_space", "when": "editorTextFocus" },
{ "key": "a", "command": "extension.vim_a", "when": "editorTextFocus" },
{ "key": "b", "command": "extension.vim_b", "when": "editorTextFocus" },
{ "key": "c", "command": "extension.vim_c", "when": "editorTextFocus" },
{ "key": "d", "command": "extension.vim_d", "when": "editorTextFocus" },
{ "key": "e", "command": "extension.vim_e", "when": "editorTextFocus" },
{ "key": "f", "command": "extension.vim_f", "when": "editorTextFocus" },
{ "key": "g", "command": "extension.vim_g", "when": "editorTextFocus" },
{ "key": "h", "command": "extension.vim_h", "when": "editorTextFocus" },
{ "key": "i", "command": "extension.vim_i", "when": "editorTextFocus" },
{ "key": "j", "command": "extension.vim_j", "when": "editorTextFocus" },
{ "key": "k", "command": "extension.vim_k", "when": "editorTextFocus" },
{ "key": "l", "command": "extension.vim_l", "when": "editorTextFocus" },
{ "key": "m", "command": "extension.vim_m", "when": "editorTextFocus" },
{ "key": "n", "command": "extension.vim_n", "when": "editorTextFocus" },
{ "key": "o", "command": "extension.vim_o", "when": "editorTextFocus" },
{ "key": "p", "command": "extension.vim_p", "when": "editorTextFocus" },
{ "key": "q", "command": "extension.vim_q", "when": "editorTextFocus" },
{ "key": "r", "command": "extension.vim_r", "when": "editorTextFocus" },
{ "key": "s", "command": "extension.vim_s", "when": "editorTextFocus" },
{ "key": "t", "command": "extension.vim_t", "when": "editorTextFocus" },
{ "key": "u", "command": "extension.vim_u", "when": "editorTextFocus" },
{ "key": "v", "command": "extension.vim_v", "when": "editorTextFocus" },
{ "key": "w", "command": "extension.vim_w", "when": "editorTextFocus" },
{ "key": "x", "command": "extension.vim_x", "when": "editorTextFocus" },
{ "key": "y", "command": "extension.vim_y", "when": "editorTextFocus" },
{ "key": "z", "command": "extension.vim_z", "when": "editorTextFocus" },

{ "key": "0", "command": "extension.vimMode_0", "when": "editorTextFocus" },
{ "key": "1", "command": "extension.vimMode_1", "when": "editorTextFocus" },
{ "key": "2", "command": "extension.vimMode_2", "when": "editorTextFocus" },
{ "key": "3", "command": "extension.vimMode_3", "when": "editorTextFocus" },
{ "key": "4", "command": "extension.vimMode_4", "when": "editorTextFocus" },
{ "key": "5", "command": "extension.vimMode_5", "when": "editorTextFocus" },
{ "key": "6", "command": "extension.vimMode_6", "when": "editorTextFocus" },
{ "key": "7", "command": "extension.vimMode_7", "when": "editorTextFocus" },
{ "key": "8", "command": "extension.vimMode_8", "when": "editorTextFocus" },
{ "key": "9", "command": "extension.vimMode_9", "when": "editorTextFocus" },
{ "key": "Shift+a", "command": "extension.vim_A", "when": "editorTextFocus" },
{ "key": "Shift+i", "command": "extension.vim_I", "when": "editorTextFocus" },
{ "key": "Shift+o", "command": "extension.vim_O", "when": "editorTextFocus" },

{ "key": "0", "command": "extension.vim_0", "when": "editorTextFocus" },
{ "key": "1", "command": "extension.vim_1", "when": "editorTextFocus" },
{ "key": "2", "command": "extension.vim_2", "when": "editorTextFocus" },
{ "key": "3", "command": "extension.vim_3", "when": "editorTextFocus" },
{ "key": "4", "command": "extension.vim_4", "when": "editorTextFocus" },
{ "key": "5", "command": "extension.vim_5", "when": "editorTextFocus" },
{ "key": "6", "command": "extension.vim_6", "when": "editorTextFocus" },
{ "key": "7", "command": "extension.vim_7", "when": "editorTextFocus" },
{ "key": "8", "command": "extension.vim_8", "when": "editorTextFocus" },
{ "key": "9", "command": "extension.vim_9", "when": "editorTextFocus" },

{ "key": "Ctrl+H", "command": "cursorLeft", "when": "editorTextFocus" },
{ "key": "Ctrl+J", "command": "cursorDown", "when": "editorTextFocus" },
Expand Down
4 changes: 0 additions & 4 deletions src/mode/mode_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export default class CommandMode extends Mode {

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

// XXX - remove this?
var commands = vscode.commands.getCommands();
commands.then(c => console.log(c));

switch (key) {
case ':':
Expand Down
24 changes: 11 additions & 13 deletions src/mode/mode_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,28 @@ export default class ModeHandler {
new VisualMode(),
];

this.SetCurrentModeByName(ModeName.Command);
this.setCurrentModeByName(ModeName.Command);
}

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

return currentMode;
}

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

// Vim never shows -- NORMAL -- in the status bar.
if (this.CurrentMode.Name !== ModeName.Command) {
this.setupStatusBarItem(ModeName[modeName].toUpperCase());
}
var statusBarText = (this.currentMode.Name === ModeName.Command) ? '' : ModeName[modeName];
this.setupStatusBarItem(statusBarText.toUpperCase());
}

HandleKeyEvent(key : string) : void {
var currentModeName = this.CurrentMode.Name;
handleKeyEvent(key : string) : void {
var currentModeName = this.currentMode.Name;

var nextMode : Mode;
var inactiveModes = _.filter(this.modes, (m) => !m.IsActive);
Expand All @@ -53,22 +51,22 @@ export default class ModeHandler {
});

if (nextMode) {
this.CurrentMode.HandleDeactivation();
this.currentMode.HandleDeactivation();

nextMode.HandleActivation(key);
this.SetCurrentModeByName(nextMode.Name);
this.setCurrentModeByName(nextMode.Name);
return;
}

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

private setupStatusBarItem(text : string) : void {
if (!this.statusBarItem) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}

this.statusBarItem.text = '-- ' + text + ' --';
this.statusBarItem.text = (text) ? '-- ' + text + ' --' : '';
this.statusBarItem.show();
}
}
13 changes: 9 additions & 4 deletions src/mode/mode_insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ export default class InsertMode extends Mode {
"I" : (position) => { return new vscode.Position(position.line, 0); },

// append after the cursor
"a" : (position) => { return position; },
"a" : (position) => { return new vscode.Position(position.line, position.character + 1); },

// append at the end of the line
"A" : (position) => { return position; },

// open blank line below current line
"o" : (position) => { return position; },
"o" : (position) => {
vscode.commands.executeCommand("editor.action.insertLineAfter");
return new vscode.Position(position.line + 1, 0);
},

// open blank line above current line
"O" : (position) => { return position; }
"O" : (position) => {
vscode.commands.executeCommand("editor.action.insertLineBefore");
return new vscode.Position(position.line, 0);
}
};
}

Expand All @@ -33,7 +39,6 @@ export default class InsertMode extends Mode {
}

HandleActivation(key : string) : void {
console.log(key);
const editor = vscode.window.activeTextEditor;
const currentPosition = editor.selection.active;

Expand Down
Loading

0 comments on commit d30450c

Please sign in to comment.