Skip to content

Commit

Permalink
fix substitution with empty string (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing authored Nov 6, 2024
1 parent fb9dd58 commit 08d75b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/cm_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ export class CodeMirror {
type CM5Result = { from: Pos, to: Pos, match: string[] } | null;
var last: CM6Result = null;
var lastCM5Result: CM5Result = null;
var afterEmptyMatch = false;

if (pos.ch == undefined) pos.ch = Number.MAX_VALUE;
var firstOffset = indexFromPos(cm.cm6.state.doc, pos);
Expand Down Expand Up @@ -507,17 +508,18 @@ export class CodeMirror {
find: function (back?: boolean): string[] | null | undefined {
var doc = cm.cm6.state.doc
if (back) {
let endAt = last ? (last.from == last.to ? last.to - 1 : last.from) : firstOffset
let endAt = last ? (afterEmptyMatch ? last.to - 1 : last.from) : firstOffset
last = prevMatchInRange(0, endAt);
} else {
let startFrom = last ? (last.from == last.to ? last.to + 1 : last.to) : firstOffset
let startFrom = last ? (afterEmptyMatch ? last.to + 1 : last.to) : firstOffset
last = nextMatch(startFrom)
}
lastCM5Result = last && {
from: posFromIndex(doc, last.from),
to: posFromIndex(doc, last.to),
match: last.match,
}
afterEmptyMatch = last ? last.from == last.to : false;
return last && last.match
},
from: function () { return lastCM5Result?.from },
Expand Down
4 changes: 4 additions & 0 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4490,6 +4490,10 @@ testVim('ex_substitute_javascript', function(cm, vim, helpers) {
// into the replace part. All should be literal (this is VIM).
helpers.doEx('s/\\(\\d+\\)/$$ $\' $` $& \\1/g')
eq('a $$ $\' $` $& 0 b', cm.getValue());

cm.setValue('W12345678OR12345D');
helpers.doEx('s/\\d//g');
eq('WORD', cm.getValue());
}, { value: 'a 0 b' });
testVim('ex_substitute_empty_arguments', function(cm,vim,helpers) {
cm.setCursor(0, 0);
Expand Down

0 comments on commit 08d75b9

Please sign in to comment.