From 3c79f32607eed0ba4830de35ab85a3bd763e2920 Mon Sep 17 00:00:00 2001 From: Jason Fields Date: Sat, 23 Mar 2019 16:26:17 -0400 Subject: [PATCH 1/2] When a search is cancelled, revert to previous search state Fixes #3616 --- src/actions/commands/actions.ts | 21 ++++++++++++--------- test/mode/normalModeTests/motions.test.ts | 7 +++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index 16df2d09d66..96cfb16bfb3 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -502,7 +502,6 @@ class CommandEsc extends BaseCommand { ModeName.VisualLine, ModeName.VisualBlock, ModeName.Normal, - ModeName.SearchInProgressMode, ModeName.SurroundInputMode, ModeName.EasyMotionMode, ModeName.EasyMotionInputMode, @@ -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; } @@ -958,8 +951,18 @@ class CommandEscInSearchMode extends BaseCommand { } public async exec(position: Position, vimState: VimState): Promise { - await vimState.setCurrentMode(ModeName.Normal); - vimState.globalState.searchState = undefined; + const searchState = vimState.globalState.searchState!; + + vimState.cursorStopPosition = searchState.searchCursorStartPosition; + + const prevSearchList = vimState.globalState.searchStatePrevious; + if (prevSearchList) { + vimState.globalState.searchState = prevSearchList[prevSearchList.length - 1]; + } else { + vimState.globalState.searchState = undefined; + } + + await vimState.setCurrentMode(searchState.previousMode); return vimState; } diff --git a/test/mode/normalModeTests/motions.test.ts b/test/mode/normalModeTests/motions.test.ts index f962bd90259..f0ea37c799d 100644 --- a/test/mode/normalModeTests/motions.test.ts +++ b/test/mode/normalModeTests/motions.test.ts @@ -380,6 +380,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/threen', + end: ['one', 'two |two', 'three three three'], + }); + newTest({ title: 'maintains column position correctly', start: ['|one one one', 'two', 'three'], From 3cc5d3cd6a73bfc388122a585b63b9f1b93e8858 Mon Sep 17 00:00:00 2001 From: Jason Fields Date: Sat, 30 Mar 2019 15:30:39 -0400 Subject: [PATCH 2/2] Code review: use ternary operator --- src/actions/commands/actions.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index 96cfb16bfb3..c7d94a93ce6 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -956,11 +956,9 @@ class CommandEscInSearchMode extends BaseCommand { vimState.cursorStopPosition = searchState.searchCursorStartPosition; const prevSearchList = vimState.globalState.searchStatePrevious; - if (prevSearchList) { - vimState.globalState.searchState = prevSearchList[prevSearchList.length - 1]; - } else { - vimState.globalState.searchState = undefined; - } + vimState.globalState.searchState = prevSearchList + ? prevSearchList[prevSearchList.length - 1] + : undefined; await vimState.setCurrentMode(searchState.previousMode);