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

Replace $& with the whole match #5929

Merged
merged 6 commits into from
Jan 30, 2014
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
17 changes: 11 additions & 6 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,21 @@ define(function (require, exports, module) {
}
}

// NOTE: we can't just use the ordinary replace() function here because the string has been
// extracted from the original text and so might be missing some context that the regexp matched.
function parseDollars(replaceWith, match) {
replaceWith = replaceWith.replace(/(\$+)(\d{1,2})/g, function (whole, dollars, index) {
replaceWith = replaceWith.replace(/(\$+)(\d{1,2}|&)/g, function (whole, dollars, index) {
var parsedIndex = parseInt(index, 10);
if (dollars.length % 2 === 1 && parsedIndex !== 0) {
return dollars.substr(1) + (match[parsedIndex] || "");
} else {
return whole;
if (dollars.length % 2 === 1) { // check if dollar signs escape themselves (for example $$1, $$$$&)
if (index === "&") { // handle $&
return dollars.substr(1) + (match[0] || "");
} else if (parsedIndex !== 0) { // handle $n or $nn, don't handle $0 or $00
return dollars.substr(1) + (match[parsedIndex] || "");
}
}
return whole;
});
replaceWith = replaceWith.replace(/\$\$/g, "$");
replaceWith = replaceWith.replace(/\$\$/g, "$"); // replace escaped dollar signs (for example $$) with single ones
return replaceWith;
}

Expand Down
53 changes: 50 additions & 3 deletions test/spec/FindReplace-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,26 @@ define(function (require, exports, module) {
expect(/Foo\$modules/i.test(myEditor.getSelectedText())).toBe(true);
});
});

it("should find a regexp and replace it with $& (whole match)", function () {
runs(function () {
twCommandManager.execute(Commands.EDIT_REPLACE);
toggleRegexp(true);
enterSearchText("(modules)\\/(\\w+)");
enterReplaceText("_$&-$2$$&");

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(":enabled")).toBe(true);
tw$("#replace-yes").click();

myEditor.setSelection({line: LINE_FIRST_REQUIRE, ch: 23}, {line: LINE_FIRST_REQUIRE, ch: 41});
expect(/_modules\/Foo-Foo\$&/i.test(myEditor.getSelectedText())).toBe(true);
});
});
});


Expand Down Expand Up @@ -987,7 +1007,7 @@ define(function (require, exports, module) {
});
});

it("should find a regexp and replace it with $nn (n has two digits)", function () {
it("should find all regexps and replace them with $nn (n has two digits)", function () {
runs(function () {
twCommandManager.execute(Commands.EDIT_REPLACE);
toggleRegexp(true);
Expand All @@ -1014,7 +1034,7 @@ define(function (require, exports, module) {
});
});

it("should find a regexp and replace it with $$n (not a subexpression, escaped dollar)", function () {
it("should find all regexps and replace them with $$n (not a subexpression, escaped dollar)", function () {
runs(function () {
twCommandManager.execute(Commands.EDIT_REPLACE);
toggleRegexp(true);
Expand All @@ -1041,7 +1061,7 @@ define(function (require, exports, module) {
});
});

it("should find a regexp and replace it with $$$n (correct subexpression)", function () {
it("should find all regexps and replace them with $$$n (correct subexpression)", function () {
runs(function () {
twCommandManager.execute(Commands.EDIT_REPLACE);
toggleRegexp(true);
Expand All @@ -1067,6 +1087,33 @@ define(function (require, exports, module) {
expect(/Baz\$modules/i.test(myEditor.getSelectedText())).toBe(true);
});
});

it("should find all regexps and replace them with $& (whole match)", function () {
runs(function () {
twCommandManager.execute(Commands.EDIT_REPLACE);
toggleRegexp(true);
enterSearchText("(modules)\\/(\\w+)");
enterReplaceText("_$&-$2$$&");

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-all").is(":enabled")).toBe(true);
tw$("#replace-all").click();
tw$(".replace-checked").click();

myEditor.setSelection({line: LINE_FIRST_REQUIRE, ch: 23}, {line: LINE_FIRST_REQUIRE, ch: 41});
expect(/_modules\/Foo-Foo\$&/i.test(myEditor.getSelectedText())).toBe(true);

myEditor.setSelection({line: LINE_FIRST_REQUIRE + 1, ch: 23}, {line: LINE_FIRST_REQUIRE + 1, ch: 41});
expect(/_modules\/Bar-Bar\$&/i.test(myEditor.getSelectedText())).toBe(true);

myEditor.setSelection({line: LINE_FIRST_REQUIRE + 2, ch: 23}, {line: LINE_FIRST_REQUIRE + 2, ch: 41});
expect(/_modules\/Baz-Baz\$&/i.test(myEditor.getSelectedText())).toBe(true);
});
});
});
});

Expand Down