Skip to content

Commit 3bd4163

Browse files
committed
Fix hunk header formatting for patches extracted from code blocks
1 parent eaf9172 commit 3bd4163

1 file changed

Lines changed: 19 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-diff-patches.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const extract_diff_patches = (clipboard_text: string): DiffPatch[] => {
6767
for (const line of patch_body_lines) {
6868
// Check if line starts with @@ and has content after it without newline
6969
const hunk_match = line.match(/^(@@ -\d+,\d+ \+\d+,\d+ @@)(.*)$/)
70-
if (hunk_match && hunk_match[2].trim() !== '') {
70+
if (hunk_match && hunk_match[2].trim() != '') {
7171
// Split hunk header and content onto separate lines
7272
formatted_patch_body_lines.push(hunk_match[1])
7373
formatted_patch_body_lines.push(hunk_match[2])
@@ -143,6 +143,24 @@ export const extract_diff_patches = (clipboard_text: string): DiffPatch[] => {
143143
patch_content = `--- a/${current_file_path}\n+++ b/${current_file_path}\n${patch_content}`
144144
}
145145

146+
// Apply hunk header formatting fix for code block patches too
147+
const patch_lines = patch_content.split('\n')
148+
const formatted_patch_lines: string[] = []
149+
for (const patch_line of patch_lines) {
150+
// Check if line starts with @@ and has content after it without newline
151+
const hunk_match = patch_line.match(
152+
/^(@@ -\d+,\d+ \+\d+,\d+ @@)(.*)$/
153+
)
154+
if (hunk_match && hunk_match[2].trim() != '') {
155+
// Split hunk header and content onto separate lines
156+
formatted_patch_lines.push(hunk_match[1])
157+
formatted_patch_lines.push(hunk_match[2])
158+
} else {
159+
formatted_patch_lines.push(patch_line)
160+
}
161+
}
162+
patch_content = formatted_patch_lines.join('\n')
163+
146164
// Ensure patch ends with a newline
147165
if (!patch_content.endsWith('\n')) {
148166
patch_content += '\n'

0 commit comments

Comments
 (0)