fix(editor): preserve literal replacement text

This commit is contained in:
Tianyi Cui
2026-07-29 23:48:43 +08:00
parent 525b3aa6c9
commit fbfe520471
2 changed files with 18 additions and 2 deletions
@@ -308,7 +308,8 @@ async function replaceInFile(
}
const before = await ctx.fs.readText(target, exec.signal)
const offsets = matchOffsets(before, oldValue)
if (offsets.length === 0) {
const offset = offsets[0]
if (offset === undefined) {
throw new FsError(
`No replacement was performed, old_str \`${oldValue}\` did not appear verbatim in ${target.displayPath}.`,
'FS_EDIT_NOT_FOUND',
@@ -325,7 +326,7 @@ async function replaceInFile(
try {
outcome = await ctx.fs.writeText(
target,
before.replace(oldValue, newValue),
before.slice(0, offset) + newValue + before.slice(offset + oldValue.length),
intent === undefined
? { kind: 'replaceIfVersion', version: info.version }
: { kind: 'replaceIfVersion', version: intent.version },