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

Commit

Permalink
Merge pull request #13237 from adobe/saurabh95/SearchHistory
Browse files Browse the repository at this point in the history
Now search history is stored in state and can be traversed using arrow keys while focus is on search text
  • Loading branch information
zaggino committed Mar 29, 2017
2 parents e1caef2 + b99b4dd commit 349a458
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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 historyIndex = 0;
$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.pop();
}
}
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);
}
historyIndex = 0;
} else if (e.keyCode === KeyEvent.DOM_VK_DOWN || e.keyCode === KeyEvent.DOM_VK_UP) {
if (e.keyCode === KeyEvent.DOM_VK_DOWN) {
historyIndex = (historyIndex - 1 + searchHistory.length) % searchHistory.length;
} else {
historyIndex = (historyIndex + 1 + searchHistory.length) % searchHistory.length;
}
$("#find-what").val(searchHistory[historyIndex]);
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;
});
38 changes: 38 additions & 0 deletions test/spec/FindInFiles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ define(function (require, exports, module) {
PreferencesManager = testWindow.brackets.test.PreferencesManager;
PreferencesManager.set("findInFiles.nodeSearch", false);
PreferencesManager.set("findInFiles.instantSearch", false);
PreferencesManager.set("maxSearchHistory", 5);
});
});

Expand Down Expand Up @@ -409,6 +410,43 @@ define(function (require, exports, module) {
expect(fileResults).toBeFalsy();
});
});

it("should verify the contents of searchHistory array", function () {
var fileEntry = FileSystem.getFileForPath(testPath + "/foo.js");
openSearchBar(fileEntry);
executeSearch("foo1");
executeSearch("foo2");
executeSearch("foo3");
executeSearch("foo4");
executeSearch("foo5");

runs(function () {
var searchHistory = PreferencesManager.getViewState("searchHistory");
expect(searchHistory.length).toBe(5);
expect(searchHistory).toEqual(["foo5", "foo4", "foo3", "foo2", "foo1"]);
});
});

it("should traverse through search history using up and down arrow keys", function () {
var fileEntry = FileSystem.getFileForPath(testPath + "/foo.js");
openSearchBar(fileEntry);
executeSearch("foo1");
executeSearch("foo2");
executeSearch("foo3");
executeSearch("foo4");
executeSearch("foo5");

runs(function () {
var searchHistory = PreferencesManager.getViewState("searchHistory");
var $searchField = $("#find-what");
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_UP, "keydown", $searchField[0]);
expect($("#find-what").val()).toBe("foo4");
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", $searchField[0]);
expect($("#find-what").val()).toBe("foo5");
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", $searchField[0]);
expect($("#find-what").val()).toBe("foo1");
});
});

it("should find start and end positions", function () {
var filePath = testPath + "/foo.js",
Expand Down

0 comments on commit 349a458

Please sign in to comment.