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

Add f, F, t and T motions #244

Merged
merged 4 commits into from
Jun 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,63 @@ class MoveRight extends BaseMovement {
}
}

@RegisterAction
class MoveFindForward extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["f", "<any>"];

public async execAction(position: Position, vimState: VimState): Promise<VimState> {
const toFind = vimState.actionState.keysPressed[1];

vimState.cursorPosition = position.findForwards(toFind);

return vimState;
}
}

@RegisterAction
class MoveFindBackward extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["F", "<any>"];

public async execAction(position: Position, vimState: VimState): Promise<VimState> {
const toFind = vimState.actionState.keysPressed[1];

vimState.cursorPosition = position.findBackwards(toFind);

return vimState;
}
}


@RegisterAction
class MoveTilForward extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["t", "<any>"];

public async execAction(position: Position, vimState: VimState): Promise<VimState> {
const toFind = vimState.actionState.keysPressed[1];

vimState.cursorPosition = position.tilForwards(toFind);

return vimState;
}
}

@RegisterAction
class MoveTilBackward extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["T", "<any>"];

public async execAction(position: Position, vimState: VimState): Promise<VimState> {
const toFind = vimState.actionState.keysPressed[1];

vimState.cursorPosition = position.tilBackwards(toFind);

return vimState;
}
}

@RegisterAction
class MoveLineEnd extends BaseMovement {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
Expand Down
4 changes: 2 additions & 2 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,6 @@ export class ModeHandler implements vscode.Disposable {
}
} else if (action === KeypressState.WaitingOnKeys) {
return true;
} else {
actionState.keysPressed = [];
}

if (action) {
Expand Down Expand Up @@ -417,6 +415,8 @@ export class ModeHandler implements vscode.Disposable {
this._vimState.actionState = new ActionState(this._vimState);
}

actionState.keysPressed = [];

return !!action;
}

Expand Down
50 changes: 50 additions & 0 deletions src/motion/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,54 @@ export class Position extends vscode.Position {

return new Position(TextEditor.getLineCount() - 1, 0).getLineEnd();
}


private findHelper(char: string, count: number, direction: number): Position {
// -1 = backwards, +1 = forwards
const line = TextEditor.getLineAt(this);
let index = this.character;

while (count && index !== -1) {
if (direction > 0) {
index = line.text.indexOf(char, index + direction);
} else {
index = line.text.lastIndexOf(char, index + direction);
}
count--;
}

if (index > -1) {
return new Position(this.line, index);
}

return null;
}

public tilForwards(char: string, count: number = 1): Position {
const position = this.findHelper(char, count, +1);
if (!position) { return this; }

return new Position(this.line, position.character - 1);
}

public tilBackwards(char: string, count: number = 1): Position {
const position = this.findHelper(char, count, -1);
if (!position) { return this; }

return new Position(this.line, position.character + 1);
}

public findForwards(char: string, count: number = 1): Position {
const position = this.findHelper(char, count, +1);
if (!position) { return this; }

return new Position(this.line, position.character);
}

public findBackwards(char: string, count: number = 1): Position {
const position = this.findHelper(char, count, -1);
if (!position) { return this; }

return position;
}
}
124 changes: 124 additions & 0 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,128 @@ suite("Mode Normal", () => {
assertEqual(TextEditor.getSelection().start.character, 3, "caw is on wrong position");
await assert.equal(modeHandler.currentMode.name, ModeName.Insert, "didn't enter insert mode");
});

test("Can handle 'f'", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'^',
'f', 't'
]);

assertEqual(TextEditor.getSelection().start.character, 3, "f failed");
});

test("Can handle 'f' twice", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'^',
'f', 't',
'f', 't'
]);

assertEqual(TextEditor.getSelection().start.character, 5, "f failed");
});

test("Can handle 'F'", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'$',
'F', 't'
]);

assertEqual(TextEditor.getSelection().start.character, 5, "F failed");
});

test("Can handle 'F' twice", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'$',
'F', 't',
'F', 't',
]);

assertEqual(TextEditor.getSelection().start.character, 3, "F failed");
});




test("Can handle 't'", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'^',
't', 't'
]);

assertEqual(TextEditor.getSelection().start.character, 2, "f failed");
});

test("Can handle 't' twice", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'^',
't', 't',
't', 't'
]);

// it does nothing the second time lawl
assertEqual(TextEditor.getSelection().start.character, 2, "f failed");
});

test("Can handle 'T'", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'$',
'T', 't'
]);

assertEqual(TextEditor.getSelection().start.character, 6, "F failed");
});

test("Can handle 'T' twice", async () => {
await modeHandler.handleMultipleKeyEvents(
'itext text'.split('')
);

await modeHandler.handleMultipleKeyEvents([
'<esc>',
'$',
'T', 't',
'T', 't',
]);

// it also does nothing the second time lawl lawl
assertEqual(TextEditor.getSelection().start.character, 6, "F failed");
});



});