Skip to content

Commit c58a7b7

Browse files
benhormannmarijnh
authored andcommitted
[vim] fix global command when line numbers change
1 parent d733766 commit c58a7b7

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

keymap/vim.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5051,28 +5051,33 @@
50515051
// now that we have the regexPart, search for regex matches in the
50525052
// specified range of lines
50535053
var query = getSearchState(cm).getQuery();
5054-
var matchedLines = [], content = '';
5054+
var matchedLines = [];
50555055
for (var i = lineStart; i <= lineEnd; i++) {
5056-
var matched = query.test(cm.getLine(i));
5056+
var line = cm.getLineHandle(i);
5057+
var matched = query.test(line.text);
50575058
if (matched) {
5058-
matchedLines.push(i+1);
5059-
content+= cm.getLine(i) + '\n';
5059+
matchedLines.push(cmd ? line : line.text);
50605060
}
50615061
}
50625062
// if there is no [cmd], just display the list of matched lines
50635063
if (!cmd) {
5064-
showConfirm(cm, content);
5064+
showConfirm(cm, matchedLines.join('\n'));
50655065
return;
50665066
}
50675067
var index = 0;
50685068
var nextCommand = function() {
50695069
if (index < matchedLines.length) {
5070-
var command = matchedLines[index] + cmd;
5070+
var line = matchedLines[index++];
5071+
var lineNum = cm.getLineNumber(line);
5072+
if (lineNum == null) {
5073+
nextCommand();
5074+
return;
5075+
}
5076+
var command = (lineNum + 1) + cmd;
50715077
exCommandDispatcher.processCommand(cm, command, {
50725078
callback: nextCommand
50735079
});
50745080
}
5075-
index++;
50765081
};
50775082
nextCommand();
50785083
},

test/vim_test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,10 +4070,22 @@ testVim('ex_sort_pattern_alpha_num', function(cm, vim, helpers) {
40704070
testVim('ex_global', function(cm, vim, helpers) {
40714071
cm.setCursor(0, 0);
40724072
helpers.doEx('g/one/s//two');
4073-
eq('two two\n two two\n two two', cm.getValue());
4073+
eq('two one\n two one\n two one', cm.getValue());
40744074
helpers.doEx('1,2g/two/s//one');
4075-
eq('one one\n one one\n two two', cm.getValue());
4075+
eq('one one\n one one\n two one', cm.getValue());
4076+
cm.openNotification = helpers.fakeOpenNotification(function(text) {
4077+
eq(' one one\n two one', text);
4078+
});
4079+
helpers.doEx('g/^ /');
40764080
}, {value: 'one one\n one one\n one one'});
4081+
testVim('ex_global_substitute_join', function(cm, vim, helpers) {
4082+
helpers.doEx('g/o/s/\\n/;');
4083+
eq('one;two\nthree\nfour;five\n', cm.getValue());
4084+
}, {value: 'one\ntwo\nthree\nfour\nfive\n'});
4085+
testVim('ex_global_substitute_split', function(cm, vim, helpers) {
4086+
helpers.doEx('g/e/s/[or]/\\n');
4087+
eq('\nne\ntwo\nth\nee\nfour\nfive\n', cm.getValue());
4088+
}, {value: 'one\ntwo\nthree\nfour\nfive\n'});
40774089
testVim('ex_global_confirm', function(cm, vim, helpers) {
40784090
cm.setCursor(0, 0);
40794091
var onKeyDown;

0 commit comments

Comments
 (0)