fixes #657 implements search history (#1147)

* fixes #657 implements search history

* Added history config option to limit amount of search history remembered. This can be used when command history is implemented as well
This commit is contained in:
Sean Kelly 2016-12-10 16:26:37 -08:00 committed by Grant Mathews
parent 2c81aa4f7c
commit c8ee059f5d
4 changed files with 68 additions and 7 deletions

View File

@ -352,6 +352,11 @@
"description": "Show where a / search matches as you type it.",
"default": true
},
"vim.history": {
"type": "number",
"description": "How much search or command history should be remembered",
"default": 50
},
"vim.autoindent": {
"type": "boolean",
"description": "Indent code automatically.",

View File

@ -1414,7 +1414,9 @@ class RightArrowInInsertMode extends ArrowsInInsertMode {
@RegisterAction
class CommandInsertInSearchMode extends BaseCommand {
modes = [ModeName.SearchInProgressMode];
keys = ["<character>"];
keys = [["<character>"],
["<up>"],
["<down>"]];
runsOnceForEveryCursor() { return this.keysPressed[0] === '\n'; }
public async exec(position: Position, vimState: VimState): Promise<VimState> {
@ -1429,22 +1431,57 @@ class CommandInsertInSearchMode extends BaseCommand {
// Repeat the previous search if no new string is entered
if (searchState.searchString === "") {
const prevSearch = vimState.searchStatePrevious!;
if (prevSearch) {
searchState.searchString = prevSearch.searchString;
const prevSearchList = vimState.searchStatePrevious!;
if (prevSearchList.length > 0) {
searchState.searchString = prevSearchList[prevSearchList.length - 1].searchString;
}
}
// Store this search
vimState.searchStatePrevious = searchState;
// Store this search if different than previous
if (vimState.searchStatePrevious.length !== 0) {
if (searchState.searchString !== vimState.searchStatePrevious[vimState.searchStatePrevious.length - 1]!.searchString) {
vimState.searchStatePrevious.push(searchState);
}
} else {
vimState.searchStatePrevious.push(searchState);
}
// Make sure search history does not exceed configuration option
if (vimState.searchStatePrevious.length > Configuration.history) {
vimState.searchStatePrevious.splice(0, 1);
}
// Update the index to the end of the search history
vimState.searchStateIndex = vimState.searchStatePrevious.length - 1;
// Move cursor to next match
vimState.cursorPosition = searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
return vimState;
} else if (key === "<up>") {
const prevSearchList = vimState.searchStatePrevious!;
if (prevSearchList[vimState.searchStateIndex] !== undefined) {
searchState.searchString = prevSearchList[vimState.searchStateIndex].searchString;
vimState.searchStateIndex -= 1;
}
} else if (key === "<down>") {
const prevSearchList = vimState.searchStatePrevious!;
if (prevSearchList[vimState.searchStateIndex] !== undefined) {
searchState.searchString = prevSearchList[vimState.searchStateIndex].searchString;
vimState.searchStateIndex += 1;
}
} else {
searchState.searchString += this.keysPressed[0];
}
// Clamp the history index to stay within bounds of search history length
if (vimState.searchStateIndex > vimState.searchStatePrevious.length - 1) {
vimState.searchStateIndex = vimState.searchStatePrevious.length - 1;
}
if (vimState.searchStateIndex < 0) {
vimState.searchStateIndex = 0;
}
return vimState;
}
}
@ -1667,6 +1704,9 @@ export class CommandSearchForwards extends BaseCommand {
vimState.searchState = new SearchState(SearchDirection.Forward, vimState.cursorPosition, "", { isRegex: true });
vimState.currentMode = ModeName.SearchInProgressMode;
// Reset search history index
vimState.searchStateIndex = vimState.searchStatePrevious.length - 1;
Configuration.hl = true;
return vimState;
@ -1683,6 +1723,9 @@ export class CommandSearchBackwards extends BaseCommand {
vimState.searchState = new SearchState(SearchDirection.Backward, vimState.cursorPosition, "", { isRegex: true });
vimState.currentMode = ModeName.SearchInProgressMode;
// Reset search history index
vimState.searchStateIndex = vimState.searchStatePrevious.length - 1;
Configuration.hl = true;
return vimState;

View File

@ -121,6 +121,11 @@ class ConfigurationClass {
*/
leader = "\\";
/**
* How much search or command history should be remembered
*/
history = 50;
/**
* Show results of / or ? search as user is typing?
*/

View File

@ -170,7 +170,15 @@ export class VimState {
public searchState: SearchState | undefined = undefined;
public searchStatePrevious: SearchState | undefined = undefined;
/**
* Index used for navigating search history with <up> and <down> when searching
*/
public searchStateIndex: number = 0;
/**
* Previous searches performed
*/
public searchStatePrevious: SearchState[] = [];
public isRecordingMacro: boolean = false;