@@ -3,6 +3,10 @@ import { createPatch } from 'diff'
33
44import { tryToDoStringReplacementWithExtraIndentation } from './generate-diffs-prompt'
55
6+ function normalizeLineEndings ( str : string ) : string {
7+ return str . replace ( / \r \n / g, '\n' )
8+ }
9+
610export async function processStrReplace (
711 path : string ,
812 replacements : { old : string ; new : string ; allowMultiple : boolean } [ ] ,
@@ -29,7 +33,6 @@ export async function processStrReplace(
2933
3034 // Process each old/new string pair
3135 let currentContent = initialContent
32- let allPatches : string [ ] = [ ]
3336 let messages : string [ ] = [ ]
3437 const lineEnding = currentContent . includes ( '\r\n' ) ? '\r\n' : '\n'
3538
@@ -42,14 +45,14 @@ export async function processStrReplace(
4245 continue
4346 }
4447
45- const normalizeLineEndings = ( str : string ) => str . replace ( / \r \n / g, '\n' )
4648 const normalizedCurrentContent = normalizeLineEndings ( currentContent )
4749 const normalizedOldStr = normalizeLineEndings ( oldStr )
50+ const normalizedNewStr = normalizeLineEndings ( newStr )
4851
4952 const match = tryMatchOldStr (
5053 normalizedCurrentContent ,
5154 normalizedOldStr ,
52- newStr ,
55+ normalizedNewStr ,
5356 allowMultiple ,
5457 )
5558 let updatedOldStr : string | null
@@ -61,15 +64,14 @@ export async function processStrReplace(
6164 updatedOldStr = null
6265 }
6366
64- const updatedContent =
67+ currentContent =
6568 updatedOldStr === null
6669 ? normalizedCurrentContent
67- : normalizedCurrentContent . replaceAll ( updatedOldStr , newStr )
68-
69- // Update current content for next iteration
70- currentContent = updatedContent . replaceAll ( '\n' , lineEnding )
70+ : normalizedCurrentContent . replaceAll ( updatedOldStr , normalizedNewStr )
7171 }
7272
73+ currentContent = currentContent . replaceAll ( '\n' , lineEnding )
74+
7375 if ( initialContent === currentContent ) {
7476 logger . debug (
7577 {
@@ -91,8 +93,6 @@ export async function processStrReplace(
9193 const hunkStartIndex = lines . findIndex ( ( line ) => line . startsWith ( '@@' ) )
9294 if ( hunkStartIndex !== - 1 ) {
9395 patch = lines . slice ( hunkStartIndex ) . join ( '\n' )
94- patch = patch . replaceAll ( '\n' , lineEnding )
95- allPatches . push ( patch )
9696 }
9797 const finalPatch = patch
9898
0 commit comments