Skip to content

Commit 8e0c129

Browse files
committed
Refine patch header parsing to accurately identify new and deleted files, and directly format newly created files
1 parent 77d12a7 commit 8e0c129

1 file changed

Lines changed: 41 additions & 16 deletions

File tree

  • packages/vscode/src/commands/apply-chat-response-command/handlers

packages/vscode/src/commands/apply-chat-response-command/handlers/patch-handler.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,40 @@ const parse_patch_header = (patch_content: string): PatchFileInfo => {
2626
const lines = patch_content.split('\n')
2727
let from_path: string | undefined
2828
let to_path: string | undefined
29+
let is_new = false
30+
let is_deleted = false
31+
let from_found = false
32+
let to_found = false
2933

3034
for (const line of lines) {
31-
const match = line.match(/^\+\+\+ b\/(.+)$/)
32-
if (match && match[1]) {
33-
to_path = match[1]
35+
if (line.startsWith('---')) {
36+
from_found = true
37+
if (line == '--- /dev/null') {
38+
is_new = true
39+
} else {
40+
const from_match = line.match(/^--- a\/(.+)$/)
41+
if (from_match && from_match[1]) {
42+
from_path = from_match[1]
43+
}
44+
}
45+
} else if (line.startsWith('+++')) {
46+
to_found = true
47+
if (line == '+++ /dev/null') {
48+
is_deleted = true
49+
} else {
50+
const match = line.match(/^\+\+\+ b\/(.+)$/)
51+
if (match && match[1]) {
52+
to_path = match[1]
53+
}
54+
}
3455
}
35-
const from_match = line.match(/^--- a\/(.+)$/)
36-
if (from_match && from_match[1]) {
37-
from_path = from_match[1]
56+
57+
if (from_found && to_found) {
58+
break
3859
}
3960
}
4061

41-
const is_new = from_path == '/dev/null'
42-
const is_deleted = to_path == '/dev/null'
43-
44-
return {
45-
from_path: from_path == '/dev/null' ? undefined : from_path,
46-
to_path: to_path == '/dev/null' ? undefined : to_path,
47-
is_new,
48-
is_deleted
49-
}
62+
return { from_path, to_path, is_new, is_deleted }
5063
}
5164

5265
export const extract_file_paths_from_patch = (
@@ -312,7 +325,19 @@ const handle_new_file_patch = async (
312325
}
313326

314327
await fs.promises.writeFile(safe_path, new_content, 'utf8')
315-
await process_modified_files(file_paths, workspace_path)
328+
try {
329+
const document = await vscode.workspace.openTextDocument(safe_path)
330+
await vscode.window.showTextDocument(document, { preview: false })
331+
await new Promise((resolve) => setTimeout(resolve, 250))
332+
await format_document(document)
333+
await document.save()
334+
} catch (error) {
335+
Logger.error({
336+
function_name: 'handle_new_file_patch',
337+
message: 'Error formatting new file',
338+
data: { file_path, error }
339+
})
340+
}
316341

317342
return { success: true, original_states, used_fallback: false }
318343
} catch (error: any) {

0 commit comments

Comments
 (0)