@@ -62,11 +62,70 @@ const find_sequence = (lines: IndexedLine[], pattern: string[]): number => {
6262 return - 1
6363}
6464
65+ const compute_lcs = ( a : string [ ] , b : string [ ] ) : [ number , number ] [ ] => {
66+ const m = a . length
67+ const n = b . length
68+ const dp : number [ ] [ ] = Array . from ( { length : m + 1 } , ( ) =>
69+ Array ( n + 1 ) . fill ( 0 )
70+ )
71+
72+ for ( let i = 1 ; i <= m ; i ++ ) {
73+ for ( let j = 1 ; j <= n ; j ++ ) {
74+ if ( a [ i - 1 ] === b [ j - 1 ] ) {
75+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1
76+ } else {
77+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] )
78+ }
79+ }
80+ }
81+
82+ const lcs : [ number , number ] [ ] = [ ]
83+ let i = m ,
84+ j = n
85+ while ( i > 0 && j > 0 ) {
86+ if ( a [ i - 1 ] === b [ j - 1 ] ) {
87+ lcs . push ( [ i - 1 , j - 1 ] )
88+ i --
89+ j --
90+ } else if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j - 1 ] ) {
91+ i --
92+ } else {
93+ j --
94+ }
95+ }
96+ return lcs . reverse ( )
97+ }
98+
99+ type UpdStructure = {
100+ gaps : string [ ] [ ]
101+ non_blanks : { text : string ; normalized : string } [ ]
102+ }
103+
104+ const get_non_blank_structure = (
105+ lines : string [ ] ,
106+ normalize : Normalizer
107+ ) : UpdStructure => {
108+ const gaps : string [ ] [ ] = [ [ ] ]
109+ const non_blanks : { text : string ; normalized : string } [ ] = [ ]
110+
111+ for ( const line of lines ) {
112+ const norm = normalize ( line )
113+ if ( norm === '' ) {
114+ gaps [ gaps . length - 1 ] . push ( line )
115+ } else {
116+ non_blanks . push ( { text : line , normalized : norm } )
117+ gaps . push ( [ ] )
118+ }
119+ }
120+ return { gaps, non_blanks }
121+ }
122+
65123type BlockMatch = {
66124 match_start : number
67125 match_end : number
68- matched_before : string
69- matched_after : string
126+ original_start : number
127+ original_end : number
128+ original_indexed_lines : IndexedLine [ ]
70129}
71130
72131const find_block = ( params : {
@@ -89,8 +148,9 @@ const find_block = (params: {
89148 return {
90149 match_start : params . from ,
91150 match_end : params . from ,
92- matched_before : '' ,
93- matched_after : ''
151+ original_start : params . from ,
152+ original_end : params . from ,
153+ original_indexed_lines : [ ]
94154 }
95155 }
96156
@@ -105,23 +165,38 @@ const find_block = (params: {
105165 const match_start = lines [ i ] . start
106166 const match_end = lines [ i + pattern . length - 1 ] . content_end
107167
108- const matched_before =
109- before . length > 0
110- ? params . content . slice (
111- match_start ,
112- lines [ i + before . length - 1 ] . content_end
113- )
114- : ''
115-
116- const matched_after =
117- after . length > 0
118- ? params . content . slice (
119- lines [ i + before . length + original . length ] . start ,
120- match_end
121- )
122- : ''
123-
124- return { match_start, match_end, matched_before, matched_after }
168+ const original_indexed_lines = lines . slice (
169+ i + before . length ,
170+ i + before . length + original . length
171+ )
172+
173+ let original_start : number
174+ let original_end : number
175+
176+ if ( original . length > 0 ) {
177+ if ( before . length > 0 ) {
178+ original_start = lines [ i + before . length - 1 ] . content_end
179+ } else {
180+ original_start = original_indexed_lines [ 0 ] . start
181+ }
182+ original_end =
183+ original_indexed_lines [ original_indexed_lines . length - 1 ] . content_end
184+ } else {
185+ if ( before . length > 0 ) {
186+ original_start = lines [ i + before . length - 1 ] . content_end
187+ } else {
188+ original_start = match_start
189+ }
190+ original_end = original_start
191+ }
192+
193+ return {
194+ match_start,
195+ match_end,
196+ original_start,
197+ original_end,
198+ original_indexed_lines
199+ }
125200}
126201
127202export const apply_search_replace_to_content = ( params : {
@@ -178,35 +253,107 @@ export const apply_search_replace_to_content = (params: {
178253 )
179254 }
180255
181- const match_index = match . match_start
182- const matched_length = match . match_end - match . match_start
183- const matched_before = match . matched_before
184- const matched_after = match . matched_after
185-
186- const updated_text = segment . updated_lines
187- . map ( ( line ) => strip_invisible ( line ) )
188- . join ( line_ending )
189-
190- const replacement_parts : string [ ] = [ ]
191- if ( matched_before ) replacement_parts . push ( matched_before )
192- if ( segment . updated_lines . length > 0 ) {
193- replacement_parts . push ( updated_text )
256+ const upd_structure = get_non_blank_structure (
257+ segment . updated_lines ,
258+ normalize_loose
259+ )
260+ const orig_normalized = match . original_indexed_lines . map ( ( l ) =>
261+ normalize_loose ( current_content . slice ( l . start , l . content_end ) )
262+ )
263+ const lcs_mapping = compute_lcs (
264+ orig_normalized ,
265+ upd_structure . non_blanks . map ( ( l ) => l . normalized )
266+ )
267+
268+ let replacement_text = ''
269+ let last_matched_orig_idx = - 1
270+
271+ for ( let j = 0 ; j < upd_structure . non_blanks . length ; j ++ ) {
272+ const mapping = lcs_mapping . find ( ( m ) => m [ 1 ] === j )
273+
274+ if ( mapping ) {
275+ const orig_idx = mapping [ 0 ]
276+
277+ let gap_start : number
278+ if ( orig_idx === 0 ) {
279+ gap_start = match . original_start
280+ } else {
281+ gap_start = match . original_indexed_lines [ orig_idx - 1 ] . content_end
282+ }
283+ const gap_end = match . original_indexed_lines [ orig_idx ] . start
284+
285+ replacement_text += current_content . slice ( gap_start , gap_end )
286+ replacement_text += upd_structure . non_blanks [ j ] . text
287+
288+ last_matched_orig_idx = orig_idx
289+ } else {
290+ let next_matched_orig_idx = match . original_indexed_lines . length
291+ for ( let k = j + 1 ; k < upd_structure . non_blanks . length ; k ++ ) {
292+ const m = lcs_mapping . find ( ( m ) => m [ 1 ] === k )
293+ if ( m ) {
294+ next_matched_orig_idx = m [ 0 ]
295+ break
296+ }
297+ }
298+
299+ let gap_to_use : string | null = null
300+ if ( last_matched_orig_idx + 1 < next_matched_orig_idx ) {
301+ const deleted_idx = last_matched_orig_idx + 1
302+ let g_start : number
303+ if ( deleted_idx === 0 ) {
304+ g_start = match . original_start
305+ } else {
306+ g_start = match . original_indexed_lines [ deleted_idx - 1 ] . content_end
307+ }
308+ const g_end = match . original_indexed_lines [ deleted_idx ] . start
309+ gap_to_use = current_content . slice ( g_start , g_end )
310+ last_matched_orig_idx = deleted_idx
311+ }
312+
313+ if ( gap_to_use !== null ) {
314+ replacement_text += gap_to_use
315+ } else {
316+ if ( replacement_text . length > 0 && ! replacement_text . endsWith ( '\n' ) ) {
317+ replacement_text += line_ending
318+ }
319+ const upd_gap = upd_structure . gaps [ j ]
320+ if ( upd_gap . length > 0 ) {
321+ replacement_text +=
322+ upd_gap . map ( strip_invisible ) . join ( line_ending ) + line_ending
323+ }
324+ }
325+
326+ replacement_text += upd_structure . non_blanks [ j ] . text
327+ }
194328 }
195- if ( matched_after ) replacement_parts . push ( matched_after )
196329
197- const replacement_text = replacement_parts . join ( line_ending )
330+ let final_gap_start : number
331+ if ( match . original_indexed_lines . length > 0 ) {
332+ final_gap_start =
333+ match . original_indexed_lines [ match . original_indexed_lines . length - 1 ]
334+ . content_end
335+ } else {
336+ final_gap_start = match . original_start
337+ }
338+ replacement_text += current_content . slice (
339+ final_gap_start ,
340+ match . original_end
341+ )
342+
343+ const last_upd_gap = upd_structure . gaps [ upd_structure . gaps . length - 1 ]
344+ if ( last_upd_gap . length > 0 ) {
345+ if ( replacement_text . length > 0 && ! replacement_text . endsWith ( '\n' ) ) {
346+ replacement_text += line_ending
347+ }
348+ replacement_text += last_upd_gap . map ( strip_invisible ) . join ( line_ending )
349+ }
198350
199351 current_content =
200- current_content . slice ( 0 , match_index ) +
352+ current_content . slice ( 0 , match . original_start ) +
201353 replacement_text +
202- current_content . slice ( match_index + matched_length )
203-
204- const content_before_after_context = matched_before
205- ? matched_before + line_ending + updated_text
206- : updated_text
207- cursor = matched_after
208- ? match_index + content_before_after_context . length
209- : match_index + replacement_text . length
354+ current_content . slice ( match . original_end )
355+
356+ cursor = match . original_start + replacement_text . length
210357 }
211358
212359 return current_content
0 commit comments