diff --git a/src/search/FindBar.js b/src/search/FindBar.js index f571eeab4..a68e973b8 100644 --- a/src/search/FindBar.js +++ b/src/search/FindBar.js @@ -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. @@ -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 @@ -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")) { diff --git a/test/spec/FindInFiles-test.js b/test/spec/FindInFiles-test.js index 769165027..0ff249359 100644 --- a/test/spec/FindInFiles-test.js +++ b/test/spec/FindInFiles-test.js @@ -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",