@@ -3,6 +3,7 @@ import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-
33import { extract_workspace_and_path } from '../clipboard-parser'
44
55export type Diff = {
6+ type : 'diff'
67 file_path : string
78 content : string
89 workspace_name ?: string
@@ -133,6 +134,7 @@ const process_collected_patch_lines = (params: {
133134 } )
134135
135136 const patch : Diff = {
137+ type : 'diff' ,
136138 file_path : relative_path ,
137139 workspace_name,
138140 content : content_str . endsWith ( '\n' ) ? content_str : content_str + '\n'
@@ -176,7 +178,12 @@ const convert_code_block_to_new_file_diff = (params: {
176178 const xml_match = first_line . match ( xml_path_regex )
177179
178180 if ( xml_match && xml_match [ 1 ] ) {
179- file_path = xml_match [ 1 ] . replace ( / \\ / g, '/' )
181+ const potential_path = xml_match [ 1 ] . replace ( / \\ / g, '/' )
182+ if ( potential_path . endsWith ( '/' ) ) {
183+ // This is a directory, not a file.
184+ return null
185+ }
186+ file_path = potential_path
180187
181188 const file_tag_start = params . lines . findIndex ( ( l ) =>
182189 l . trim ( ) . startsWith ( '<file' )
@@ -226,8 +233,11 @@ const convert_code_block_to_new_file_diff = (params: {
226233 const potential_path = match [ 1 ]
227234
228235 if (
236+ ! potential_path . endsWith ( '/' ) &&
237+ ! potential_path . endsWith ( '\\' ) &&
229238 ( potential_path . includes ( '.' ) || potential_path . includes ( '/' ) ) &&
230- ! potential_path . includes ( ' ' )
239+ ! potential_path . includes ( ' ' ) &&
240+ / [ a - z A - Z 0 - 9 ] / . test ( potential_path )
231241 ) {
232242 const rest_of_line = line
233243 . substring ( line . indexOf ( potential_path ) + potential_path . length )
@@ -279,6 +289,7 @@ const convert_code_block_to_new_file_diff = (params: {
279289 ] . join ( '\n' )
280290
281291 return {
292+ type : 'diff' ,
282293 file_path : relative_path ,
283294 workspace_name,
284295 content : patch_content + '\n'
@@ -299,6 +310,7 @@ const convert_code_block_to_new_file_diff = (params: {
299310 ] . join ( '\n' )
300311
301312 return {
313+ type : 'diff' ,
302314 file_path : relative_path ,
303315 workspace_name,
304316 content : patch_content + '\n'
@@ -417,13 +429,30 @@ const extract_all_code_block_patches = (params: {
417429 // Process each found code block in order of appearance.
418430 for ( let i = 0 ; i < code_blocks . length ; i ++ ) {
419431 const block = code_blocks [ i ]
432+ const block_lines = lines . slice ( block . start + 1 , block . end )
433+
434+ if ( block . type != 'diff' && block . type != 'patch' ) {
435+ const has_truncation_comment = block_lines . some ( ( line ) => {
436+ const trimmed = line . trim ( )
437+ return (
438+ trimmed . startsWith ( '// ...' ) ||
439+ trimmed . startsWith ( '# ...' ) ||
440+ trimmed . startsWith ( '/* ...' ) ||
441+ trimmed . startsWith ( '* ...' ) ||
442+ trimmed . startsWith ( '-- ...' ) ||
443+ trimmed . startsWith ( '<!-- ...' )
444+ )
445+ } )
446+ if ( has_truncation_comment ) {
447+ continue
448+ }
449+ }
420450
421451 const text_before = lines . slice ( last_block_end + 1 , block . start ) . join ( '\n' )
422452 if ( text_before . trim ( ) ) {
423453 items . push ( { type : 'text' , content : text_before . trim ( ) } )
424454 }
425455
426- const block_lines = lines . slice ( block . start + 1 , block . end ) // Exclude the ``` lines
427456 if ( block . type == 'diff' || block . type == 'patch' ) {
428457 items . push ( ...( diff_block_patches . get ( block . start ) || [ ] ) )
429458 } else {
@@ -468,6 +497,10 @@ const extract_all_code_block_patches = (params: {
468497 }
469498
470499 if ( extracted ) {
500+ if ( extracted . endsWith ( '/' ) || extracted . endsWith ( '\\' ) ) {
501+ continue
502+ }
503+
471504 file_path_hint = extracted
472505 const { workspace_name } = extract_workspace_and_path ( {
473506 raw_file_path : extracted ,
@@ -485,6 +518,17 @@ const extract_all_code_block_patches = (params: {
485518 } )
486519
487520 if ( patch ) {
521+ const last_item = items . length > 0 ? items [ items . length - 1 ] : undefined
522+ if (
523+ file_path_hint &&
524+ last_item &&
525+ last_item . type == 'text' &&
526+ ( last_item . content . trim ( ) == file_path_hint ||
527+ last_item . content . trim ( ) == `\`${ file_path_hint } \`` )
528+ ) {
529+ items . pop ( )
530+ }
531+
488532 const file_key = `${ patch . workspace_name || '' } :${ patch . file_path } `
489533
490534 if ( ! files_with_diffs . has ( file_key ) ) {
@@ -493,6 +537,11 @@ const extract_all_code_block_patches = (params: {
493537 }
494538 items . push ( patch )
495539 }
540+ } else {
541+ const block_content = lines . slice ( block . start , block . end + 1 ) . join ( '\n' )
542+ if ( block_lines . join ( '' ) . trim ( ) ) {
543+ items . push ( { type : 'text' , content : block_content } )
544+ }
496545 }
497546 }
498547 last_block_end = block . end
0 commit comments