Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support << and >> #32

Merged
merged 3 commits into from
Nov 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,20 @@ 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"));
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"));
Expand All @@ -91,7 +90,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);
}

Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@
{ "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" }
]
},
"scripts": {
Expand Down
51 changes: 31 additions & 20 deletions src/mode/mode_normal.ts
Original file line number Diff line number Diff line change
@@ -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('');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var ==> let ?


if (this.keyHandler[keysPressed] !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be enough? if (this.keyHandler[keysPressed]) ...

keyHandled = true;
this.keyHandler[keysPressed]();
break;
}
}

if (keyHandled) {
this.keyHistory = [];
} else {
this.keyHistory.push(key);
}
}
}
17 changes: 17 additions & 0 deletions test/mode/mode_normal.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
2 changes: 1 addition & 1 deletion tsd.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bundle": "typings/tsd.d.ts",
"installed": {
"lodash/lodash.d.ts": {
"commit": "c560ba6f0f55ce6f50e9d06e37b5bc0e2b9117b0"
"commit": "36c6ce86fb64bafb28ec95dc78c6b98228b45c8d"
}
}
}