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

Refactor commands [WIP] #234

Merged
merged 57 commits into from
Jun 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
83afbcf
Begin command refactor.
johnfn May 29, 2016
13fdd4c
Add the remaining keys.
johnfn May 29, 2016
f160a86
Passing most tests.
johnfn May 29, 2016
49b8a2f
Finish up normal mode rewrite.
johnfn May 29, 2016
7675cb6
Refactor actions to return positions.
johnfn May 29, 2016
bf5c42e
Refactor Normal & Visual mode to use same actions
johnfn May 29, 2016
7d15fff
Rewrite modes to take Actions, not key presses.
johnfn May 29, 2016
ee15155
Make operators inherit from a common base class.
johnfn May 29, 2016
409822f
Track action state in ModeHandler and write core processing fn.
johnfn May 30, 2016
a6d01bd
Remove a bunch of stuff.
johnfn May 30, 2016
c6db2d5
Respect modes for actions.
johnfn May 30, 2016
0b52969
Make all state transitions into Actions.
johnfn May 30, 2016
06ae75f
Clean up, fix <esc>.
johnfn May 30, 2016
3f2df04
Add x/X actions.
johnfn May 30, 2016
e0eb20c
Handle a lot of weird edge cases.
johnfn May 31, 2016
97c615c
Add a special case JUST for de.
johnfn May 31, 2016
8fe79c9
Add C.
johnfn May 31, 2016
b87a41a
Shove everything in one massive class.
johnfn May 31, 2016
f6d90dd
Fix cw.
johnfn May 31, 2016
bc8d0fe
Fix cW as well.
johnfn May 31, 2016
5bde23c
Pass all [operator][motion] normal mode tests!
johnfn May 31, 2016
c3683f0
Begin adding/rewriting visual mode tests.
johnfn May 31, 2016
8a6c826
Fix all visual mode tests!
johnfn May 31, 2016
0fce025
Fix a bug with deleting in Visual Mode.
johnfn Jun 2, 2016
da30a64
Fix gg, and refactor some logic along the way.
johnfn Jun 2, 2016
e84f583
Add test for gg.
johnfn Jun 2, 2016
beef8a6
Refactor a lot of deletes to use DeleteOperator.
johnfn Jun 2, 2016
87a3d99
Make DeleteOperator inclusive/exclusive.
johnfn Jun 2, 2016
8019bbe
Remove an unused function.
johnfn Jun 2, 2016
4b43b73
Fix off-by-one in reverse ranges.
johnfn Jun 2, 2016
a9476af
Add VimState, keep track of desired column again.
johnfn Jun 2, 2016
35eefbe
Clean up desiredColumn and add test.
johnfn Jun 2, 2016
4982f4b
Nest ActionState within VimState.
johnfn Jun 2, 2016
1114f9b
Nest ActionState within VimState and return VimState from actions.
johnfn Jun 2, 2016
9e055d6
Don't pass ActionState, only VimState.
johnfn Jun 2, 2016
a6c2144
Refactor the 'ready to run' logic.
johnfn Jun 3, 2016
af00ab7
Fix dl at end of line and add test.
johnfn Jun 3, 2016
b7ecb56
Add commands like $ force desiredColumn to EOL.
johnfn Jun 3, 2016
00cad54
Handle the returned VimState in a unified way.
johnfn Jun 3, 2016
7410857
Simplify logic.
johnfn Jun 3, 2016
f9fed66
Update mode via VimState.
johnfn Jun 3, 2016
f837304
Remove unnecessary parameter.
johnfn Jun 3, 2016
0f3fa9e
Remove handleDeactivation, which caused weird bugs.
johnfn Jun 3, 2016
e905f40
Stop passing in modeHandler to actions.
johnfn Jun 3, 2016
0fc8e94
Add a bunch of commands.
johnfn Jun 3, 2016
efc6133
Add 2 more commands back.
johnfn Jun 3, 2016
37a6abb
Add x operator in visual mode.
johnfn Jun 3, 2016
1a0cbb9
Add yank and put back.
johnfn Jun 3, 2016
e0e6974
Add text object and remove concept of inclusive/exclusive motions.
johnfn Jun 4, 2016
b662bad
Totally remove position options.
johnfn Jun 4, 2016
85b6d8c
Text objects work with d/c/y!!
johnfn Jun 5, 2016
88f2e57
Add iw text object.
johnfn Jun 5, 2016
3143f14
Fix an 'aw' bug.
johnfn Jun 5, 2016
d320241
Make "aw" motion work and pass tests.
johnfn Jun 5, 2016
cab7dd4
Fix iw motion.
johnfn Jun 5, 2016
668dfa1
Add put test and pass all tests.
johnfn Jun 5, 2016
3ce1c28
Fix all tslint errors.
johnfn Jun 5, 2016
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
Prev Previous commit
Next Next commit
Add a bunch of commands.
  • Loading branch information
