Skip to content

Commit 7481d43

Browse files
committed
Refine path extraction logic to ignore directory paths when converting code blocks to new file diffs
1 parent 376b0ee commit 7481d43

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

  • packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/extract-diff-patches

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/extract-diff-patches/extract-diffs.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ const convert_code_block_to_new_file_diff = (params: {
178178
const xml_match = first_line.match(xml_path_regex)
179179

180180
if (xml_match && xml_match[1]) {
181-
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
182187

183188
const file_tag_start = params.lines.findIndex((l) =>
184189
l.trim().startsWith('<file')
@@ -228,6 +233,8 @@ const convert_code_block_to_new_file_diff = (params: {
228233
const potential_path = match[1]
229234

230235
if (
236+
!potential_path.endsWith('/') &&
237+
!potential_path.endsWith('\\') &&
231238
(potential_path.includes('.') || potential_path.includes('/')) &&
232239
!potential_path.includes(' ') &&
233240
/[a-zA-Z0-9]/.test(potential_path)
@@ -490,6 +497,10 @@ const extract_all_code_block_patches = (params: {
490497
}
491498

492499
if (extracted) {
500+
if (extracted.endsWith('/') || extracted.endsWith('\\')) {
501+
continue
502+
}
503+
493504
file_path_hint = extracted
494505
const { workspace_name } = extract_workspace_and_path({
495506
raw_file_path: extracted,
@@ -507,6 +518,17 @@ const extract_all_code_block_patches = (params: {
507518
})
508519

509520
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+
510532
const file_key = `${patch.workspace_name || ''}:${patch.file_path}`
511533

512534
if (!files_with_diffs.has(file_key)) {

0 commit comments

Comments
 (0)