Skip to content

Commit

Permalink
Reselect visual implemented (gv) (#1141)
Browse files Browse the repository at this point in the history
* fixes #986

* update roadmap for gv
  • Loading branch information
xconverge authored and johnfn committed Dec 9, 2016
1 parent 170f2a2 commit 2c81aa4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Status | Command | Description
:white_check_mark: | v | start highlighting characters
:white_check_mark: | V | start highlighting linewise
:white_check_mark:| o | exchange cursor position with start of highlighting
| gv | start highlighting on previous visual area
:white_check_mark:| gv | start highlighting on previous visual area
:white_check_mark: | v | highlight characters or stop highlighting
:white_check_mark: | V | highlight linewise or stop highlighting
:white_check_mark: | CTRL-V | highlight blockwise or stop highlighting
Expand Down
22 changes: 22 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,28 @@ class CommandVisualMode extends BaseCommand {
}
}

@RegisterAction
class CommandReselectVisual extends BaseCommand {
modes = [ModeName.Normal];
keys = ["g", "v"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// Try to restore selection only if valid
if (vimState.lastVisualSelectionEnd !== undefined &&
vimState.lastVisualSelectionStart !== undefined &&
vimState.lastVisualMode !== undefined) {

if (vimState.lastVisualSelectionEnd.line <= (TextEditor.getLineCount() - 1)) {
vimState.currentMode = vimState.lastVisualMode;
vimState.cursorStartPosition = vimState.lastVisualSelectionStart;
vimState.cursorPosition = vimState.lastVisualSelectionEnd;
}

}
return vimState;
}
}

@RegisterAction
class CommandVisualBlockMode extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualBlock];
Expand Down
20 changes: 19 additions & 1 deletion src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ export class VimState {

public replaceState: ReplaceState | undefined = undefined;

/**
* Stores last visual mode for gv
*/
public lastVisualMode: ModeName;

/**
* Last selection that was active
*/
public lastVisualSelectionStart: Position;
public lastVisualSelectionEnd: Position;

/**
* The mode Vim will be in once this action finishes.
*/
Expand Down Expand Up @@ -961,6 +972,13 @@ export class ModeHandler implements vscode.Disposable {

vimState.historyTracker.setLastHistoryEndPosition(vimState.allCursors.map(x => x.stop));

if (vimState.getModeObject(this).isVisualMode) {
// Store selection for commands like gv
this._vimState.lastVisualMode = this._vimState.currentMode;
this._vimState.lastVisualSelectionStart = this._vimState.cursorStartPosition;
this._vimState.lastVisualSelectionEnd = this._vimState.cursorPosition;
}

// Updated desired column

const movement = action instanceof BaseMovement ? action : undefined;
Expand Down Expand Up @@ -1035,7 +1053,7 @@ export class ModeHandler implements vscode.Disposable {
vimState.allCursors[i] = vimState.allCursors[i].withNewStop(result);

if (!vimState.getModeObject(this).isVisualMode &&
!vimState.recordedState.operator) {
!vimState.recordedState.operator) {

vimState.allCursors[i] = vimState.allCursors[i].withNewStart(result);
}
Expand Down
8 changes: 8 additions & 0 deletions test/mode/modeVisual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,12 @@ suite("Mode Visual", () => {
});
});

newTest({
title: "Can do gv to reselect previous selection",
start: ["tes|ttest"],
keysPressed: "vl<Esc>llgvd",
end: ["tes|est"],
endMode: ModeName.Normal
});

});

0 comments on commit 2c81aa4

Please sign in to comment.