johnfn committed Jun 3, 2016
commit 0fc8e9486fcdc7a67bf6b000e4aa1fc6922d4786
73 changes: 35 additions & 38 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModeHandler, ActionState, VimState } from './../mode/modeHandler';
import { ModeHandler, VimCommandActions, VimState } from './../mode/modeHandler';
import { ModeName } from './../mode/mode';
import { TextEditor } from './../textEditor';
import { Register } from './../register/register';
Expand Down Expand Up @@ -229,103 +229,102 @@ export class YankOperator extends BaseOperator {
}
}

/*

@RegisterAction
Copy link
Member

Choose a reason for hiding this comment

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

Oohh! neat, didn't know TS/JS had this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's really snazzy. Unfortunately I'm running into some annoying bugs where depending on compile order things might not work, so I might have to remove it...

class ActionEnterCommand extends BaseAction {
class CommandShowCommandLine extends BaseCommand {
modes = [ModeName.Normal];
key = ":";

public async execAction(position: Position): Promise<VimState> {
await showCmdLine("", modeHandler);
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.ShowCommandLine;

return {};
return vimState;
}
}

@RegisterAction
class ActionFind extends BaseAction {
class CommandFind extends BaseCommand {
modes = [ModeName.Normal];
key = "/";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("actions.find");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.Find;

return {};
return vimState;
}
}

@RegisterAction
class ActionFold extends BaseAction {
modes = [ModeName.Normal];
class CommandFold extends BaseCommand {
modes = [ModeName.Visual];
key = "zc";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("editor.fold");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.Fold;

return {};
return vimState;
}
}

@RegisterAction
class ActionUnfold extends BaseAction {
class CommandUnfold extends BaseCommand {
modes = [ModeName.Normal];
key = "zo";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("editor.unfold");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.Unfold;

return {};
return vimState;
}
}

@RegisterAction
class ActionFoldAll extends BaseAction {
class CommandFoldAll extends BaseCommand {
modes = [ModeName.Normal];
key = "zC";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("editor.foldAll");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.FoldAll;

return {};
return vimState;
}
}

@RegisterAction
class ActionUnfoldAll extends BaseAction {
class CommandUnfoldAll extends BaseCommand {
modes = [ModeName.Normal];
key = "zO";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("editor.unfoldAll");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.UnfoldAll;

return {};
return vimState;
}
}

@RegisterAction
class ActionUndo extends BaseAction {
class CommandUndo extends BaseCommand {
modes = [ModeName.Normal];
key = "u";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("undo");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.Undo;

return { undo: true };
return vimState;
}
}

@RegisterAction
class ActionRedo extends BaseAction {
class CommandRedo extends BaseCommand {
modes = [ModeName.Normal];
key = "ctrl+r";

public async execAction(position: Position): Promise<VimState> {
await vscode.commands.executeCommand("redo");
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimCommandActions.Redo;

return { redo: true };
return vimState;
}
}
*/

