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

Only use first line of primary selection when populating FindInFiles bar #8470

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/search/FindInFilesUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ define(function (require, exports, module) {

// Default to searching for the current selection
var currentEditor = EditorManager.getActiveEditor(),
initialString = currentEditor && currentEditor.getSelectedText();
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.
initialString = _findBar.getQueryInfo().query;
initialQuery = _findBar.getQueryInfo().query;
} else if (currentEditor) {
initialQuery = FindUtils.getInitialQueryFromSelection(currentEditor);
}

FindInFiles.clearSearch();
Expand All @@ -149,7 +151,7 @@ define(function (require, exports, module) {
_findBar = new FindBar({
multifile: true,
replace: showReplace,
initialQuery: initialString,
initialQuery: initialQuery,
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER,
scopeLabel: FindUtils.labelForScope(scope)
});
Expand Down Expand Up @@ -403,4 +405,4 @@ define(function (require, exports, module) {
// For unit testing
exports._showFindBar = _showFindBar;
exports._closeFindBar = _closeFindBar;
});
});
10 changes: 1 addition & 9 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,7 @@ define(function (require, exports, module) {
// Use the previous query. This can happen if the user switches from Find to Replace.
initialQuery = findBar.getQueryInfo().query;
} else {
// Prepopulate with the current primary selection, if any
var sel = editor.getSelection();
initialQuery = cm.getRange(sel.start, sel.end);

// Eliminate newlines since we don't generally support searching across line boundaries (#2960)
var newline = initialQuery.indexOf("\n");
if (newline !== -1) {
initialQuery = initialQuery.substr(0, newline);
}
initialQuery = FindUtils.getInitialQueryFromSelection(editor);
}

// Close our previous find bar, if any. (The open() of the new findBar will
Expand Down
26 changes: 21 additions & 5 deletions src/search/FindUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ 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
*/
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 "";
}

/**
* Does a set of replacements in a single document in memory.
* @param {!Document} doc The document to do the replacements in.
Expand Down Expand Up @@ -255,9 +270,10 @@ define(function (require, exports, module) {
}
}

exports.parseDollars = parseDollars;
exports.hasCheckedMatches = hasCheckedMatches;
exports.performReplacements = performReplacements;
exports.labelForScope = labelForScope;
exports.ERROR_FILE_CHANGED = "fileChanged";
exports.parseDollars = parseDollars;
exports.getInitialQueryFromSelection = getInitialQueryFromSelection;
exports.hasCheckedMatches = hasCheckedMatches;
exports.performReplacements = performReplacements;
exports.labelForScope = labelForScope;
exports.ERROR_FILE_CHANGED = "fileChanged";
});
46 changes: 46 additions & 0 deletions test/spec/FindInFiles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,52 @@ define(function (require, exports, module) {
});

describe("Full workflow", function () {
it("should prepopulate the find bar with selected text", function () {
var doc, editor;

openTestProjectCopy(defaultSourcePath);
runs(function () {
waitsForDone(CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, { fullPath: testPath + "/foo.html" }), "open file");
});
runs(function () {
doc = DocumentManager.getOpenDocumentForPath(testPath + "/foo.html");
expect(doc).toBeTruthy();
DocumentManager.setCurrentDocument(doc);
editor = doc._masterEditor;
expect(editor).toBeTruthy();
editor.setSelection({line: 4, ch: 7}, {line: 4, ch: 10});
});

openSearchBar(null);
runs(function () {
expect($("#find-what").val()).toBe("Foo");
});
waitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL), "closing all files");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary? The other tests below don't need it...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nm, I see they don't initially open any files while these two tests do.

});

it("should prepopulate the find bar with only first line of selected text", function () {
var doc, editor;

openTestProjectCopy(defaultSourcePath);
runs(function () {
waitsForDone(CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, { fullPath: testPath + "/foo.html" }), "open file");
});
runs(function () {
doc = DocumentManager.getOpenDocumentForPath(testPath + "/foo.html");
expect(doc).toBeTruthy();
DocumentManager.setCurrentDocument(doc);
editor = doc._masterEditor;
expect(editor).toBeTruthy();
editor.setSelection({line: 4, ch: 7}, {line: 6, ch: 10});
});

openSearchBar(null);
runs(function () {
expect($("#find-what").val()).toBe("Foo</title>");
});
waitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL), "closing all files");
});

it("should show results from the search with all checkboxes checked", function () {
showSearchResults("foo", "bar");
runs(function () {
Expand Down