Skip to content

Commit 5073c49

Browse files
committed
Enhance the clipboard parser to support diff blocks without hunk headers and update edit format instructions for better formatting consistency
1 parent a63389c commit 5073c49

7 files changed

Lines changed: 70 additions & 8 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,21 @@ describe('clipboard-parser', () => {
19991999
})
20002000
})
20012001

2002+
it('parses diff format when the file path is specified in a markdown heading above a diff block that lacks hunk headers', () => {
2003+
const test_case = 'diff-markdown-path-above-no-hunk-header'
2004+
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)
2005+
const result = parse_response({
2006+
response: text,
2007+
is_single_root_folder_workspace: true
2008+
})
2009+
expect(result).toHaveLength(1)
2010+
expect(result[0]).toMatchObject({
2011+
type: 'diff',
2012+
file_path: 'src/index.ts',
2013+
content: load_test_case_file('diffs', test_case, '1-file.txt')
2014+
})
2015+
})
2016+
20022017
it('parses diff format with redundant closing backticks', () => {
20032018
const test_case = 'diff-markdown-redundant-closing-backticks'
20042019
const text = load_test_case_file(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ export const parse_response = (params: {
9797
if (
9898
hunk_header_regex.test(processed_response) ||
9999
diff_header_regex.test(processed_response) ||
100-
git_diff_header_regex.test(processed_response)
100+
git_diff_header_regex.test(processed_response) ||
101+
processed_response.includes('```diff') ||
102+
processed_response.includes('```patch')
101103
) {
102104
const patches_or_text = extract_diffs({
103105
clipboard_text: processed_response,

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,28 @@ const build_patch_content = (params: {
10201020
if (to_line) final_header.push(to_line)
10211021

10221022
patch_lines = [...final_header, ...formatted_body_lines]
1023+
} else {
1024+
const content_start_index = patch_lines.findIndex(
1025+
(line) =>
1026+
!line.startsWith('--- ') &&
1027+
!line.startsWith('+++ ') &&
1028+
!line.startsWith('diff --git')
1029+
)
1030+
1031+
if (content_start_index !== -1) {
1032+
const header_lines = patch_lines.slice(0, content_start_index)
1033+
const content_lines = patch_lines.slice(content_start_index)
1034+
const is_new_file_content =
1035+
content_lines.length > 0 &&
1036+
content_lines.every(
1037+
(line) => line.startsWith('+') || line.trim() === ''
1038+
)
1039+
1040+
if (is_new_file_content) {
1041+
const hunk_header = `@@ -0,0 +1,${content_lines.length} @@`
1042+
patch_lines = [...header_lines, hunk_header, ...content_lines]
1043+
}
1044+
}
10231045
}
10241046

10251047
patch_content = patch_lines
@@ -1052,7 +1074,18 @@ const build_patch_content = (params: {
10521074
is_single_root_folder_workspace: false
10531075
}).relative_path
10541076
: params.file_path
1055-
patch_content = `--- a/${relative_file_path}\n+++ b/${relative_file_path}`
1077+
const content_lines = params.lines
1078+
const is_new_file =
1079+
content_lines.length > 0 &&
1080+
content_lines.every((l) => l.startsWith('+') || l.trim() === '')
1081+
1082+
let body = params.lines.join('\n')
1083+
if (is_new_file) {
1084+
const hunk_header = `@@ -0,0 +1,${content_lines.length} @@`
1085+
body = `${hunk_header}\n${body}`
1086+
}
1087+
1088+
patch_content = `--- a/${relative_file_path}\n+++ b/${relative_file_path}\n${body}`
10561089
} else {
10571090
const patch_body_lines = params.lines.slice(content_start_index)
10581091
const formatted_patch_body_lines = format_hunk_headers(patch_body_lines)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--- a/src/index.ts
2+
+++ b/src/index.ts
3+
@@ -0,0 +1,2 @@
4+
+console.log("hello")
5+
+console.log("world")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### `src/index.ts`:
2+
3+
```diff
4+
+console.log("hello")
5+
+console.log("world")
6+
```

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diffs/diff-markdown-path-above/diff-markdown-path-above.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
@@ -1,3 +1,3 @@
55
console.log("hello")
66
-console.log("old message")
7-
+console.log("new message")
7+
+console.log("new message")
8+
```

‎packages/vscode/src/constants/edit-format-instructions.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const intro = `Whenever showing a new, updated, renamed or deleted file, provide a brief explanation, then print the path in a markdown heading (e.g. ### New file: \`src/examples/hello.py\`, ### Updated file: \`src/examples/hello.py\`, ### Renamed file: \`src/examples/hello.py\` (old) \`src/welcome.py\` (new), ### Deleted file: \`src/examples/hello.py\`), followed by a file within a markdown code block, when necessary.`
1+
const intro = `Whenever showing a new, updated, renamed or deleted file, provide a brief explanation, then print the path in a markdown heading (e.g. ### New file: \`src/examples/hello.py\`, ### Updated file: \`src/examples/hello.py\`, ### Renamed file: \`src/examples/hello.py\` (old) \`src/welcome.py\` (new), ### Deleted file: \`src/examples/hello.py\`)`
22

3-
export const EDIT_FORMAT_INSTRUCTIONS_WHOLE = `${intro} You must show file contents in full—as I have a disability which means I can't type and need to be able to just copy and paste. Example:
3+
export const EDIT_FORMAT_INSTRUCTIONS_WHOLE = `${intro}, followed by a markdown code block with the file's full contents—as I have a disability which means I can't type and need to be able to just copy and paste. Example:
44
Updated the greeting text.
55
66
### Updated file: \`src/examples/hello.py\`
@@ -12,7 +12,7 @@ def show_greeting():
1212
print(GREETING)
1313
\`\`\``
1414

15-
export const EDIT_FORMAT_INSTRUCTIONS_TRUNCATED = `${intro} As a space saving measure, replace unchanged function bodies with ellipsis comments, e.g. "# ...". Example:
15+
export const EDIT_FORMAT_INSTRUCTIONS_TRUNCATED = `${intro}, followed by a markdown code block with file's contents. As a space saving measure, replace unchanged function bodies with ellipsis comments, e.g. "# ...". Example:
1616
Updated the greeting text.
1717
1818
### Updated file: \`src/examples/hello.py\`
@@ -28,7 +28,7 @@ def show_greeting():
2828
# ...
2929
\`\`\``
3030

31-
export const EDIT_FORMAT_INSTRUCTIONS_DIFF = `${intro} Whenever showing changes to the original files, use the Unified Diff format. Example:
31+
export const EDIT_FORMAT_INSTRUCTIONS_DIFF = `${intro}, followed by a unified diff within a markdown code block. Example:
3232
Updated the greeting text.
3333
3434
### Updated file: \`src/examples/hello.py\`
@@ -44,7 +44,7 @@ Updated the greeting text.
4444
print(GREETING)
4545
\`\`\``
4646

47-
export const EDIT_FORMAT_INSTRUCTIONS_BEFORE_AFTER = `${intro} Whenever showing changes to the original files, use Git-style merge conflict markers. Example:
47+
export const EDIT_FORMAT_INSTRUCTIONS_BEFORE_AFTER = `${intro}, followed by a markdown code block showing the original and updated code snippets with Git-style merge conflict markers. Example:
4848
Updated the greeting text.
4949
5050
### Updated file: \`src/examples/hello.py\`

0 commit comments

Comments
 (0)