Skip to content

Commit

Permalink
Merge pull request #112 from VSCodeVim/motion
Browse files Browse the repository at this point in the history
Motion Fixes
  • Loading branch information
jpoon committed Dec 29, 2015
2 parents da0753f + eebf8bc commit dc00fa9
Show file tree
Hide file tree
Showing 15 changed files with 686 additions and 635 deletions.
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ When submitting a PR, ensure:
2. Fork and clone the repo, then

```bash
$ npm install
$ npm install -g gulp
$ gulp init
$ npm install
```

3. Open the folder in VS Code
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
"postinstall": "gulp init"
},
"devDependencies": {
"gulp": "^3.9.0",
Expand Down
28 changes: 20 additions & 8 deletions src/mode/mode.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import {Motion} from './../motion/motion';

export enum ModeName {
Normal,
Insert,
Visual,
}

export abstract class Mode {
private isActive : boolean;
private name : ModeName;
private _isActive : boolean;
private _name : ModeName;
private _motion : Motion;
protected keyHistory : string[];

constructor(name: ModeName) {
this.name = name;
this.isActive = false;
constructor(name: ModeName, motion : Motion) {
this._name = name;
this._motion = motion;
this._isActive = false;
this.keyHistory = [];
}

get Name(): ModeName {
return this.name;
return this._name;
}

get Motion() : Motion {
return this._motion;
}

set Motion(val : Motion) {
this._motion = val;
}

get IsActive() : boolean {
return this.isActive;
return this._isActive;
}

set IsActive(val : boolean) {
this.isActive = val;
this._isActive = val;
}

public HandleDeactivation() : void {
Expand Down
51 changes: 31 additions & 20 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,52 @@ import * as _ from 'lodash';
import * as vscode from 'vscode';

import {Mode, ModeName} from './mode';
import {Motion, MotionMode} from './../motion/motion';
import NormalMode from './modeNormal';
import InsertMode from './modeInsert';
import VisualMode from './modeVisual';
import Configuration from '../configuration';

export default class ModeHandler {
private modes : Mode[];
private statusBarItem : vscode.StatusBarItem;
configuration : Configuration;
export default class ModeHandler implements vscode.Disposable {
private _motion : Motion;
private _modes : Mode[];
private _statusBarItem : vscode.StatusBarItem;
private _configuration : Configuration;

constructor() {
this.configuration = Configuration.fromUserFile();
this._configuration = Configuration.fromUserFile();

this.modes = [
new NormalMode(),
new InsertMode(),
new VisualMode(),
this._motion = new Motion();
this._modes = [
new NormalMode(this._motion),
new InsertMode(this._motion),
];

this.setCurrentModeByName(ModeName.Normal);
}

get currentMode() : Mode {
var currentMode = this.modes.find((mode, index) => {
var currentMode = this._modes.find((mode, index) => {
return mode.IsActive;
});

return currentMode;
}

setCurrentModeByName(modeName : ModeName) {
this.modes.forEach(mode => {
this._modes.forEach(mode => {
mode.IsActive = (mode.Name === modeName);
});

switch (modeName) {
case ModeName.Insert:
this._motion = this._motion.changeMode(MotionMode.Caret);
break;

case ModeName.Normal:
this._motion = this._motion.changeMode(MotionMode.Cursor);
break;
}

var statusBarText = (this.currentMode.Name === ModeName.Normal) ? '' : ModeName[modeName];
this.setupStatusBarItem(statusBarText.toUpperCase());
}
Expand All @@ -45,11 +56,11 @@ export default class ModeHandler {
// Due to a limitation in Electron, en-US QWERTY char codes are used in international keyboards.
// We'll try to mitigate this problem until it's fixed upstream.
// https://github.com/Microsoft/vscode/issues/713
key = this.configuration.keyboardLayout.translate(key);
key = this._configuration.keyboardLayout.translate(key);

var currentModeName = this.currentMode.Name;
var nextMode : Mode;
var inactiveModes = _.filter(this.modes, (m) => !m.IsActive);
var inactiveModes = _.filter(this._modes, (m) => !m.IsActive);

_.forEach(inactiveModes, (m, i) => {
if (m.ShouldBeActivated(key, currentModeName)) {
Expand All @@ -59,7 +70,6 @@ export default class ModeHandler {

if (nextMode) {
this.currentMode.HandleDeactivation();

nextMode.HandleActivation(key);
this.setCurrentModeByName(nextMode.Name);
return;
Expand All @@ -69,15 +79,16 @@ export default class ModeHandler {
}

private setupStatusBarItem(text : string) : void {
if (!this.statusBarItem) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
if (!this._statusBarItem) {
this._statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}

this.statusBarItem.text = (text) ? '-- ' + text + ' --' : '';
this.statusBarItem.show();
this._statusBarItem.text = (text) ? '-- ' + text + ' --' : '';
this._statusBarItem.show();
}

dispose() {
this.statusBarItem.dispose();
this._statusBarItem.dispose();
this._motion.dispose();
}
}
17 changes: 5 additions & 12 deletions src/mode/modeInsert.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import * as vscode from 'vscode';
import {ModeName, Mode} from './mode';
import TextEditor from './../textEditor';
import {Cursor} from './../motion/motion';
import {Motion} from './../motion/motion';

export default class InsertMode extends Mode {
private _cursor : Cursor;
private get cursor() : Cursor {
this._cursor = this._cursor || new Cursor();
return this._cursor;
}

private activationKeyHandler : { [ key : string] : (cursor : Cursor) => Thenable<{}> } = {
private activationKeyHandler : { [ key : string] : (motion : Motion) => Thenable<{}> } = {
"i" : () => {
// insert at cursor
return Promise.resolve({});
Expand All @@ -37,17 +31,16 @@ export default class InsertMode extends Mode {
}
};

constructor() {
super(ModeName.Insert);
constructor(motion : Motion) {
super(ModeName.Insert, motion);
}

ShouldBeActivated(key : string, currentMode : ModeName) : boolean {
return key in this.activationKeyHandler;
}

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

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

export default class NormalMode extends Mode {
private _caret : Caret;
private get caret() : Caret {
this._caret = this._caret || new Caret();
return this._caret;
}

private keyHandler : { [key : string] : (caret : Caret) => Thenable<{}>; } = {
private keyHandler : { [key : string] : (motion : Motion) => Thenable<{}>; } = {
":" : () => { return showCmdLine(); },
"u" : () => { return vscode.commands.executeCommand("undo"); },
"ctrl+r" : () => { return vscode.commands.executeCommand("redo"); },
Expand All @@ -36,16 +30,17 @@ export default class NormalMode extends Mode {
"esc": () => { return vscode.commands.executeCommand("workbench.action.closeMessages"); }
};

constructor() {
super(ModeName.Normal);
constructor(motion : Motion) {
super(ModeName.Normal, motion);
}

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

HandleActivation(key : string) : Thenable<{}> {
return Promise.resolve(this.caret.reset().left().move());
this.Motion.left().move();
return Promise.resolve(this.Motion);
}

HandleKeyEvent(key : string) : Thenable<{}> {
Expand All @@ -65,7 +60,7 @@ export default class NormalMode extends Mode {

if (keyHandled) {
this.keyHistory = [];
return this.keyHandler[keysPressed](this.caret);
return this.keyHandler[keysPressed](this.Motion);
}

resolve();
Expand Down
21 changes: 0 additions & 21 deletions src/mode/modeVisual.ts

This file was deleted.

Loading

0 comments on commit dc00fa9

Please sign in to comment.