@@ -640,7 +640,6 @@ const extract_all_code_block_patches = (params: {
640640 }
641641 code_blocks . push ( ...xml_blocks )
642642 code_blocks . sort ( ( a , b ) => a . start - b . start )
643- const files_with_diffs = new Set < string > ( )
644643 const diff_block_patches = new Map < number , Diff [ ] > ( )
645644
646645 for ( let i = 0 ; i < code_blocks . length ; i ++ ) {
@@ -692,10 +691,6 @@ const extract_all_code_block_patches = (params: {
692691 } )
693692 if ( parsed_patches . length > 0 ) {
694693 diff_block_patches . set ( block . start , parsed_patches )
695- for ( const p of parsed_patches ) {
696- const file_key = `${ p . workspace_name || '' } :${ p . file_path } `
697- files_with_diffs . add ( file_key )
698- }
699694 }
700695 }
701696 }
@@ -717,19 +712,7 @@ const extract_all_code_block_patches = (params: {
717712 } )
718713 items . push ( ...preceding_items )
719714
720- let patches = diff_block_patches . get ( block . start ) || [ ]
721-
722- if ( patches . length > 0 ) {
723- patches = patches . filter ( ( p ) => {
724- const is_redundant = preceding_items . some (
725- ( item ) =>
726- item . type == 'diff' &&
727- item . file_path == p . file_path &&
728- item . workspace_name == p . workspace_name
729- )
730- return ! is_redundant
731- } )
732- }
715+ const patches = diff_block_patches . get ( block . start ) || [ ]
733716
734717 if ( patches . length > 0 ) {
735718 const last_item = items . length > 0 ? items [ items . length - 1 ] : undefined
@@ -786,36 +769,32 @@ const extract_all_code_block_patches = (params: {
786769 } )
787770
788771 if ( patch ) {
789- const file_key = `${ patch . workspace_name || '' } :${ patch . file_path } `
790- if ( ! files_with_diffs . has ( file_key ) ) {
791- const text_before_lines = lines . slice ( last_block_end + 1 , block . start )
792- items . push (
793- ...process_text_for_deleted_files ( {
794- lines : text_before_lines ,
795- is_single_root : params . is_single_root
796- } )
797- )
772+ const text_before_lines = lines . slice ( last_block_end + 1 , block . start )
773+ items . push (
774+ ...process_text_for_deleted_files ( {
775+ lines : text_before_lines ,
776+ is_single_root : params . is_single_root
777+ } )
778+ )
798779
799- const last_item =
800- items . length > 0 ? items [ items . length - 1 ] : undefined
801- if ( last_item && last_item . type == 'text' ) {
802- remove_path_line_from_text_block ( {
803- text_item : last_item ,
804- target_file_path : patch . file_path ,
805- is_single_root : params . is_single_root
806- } )
807-
808- if ( ! last_item . content ) {
809- items . pop ( )
810- }
811- }
780+ const last_item = items . length > 0 ? items [ items . length - 1 ] : undefined
781+ if ( last_item && last_item . type == 'text' ) {
782+ remove_path_line_from_text_block ( {
783+ text_item : last_item ,
784+ target_file_path : patch . file_path ,
785+ is_single_root : params . is_single_root
786+ } )
812787
813- if ( hint_result . workspace_name && ! patch . workspace_name ) {
814- patch . workspace_name = hint_result . workspace_name
788+ if ( ! last_item . content ) {
789+ items . pop ( )
815790 }
816- items . push ( patch )
817- last_block_end = block . end
818791 }
792+
793+ if ( hint_result . workspace_name && ! patch . workspace_name ) {
794+ patch . workspace_name = hint_result . workspace_name
795+ }
796+ items . push ( patch )
797+ last_block_end = block . end
819798 }
820799 }
821800 }
@@ -900,6 +879,27 @@ const parse_multiple_raw_patches = (params: {
900879 return patches
901880}
902881
882+ const filter_last_diff_per_file = (
883+ items : DiffOrTextBlock [ ]
884+ ) : DiffOrTextBlock [ ] => {
885+ const seen_files = new Set < string > ( )
886+ const result : DiffOrTextBlock [ ] = [ ]
887+
888+ for ( let i = items . length - 1 ; i >= 0 ; i -- ) {
889+ const item = items [ i ]
890+ if ( item . type === 'diff' ) {
891+ const file_key = `${ item . workspace_name || '' } :${ item . file_path } `
892+ if ( ! seen_files . has ( file_key ) ) {
893+ seen_files . add ( file_key )
894+ result . unshift ( item )
895+ }
896+ } else {
897+ result . unshift ( item )
898+ }
899+ }
900+ return result
901+ }
902+
903903export const extract_diffs = ( params : {
904904 clipboard_text : string
905905 is_single_root : boolean
@@ -918,17 +918,19 @@ export const extract_diffs = (params: {
918918 xml_file_tag_start_regex . test ( line . trim ( ) )
919919 )
920920
921+ let items : DiffOrTextBlock [ ]
921922 if ( uses_code_blocks || uses_xml_blocks ) {
922- return extract_all_code_block_patches ( {
923+ items = extract_all_code_block_patches ( {
923924 normalized_text,
924925 is_single_root : params . is_single_root
925926 } )
927+ } else {
928+ items = parse_multiple_raw_patches ( {
929+ all_lines : lines ,
930+ is_single_root : params . is_single_root
931+ } )
926932 }
927-
928- return parse_multiple_raw_patches ( {
929- all_lines : lines ,
930- is_single_root : params . is_single_root
931- } )
933+ return filter_last_diff_per_file ( items )
932934}
933935
934936export const extract_paths_from_lines = (
0 commit comments