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

Handle regexp replacements with $1, $2, etc. properly #5618

Merged
merged 6 commits into from
Oct 22, 2013
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
96 changes: 39 additions & 57 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -508,7 +508,7 @@ define(function (require, exports, module) {
'</button> <button id="replace-all" class="btn">' + Strings.BUTTON_REPLACE_ALL +
'</button> <button id="replace-stop" class="btn">' + Strings.BUTTON_STOP + '</button>';

function replace(editor, all) {
function replace(editor) {
var cm = editor._codeMirror;
createModalBar(replaceQueryDialog, true);
$(modalBar).on("commit", function (e, query) {
Expand All @@ -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();
});
});

Expand Down Expand Up @@ -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);
});
});
31 changes: 31 additions & 0 deletions test/spec/FindReplace-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

Expand Down