From 8db75266d0da48bfd1257d528373549dc0814584 Mon Sep 17 00:00:00 2001 From: cvzakharchenko Date: Mon, 11 Aug 2025 13:55:45 +0200 Subject: [PATCH] Issue 1676: Don't eat up the last newline in a multi-line replacement (#1777) --- packages/opencode/src/tool/edit.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index d3883b53..1b31b0aa 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -188,7 +188,10 @@ export const LineTrimmedReplacer: Replacer = function* (content, find) { let matchEndIndex = matchStartIndex for (let k = 0; k < searchLines.length; k++) { - matchEndIndex += originalLines[i + k].length + 1 + matchEndIndex += originalLines[i + k].length + if (k < searchLines.length - 1) { + matchEndIndex += 1 // Add newline character except for the last line + } } yield content.substring(matchStartIndex, matchEndIndex)