@@ -6,6 +6,7 @@ type ReplaceLine = {
66type SearchBlock = {
77 search_lines : string [ ]
88 replace_lines : ReplaceLine [ ]
9+ after_search_lines : string [ ]
910 search_block_start_index : number
1011 actual_original_line_count : number
1112 search_to_original_map : Map < number , number >
@@ -102,6 +103,7 @@ export const apply_diff = (params: {
102103 const search_replace_blocks : SearchBlock [ ] = [ ]
103104 let search_chunks : string [ ] = [ ]
104105 let replace_chunks : ReplaceLine [ ] = [ ]
106+ let after_search_chunks : string [ ] = [ ]
105107
106108 let inside_replace_block = false
107109 let current_block_has_changes = false
@@ -112,6 +114,7 @@ export const apply_diff = (params: {
112114 search_replace_blocks . push ( {
113115 search_lines : search_chunks ,
114116 replace_lines : replace_chunks ,
117+ after_search_lines : after_search_chunks ,
115118 search_block_start_index : - 1 ,
116119 actual_original_line_count : 0 ,
117120 search_to_original_map : new Map ( )
@@ -120,6 +123,7 @@ export const apply_diff = (params: {
120123 }
121124 search_chunks = [ ]
122125 replace_chunks = [ ]
126+ after_search_chunks = [ ]
123127 inside_replace_block = false
124128 current_block_has_changes = false
125129 }
@@ -150,6 +154,14 @@ export const apply_diff = (params: {
150154 search_chunks . push ( line . replace ( / ^ - / , '' ) . replace ( / ^ ~ / , '' ) )
151155 }
152156
157+ if ( ! line . startsWith ( '-' ) ) {
158+ if ( line . startsWith ( '~nnn' ) ) {
159+ after_search_chunks . push ( '~nnn' )
160+ } else {
161+ after_search_chunks . push ( line . replace ( / ^ ~ / , '' ) )
162+ }
163+ }
164+
153165 if ( line . startsWith ( '~nnn' ) ) {
154166 replace_chunks . push ( {
155167 content : line_original . replace ( / ^ ~ n n n / , '' ) + '\n' ,
@@ -174,11 +186,13 @@ export const apply_diff = (params: {
174186 content : line_original . replace ( / ^ \+ ~ n n n / , '' ) + '\n' ,
175187 search_index : null
176188 } )
189+ after_search_chunks . push ( '~nnn' )
177190 } else {
178191 replace_chunks . push ( {
179192 content : line_original . replace ( / ^ \+ / , '' ) + '\n' ,
180193 search_index : null
181194 } )
195+ after_search_chunks . push ( line . replace ( / ^ \+ / , '' ) )
182196 }
183197 }
184198 }
@@ -190,6 +204,12 @@ export const apply_diff = (params: {
190204 ) {
191205 search_chunks . pop ( )
192206 replace_chunks . pop ( )
207+ if (
208+ after_search_chunks . length > 0 &&
209+ after_search_chunks [ after_search_chunks . length - 1 ] == '~nnn'
210+ ) {
211+ after_search_chunks . pop ( )
212+ }
193213 }
194214 push_block ( )
195215 }
@@ -318,8 +338,59 @@ export const apply_diff = (params: {
318338 )
319339 }
320340
321- const indices = Array . from ( matched_info . map . values ( ) )
322- previous_found_index = Math . max ( ...indices ) + 1
341+ // Check if the patch is already applied by matching the after-side
342+ const has_additions = block . replace_lines . some (
343+ ( l ) => l . search_index === null
344+ )
345+ if ( has_additions && block . after_search_lines . length > 0 ) {
346+ const check_start = Math . max (
347+ 0 ,
348+ matched_info . start_index - block . after_search_lines . length
349+ )
350+ const check_end = Math . min (
351+ original_code_lines_normalized . length - 1 ,
352+ matched_info . start_index + matched_info . actual_count
353+ )
354+
355+ let already_applied = false
356+ for ( let j = check_start ; j <= check_end ; j ++ ) {
357+ let after_ptr = 0
358+ let orig_ptr = j
359+
360+ while (
361+ after_ptr < block . after_search_lines . length &&
362+ orig_ptr < original_code_lines_normalized . length
363+ ) {
364+ const a_val = block . after_search_lines [ after_ptr ]
365+ const o_val = original_code_lines_normalized [ orig_ptr ] . value
366+
367+ if ( a_val === o_val ) {
368+ after_ptr ++
369+ orig_ptr ++
370+ } else if ( o_val === '~nnn' ) {
371+ orig_ptr ++
372+ } else if ( a_val === '~nnn' ) {
373+ after_ptr ++
374+ } else {
375+ break
376+ }
377+ }
378+
379+ if ( after_ptr === block . after_search_lines . length ) {
380+ already_applied = true
381+ break
382+ }
383+ }
384+
385+ if ( already_applied ) {
386+ block . search_block_start_index = - 2
387+ }
388+ }
389+
390+ if ( block . search_block_start_index !== - 2 ) {
391+ const indices = Array . from ( matched_info . map . values ( ) )
392+ previous_found_index = Math . max ( ...indices ) + 1
393+ }
323394 } else {
324395 block . search_block_start_index = - 2
325396 }
0 commit comments