Reselect visual implemented (gv) (#1141)

* fixes #986

* update roadmap for gv
This commit is contained in:
Sean Kelly 2016-12-08 23:36:47 -08:00 committed by Grant Mathews
parent 170f2a2a95
commit 2c81aa4f7c
4 changed files with 50 additions and 2 deletions

View File

@ -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

View File

@ -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];

View File

@ -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.
*/
@ -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;
@ -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);
}

View File

@ -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
});
});