Skip to content

Commit

Permalink
When a search is cancelled, revert to previous search state (#3617)
Browse files Browse the repository at this point in the history
* When a search is cancelled, revert to previous search state

Fixes #3616

* Code review: use ternary operator
  • Loading branch information
J-Fields authored and jpoon committed Apr 2, 2019
1 parent 961c302 commit 1f6c198
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ class CommandEsc extends BaseCommand {
ModeName.VisualLine,
ModeName.VisualBlock,
ModeName.Normal,
ModeName.SearchInProgressMode,
ModeName.SurroundInputMode,
ModeName.EasyMotionMode,
ModeName.EasyMotionInputMode,
Expand Down Expand Up @@ -536,12 +535,6 @@ class CommandEsc extends BaseCommand {
vimState.cursors = vimState.cursors.map(x => x.withNewStop(x.stop.getLeft()));
}

if (vimState.currentMode === ModeName.SearchInProgressMode) {
if (vimState.globalState.searchState) {
vimState.cursorStopPosition = vimState.globalState.searchState.searchCursorStartPosition;
}
}

if (vimState.currentMode === ModeName.Normal && vimState.isMultiCursor) {
vimState.isMultiCursor = false;
}
Expand Down Expand Up @@ -961,8 +954,16 @@ class CommandEscInSearchMode extends BaseCommand {
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vimState.setCurrentMode(ModeName.Normal);
vimState.globalState.searchState = undefined;
const searchState = vimState.globalState.searchState!;

vimState.cursorStopPosition = searchState.searchCursorStartPosition;

const prevSearchList = vimState.globalState.searchStatePrevious;
vimState.globalState.searchState = prevSearchList
? prevSearchList[prevSearchList.length - 1]
: undefined;

await vimState.setCurrentMode(searchState.previousMode);

return vimState;
}
Expand Down
7 changes: 7 additions & 0 deletions test/mode/normalModeTests/motions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ suite('Motions in Normal Mode', () => {
// });
// });

newTest({
title: 'cancelled search reverts to previous search state',
start: ['|one', 'two two', 'three three three'],
keysPressed: '/two\n/three<Esc>n',
end: ['one', 'two |two', 'three three three'],
});

newTest({
title: 'Backspace on empty search cancels',
start: ['|one two three'],
Expand Down

0 comments on commit 1f6c198

Please sign in to comment.