Skip to content

Commit 6f10c87

Browse files
committed
Enhance diff parsing to automatically associate a diff code block with a file path specified in preceding plain text or markdown headings, and remove the preceding text if it only contains the file path
1 parent 2cd3b0e commit 6f10c87

4 files changed

Lines changed: 99 additions & 3 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,5 +1259,21 @@ describe('clipboard-parser', () => {
12591259
content: load_test_case_file(test_case, '1-file.txt')
12601260
})
12611261
})
1262+
1263+
it('parses diff format with file path specified in plain text above a markdown code block', () => {
1264+
const test_case = 'diff-markdown-path-above'
1265+
const text = load_test_case_file(test_case, `${test_case}.txt`)
1266+
const result = parse_response({
1267+
response: text,
1268+
is_single_root_folder_workspace: true
1269+
})
1270+
1271+
expect(result).toHaveLength(1)
1272+
expect(result[0]).toMatchObject({
1273+
type: 'diff',
1274+
file_path: 'src/index.ts',
1275+
content: load_test_case_file(test_case, '1-file.txt')
1276+
})
1277+
})
12621278
})
12631279
})

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

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,25 +391,75 @@ const extract_all_code_block_patches = (params: {
391391
const block = code_blocks[i]
392392
if (block.type == 'diff' || block.type == 'patch') {
393393
let file_path_hint: string | undefined
394+
let is_new_file_from_hint = false
394395
if (i > 0) {
395396
const prev_block_lines = lines.slice(
396397
code_blocks[i - 1].start + 1,
397398
code_blocks[i - 1].end
398399
)
399-
const xml_path_regex = /<file\s+path=["']([^"']+)["']/
400+
const xml_path_regex = /<file\s+path=["']([^"']+)["']([^>]*)>/
400401
const non_empty_lines = prev_block_lines.filter((l) => l.trim() != '')
401402
if (non_empty_lines.length === 1) {
402403
const match = non_empty_lines[0].match(xml_path_regex)
403404
if (match && match[1]) {
404405
file_path_hint = match[1]
406+
const attributes = match[2] || ''
407+
if (/\snew\b/.test(attributes)) {
408+
is_new_file_from_hint = true
409+
}
410+
}
411+
}
412+
}
413+
414+
if (!file_path_hint && block.start > 0) {
415+
for (let j = block.start - 1; j >= Math.max(0, block.start - 5); j--) {
416+
const prev_line = lines[j].trim()
417+
if (!prev_line) continue
418+
419+
let extracted = extract_path_from_line_of_code(prev_line)
420+
421+
if (!extracted) {
422+
let potential_path = prev_line
423+
if (potential_path.endsWith(':')) {
424+
potential_path = potential_path.slice(0, -1).trim()
425+
}
426+
if (
427+
potential_path &&
428+
(potential_path.includes('/') ||
429+
potential_path.includes('\\') ||
430+
potential_path.includes('.')) &&
431+
!potential_path.endsWith('.') &&
432+
/^[a-zA-Z0-9_./@-]+$/.test(potential_path)
433+
) {
434+
extracted = potential_path
435+
}
436+
}
437+
438+
if (extracted) {
439+
let all_intermediate_lines_empty = true
440+
for (let k = j + 1; k < block.start; k++) {
441+
if (lines[k].trim() !== '') {
442+
all_intermediate_lines_empty = false
443+
break
444+
}
445+
}
446+
if (all_intermediate_lines_empty) {
447+
file_path_hint = extracted
448+
break
449+
}
405450
}
406451
}
407452
}
408453
const block_lines = lines.slice(block.start + 1, block.end)
409454
if (file_path_hint) {
410455
const { from_path, to_path } = extract_paths_from_lines(block_lines)
411456
if (!from_path && !to_path) {
412-
block_lines.unshift(`--- /dev/null`, `+++ b/${file_path_hint}`)
457+
block_lines.unshift(
458+
is_new_file_from_hint
459+
? '--- /dev/null'
460+
: `--- a/${file_path_hint}`,
461+
`+++ b/${file_path_hint}`
462+
)
413463
}
414464
}
415465
const parsed_patches = parse_multiple_raw_patches({
@@ -456,7 +506,24 @@ const extract_all_code_block_patches = (params: {
456506
}
457507

458508
if (block.type == 'diff' || block.type == 'patch') {
459-
items.push(...(diff_block_patches.get(block.start) || []))
509+
const patches = diff_block_patches.get(block.start) || []
510+
if (patches.length > 0) {
511+
const last_item = items.length > 0 ? items[items.length - 1] : undefined
512+
if (last_item && last_item.type == 'text') {
513+
let potential_path = last_item.content.trim()
514+
if (potential_path.endsWith(':')) {
515+
potential_path = potential_path.slice(0, -1).trim()
516+
}
517+
const { relative_path } = extract_workspace_and_path({
518+
raw_file_path: potential_path,
519+
is_single_root_folder_workspace: params.is_single_root
520+
})
521+
if (relative_path == patches[0].file_path) {
522+
items.pop()
523+
}
524+
}
525+
}
526+
items.push(...patches)
460527
} else {
461528
// Check if this block was a hint for the next block.
462529
if (i < code_blocks.length - 1) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--- a/src/index.ts
2+
+++ b/src/index.ts
3+
@@ -1,3 +1,3 @@
4+
console.log("hello")
5+
-console.log("old message")
6+
+console.log("new message")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
src/index.ts:
2+
3+
```diff
4+
@@ -1,3 +1,3 @@
5+
console.log("hello")
6+
-console.log("old message")
7+
+console.log("new message")

0 commit comments

Comments
 (0)