Skip to content

Commit

Permalink
Implement undo and dw
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Nov 26, 2015
1 parent 2ced57b commit edae94b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
20 changes: 5 additions & 15 deletions src/mode/modeInsert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ModeName, Mode} from './mode';
import * as vscode from 'vscode';
import {ModeName, Mode} from './mode';
import TextEditor from './../textEditor';

export default class InsertMode extends Mode {
private activationKeyHandler : { [ key : string] : (position : vscode.Position) => vscode.Position; } = {};
Expand Down Expand Up @@ -39,24 +40,13 @@ export default class InsertMode extends Mode {
}

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

var newPosition = this.activationKeyHandler[key](currentPosition);
var newSelection = new vscode.Selection(newPosition, newPosition);

editor.selection = newSelection;
var newPosition = this.activationKeyHandler[key](TextEditor.GetCurrentPosition());
TextEditor.SetCurrentPosition(newPosition);
}

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

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

editor.edit(t => {
t.insert(position, this.ResolveKeyValue(key));
});
TextEditor.Insert(this.ResolveKeyValue(key));
}

// Some keys have names that are different to their value.
Expand Down
17 changes: 9 additions & 8 deletions src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export default class CommandMode extends Mode {
super(ModeName.Normal);

this.keyHandler = {
":" : () => { showCmdLine(); },
":" : () => { showCmdLine(); },
"u" : () => { vscode.commands.executeCommand("undo"); },
"h" : () => { vscode.commands.executeCommand("cursorLeft"); },
"j" : () => { vscode.commands.executeCommand("cursorDown"); },
"k" : () => { vscode.commands.executeCommand("cursorUp"); },
"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"); }
"dd" : () => { vscode.commands.executeCommand("editor.action.deleteLines"); },
"dw" : () => { vscode.commands.executeCommand("deleteWordRight"); }
};
}

Expand All @@ -30,10 +32,11 @@ export default class CommandMode extends Mode {
}

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

let keyHandled = false;
for (let window = 0; window <= this.keyHistory.length; window++) {
var keysPressed = key + _.takeRight(this.keyHistory, window).join('');

for (let window = 1; window <= this.keyHistory.length; window++) {
var keysPressed = _.takeRight(this.keyHistory, window).join('');
if (this.keyHandler[keysPressed] !== undefined) {
keyHandled = true;
this.keyHandler[keysPressed]();
Expand All @@ -43,8 +46,6 @@ export default class CommandMode extends Mode {

if (keyHandled) {
this.keyHistory = [];
} else {
this.keyHistory.push(key);
}
}
}
46 changes: 46 additions & 0 deletions src/textEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as vscode from "vscode";

export default class TextEditor {
static Insert(text: string, position: vscode.Position = null) {
if (position === null) {
position = vscode.window.activeTextEditor.selection.active;
}

vscode.window.activeTextEditor.edit((editBuilder) => {
editBuilder.insert(position, text);
});
}

static Delete(range: vscode.Range) {
vscode.window.activeTextEditor.edit((editBuilder) => {
editBuilder.delete(range);
});
}

static Replace(range: vscode.Range, text: string) {
vscode.window.activeTextEditor.edit((editBuilder) => {
editBuilder.replace(range, text);
});
}

static ReadLine(lineNo: number = null): string {
if (lineNo === null) {
lineNo = vscode.window.activeTextEditor.selection.active.line;
}

if (vscode.window.activeTextEditor.document.lineCount < lineNo) {
throw new RangeError();
}

return vscode.window.activeTextEditor.document.lineAt(lineNo).text;
}

static GetCurrentPosition(): vscode.Position {
return vscode.window.activeTextEditor.selection.active;
}

static SetCurrentPosition(position: vscode.Position) {
var newSelection = new vscode.Selection(position, position);
vscode.window.activeTextEditor.selection = newSelection;
}
}

0 comments on commit edae94b

Please sign in to comment.