From d30450c188058205ce8baf90754d1cfdb55011c1 Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Fri, 20 Nov 2015 01:28:13 -0800 Subject: [PATCH] Implements basic key bindings in the various modes. * Toggling between modes using 'esc' and insert keys Supported Commands: Insert Mode - i, I, a, A, o, O Command Mode - h, j, k, l --- .travis.yml | 8 ++-- appveyor.yml | 1 + extension.ts | 87 ++++++++++++++++++---------------- package.json | 83 ++++++++++++++++---------------- src/mode/mode_command.ts | 4 -- src/mode/mode_handler.ts | 24 +++++----- src/mode/mode_insert.ts | 13 +++-- test/mode/mode_handler.test.ts | 17 +++---- 8 files changed, 122 insertions(+), 115 deletions(-) diff --git a/.travis.yml b/.travis.yml index ff5bc4529342..55ab581a0cef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml index 01f7709cc26d..2f39032e2e66 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,6 @@ environment: node_js_version: "4.1.1" + TSD_GITHUB_TOKEN: "4c7f278997af83ba584aaa9c5722d5ecbbcb1dd9" install: - ps: install-product node $env:node_js_version diff --git a/extension.ts b/extension.ts index 16ffaec55dc9..9d26811b342e 100644 --- a/extension.ts +++ b/extension.ts @@ -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); } diff --git a/package.json b/package.json index 07614c89f2e2..b51fe6543f9c 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/mode/mode_command.ts b/src/mode/mode_command.ts index 11f2caed36b9..7717d1110773 100644 --- a/src/mode/mode_command.ts +++ b/src/mode/mode_command.ts @@ -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 ':': diff --git a/src/mode/mode_handler.ts b/src/mode/mode_handler.ts index 084643df6431..51b6f04e36ea 100644 --- a/src/mode/mode_handler.ts +++ b/src/mode/mode_handler.ts @@ -18,10 +18,10 @@ 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; }); @@ -29,19 +29,17 @@ export default class ModeHandler { 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); @@ -53,14 +51,14 @@ 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 { @@ -68,7 +66,7 @@ export default class ModeHandler { this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); } - this.statusBarItem.text = '-- ' + text + ' --'; + this.statusBarItem.text = (text) ? '-- ' + text + ' --' : ''; this.statusBarItem.show(); } } \ No newline at end of file diff --git a/src/mode/mode_insert.ts b/src/mode/mode_insert.ts index a7dabe4d36dd..ff62b4f3bae4 100644 --- a/src/mode/mode_insert.ts +++ b/src/mode/mode_insert.ts @@ -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); + } }; } @@ -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; diff --git a/test/mode/mode_handler.test.ts b/test/mode/mode_handler.test.ts index e87d44d78ffd..9334466a6405 100644 --- a/test/mode/mode_handler.test.ts +++ b/test/mode/mode_handler.test.ts @@ -1,26 +1,27 @@ import * as assert from 'assert'; import {ModeName} from '../../src/mode/mode'; import ModeHandler from '../../src/mode/mode_handler'; +import * as vscode from 'vscode'; suite("Mode Handler", () => { test("ctor", () => { var modeHandler = new ModeHandler(); - assert.equal(modeHandler.CurrentMode.Name, ModeName.Command); - assert.equal(modeHandler.CurrentMode.IsActive, true); + assert.equal(modeHandler.currentMode.Name, ModeName.Command); + assert.equal(modeHandler.currentMode.IsActive, true); }); test("can set current mode", () => { var modeHandler = new ModeHandler(); - modeHandler.SetCurrentModeByName(ModeName.Command); - assert.equal(modeHandler.CurrentMode.Name, ModeName.Command); + modeHandler.setCurrentModeByName(ModeName.Command); + assert.equal(modeHandler.currentMode.Name, ModeName.Command); - modeHandler.SetCurrentModeByName(ModeName.Insert); - assert.equal(modeHandler.CurrentMode.Name, ModeName.Insert); + modeHandler.setCurrentModeByName(ModeName.Insert); + assert.equal(modeHandler.currentMode.Name, ModeName.Insert); - modeHandler.SetCurrentModeByName(ModeName.Visual); - assert.equal(modeHandler.CurrentMode.Name, ModeName.Visual); + modeHandler.setCurrentModeByName(ModeName.Visual); + assert.equal(modeHandler.currentMode.Name, ModeName.Visual); }); });