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

Commit 451fd32

Browse files
committed
Merge pull request #9601 from MarcelGerber/find-keep-query
Keep query and replace text when switching between Replace and Replace In Files
2 parents 8fd477a + 0358030 commit 451fd32

5 files changed

Lines changed: 53 additions & 32 deletions

File tree

src/htmlContent/findreplace-bar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
{{#replace}}
1515
<div id="replace-group" {{#scope}}class="has-scope"{{/scope}}><!--
16-
--><input type="text" id="replace-with" placeholder="{{Strings.REPLACE_PLACEHOLDER}}"/><!--
16+
--><input type="text" id="replace-with" placeholder="{{Strings.REPLACE_PLACEHOLDER}}" value="{{initialReplaceText}}" /><!--
1717
{{^multifile}}
1818
--><button id="replace-yes" class="btn no-focus" tabindex="-1">{{Strings.BUTTON_REPLACE}}</button><!--
1919
{{/multifile}}

src/search/FindBar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ define(function (require, exports, module) {
7777
replace: false,
7878
queryPlaceholder: "",
7979
initialQuery: "",
80+
initialReplaceText: "",
8081
scopeLabel: ""
8182
};
8283
this._options = _.extend(defaults, options);

src/search/FindInFilesUI.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,9 @@ define(function (require, exports, module) {
129129
return;
130130
}
131131

132-
// Default to searching for the current selection
132+
// Get initial query/replace text
133133
var currentEditor = EditorManager.getActiveEditor(),
134-
initialQuery = "";
135-
136-
if (_findBar && !_findBar.isClosed()) {
137-
// The modalBar was already up. When creating the new modalBar, copy the
138-
// current query instead of using the passed-in selected text.
139-
initialQuery = _findBar.getQueryInfo().query;
140-
} else if (currentEditor) {
141-
initialQuery = FindUtils.getInitialQueryFromSelection(currentEditor);
142-
}
134+
initialQuery = FindUtils.getInitialQuery(_findBar, currentEditor);
143135

144136
// Close our previous find bar, if any. (The open() of the new _findBar will
145137
// take care of closing any other find bar instances.)
@@ -150,7 +142,8 @@ define(function (require, exports, module) {
150142
_findBar = new FindBar({
151143
multifile: true,
152144
replace: showReplace,
153-
initialQuery: initialQuery,
145+
initialQuery: initialQuery.query,
146+
initialReplaceText: initialQuery.replaceText,
154147
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER,
155148
scopeLabel: FindUtils.labelForScope(scope)
156149
});

src/search/FindReplace.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,7 @@ define(function (require, exports, module) {
593593
state.searchStartPos = editor.getCursorPos(false, "start");
594594

595595
// Prepopulate the search field
596-
var initialQuery;
597-
if (findBar) {
598-
// Use the previous query. This can happen if the user switches from Find to Replace.
599-
initialQuery = findBar.getQueryInfo().query;
600-
} else {
601-
initialQuery = FindUtils.getInitialQueryFromSelection(editor);
602-
}
596+
var initialQuery = FindUtils.getInitialQuery(findBar, editor);
603597

604598
// Close our previous find bar, if any. (The open() of the new findBar will
605599
// take care of closing any other find bar instances.)
@@ -611,7 +605,8 @@ define(function (require, exports, module) {
611605
findBar = new FindBar({
612606
multifile: false,
613607
replace: replace,
614-
initialQuery: initialQuery,
608+
initialQuery: initialQuery.query,
609+
initialReplaceText: initialQuery.replaceText,
615610
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER
616611
});
617612
findBar.open();

src/search/FindUtils.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ define(function (require, exports, module) {
3232
MainViewManager = require("view/MainViewManager"),
3333
FileSystem = require("filesystem/FileSystem"),
3434
FileUtils = require("file/FileUtils"),
35+
FindBar = require("search/FindBar").FindBar,
3536
ProjectManager = require("project/ProjectManager"),
3637
Strings = require("strings"),
3738
StringUtils = require("utils/StringUtils"),
@@ -71,19 +72,50 @@ define(function (require, exports, module) {
7172
return replaceWith;
7273
}
7374

74-
/*
75-
* Returns the string used to prepopulate the find bar
76-
* @param {!Editor} editor
77-
* @return {string} first line of primary selection to populate the find bar
75+
/**
76+
* Gets you the right query and replace text to prepopulate the Find Bar.
77+
* @param {?FindBar} currentFindBar The currently open Find Bar, if any
78+
* @param {?Editor} The active editor, if any
79+
* @return {query: string, replaceText: string} Query and Replace text to prepopulate the Find Bar with
7880
*/
79-
function getInitialQueryFromSelection(editor) {
80-
var selectionText = editor.getSelectedText();
81-
if (selectionText) {
82-
return selectionText
83-
.replace(/^\n*/, "") // Trim possible newlines at the very beginning of the selection
84-
.split("\n")[0];
81+
function getInitialQuery(currentFindBar, editor) {
82+
var query = "",
83+
replaceText = "";
84+
85+
/*
86+
* Returns the string used to prepopulate the find bar
87+
* @param {!Editor} editor
88+
* @return {string} first line of primary selection to populate the find bar
89+
*/
90+
function getInitialQueryFromSelection(editor) {
91+
var selectionText = editor.getSelectedText();
92+
if (selectionText) {
93+
return selectionText
94+
.replace(/^\n*/, "") // Trim possible newlines at the very beginning of the selection
95+
.split("\n")[0];
96+
}
97+
return "";
8598
}
86-
return "";
99+
100+
if (currentFindBar && !currentFindBar.isClosed()) {
101+
// The modalBar was already up. When creating the new modalBar, copy the
102+
// current query instead of using the passed-in selected text.
103+
query = currentFindBar.getQueryInfo().query;
104+
replaceText = currentFindBar.getReplaceText();
105+
} else {
106+
var openedFindBar = FindBar._bars && _.find(FindBar._bars, function (bar) {
107+
return !bar.isClosed();
108+
});
109+
110+
if (openedFindBar) {
111+
query = openedFindBar.getQueryInfo().query;
112+
replaceText = openedFindBar.getReplaceText();
113+
} else if (editor) {
114+
query = getInitialQueryFromSelection(editor);
115+
}
116+
}
117+
118+
return {query: query, replaceText: replaceText};
87119
}
88120

89121
/**
@@ -311,7 +343,7 @@ define(function (require, exports, module) {
311343
}
312344

313345
exports.parseDollars = parseDollars;
314-
exports.getInitialQueryFromSelection = getInitialQueryFromSelection;
346+
exports.getInitialQuery = getInitialQuery;
315347
exports.hasCheckedMatches = hasCheckedMatches;
316348
exports.performReplacements = performReplacements;
317349
exports.labelForScope = labelForScope;

0 commit comments

Comments
 (0)