Skip to content

Commit

Permalink
Merge pull request #1515 from xconverge/make-surround-repeatable
Browse files Browse the repository at this point in the history
Make surround repeatable with dot
  • Loading branch information
xconverge authored Apr 19, 2017
2 parents 3c303c2 + 9f90375 commit 737ef47
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
15 changes: 15 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6716,6 +6716,11 @@ class CommandSurroundModeStart extends BaseCommand {

if (!operatorString) { return vimState; }

// Start to record the keys to store for playback of surround using dot
vimState.recordedState.surroundKeys.push(vimState.keyHistory[vimState.keyHistory.length - 2]);
vimState.recordedState.surroundKeys.push("s");
vimState.recordedState.surroundKeyIndexStart = vimState.keyHistory.length;

vimState.surround = {
active : true,
target : undefined,
Expand Down Expand Up @@ -6760,6 +6765,10 @@ class CommandSurroundModeStartVisual extends BaseCommand {
return vimState;
}

// Start to record the keys to store for playback of surround using dot
vimState.recordedState.surroundKeys.push("S");
vimState.recordedState.surroundKeyIndexStart = vimState.keyHistory.length;

// Make sure cursor positions are ordered correctly for top->down or down->top selection
if (vimState.cursorStartPosition.line > vimState.cursorPosition.line) {
[vimState.cursorPosition, vimState.cursorStartPosition] =
Expand Down Expand Up @@ -6888,9 +6897,15 @@ class CommandSurroundAddToReplacement extends BaseCommand {
public static Finish(vimState: VimState): boolean {
vimState.recordedState.hasRunOperator = false;
vimState.recordedState.actionsRun = [];
vimState.recordedState.hasRunSurround = true;
vimState.surround = undefined;
vimState.currentMode = ModeName.Normal;

// Record keys that were pressed since surround started
for (let i = vimState.recordedState.surroundKeyIndexStart; i < vimState.keyHistory.length; i++) {
vimState.recordedState.surroundKeys.push(vimState.keyHistory[i]);
}

return false;
}

Expand Down
34 changes: 22 additions & 12 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ export class RecordedState {

public hasRunOperator = false;

public hasRunSurround = false;
public surroundKeys: string[] = [];
public surroundKeyIndexStart = 0;

/**
* This is kind of a hack and should be associated with something like this:
*
Expand Down Expand Up @@ -407,6 +411,8 @@ export class RecordedState {
res.actionKeys = this.actionKeys.slice(0);
res.actionsRun = this.actionsRun.slice(0);
res.hasRunOperator = this.hasRunOperator;
res.hasRunSurround = this.hasRunSurround;
res.surroundKeys = this.surroundKeys;

return res;
}
Expand Down Expand Up @@ -1558,28 +1564,32 @@ export class ModeHandler implements vscode.Disposable {

async rerunRecordedState(vimState: VimState, recordedState: RecordedState): Promise<VimState> {
const actions = recordedState.actionsRun.slice(0);
const hasRunSurround = recordedState.hasRunSurround;
const surroundKeys = recordedState.surroundKeys;

vimState.isRunningDotCommand = true;
recordedState = new RecordedState();
vimState.recordedState = recordedState;
vimState.isRunningDotCommand = true;

let i = 0;
// Replay surround if applicable, otherwise rerun actions
if (hasRunSurround) {
await this.handleMultipleKeyEvents(surroundKeys);
} else {
let i = 0;
for (let action of actions) {
recordedState.actionsRun = actions.slice(0, ++i);
vimState = await this.runAction(vimState, recordedState, action);

for (let action of actions) {
recordedState.actionsRun = actions.slice(0, ++i);
vimState = await this.runAction(vimState, recordedState, action);
if (vimState.lastMovementFailed) {
return vimState;
}

if (vimState.lastMovementFailed) {
return vimState;
await this.updateView(vimState);
}

await this.updateView(vimState);
recordedState.actionsRun = actions;
}

vimState.isRunningDotCommand = false;

recordedState.actionsRun = actions;

return vimState;
}

Expand Down

0 comments on commit 737ef47

Please sign in to comment.