Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Now search history is stored in state and can be traversed using arrow keys while focus is on search text #13237

Merged
merged 4 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ define({
"FIND_MATCH_INDEX" : "{0} of {1}",
"FIND_NO_RESULTS" : "No results",
"FIND_QUERY_PLACEHOLDER" : "Find\u2026",
"FIND_HISTORY_MAX_COUNT" : "Maximum Number of Search Items in Search History",
"REPLACE_PLACEHOLDER" : "Replace with\u2026",
"BUTTON_REPLACE_ALL" : "Replace All",
"BUTTON_REPLACE_BATCH" : "Batch\u2026",
Expand Down
26 changes: 26 additions & 0 deletions src/search/FindBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ define(function (require, exports, module) {
FindBar._addFindBar(this);

var $root = this._modalBar.getRoot();
var currentIndex = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename this to historyIndex or something little more specific

$root
.on("input", "#find-what", function () {
self.trigger("queryChange");
Expand Down Expand Up @@ -310,9 +311,21 @@ define(function (require, exports, module) {
if (intervalId === 0) {
intervalId = window.setInterval(executeSearchIfNeeded, 50);
}
var searchHistory = PreferencesManager.getViewState("searchHistory");
var maxCount = PreferencesManager.get("maxSearchHistory");
if (e.keyCode === KeyEvent.DOM_VK_RETURN) {
e.preventDefault();
e.stopPropagation();
var searchQueryIndex = searchHistory.indexOf($('#find-what').val());
if (searchQueryIndex !== -1) {
searchHistory.splice(searchQueryIndex, 1);
} else {
if (searchHistory.length === maxCount) {
searchHistory.splice(maxCount - 1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't .pop() the same thing?

}
}
searchHistory.unshift($('#find-what').val());
PreferencesManager.setViewState("searchHistory", searchHistory);
lastQueriedText = self.getQueryInfo().query;
if (self._options.multifile) {
if ($(e.target).is("#find-what")) {
Expand All @@ -333,6 +346,15 @@ define(function (require, exports, module) {
// if Shift is held down).
self.trigger("doFind", e.shiftKey);
}
currentIndex = 0;
} else if (e.keyCode === KeyEvent.DOM_VK_DOWN) {
currentIndex = (currentIndex - 1 + Math.min(maxCount, searchHistory.length)) % Math.min(maxCount, searchHistory.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (140 chars)

$("#find-what").val(searchHistory[currentIndex]);
self.trigger("queryChange");
} else if (e.keyCode === KeyEvent.DOM_VK_UP) {
currentIndex = (currentIndex + 1 + Math.min(maxCount, searchHistory.length)) % Math.min(maxCount, searchHistory.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you wrap if (e.keyCode === KeyEvent.DOM_VK_DOWN) and if (e.keyCode === KeyEvent.DOM_VK_UP) { codes into a function taking an argument up or down? code seems to repeat itself

$("#find-what").val(searchHistory[currentIndex]);
self.trigger("queryChange");
}
});

Expand Down Expand Up @@ -616,6 +638,10 @@ define(function (require, exports, module) {

PreferencesManager.stateManager.definePreference("caseSensitive", "boolean", false);
PreferencesManager.stateManager.definePreference("regexp", "boolean", false);
PreferencesManager.stateManager.definePreference("searchHistory", "array", []);
PreferencesManager.definePreference("maxSearchHistory", "number", 10, {
description: Strings.FIND_HISTORY_MAX_COUNT
});

exports.FindBar = FindBar;
});