Skip to content

Commit

Permalink
Add searches from * and # to the search history (VSCodeVim#3724)
Browse files Browse the repository at this point in the history
Also refactored the search logic a bit to make this easier
Fixes VSCodeVim#3031
  • Loading branch information
J-Fields authored and jpoon committed May 4, 2019
1 parent b342a75 commit 45a058d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
27 changes: 4 additions & 23 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
23 changes: 20 additions & 3 deletions src/state/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions test/mode/normalModeTests/motions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 45a058d

Please sign in to comment.