Skip to content

Commit

Permalink
fixes #657 implements search history (#1147)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xconverge authored and johnfn committed Dec 11, 2016
1 parent 2c81aa4 commit c8ee059
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
55 changes: 49 additions & 6 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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 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);
}
// Store this search
vimState.searchStatePrevious = searchState;

// 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;
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*/
Expand Down
10 changes: 9 additions & 1 deletion src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c8ee059

Please sign in to comment.