Skip to content

Commit 08d75b9

Browse files
authored
fix substitution with empty string (#189)
1 parent fb9dd58 commit 08d75b9

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/cm_adapter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ export class CodeMirror {
470470
type CM5Result = { from: Pos, to: Pos, match: string[] } | null;
471471
var last: CM6Result = null;
472472
var lastCM5Result: CM5Result = null;
473+
var afterEmptyMatch = false;
473474

474475
if (pos.ch == undefined) pos.ch = Number.MAX_VALUE;
475476
var firstOffset = indexFromPos(cm.cm6.state.doc, pos);
@@ -507,17 +508,18 @@ export class CodeMirror {
507508
find: function (back?: boolean): string[] | null | undefined {
508509
var doc = cm.cm6.state.doc
509510
if (back) {
510-
let endAt = last ? (last.from == last.to ? last.to - 1 : last.from) : firstOffset
511+
let endAt = last ? (afterEmptyMatch ? last.to - 1 : last.from) : firstOffset
511512
last = prevMatchInRange(0, endAt);
512513
} else {
513-
let startFrom = last ? (last.from == last.to ? last.to + 1 : last.to) : firstOffset
514+
let startFrom = last ? (afterEmptyMatch ? last.to + 1 : last.to) : firstOffset
514515
last = nextMatch(startFrom)
515516
}
516517
lastCM5Result = last && {
517518
from: posFromIndex(doc, last.from),
518519
to: posFromIndex(doc, last.to),
519520
match: last.match,
520521
}
522+
afterEmptyMatch = last ? last.from == last.to : false;
521523
return last && last.match
522524
},
523525
from: function () { return lastCM5Result?.from },

test/vim_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4490,6 +4490,10 @@ testVim('ex_substitute_javascript', function(cm, vim, helpers) {
44904490
// into the replace part. All should be literal (this is VIM).
44914491
helpers.doEx('s/\\(\\d+\\)/$$ $\' $` $& \\1/g')
44924492
eq('a $$ $\' $` $& 0 b', cm.getValue());
4493+
4494+
cm.setValue('W12345678OR12345D');
4495+
helpers.doEx('s/\\d//g');
4496+
eq('WORD', cm.getValue());
44934497
}, { value: 'a 0 b' });
44944498
testVim('ex_substitute_empty_arguments', function(cm,vim,helpers) {
44954499
cm.setCursor(0, 0);

0 commit comments

Comments
 (0)