Skip to content

Commit

Permalink
Refactor motion.
Browse files Browse the repository at this point in the history
Create a base motion class in which cursor and caret extend. The difference
between the two classes:

* Caret's can never have a position that at the very end of the string
* Whenever we do get a styling for caret block, that logic
can be placed in their respective classes.

Another factor for the need to refactor was it was getting cumbersome to
manipulate motions. With this refactor, motions are now composable. For instance,

`new Cursor().left().up().down().move();`

It will only be until the `move()` will it actually move the cursor.
  • Loading branch information
jpoon committed Dec 3, 2015
1 parent d1be37c commit 364d5b5
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 513 deletions.
12 changes: 0 additions & 12 deletions src/cursor/caret.ts

This file was deleted.

247 changes: 0 additions & 247 deletions src/cursor/cursor.ts

This file was deleted.

18 changes: 11 additions & 7 deletions src/mode/modeInsert.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import * as vscode from 'vscode';
import {ModeName, Mode} from './mode';
import TextEditor from './../textEditor';
import Cursor from './../cursor/cursor';
import {Cursor} from './../motion/motion';

export default class InsertMode extends Mode {

private activationKeyHandler : { [ key : string] : () => Thenable<void> | void; };
private activationKeyHandler : { [ key : string] : (cursor : Cursor) => Thenable<void> | void; };
private cursor : Cursor = new Cursor();

constructor() {
super(ModeName.Insert);

this.activationKeyHandler = {
// insert at cursor
"i" : () => { Cursor.move(Cursor.currentPosition()); },
"i" : (cursor) => {
// nothing
},

// insert at the beginning of the line
"I" : () => { Cursor.move(Cursor.lineBegin()); },
"I" : (cursor) => { cursor.lineBegin().move(); },

// append after the cursor
"a" : () => { Cursor.move(Cursor.right()); },
"a" : (cursor) => { cursor.right().move(); },

// append at the end of the line
"A" : () => { Cursor.move(Cursor.lineEnd()); },
"A" : (cursor) => { cursor.lineEnd().move(); },

// open blank line below current line
"o" : () => {
Expand All @@ -40,7 +43,8 @@ export default class InsertMode extends Mode {
}

HandleActivation(key : string) : Thenable<void> | void {
return this.activationKeyHandler[key]();
this.cursor.reset();
return this.activationKeyHandler[key](this.cursor);
}

HandleKeyEvent(key : string) : Thenable<boolean> {
Expand Down
33 changes: 17 additions & 16 deletions src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as _ from 'lodash';
import * as vscode from 'vscode';
import {ModeName, Mode} from './mode';
import {showCmdLine} from './../cmd_line/main';
import Caret from './../cursor/caret';
import TextEditor from './../textEditor';
import {Caret} from './../motion/motion';

export default class CommandMode extends Mode {
private keyHandler : { [key : string] : () => void; } = {};
private caret : Caret = new Caret();

constructor() {
super(ModeName.Normal);
Expand All @@ -15,35 +15,34 @@ export default class CommandMode extends Mode {
":" : () => { showCmdLine(); },
"u" : () => { vscode.commands.executeCommand("undo"); },
"ctrl+r" : () => { vscode.commands.executeCommand("redo"); },
"h" : () => { Caret.move(Caret.left()); },
"j" : () => { Caret.move(Caret.down()); },
"k" : () => { Caret.move(Caret.up()); },
"l" : () => { Caret.move(Caret.right()); },
"$" : () => { Caret.move(Caret.lineEnd()); },
"^" : () => { Caret.move(Caret.lineBegin()); },
"gg" : () => { Caret.move(Caret.firstLineNonBlankChar()); },
"G" : () => { Caret.move(Caret.lastLineNonBlankChar()); },
"w" : () => { Caret.move(Caret.wordRight()); },
"b" : () => { Caret.move(Caret.wordLeft()); },
"h" : () => { this.caret.left().move(); },
"j" : () => { this.caret.down().move(); },
"k" : () => { this.caret.up().move(); },
"l" : () => { this.caret.right().move(); },
"$" : () => { this.caret.lineEnd().move(); },
"^" : () => { this.caret.lineBegin().move(); },
"gg" : () => {this.caret.firstLineNonBlankChar().move(); },
"G" : () => { this.caret.lastLineNonBlankChar().move(); },
"w" : () => { this.caret.wordRight().move(); },
"b" : () => { this.caret.wordLeft().move(); },
">>" : () => { vscode.commands.executeCommand("editor.action.indentLines"); },
"<<" : () => { vscode.commands.executeCommand("editor.action.outdentLines"); },
"dd" : () => { vscode.commands.executeCommand("editor.action.deleteLines"); },
"dw" : () => { vscode.commands.executeCommand("deleteWordRight"); },
"db" : () => { vscode.commands.executeCommand("deleteWordLeft"); },
"esc": () => { vscode.commands.executeCommand("workbench.action.closeMessages"); },
"x" : () => { this.CommandDelete(1); }
// "x" : () => { this.CommandDelete(1); }
"esc": () => { vscode.commands.executeCommand("workbench.action.closeMessages"); }
};
}

ShouldBeActivated(key : string, currentMode : ModeName) : boolean {
if (key === 'esc' || key === 'ctrl+[') {
Caret.move(Caret.left());
return true;
}
}

HandleActivation(key : string) : void {
// do nothing
this.caret.reset();
}

HandleKeyEvent(key : string) : void {
Expand All @@ -65,6 +64,7 @@ export default class CommandMode extends Mode {
}
}

/*
private CommandDelete(n: number) : void {
let pos = Caret.currentPosition();
let end = pos.translate(0, n);
Expand All @@ -77,4 +77,5 @@ export default class CommandMode extends Mode {
}
});
}
*/
}
Loading

0 comments on commit 364d5b5

Please sign in to comment.