From 45a058de29c9ae5537f0ba16b0551c2371e54dfa Mon Sep 17 00:00:00 2001 From: Jason Fields Date: Sat, 4 May 2019 01:34:00 -0400 Subject: [PATCH] Add searches from * and # to the search history (#3724) Also refactored the search logic a bit to make this easier Fixes #3031 --- src/actions/commands/actions.ts | 27 ++++------------------- src/state/globalState.ts | 23 ++++++++++++++++--- test/mode/normalModeTests/motions.test.ts | 14 ++++++++++++ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index 3ff2b72a2a8..1c98aed28fa 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -895,28 +895,7 @@ class CommandInsertInSearchMode extends BaseCommand { } } - // Store this search if different than previous - if (vimState.globalState.searchStatePrevious.length !== 0) { - let previousSearchState = vimState.globalState.searchStatePrevious; - if ( - searchState.searchString !== - previousSearchState[previousSearchState.length - 1]!.searchString - ) { - previousSearchState.push(searchState); - await vimState.globalState.addNewSearchHistoryItem(searchState.searchString); - } - } else { - vimState.globalState.searchStatePrevious.push(searchState); - await vimState.globalState.addNewSearchHistoryItem(searchState.searchString); - } - - // Make sure search history does not exceed configuration option - if (vimState.globalState.searchStatePrevious.length > configuration.history) { - vimState.globalState.searchStatePrevious.splice(0, 1); - } - - // Update the index to the end of the search history - vimState.globalState.searchStateIndex = vimState.globalState.searchStatePrevious.length - 1; + vimState.globalState.addSearchStateToHistory(searchState); // Move cursor to next match vimState.cursorStopPosition = searchState.getNextSearchMatchPosition( @@ -1150,7 +1129,7 @@ async function searchCurrentSelection(vimState: VimState, direction: SearchDirec }); } -function createSearchStateAndMoveToMatch(args: { +async function createSearchStateAndMoveToMatch(args: { needle?: string | undefined; vimState: VimState; direction: SearchDirection; @@ -1181,6 +1160,8 @@ function createSearchStateAndMoveToMatch(args: { // Turn one of the highlighting flags back on (turned off with :nohl) vimState.globalState.hl = true; + vimState.globalState.addSearchStateToHistory(vimState.globalState.searchState); + return vimState; } diff --git a/src/state/globalState.ts b/src/state/globalState.ts index 9253a61be06..03146b5c898 100644 --- a/src/state/globalState.ts +++ b/src/state/globalState.ts @@ -5,6 +5,7 @@ import { RecordedState } from './../state/recordedState'; import { SearchHistory } from '../history/historyFile'; import { SearchState, SearchDirection } from './searchState'; import { SubstituteState } from './substituteState'; +import { configuration } from '../configuration/configuration'; /** * State which stores global state (across editors) @@ -79,10 +80,26 @@ class GlobalState { this._searchStatePrevious = this._searchStatePrevious.concat(states); } - public async addNewSearchHistoryItem(searchString: string) { - if (this._searchHistory !== undefined) { - await this._searchHistory.add(searchString); + public async addSearchStateToHistory(searchState: SearchState) { + const prevSearchString = + this.searchStatePrevious.length === 0 + ? undefined + : this.searchStatePrevious[this.searchStatePrevious.length - 1]!.searchString; + // Store this search if different than previous + if (searchState.searchString !== prevSearchString) { + this.searchStatePrevious.push(searchState); + if (this._searchHistory !== undefined) { + await this._searchHistory.add(searchState.searchString); + } } + + // Make sure search history does not exceed configuration option + if (this.searchStatePrevious.length > configuration.history) { + this.searchStatePrevious.splice(0, 1); + } + + // Update the index to the end of the search history + this.searchStateIndex = this.searchStatePrevious.length - 1; } public get jumpTracker(): JumpTracker { diff --git a/test/mode/normalModeTests/motions.test.ts b/test/mode/normalModeTests/motions.test.ts index 6d93d8de219..da8d5d1135b 100644 --- a/test/mode/normalModeTests/motions.test.ts +++ b/test/mode/normalModeTests/motions.test.ts @@ -544,6 +544,20 @@ suite('Motions in Normal Mode', () => { end: ['|blah duh blah duh blah'], }); + // These tests take advantage of the fact that an empty search repeats the last search + newTest({ + title: '* adds to search history', + start: ['|ONE two three ONE two three four ONE'], + keysPressed: '*/\n', + end: ['ONE two three ONE two three four |ONE'], + }); + newTest({ + title: '# adds to search history', + start: ['ONE two three ONE two three four |ONE'], + keysPressed: '#?\n', + end: ['|ONE two three ONE two three four ONE'], + }); + newTest({ title: 'Can handle |', start: ['blah duh blah duh |blah'],