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 #9601 from MarcelGerber/find-keep-query
Browse files Browse the repository at this point in the history
Keep query and replace text when switching between Replace and Replace In Files
  • Loading branch information
redmunds committed Oct 21, 2014
2 parents 8fd477a + 0358030 commit 451fd32
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/htmlContent/findreplace-bar.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{{#replace}}
<div id="replace-group" {{#scope}}class="has-scope"{{/scope}}><!--
--><input type="text" id="replace-with" placeholder="{{Strings.REPLACE_PLACEHOLDER}}"/><!--
--><input type="text" id="replace-with" placeholder="{{Strings.REPLACE_PLACEHOLDER}}" value="{{initialReplaceText}}" /><!--
{{^multifile}}
--><button id="replace-yes" class="btn no-focus" tabindex="-1">{{Strings.BUTTON_REPLACE}}</button><!--
{{/multifile}}
Expand Down
1 change: 1 addition & 0 deletions src/search/FindBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ define(function (require, exports, module) {
replace: false,
queryPlaceholder: "",
initialQuery: "",
initialReplaceText: "",
scopeLabel: ""
};
this._options = _.extend(defaults, options);
Expand Down
15 changes: 4 additions & 11 deletions src/search/FindInFilesUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,9 @@ define(function (require, exports, module) {
return;
}

// Default to searching for the current selection
// Get initial query/replace text
var currentEditor = EditorManager.getActiveEditor(),
initialQuery = "";

if (_findBar && !_findBar.isClosed()) {
// The modalBar was already up. When creating the new modalBar, copy the
// current query instead of using the passed-in selected text.
initialQuery = _findBar.getQueryInfo().query;
} else if (currentEditor) {
initialQuery = FindUtils.getInitialQueryFromSelection(currentEditor);
}
initialQuery = FindUtils.getInitialQuery(_findBar, currentEditor);

// Close our previous find bar, if any. (The open() of the new _findBar will
// take care of closing any other find bar instances.)
Expand All @@ -150,7 +142,8 @@ define(function (require, exports, module) {
_findBar = new FindBar({
multifile: true,
replace: showReplace,
initialQuery: initialQuery,
initialQuery: initialQuery.query,
initialReplaceText: initialQuery.replaceText,
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER,
scopeLabel: FindUtils.labelForScope(scope)
});
Expand Down
11 changes: 3 additions & 8 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,7 @@ define(function (require, exports, module) {
state.searchStartPos = editor.getCursorPos(false, "start");

// Prepopulate the search field
var initialQuery;
if (findBar) {
// Use the previous query. This can happen if the user switches from Find to Replace.
initialQuery = findBar.getQueryInfo().query;
} else {
initialQuery = FindUtils.getInitialQueryFromSelection(editor);
}
var initialQuery = FindUtils.getInitialQuery(findBar, editor);

// Close our previous find bar, if any. (The open() of the new findBar will
// take care of closing any other find bar instances.)
Expand All @@ -611,7 +605,8 @@ define(function (require, exports, module) {
findBar = new FindBar({
multifile: false,
replace: replace,
initialQuery: initialQuery,
initialQuery: initialQuery.query,
initialReplaceText: initialQuery.replaceText,
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER
});
findBar.open();
Expand Down
56 changes: 44 additions & 12 deletions src/search/FindUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ define(function (require, exports, module) {
MainViewManager = require("view/MainViewManager"),
FileSystem = require("filesystem/FileSystem"),
FileUtils = require("file/FileUtils"),
FindBar = require("search/FindBar").FindBar,
ProjectManager = require("project/ProjectManager"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
Expand Down Expand Up @@ -71,19 +72,50 @@ define(function (require, exports, module) {
return replaceWith;
}

/*
* Returns the string used to prepopulate the find bar
* @param {!Editor} editor
* @return {string} first line of primary selection to populate the find bar
/**
* Gets you the right query and replace text to prepopulate the Find Bar.
* @param {?FindBar} currentFindBar The currently open Find Bar, if any
* @param {?Editor} The active editor, if any
* @return {query: string, replaceText: string} Query and Replace text to prepopulate the Find Bar with
*/
function getInitialQueryFromSelection(editor) {
var selectionText = editor.getSelectedText();
if (selectionText) {
return selectionText
.replace(/^\n*/, "") // Trim possible newlines at the very beginning of the selection
.split("\n")[0];
function getInitialQuery(currentFindBar, editor) {
var query = "",
replaceText = "";

/*
* Returns the string used to prepopulate the find bar
* @param {!Editor} editor
* @return {string} first line of primary selection to populate the find bar
*/
function getInitialQueryFromSelection(editor) {
var selectionText = editor.getSelectedText();
if (selectionText) {
return selectionText
.replace(/^\n*/, "") // Trim possible newlines at the very beginning of the selection
.split("\n")[0];
}
return "";
}
return "";

if (currentFindBar && !currentFindBar.isClosed()) {
// The modalBar was already up. When creating the new modalBar, copy the
// current query instead of using the passed-in selected text.
query = currentFindBar.getQueryInfo().query;
replaceText = currentFindBar.getReplaceText();
} else {
var openedFindBar = FindBar._bars && _.find(FindBar._bars, function (bar) {
return !bar.isClosed();
});

if (openedFindBar) {
query = openedFindBar.getQueryInfo().query;
replaceText = openedFindBar.getReplaceText();
} else if (editor) {
query = getInitialQueryFromSelection(editor);
}
}

return {query: query, replaceText: replaceText};
}

/**
Expand Down Expand Up @@ -311,7 +343,7 @@ define(function (require, exports, module) {
}

exports.parseDollars = parseDollars;
exports.getInitialQueryFromSelection = getInitialQueryFromSelection;
exports.getInitialQuery = getInitialQuery;
exports.hasCheckedMatches = hasCheckedMatches;
exports.performReplacements = performReplacements;
exports.labelForScope = labelForScope;
Expand Down

0 comments on commit 451fd32

Please sign in to comment.