From 04dd825cd1b2f393d0b0cb54e2f26f62c1ac023e Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Thu, 26 Nov 2015 01:41:36 -0800 Subject: [PATCH 1/3] update lodash tsd to latest --- tsd.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsd.json b/tsd.json index 45ec9477a97..48d4c3eafa8 100644 --- a/tsd.json +++ b/tsd.json @@ -6,7 +6,7 @@ "bundle": "typings/tsd.d.ts", "installed": { "lodash/lodash.d.ts": { - "commit": "c560ba6f0f55ce6f50e9d06e37b5bc0e2b9117b0" + "commit": "36c6ce86fb64bafb28ec95dc78c6b98228b45c8d" } } } From 79a17719b3eb7caee95b15c6424f3e2269d9dd88 Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Thu, 26 Nov 2015 01:42:05 -0800 Subject: [PATCH 2/3] support indentation and dd --- extension.ts | 7 ++++- package.json | 6 ++++- src/mode/mode_normal.ts | 51 +++++++++++++++++++++-------------- test/mode/mode_normal.test.ts | 17 ++++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 test/mode/mode_normal.test.ts diff --git a/extension.ts b/extension.ts index ee972ffc278..bde09568f52 100644 --- a/extension.ts +++ b/extension.ts @@ -91,7 +91,12 @@ export function activate(context: vscode.ExtensionContext) { vscode.commands.registerCommand('extension.vim_7', () => handleKeyEvent("7")); vscode.commands.registerCommand('extension.vim_8', () => handleKeyEvent("8")); vscode.commands.registerCommand('extension.vim_9', () => handleKeyEvent("9")); - + + vscode.commands.registerCommand('extension.vim_ctrl_[', () => handleKeyEvent("ctrl+[")); + + vscode.commands.registerCommand('extension.vim_<', () => handleKeyEvent("<")); + vscode.commands.registerCommand('extension.vim_>', () => handleKeyEvent(">")); + context.subscriptions.push(cmdLineDisposable); } diff --git a/package.json b/package.json index 9561590a77e..ebf565debbb 100644 --- a/package.json +++ b/package.json @@ -105,8 +105,12 @@ { "key": "Ctrl+J", "command": "cursorDown", "when": "editorTextFocus" }, { "key": "Ctrl+K", "command": "cursorUp", "when": "editorTextFocus" }, { "key": "Ctrl+L", "command": "cursorRight", "when": "editorTextFocus" }, + { "key": "Ctrl+[", "command": "extension.vim_ctrl_[", "when": "editorTextFocus" }, - { "key": "Ctrl+Shift+.", "command": "extension.showCmdLine", "when": "editorTextFocus" } + { "key": "Ctrl+Shift+.", "command": "extension.showCmdLine", "when": "editorTextFocus" }, + + { "key": "Shift+,", "command": "extension.vim_<", "when": "editorTextFocus" }, + { "key": "Shift+.", "command": "extension.vim_>", "when": "editorTextFocus" } ] }, "scripts": { diff --git a/src/mode/mode_normal.ts b/src/mode/mode_normal.ts index 7bb4c50ca57..b80b80f0d5e 100644 --- a/src/mode/mode_normal.ts +++ b/src/mode/mode_normal.ts @@ -1,39 +1,50 @@ +import * as _ from 'lodash'; +import * as vscode from 'vscode'; import {ModeName, Mode} from './mode'; import {showCmdLine} from './../cmd_line/main'; -import * as vscode from 'vscode'; export default class CommandMode extends Mode { + private keyHandler : { [key : string] : () => void; } = {}; + constructor() { super(ModeName.Normal); + + this.keyHandler = { + ":" : () => { showCmdLine(); }, + "h" : () => { vscode.commands.executeCommand("cursorLeft"); }, + "j" : () => { vscode.commands.executeCommand("cursorDown"); }, + "k" : () => { vscode.commands.executeCommand("cursorUp"); }, + "l" : () => { vscode.commands.executeCommand("cursorRight"); }, + ">>" : () => { vscode.commands.executeCommand("editor.action.indentLines"); }, + "<<" : () => { vscode.commands.executeCommand("editor.action.outdentLines"); }, + "dd" : () => { vscode.commands.executeCommand("editor.action.deleteLines"); } + }; } ShouldBeActivated(key : string, currentMode : ModeName) : boolean { - return (key === 'esc'); + return (key === 'esc' || key === 'ctrl+['); } HandleActivation(key : string) : void { // do nothing - } + } HandleKeyEvent(key : string) : void { - this.keyHistory.push(key); - - switch (key) { - case ':': - showCmdLine(); - break; - 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"); + let keyHandled = false; + for (let window = 0; window <= this.keyHistory.length; window++) { + var keysPressed = key + _.takeRight(this.keyHistory, window).join(''); + + if (this.keyHandler[keysPressed] !== undefined) { + keyHandled = true; + this.keyHandler[keysPressed](); break; + } + } + + if (keyHandled) { + this.keyHistory = []; + } else { + this.keyHistory.push(key); } } } \ No newline at end of file diff --git a/test/mode/mode_normal.test.ts b/test/mode/mode_normal.test.ts new file mode 100644 index 00000000000..e583976f537 --- /dev/null +++ b/test/mode/mode_normal.test.ts @@ -0,0 +1,17 @@ +import * as assert from 'assert'; + +import ModeNormal from '../../src/mode/mode_normal'; +import {ModeName} from '../../src/mode/mode'; + +suite("Mode Normal", () => { + test("can be activated", () => { + let activationKeys = ['esc', 'ctrl+[']; + + let modeHandler = new ModeNormal(); + + for (let i = 0; i < activationKeys.length; i++) { + let key = activationKeys[i]; + assert.equal(modeHandler.ShouldBeActivated(key, ModeName.Insert), true, key); + } + }); +}); From 4e190a66267a26c35f773a692991838c3a6d3da3 Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Thu, 26 Nov 2015 01:45:20 -0800 Subject: [PATCH 3/3] organize keybindings -- remove ones that were created for testing --- extension.ts | 5 ++--- package.json | 6 ------ 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/extension.ts b/extension.ts index bde09568f52..13f08a72f85 100644 --- a/extension.ts +++ b/extension.ts @@ -54,9 +54,6 @@ export function activate(context: vscode.ExtensionContext) { 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.vim_B', () => handleKeyEvent("B")); vscode.commands.registerCommand('extension.vim_C', () => handleKeyEvent("C")); vscode.commands.registerCommand('extension.vim_D', () => handleKeyEvent("D")); @@ -64,11 +61,13 @@ export function activate(context: vscode.ExtensionContext) { 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")); diff --git a/package.json b/package.json index ebf565debbb..078d8614072 100644 --- a/package.json +++ b/package.json @@ -101,14 +101,8 @@ { "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" }, - { "key": "Ctrl+K", "command": "cursorUp", "when": "editorTextFocus" }, - { "key": "Ctrl+L", "command": "cursorRight", "when": "editorTextFocus" }, { "key": "Ctrl+[", "command": "extension.vim_ctrl_[", "when": "editorTextFocus" }, - { "key": "Ctrl+Shift+.", "command": "extension.showCmdLine", "when": "editorTextFocus" }, - { "key": "Shift+,", "command": "extension.vim_<", "when": "editorTextFocus" }, { "key": "Shift+.", "command": "extension.vim_>", "when": "editorTextFocus" } ]