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

Commit 3051977

Browse files
committed
Merge pull request #8470 from MarcelGerber/find-in-files-selection-newline
Only use first line of primary selection when populating FindInFiles bar. Consolidate code with FindReplace.
2 parents 884ee84 + ae4881a commit 3051977

File tree

4 files changed

+74
-18
lines changed

4 files changed

+74
-18
lines changed

src/search/FindInFilesUI.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +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;
138+
initialQuery = _findBar.getQueryInfo().query;
139+
} else if (currentEditor) {
140+
initialQuery = FindUtils.getInitialQueryFromSelection(currentEditor);
139141
}
140142

141143
FindInFiles.clearSearch();
@@ -149,7 +151,7 @@ define(function (require, exports, module) {
149151
_findBar = new FindBar({
150152
multifile: true,
151153
replace: showReplace,
152-
initialQuery: initialString,
154+
initialQuery: initialQuery,
153155
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER,
154156
scopeLabel: FindUtils.labelForScope(scope)
155157
});
@@ -403,4 +405,4 @@ define(function (require, exports, module) {
403405
// For unit testing
404406
exports._showFindBar = _showFindBar;
405407
exports._closeFindBar = _closeFindBar;
406-
});
408+
});

src/search/FindReplace.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -610,15 +610,7 @@ define(function (require, exports, module) {
610610
// Use the previous query. This can happen if the user switches from Find to Replace.
611611
initialQuery = findBar.getQueryInfo().query;
612612
} else {
613-
// Prepopulate with the current primary selection, if any
614-
var sel = editor.getSelection();
615-
initialQuery = cm.getRange(sel.start, sel.end);
616-
617-
// Eliminate newlines since we don't generally support searching across line boundaries (#2960)
618-
var newline = initialQuery.indexOf("\n");
619-
if (newline !== -1) {
620-
initialQuery = initialQuery.substr(0, newline);
621-
}
613+
initialQuery = FindUtils.getInitialQueryFromSelection(editor);
622614
}
623615

624616
// 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
});

test/spec/FindInFiles-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,52 @@ define(function (require, exports, module) {
17221722
});
17231723

17241724
describe("Full workflow", function () {
1725+
it("should prepopulate the find bar with selected text", function () {
1726+
var doc, editor;
1727+
1728+
openTestProjectCopy(defaultSourcePath);
1729+
runs(function () {
1730+
waitsForDone(CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, { fullPath: testPath + "/foo.html" }), "open file");
1731+
});
1732+
runs(function () {
1733+
doc = DocumentManager.getOpenDocumentForPath(testPath + "/foo.html");
1734+
expect(doc).toBeTruthy();
1735+
DocumentManager.setCurrentDocument(doc);
1736+
editor = doc._masterEditor;
1737+
expect(editor).toBeTruthy();
1738+
editor.setSelection({line: 4, ch: 7}, {line: 4, ch: 10});
1739+
});
1740+
1741+
openSearchBar(null);
1742+
runs(function () {
1743+
expect($("#find-what").val()).toBe("Foo");
1744+
});
1745+
waitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL), "closing all files");
1746+
});
1747+
1748+
it("should prepopulate the find bar with only first line of selected text", function () {
1749+
var doc, editor;
1750+
1751+
openTestProjectCopy(defaultSourcePath);
1752+
runs(function () {
1753+
waitsForDone(CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, { fullPath: testPath + "/foo.html" }), "open file");
1754+
});
1755+
runs(function () {
1756+
doc = DocumentManager.getOpenDocumentForPath(testPath + "/foo.html");
1757+
expect(doc).toBeTruthy();
1758+
DocumentManager.setCurrentDocument(doc);
1759+
editor = doc._masterEditor;
1760+
expect(editor).toBeTruthy();
1761+
editor.setSelection({line: 4, ch: 7}, {line: 6, ch: 10});
1762+
});
1763+
1764+
openSearchBar(null);
1765+
runs(function () {
1766+
expect($("#find-what").val()).toBe("Foo</title>");
1767+
});
1768+
waitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL), "closing all files");
1769+
});
1770+
17251771
it("should show results from the search with all checkboxes checked", function () {
17261772
showSearchResults("foo", "bar");
17271773
runs(function () {

0 commit comments

Comments
 (0)