diff --git a/src/search/FindReplace.js b/src/search/FindReplace.js index 2a81f4562cc..69855b12cb7 100644 --- a/src/search/FindReplace.js +++ b/src/search/FindReplace.js @@ -468,7 +468,7 @@ define(function (require, exports, module) { .reverse() .forEach(function (checkedRow) { var match = results[$(checkedRow).data("match")], - rw = typeof replaceWhat === "string" ? replaceWith : replaceWith.replace(/\$(\d)/, replaceFunction); + rw = typeof replaceWhat === "string" ? replaceWith : replaceWith.replace(/\$(\d)/g, replaceFunction); editor.document.replaceRange(rw, match.from, match.to, "+replaceAll"); }); _closeReplaceAllPanel(); @@ -508,7 +508,7 @@ define(function (require, exports, module) { ' '; - function replace(editor, all) { + function replace(editor) { var cm = editor._codeMirror; createModalBar(replaceQueryDialog, true); $(modalBar).on("commit", function (e, query) { @@ -524,62 +524,44 @@ define(function (require, exports, module) { createModalBar(replacementQueryDialog, true, false); $(modalBar).on("commit", function (e, text) { text = text || ""; - var match, - fnMatch = function (w, i) { return match[i]; }; - if (all) { - cm.compoundChange(function () { - cm.operation(function () { - var cursor = getSearchCursor(cm, query); - while (cursor.findNext()) { - if (typeof query !== "string") { - match = cm.getRange(cursor.from(), cursor.to()).match(query); - cursor.replace(text.replace(/\$(\d)/, fnMatch)); - } else { - cursor.replace(text); - } - } - }); - }); - } else { - clearSearch(cm); - var cursor = getSearchCursor(cm, query, cm.getCursor(true)); - var advance = function () { - var start = cursor.from(), - match = cursor.findNext(); - if (!match) { - cursor = getSearchCursor(cm, query); - match = cursor.findNext(); - if (!match || - (start && cursor.from().line === start.line && cursor.from().ch === start.ch)) { - // No more matches, so destroy modalBar - modalBar = null; - return; - } + clearSearch(cm); + var cursor = getSearchCursor(cm, query, cm.getCursor(true)); + var advance = function () { + var start = cursor.from(), + match = cursor.findNext(); + if (!match) { + cursor = getSearchCursor(cm, query); + match = cursor.findNext(); + if (!match || + (start && cursor.from().line === start.line && cursor.from().ch === start.ch)) { + // No more matches, so destroy modalBar + modalBar = null; + return; } - editor.setSelection(cursor.from(), cursor.to(), true, Editor.BOUNDARY_CHECK_NORMAL); - createModalBar(doReplaceConfirm, true, false); - modalBar.getRoot().on("click", function (e) { - var animate = (e.target.id !== "replace-yes" && e.target.id !== "replace-no"); - modalBar.close(true, animate); - if (e.target.id === "replace-yes") { - doReplace(match); - } else if (e.target.id === "replace-no") { - advance(); - } else if (e.target.id === "replace-all") { - _showReplaceAllPanel(editor, query, text, fnMatch); - } else if (e.target.id === "replace-stop") { - // Destroy modalBar on stop - modalBar = null; - } - }); - }; - var doReplace = function (match) { - cursor.replace(typeof query === "string" ? text : - text.replace(/\$(\d)/, fnMatch)); - advance(); - }; + } + editor.setSelection(cursor.from(), cursor.to(), true, Editor.BOUNDARY_CHECK_NORMAL); + createModalBar(doReplaceConfirm, true, false); + modalBar.getRoot().on("click", function (e) { + var animate = (e.target.id !== "replace-yes" && e.target.id !== "replace-no"); + modalBar.close(true, animate); + if (e.target.id === "replace-yes") { + doReplace(match); + } else if (e.target.id === "replace-no") { + advance(); + } else if (e.target.id === "replace-all") { + _showReplaceAllPanel(editor, query, text, function (w, i) { return match[i]; }); + } else if (e.target.id === "replace-stop") { + // Destroy modalBar on stop + modalBar = null; + } + }); + }; + var doReplace = function (match) { + cursor.replace(typeof query === "string" ? text : + text.replace(/\$(\d)/g, function (w, i) { return match[i]; })); advance(); - } + }; + advance(); }); }); @@ -648,4 +630,4 @@ define(function (require, exports, module) { CommandManager.register(Strings.CMD_FIND_NEXT, Commands.EDIT_FIND_NEXT, _findNext); CommandManager.register(Strings.CMD_REPLACE, Commands.EDIT_REPLACE, _replace); CommandManager.register(Strings.CMD_FIND_PREVIOUS, Commands.EDIT_FIND_PREVIOUS, _findPrevious); -}); +}); \ No newline at end of file diff --git a/test/spec/FindReplace-test.js b/test/spec/FindReplace-test.js index 2287612995a..67f8a905932 100644 --- a/test/spec/FindReplace-test.js +++ b/test/spec/FindReplace-test.js @@ -725,6 +725,37 @@ define(function (require, exports, module) { expect(/bar/i.test(myEditor.getSelectedText())).toBe(true); }); }); + + it("should find and replace a regexp with $n substitutions", function () { + runs(function () { + twCommandManager.execute(Commands.EDIT_REPLACE); + enterSearchText("/(modules)\\/(\\w+)/"); + pressEnter(); + }); + + waitsForSearchBarReopen(); + + runs(function () { + enterSearchText("$2:$1"); + pressEnter(); + }); + + waitsForSearchBarReopen(); + + runs(function () { + var expectedMatch = {start: {line: LINE_FIRST_REQUIRE, ch: 23}, end: {line: LINE_FIRST_REQUIRE, ch: 34}}; + + expectSelection(expectedMatch); + expect(/foo/i.test(myEditor.getSelectedText())).toBe(true); + + expect(tw$("#replace-yes").is(":visible")).toBe(true); + tw$("#replace-yes").click(); + tw$("#replace-stop").click(); + + myEditor.setSelection(expectedMatch.start, expectedMatch.end); + expect(/Foo:modules/i.test(myEditor.getSelectedText())).toBe(true); + }); + }); }); });