Skip to content

Commit

Permalink
Merge pull request adobe#13342 from adobe/saurabh95/SearchHistory
Browse files Browse the repository at this point in the history
Now initial query is also added to searchHistory queue
  • Loading branch information
zaggino authored May 5, 2017
2 parents 4aab9a7 + cd50b51 commit c8c3add
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/search/FindBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ define(function (require, exports, module) {
$elem.attr("title", oldTitle + "(" + KeyBindingManager.formatKeyDescriptor(replaceShortcut.displayKey) + ")");
}
};

/**
* @private
* Adds element to the search history queue.
* @param {string} search string that needs to be added to history.
*/
FindBar.prototype._addElementToSearchHistory = function (searchVal) {
if (searchVal) {
var searchHistory = PreferencesManager.getViewState("searchHistory");
var maxCount = PreferencesManager.get("maxSearchHistory");
var searchQueryIndex = searchHistory.indexOf(searchVal);
if (searchQueryIndex !== -1) {
searchHistory.splice(searchQueryIndex, 1);
} else {
if (searchHistory.length === maxCount) {
searchHistory.pop();
}
}
searchHistory.unshift(searchVal);
PreferencesManager.setViewState("searchHistory", searchHistory);
}
};

/**
* Opens the Find bar, closing any other existing Find bars.
Expand All @@ -248,6 +270,8 @@ define(function (require, exports, module) {
templateVars.Strings = Strings;
templateVars.replaceBatchLabel = (templateVars.multifile ? Strings.BUTTON_REPLACE_ALL_IN_FILES : Strings.BUTTON_REPLACE_BATCH);
templateVars.replaceAllLabel = Strings.BUTTON_REPLACE_ALL;

self._addElementToSearchHistory(this._options.initialQuery);

this._modalBar = new ModalBar(Mustache.render(_searchBarTemplate, templateVars), true); // 2nd arg = auto-close on Esc/blur

Expand Down Expand Up @@ -327,24 +351,10 @@ 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 searchVal = self.$("#find-what").val();
var searchQueryIndex = searchHistory.indexOf(searchVal);
if (searchQueryIndex !== -1) {
searchHistory.splice(searchQueryIndex, 1);
} else {
if (searchHistory.length === maxCount) {
searchHistory.pop();
}
}
if (searchVal) {
searchHistory.unshift(searchVal);
}
PreferencesManager.setViewState("searchHistory", searchHistory);
self._addElementToSearchHistory(self.$("#find-what").val());
lastQueriedText = self.getQueryInfo().query;
if (self._options.multifile) {
if ($(e.target).is("#find-what")) {
Expand Down
22 changes: 22 additions & 0 deletions test/spec/FindInFiles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,28 @@ define(function (require, exports, module) {
expect($("#find-what").val()).toBe("foo1");
});
});

it("should add element to search history if it is pre-filled in search bar", function () {
var fileEntry = FileSystem.getFileForPath(testPath + "/foo.js");
openSearchBar(fileEntry);

runs(function () {
$("#find-what").val("some");
closeSearchBar();
// Adding delay to make sure that search bar is closed
setTimeout(function () {
openSearchBar(fileEntry);
expect($("#find-what").val()).toBe("some");
var searchHistory = PreferencesManager.getViewState("searchHistory");
expect(searchHistory[0]).toBe("some");
var $searchField = $("#find-what");
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_UP, "keydown", $searchField[0]);
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_UP, "keydown", $searchField[0]);
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_RETURN, "keydown", $searchField[0]);
expect($("#find-what").val()).toBe("some");
}, 500);
});
});

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

0 comments on commit c8c3add

Please sign in to comment.