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

Populate the replace input with the currently selected text #1962

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 7 additions & 2 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,14 @@ define(function (require, exports, module) {
findFirst(getDialogTextField().attr("value"));
});
}

function getCurrentEditorSelectedText() {
var currentEditor = EditorManager.getFocusedEditor();
return (currentEditor && currentEditor.getSelectedText()) || "";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now used in QuickOpen and FindInFiles (sort of) other than here. Maybe it would be worth refactoring at some point...

}

var replaceQueryDialog = Strings.CMD_REPLACE +
': <input type="text" style="width: 10em"/> <span style="color: #888">(' +
': <input type="text" style="width: 10em" value="{{SELECTION}}"/> <span style="color: #888">(' +
Strings.SEARCH_REGEXP_INFO + ')</span>';
var replacementQueryDialog = Strings.WITH +
': <input type="text" style="width: 10em"/>';
Expand All @@ -198,7 +203,7 @@ define(function (require, exports, module) {
'</button> <button' + style + '>' + Strings.BUTTON_STOP + '</button>';

function replace(cm, all) {
dialog(cm, replaceQueryDialog, Strings.CMD_REPLACE, function (query) {
dialog(cm, replaceQueryDialog.replace("{{SELECTION}}", getCurrentEditorSelectedText()), Strings.CMD_REPLACE, function (query) {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't do any escaping, so it could break the HTML if there are quotes/etc. in the string.

Why not do it the same way as _launchFind()? That should work just as well for the Replace dialog. And since _replace() only does anything when there is an Editor available, I think you could ditch getCurrentEditorSelectedText() and follow _launchFind()'s example there too -- i.e. simpler logic for getting the initial string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see... didn't notice that...

I'm closing this pull request then and will submit a new one following your directions soon

if (!query) {
return;
}
Expand Down