@@ -7,10 +7,10 @@ import { create_safe_path, sanitize_file_name } from '@/utils/path-sanitizer'
77import { FileItem } from '../utils/clipboard-parser'
88import { OriginalFileState } from '../types/original-file-state'
99import { remove_directory_if_empty } from '../utils/file-operations'
10-
11- type Segment =
12- | { type : 'common' ; lines : string [ ] }
13- | { type : ' conflict' ; original_lines : string [ ] ; updated_lines : string [ ] }
10+ import {
11+ apply_conflict_markers_to_content ,
12+ parse_conflict_segments
13+ } from '../utils/ conflict-marker-parser'
1414
1515export const handle_conflict_markers = async (
1616 files : FileItem [ ]
@@ -265,145 +265,3 @@ export const handle_conflict_markers = async (
265265 return { success : false }
266266 }
267267}
268-
269- const parse_conflict_segments = ( content : string ) : Segment [ ] => {
270- const lines = content . split ( '\n' )
271- const segments : Segment [ ] = [ ]
272- let current_lines : string [ ] = [ ]
273-
274- let state : 'NORMAL' | 'ORIGINAL' | 'UPDATED' = 'NORMAL'
275-
276- let original_buffer : string [ ] = [ ]
277- let updated_buffer : string [ ] = [ ]
278-
279- for ( const line of lines ) {
280- if ( line . trim ( ) . startsWith ( '<<<<<<<' ) ) {
281- if ( state == 'NORMAL' ) {
282- if ( current_lines . length > 0 ) {
283- segments . push ( { type : 'common' , lines : [ ...current_lines ] } )
284- current_lines = [ ]
285- }
286- state = 'ORIGINAL'
287- original_buffer = [ ]
288- updated_buffer = [ ]
289- } else {
290- if ( state == 'ORIGINAL' ) original_buffer . push ( line )
291- if ( state == 'UPDATED' ) updated_buffer . push ( line )
292- }
293- continue
294- }
295-
296- if ( line . trim ( ) . startsWith ( '=======' ) ) {
297- if ( state == 'ORIGINAL' ) {
298- state = 'UPDATED'
299- } else if ( state == 'NORMAL' ) {
300- current_lines . push ( line )
301- } else {
302- updated_buffer . push ( line )
303- }
304- continue
305- }
306-
307- if ( line . trim ( ) . startsWith ( '>>>>>>>' ) ) {
308- if ( state == 'UPDATED' ) {
309- segments . push ( {
310- type : 'conflict' ,
311- original_lines : original_buffer ,
312- updated_lines : updated_buffer
313- } )
314- state = 'NORMAL'
315- original_buffer = [ ]
316- updated_buffer = [ ]
317- } else if ( state == 'ORIGINAL' ) {
318- segments . push ( {
319- type : 'conflict' ,
320- original_lines : original_buffer ,
321- updated_lines : [ ]
322- } )
323- state = 'NORMAL'
324- original_buffer = [ ]
325- updated_buffer = [ ]
326- } else {
327- current_lines . push ( line )
328- }
329- continue
330- }
331-
332- if ( state == 'NORMAL' ) {
333- current_lines . push ( line )
334- } else if ( state == 'ORIGINAL' ) {
335- original_buffer . push ( line )
336- } else if ( state == 'UPDATED' ) {
337- updated_buffer . push ( line )
338- }
339- }
340-
341- if ( state == 'NORMAL' && current_lines . length > 0 ) {
342- segments . push ( { type : 'common' , lines : current_lines } )
343- }
344-
345- return segments
346- }
347-
348- const apply_conflict_markers_to_content = (
349- original_content : string ,
350- markers_content : string
351- ) : string => {
352- let current_content = original_content
353- const segments = parse_conflict_segments ( markers_content )
354- let cursor = 0
355-
356- for ( let i = 0 ; i < segments . length ; i ++ ) {
357- const segment = segments [ i ]
358- if ( segment . type == 'conflict' ) {
359- const prev = i > 0 ? segments [ i - 1 ] : undefined
360- const next = i < segments . length - 1 ? segments [ i + 1 ] : undefined
361-
362- const context_before = prev && prev . type == 'common' ? prev . lines : [ ]
363- const context_after = next && next . type == 'common' ? next . lines : [ ]
364-
365- const search_lines = [
366- ...context_before ,
367- ...segment . original_lines ,
368- ...context_after
369- ]
370- const search_text = search_lines . join ( '\n' )
371-
372- const replace_lines = [
373- ...context_before ,
374- ...segment . updated_lines ,
375- ...context_after
376- ]
377- const replace_text = replace_lines . join ( '\n' )
378-
379- const index = current_content . indexOf ( search_text , cursor )
380-
381- if ( index == - 1 ) {
382- throw new Error (
383- `Could not find content to replace for conflict marker. Context:\n${ search_text . slice ( 0 , 100 ) } ...`
384- )
385- }
386-
387- current_content =
388- current_content . slice ( 0 , index ) +
389- replace_text +
390- current_content . slice ( index + search_text . length )
391-
392- // Advance cursor to skip the replaced part, but ensure we are positioned
393- // to match the 'context_after' which serves as 'context_before' for the next segment.
394- const unique_replaced_lines = [
395- ...context_before ,
396- ...segment . updated_lines
397- ]
398- const unique_replaced_text = unique_replaced_lines . join ( '\n' )
399-
400- let advance = unique_replaced_text . length
401- if ( context_after . length > 0 && unique_replaced_lines . length > 0 ) {
402- advance += 1 // Account for newline joining unique part and context_after
403- }
404-
405- cursor = index + advance
406- }
407- }
408- return current_content
409- }
0 commit comments