Skip to content

Commit a4336a2

Browse files
committed
Enable parsing of standard diff patches embedded within non-diff markdown code blocks by detecting hunk headers
1 parent aee030e commit a4336a2

5 files changed

Lines changed: 57 additions & 1 deletion

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
@@ -622,6 +622,22 @@ describe('clipboard-parser', () => {
622622
)
623623
})
624624

625+
it('parses a diff inside a non-diff code block', () => {
626+
const test_case = 'diff-in-non-diff-code-block'
627+
const text = load_test_case_file(test_case, `${test_case}.txt`)
628+
const result = parse_response({
629+
response: text,
630+
is_single_root_folder_workspace: true
631+
})
632+
633+
expect(result.type).toBe('patches')
634+
expect(result.patches).toHaveLength(1)
635+
expect(result.patches![0].file_path).toBe('src/index.ts')
636+
expect(result.patches![0].content).toBe(
637+
load_test_case_file(test_case, 'file-1.txt')
638+
)
639+
})
640+
625641
it('parses diff format for a file deletion', () => {
626642
const test_case = 'diff-file-deletion'
627643
const text = load_test_case_file(test_case, `${test_case}.txt`)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,14 @@ export const parse_response = (params: {
734734

735735
const processed_response = params.response.replace(/``````/g, '```\n```')
736736

737+
const hunk_header_regex = /^(@@\s+-\d+(?:,\d+)?\s+\+\d+(?:,\d+)?\s+@@)/m
738+
737739
if (
738740
processed_response.includes('```diff') ||
739741
processed_response.includes('```patch') ||
740742
processed_response.startsWith('--- ') ||
741-
processed_response.startsWith('diff --git')
743+
processed_response.startsWith('diff --git') ||
744+
hunk_header_regex.test(processed_response)
742745
) {
743746
const patches = extract_diffs({
744747
clipboard_text: processed_response,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ const convert_code_block_to_new_file_diff = (params: {
249249
return null
250250
}
251251

252+
const first_real_content_line = content_lines.find((l) => l.trim() != '')
253+
if (
254+
first_real_content_line &&
255+
first_real_content_line
256+
.trim()
257+
.match(/^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@)/)
258+
) {
259+
const { workspace_name, relative_path } = extract_workspace_and_path(
260+
file_path,
261+
params.is_single_root
262+
)
263+
const patch_content = [
264+
`--- a/${relative_path}`,
265+
`+++ b/${relative_path}`,
266+
...content_lines
267+
].join('\n')
268+
269+
return {
270+
file_path: relative_path,
271+
workspace_name,
272+
content: patch_content + '\n'
273+
}
274+
}
275+
252276
const { workspace_name, relative_path } = extract_workspace_and_path(
253277
file_path,
254278
params.is_single_root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```typescript
2+
src/index.ts
3+
@@ -1,3 +1,3 @@
4+
console.log("hello")
5+
-console.log("old message")
6+
+console.log("new message")
7+
```
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")

0 commit comments

Comments
 (0)