Skip to content

Commit 1770901

Browse files
benhormannmarijnh
authored andcommitted
[vim] fix substitute command when splitting or joining lines
1 parent 9d0f9d1 commit 1770901

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

keymap/vim.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4527,7 +4527,7 @@
45274527
if (start instanceof Array) {
45284528
return inArray(pos, start);
45294529
} else {
4530-
if (end) {
4530+
if (typeof end == 'number') {
45314531
return (pos >= start && pos <= end);
45324532
} else {
45334533
return pos == start;
@@ -5268,7 +5268,7 @@
52685268
// Set up all the functions.
52695269
cm.state.vim.exMode = true;
52705270
var done = false;
5271-
var lastPos = searchCursor.from();
5271+
var lastPos, modifiedLineNumber;
52725272
function replaceAll() {
52735273
cm.operation(function() {
52745274
while (!done) {
@@ -5281,14 +5281,17 @@
52815281
function replace() {
52825282
var text = cm.getRange(searchCursor.from(), searchCursor.to());
52835283
var newText = text.replace(query, replaceWith);
5284+
var unmodifiedLineNumber = searchCursor.to().line;
52845285
searchCursor.replace(newText);
5286+
modifiedLineNumber = searchCursor.to().line;
5287+
lineEnd += modifiedLineNumber - unmodifiedLineNumber;
52855288
}
52865289
function next() {
52875290
// The below only loops to skip over multiple occurrences on the same
52885291
// line when 'global' is not true.
52895292
while(searchCursor.findNext() &&
52905293
isInRange(searchCursor.from(), lineStart, lineEnd)) {
5291-
if (!global && lastPos && searchCursor.from().line == lastPos.line) {
5294+
if (!global && searchCursor.from().line == modifiedLineNumber) {
52925295
continue;
52935296
}
52945297
cm.scrollIntoView(searchCursor.from(), 30);

test/vim_test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4307,10 +4307,22 @@ testSubstitute('ex_substitute_newline_match', {
43074307
value: 'one,two \n three,four',
43084308
expectedValue: 'one,two , three,four',
43094309
expr: '%s/\\n/,/g'});
4310+
testSubstitute('ex_substitute_newline_join_global', {
4311+
value: 'one,two \n three,four \n five \n six',
4312+
expectedValue: 'one,two \n three,four , five \n six',
4313+
expr: '2s/\\n/,/g'});
43104314
testSubstitute('ex_substitute_newline_replacement', {
4311-
value: 'one,two \n three,four',
4312-
expectedValue: 'one\ntwo \n three\nfour',
4315+
value: 'one,two, \n three,four,',
4316+
expectedValue: 'one\ntwo\n \n three\nfour\n',
43134317
expr: '%s/,/\\n/g'});
4318+
testSubstitute('ex_substitute_newline_multiple_splits', {
4319+
value: 'one,two, \n three,four,five,six, \n seven,',
4320+
expectedValue: 'one,two, \n three\nfour\nfive\nsix\n \n seven,',
4321+
expr: '2s/,/\\n/g'});
4322+
testSubstitute('ex_substitute_newline_first_occurrences', {
4323+
value: 'one,two, \n three,four,five,six, \n seven,',
4324+
expectedValue: 'one\ntwo, \n three\nfour,five,six, \n seven\n',
4325+
expr: '%s/,/\\n/'});
43144326
testSubstitute('ex_substitute_braces_word', {
43154327
value: 'ababab abb ab{2}',
43164328
expectedValue: 'ab abb ab{2}',

0 commit comments

Comments
 (0)