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

Commit ae4881a

Browse files
author
Marcel Gerber
committed
Introduce FindUtils.getInitialQueryFromSelection
1 parent d62e08e commit ae4881a

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/search/FindInFilesUI.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,14 @@ define(function (require, exports, module) {
130130

131131
// Default to searching for the current selection
132132
var currentEditor = EditorManager.getActiveEditor(),
133-
initialString = currentEditor && currentEditor.getSelectedText();
133+
initialQuery = "";
134134

135135
if (_findBar && !_findBar.isClosed()) {
136136
// The modalBar was already up. When creating the new modalBar, copy the
137137
// current query instead of using the passed-in selected text.
138-
initialString = _findBar.getQueryInfo().query;
139-
} else if (initialString) {
140-
// Eliminate newlines since we don't generally support searching across line boundaries (#2960)
141-
var newline = initialString.indexOf("\n");
142-
if (newline !== -1) {
143-
initialString = initialString.substr(0, newline);
144-
}
138+
initialQuery = _findBar.getQueryInfo().query;
139+
} else if (currentEditor) {
140+
initialQuery = FindUtils.getInitialQueryFromSelection(currentEditor);
145141
}
146142

147143
FindInFiles.clearSearch();
@@ -155,7 +151,7 @@ define(function (require, exports, module) {
155151
_findBar = new FindBar({
156152
multifile: true,
157153
replace: showReplace,
158-
initialQuery: initialString,
154+
initialQuery: initialQuery,
159155
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER,
160156
scopeLabel: FindUtils.labelForScope(scope)
161157
});
@@ -409,4 +405,4 @@ define(function (require, exports, module) {
409405
// For unit testing
410406
exports._showFindBar = _showFindBar;
411407
exports._closeFindBar = _closeFindBar;
412-
});
408+
});

src/search/FindReplace.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,7 @@ define(function (require, exports, module) {
563563
// Use the previous query. This can happen if the user switches from Find to Replace.
564564
initialQuery = findBar.getQueryInfo().query;
565565
} else {
566-
// Prepopulate with the current primary selection, if any
567-
initialQuery = editor.getSelectedText();
568-
569-
// Eliminate newlines since we don't generally support searching across line boundaries (#2960)
570-
var newline = initialQuery.indexOf("\n");
571-
if (newline !== -1) {
572-
initialQuery = initialQuery.substr(0, newline);
573-
}
566+
initialQuery = FindUtils.getInitialQueryFromSelection(editor);
574567
}
575568

576569
// Close our previous find bar, if any. (The open() of the new findBar will

src/search/FindUtils.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ define(function (require, exports, module) {
7171
return replaceWith;
7272
}
7373

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
78+
*/
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];
85+
}
86+
return "";
87+
}
88+
7489
/**
7590
* Does a set of replacements in a single document in memory.
7691
* @param {!Document} doc The document to do the replacements in.
@@ -255,9 +270,10 @@ define(function (require, exports, module) {
255270
}
256271
}
257272

258-
exports.parseDollars = parseDollars;
259-
exports.hasCheckedMatches = hasCheckedMatches;
260-
exports.performReplacements = performReplacements;
261-
exports.labelForScope = labelForScope;
262-
exports.ERROR_FILE_CHANGED = "fileChanged";
273+
exports.parseDollars = parseDollars;
274+
exports.getInitialQueryFromSelection = getInitialQueryFromSelection;
275+
exports.hasCheckedMatches = hasCheckedMatches;
276+
exports.performReplacements = performReplacements;
277+
exports.labelForScope = labelForScope;
278+
exports.ERROR_FILE_CHANGED = "fileChanged";
263279
});

0 commit comments

Comments
 (0)