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

Re-implement `` and '' with jumpTracker #3242

Merged
merged 3 commits into from
Dec 6, 2018
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
31 changes: 13 additions & 18 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { RegisterAction } from './../base';
import { BaseAction } from './../base';
import { commandLine } from './../../cmd_line/commandLine';
import * as operator from './../operator';
import { Jump } from '../../jumps/jump';

export class DocumentContentChangeAction extends BaseAction {
contentChanges: {
Expand Down Expand Up @@ -2853,15 +2854,7 @@ class CommandNavigateLast extends BaseCommand {
isJump = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const oldActiveEditor = vimState.editor;

await vscode.commands.executeCommand('workbench.action.navigateLast');

if (oldActiveEditor === vimState.editor) {
vimState.cursorPosition = Position.FromVSCodePosition(vimState.editor.selection.start);
}

return vimState;
return vimState.globalState.jumpTracker.jumpBack(position, vimState);
}
}

Expand All @@ -2873,17 +2866,19 @@ class CommandNavigateLastBOL extends BaseCommand {
return false;
}
isJump = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const oldActiveEditor = vimState.editor;

await vscode.commands.executeCommand('workbench.action.navigateLast');

if (oldActiveEditor === vimState.editor) {
const pos = Position.FromVSCodePosition(vimState.editor.selection.start);
vimState.cursorPosition = pos.getFirstLineNonBlankChar();
const lastJump = vimState.globalState.jumpTracker.end;
if (lastJump == null) {
// This command goes to the last jump, and there is no previous jump, so there's nothing to do.
return vimState;
}

const jump = new Jump({
editor: vimState.editor,
fileName: vimState.editor.document.fileName,
position: lastJump.position.getLineBegin(),
});
vimState.globalState.jumpTracker.recordJump(Jump.fromStateNow(vimState), jump);
vimState.cursorPosition = jump.position;
return vimState;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/jumps/jump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ export class Jump {
}

/**
* Determine whether another jump matches the same file path and line number,
* regardless of whether the column numbers match.
*
* Determine whether another jump matches the same file path, line number, and character column.
* @param other - Another Jump to compare against
*/
public onSameLine(other: Jump | null | undefined): boolean {
public isSamePosition(other: Jump | null | undefined): boolean {
return (
!other || (this.fileName === other.fileName && this.position.line === other.position.line)
!other ||
(this.fileName === other.fileName &&
this.position.line === other.position.line &&
this.position.character === other.position.character)
);
}
}
10 changes: 5 additions & 5 deletions src/jumps/jumpTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class JumpTracker {
* @param to - File/position jumped to
*/
public recordJump(from: Jump | null, to?: Jump | null) {
if (from && to && from.onSameLine(to)) {
if (from && to && from.isSamePosition(to)) {
return;
}

Expand Down Expand Up @@ -313,10 +313,10 @@ export class JumpTracker {

pushJump(from: Jump | null, to?: Jump | null) {
if (from) {
this.clearJumpsOnSameLine(from);
this.clearJumpsOnSamePosition(from);
}

if (from && !from.onSameLine(to)) {
if (from && !from.isSamePosition(to)) {
this._jumps.push(from);
}

Expand Down Expand Up @@ -347,8 +347,8 @@ export class JumpTracker {
}
}

clearJumpsOnSameLine(jump: Jump): void {
this._jumps = this._jumps.filter(j => j === jump || !j.onSameLine(jump));
clearJumpsOnSamePosition(jump: Jump): void {
this._jumps = this._jumps.filter(j => j === jump || !j.isSamePosition(jump));
}

removeDuplicateJumps() {
Expand Down
36 changes: 35 additions & 1 deletion test/jumpTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,48 @@ suite('Record and navigate jumps', () => {
end: ['|start', '{', 'a1', 'b1', 'a2', 'b2', '}', 'end'],
jumps: ['start', '{', 'b1', 'a2', 'a1'],
});

newJumpTest({
title: 'Can enter number to jump back multiple times',
start: ['|start', '{', 'a1', 'b1', 'a2', 'b2', '}', 'end'],
keysPressed: 'Gggj%2<C-o>',
end: ['start', '{', 'a1', 'b1', 'a2', 'b2', '}', '|end'],
jumps: ['start', '|end', '{', '}'],
});
newJumpTest({
title: 'Can track one-line `` jumps',
start: ['|start', 'var foo = {"a", "b"}', 'end'],
keysPressed: 'jf{%r]``r[',
end: ['start', 'var foo = |["a", "b"]', 'end'],
jumps: ['var foo = ["a", "b"]', 'var foo = ["a", "b"]'],
});
newJumpTest({
title: 'Can track one-line double `` jumps',
start: ['|start', 'var foo = {"a", "b"}', 'end'],
keysPressed: 'jf{%r]``r[``',
end: ['start', 'var foo = ["a", "b"|]', 'end'],
jumps: ['var foo = ["a", "b"]', 'var foo = ["a", "b"]'],
});
newJumpTest({
title: "Can track one-line '' jumps",
start: ['|start', 'var foo = {"a", "b"}', 'end'],
keysPressed: "jf{%r]``r[''",
end: ['start', '|var foo = ["a", "b"]', 'end'],
jumps: ['var foo = ["a", "b"]', 'var foo = ["a", "b"]'],
});
newJumpTest({
title: "Can track one-line double '' jumps",
start: ['|start', 'var foo = {"a", "b"}', 'end'],
keysPressed: "jf{%r]``r[''''",
end: ['start', '|var foo = ["a", "b"]', 'end'],
jumps: ['var foo = ["a", "b"]', 'var foo = ["a", "b"]'],
});
newJumpTest({
title: "Can handle '' jumps with no previous jump",
start: ['|start', 'var foo = {"a", "b"}', 'end'],
keysPressed: "''",
end: ['|start', 'var foo = {"a", "b"}', 'end'],
jumps: [],
});
});

suite('Can shifts jump lines up after deleting a line with Visual Line Mode', () => {
Expand Down