Skip to content

Commit f0452fe

Browse files
nightwingmarijnh
authored andcommitted
[vim] add support for alternate delimiters for the replace command
1 parent 251a322 commit f0452fe

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

keymap/vim.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,7 +3683,15 @@
36833683
}
36843684
}
36853685
function splitBySlash(argString) {
3686-
var slashes = findUnescapedSlashes(argString) || [];
3686+
return splitBySeparator(argString, '/');
3687+
}
3688+
3689+
function findUnescapedSlashes(argString) {
3690+
return findUnescapedSeparators(argString, '/');
3691+
}
3692+
3693+
function splitBySeparator(argString, separator) {
3694+
var slashes = findUnescapedSeparators(argString, separator) || [];
36873695
if (!slashes.length) return [];
36883696
var tokens = [];
36893697
// in case of strings like foo/bar
@@ -3695,12 +3703,15 @@
36953703
return tokens;
36963704
}
36973705

3698-
function findUnescapedSlashes(str) {
3706+
function findUnescapedSeparators(str, separator) {
3707+
if (!separator)
3708+
separator = '/';
3709+
36993710
var escapeNextChar = false;
37003711
var slashes = [];
37013712
for (var i = 0; i < str.length; i++) {
37023713
var c = str.charAt(i);
3703-
if (!escapeNextChar && c == '/') {
3714+
if (!escapeNextChar && c == separator) {
37043715
slashes.push(i);
37053716
}
37063717
escapeNextChar = !escapeNextChar && (c == '\\');
@@ -4566,7 +4577,7 @@
45664577
'any other getSearchCursor implementation.');
45674578
}
45684579
var argString = params.argString;
4569-
var tokens = argString ? splitBySlash(argString) : [];
4580+
var tokens = argString ? splitBySeparator(argString, argString[0]) : [];
45704581
var regexPart, replacePart = '', trailing, flagsPart, count;
45714582
var confirm = false; // Whether to confirm each replace.
45724583
var global = false; // True to replace all instances on a line, false to replace only 1.
@@ -4610,7 +4621,7 @@
46104621
global = true;
46114622
flagsPart.replace('g', '');
46124623
}
4613-
regexPart = regexPart + '/' + flagsPart;
4624+
regexPart = regexPart.replace(/\//g, "\\/") + '/' + flagsPart;
46144625
}
46154626
}
46164627
if (regexPart) {

test/vim_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3648,6 +3648,11 @@ testVim('ex_substitute_same_line', function(cm, vim, helpers) {
36483648
helpers.doEx('s/one/two/g');
36493649
eq('one one\n two two', cm.getValue());
36503650
}, { value: 'one one\n one one'});
3651+
testVim('ex_substitute_alternate_separator', function(cm, vim, helpers) {
3652+
cm.setCursor(1, 0);
3653+
helpers.doEx('s#o/e#two#g');
3654+
eq('o/e o/e\n two two', cm.getValue());
3655+
}, { value: 'o/e o/e\n o/e o/e'});
36513656
testVim('ex_substitute_full_file', function(cm, vim, helpers) {
36523657
cm.setCursor(1, 0);
36533658
helpers.doEx('%s/one/two/g');

0 commit comments

Comments
 (0)