@RegisterAction
class CommandEsc extends BaseCommand {
Expand Down Expand Up @@ -466,7 +465,6 @@ class CommandInsertNewLineAbove extends BaseCommand {
key = "O";

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// TODO: This code no good.
await vscode.commands.executeCommand("editor.action.insertLineBefore");

vimState.currentMode = ModeName.Insert;
Expand All @@ -481,7 +479,6 @@ class CommandInsertNewLineBefore extends BaseCommand {
key = "o";

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// TODO: This code no good.
await vscode.commands.executeCommand("editor.action.insertLineAfter");

vimState.currentMode = ModeName.Insert;
Expand Down
100 changes: 70 additions & 30 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { Configuration } from '../configuration/configuration';
import { Position, PositionOptions } from './../motion/position';
import { TextEditor } from '../../src/textEditor';
import { showCmdLine } from '../../src/cmd_line/main';

// TODO: This is REALLY dumb...
// figure out some way to force include this stuff...
Expand All @@ -28,6 +29,60 @@ new YankOperator();
// TODO - or maybe just get rid of decorators
// they're nice but introduce a weird class of bugs ;_;

export enum VimCommandActions {
DoNothing,
ShowCommandLine,
Find,
Fold,
Unfold,
FoldAll,
UnfoldAll,
Undo,
Redo,
}

/**
* The VimState class holds permanent state that carries over from action
* to action.
*
* Actions defined in actions.ts are only allowed to mutate a VimState in order to
* indicate what they want to do.
*/
export class VimState {
/**
* The column the cursor wants to be at, or Number.POSITIVE_INFINITY if it should always
* be the rightmost column.
*
* Example: If you go to the end of a 20 character column, this value
* will be 20, even if you press j and the next column is only 5 characters.
* This is because if the third column is 25 characters, the cursor will go
* back to the 20th column.
*/
public desiredColumn = 0;

/**
* The position the cursor will be when this action finishes.
*/
public cursorPosition = new Position(0, 0, PositionOptions.CharacterWiseInclusive);

/**
* The mode Vim will be in once this action finishes.
*/
public currentMode = ModeName.Normal;

/**
* This is for oddball commands that don't manipulate text in any way.
*/
public commandAction = VimCommandActions.DoNothing;

private _actionState = new ActionState(this);
public get actionState(): ActionState { return this._actionState; }
public set actionState(a: ActionState) {
a.vimState = this;
this._actionState = a;
}
}

/**
* The ActionState class represents state relevant to the current
* action that the user is doing. Example: Imagine that the user types:
Expand Down Expand Up @@ -123,36 +178,6 @@ export class ActionState {
}
}

/**
* The VimState class holds permanent state that carries over from action
* to action.
*
* TODO: Perhaps this should have ActionState inside it, and be returned from all commands.
*/
export class VimState {
/**
* The column the cursor wants to be at, or Number.POSITIVE_INFINITY if it should always
* be the rightmost column.
*
* Example: If you go to the end of a 20 character column, this value
* will be 20, even if you press j and the next column is only 5 characters.
* This is because if the third column is 25 characters, the cursor will go
* back to the 20th column.
*/
public desiredColumn = 0;

public cursorPosition = new Position(0, 0, PositionOptions.CharacterWiseInclusive);

public currentMode = ModeName.Normal;

private _actionState = new ActionState(this);
public get actionState(): ActionState { return this._actionState; }
public set actionState(a: ActionState) {
a.vimState = this;
this._actionState = a;
}
}

export class ModeHandler implements vscode.Disposable {
private __motion: Motion;
private _modes: Mode[];
Expand Down Expand Up @@ -278,6 +303,21 @@ export class ModeHandler implements vscode.Disposable {
if (actionState.readyToExecute) {
this._vimState = await this.executeState();

if (this._vimState.commandAction !== VimCommandActions.DoNothing) {
switch (this._vimState.commandAction) {
case VimCommandActions.ShowCommandLine: await showCmdLine("", this); break;
case VimCommandActions.Find: await vscode.commands.executeCommand("actions.find"); break;
case VimCommandActions.Fold: await vscode.commands.executeCommand("editor.fold"); break;
case VimCommandActions.Unfold: await vscode.commands.executeCommand("editor.unfold"); break;
case VimCommandActions.FoldAll: await vscode.commands.executeCommand("editor.foldAll"); break;
case VimCommandActions.UnfoldAll: await vscode.commands.executeCommand("editor.unfoldAll"); break;
case VimCommandActions.Undo: await vscode.commands.executeCommand("undo"); break;
case VimCommandActions.Redo: await vscode.commands.executeCommand("redo"); break;
}

this._vimState.commandAction = VimCommandActions.DoNothing;
}

// Update mode

if (this._vimState.currentMode !== this.currentModeName) {
Expand Down