Skip to content

Commit

Permalink
handle indenting
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Nov 25, 2015
1 parent 85e6748 commit f668fb8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
7 changes: 6 additions & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
48 changes: 30 additions & 18 deletions src/mode/mode_command.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
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] : (position : vscode.Position) => void; } = {};

constructor() {
super(ModeName.Command);

this.keyHandler = {
":" : (position) => { showCmdLine() },
"h" : (position) => { vscode.commands.executeCommand("cursorLeft"); },
"j" : (position) => { vscode.commands.executeCommand("cursorDown"); },
"k" : (position) => { vscode.commands.executeCommand("cursorUp"); },
"l" : (position) => { vscode.commands.executeCommand("cursorRight"); },
">>" : (position) => { vscode.commands.executeCommand("editor.action.indentLines"); },
"<<" : (position) => { vscode.commands.executeCommand("editor.action.outdentLines"); },
};
}

ShouldBeActivated(key : string, currentMode : ModeName) : boolean {
return (key === 'esc');
return (key === 'esc' || key == 'ctrl+[');
}

HandleActivation(key : string) : void {
Expand All @@ -17,23 +30,22 @@ export default class CommandMode extends Mode {

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

const editor = vscode.window.activeTextEditor;
const currentPosition = editor.selection.active;

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");
break;
var keyHandled = false;
for (var window = 1; window <= 2 && window <= this.keyHistory.length && keyHandled === false; window++) {
var keys : string = _.takeRight(this.keyHistory, window).join('').toString();

if (this.keyHandler[keys] !== undefined) {
keyHandled = true;
this.keyHandler[keys](currentPosition);
}
}

if (keyHandled) {
this.keyHistory = [];
}
}
}

0 comments on commit f668fb8

Please sign in to comment.