Skip to content

Commit c838b1b

Browse files
committed
Implement file system listeners to automatically track and update the review state when files are created or deleted outside the active review process
1 parent 0b6817f commit c838b1b

1 file changed

Lines changed: 165 additions & 8 deletions

File tree

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

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

Lines changed: 165 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,6 @@ export const review = async (params: {
314314

315315
const text_document_change_listener =
316316
vscode.workspace.onDidChangeTextDocument((event) => {
317-
if (!params.view_provider) return
318-
319317
const changed_doc_path = event.document.uri.fsPath
320318
const changed_file_in_review = prepared_files.find(
321319
(pf) => pf.sanitized_path == changed_doc_path
@@ -411,15 +409,172 @@ export const review = async (params: {
411409
params.original_states.push(new_original_state)
412410
prepared_files.push(new_prepared_file)
413411
create_temp_files_with_original_content([new_prepared_file])
414-
if (params.view_provider) {
415-
params.view_provider.send_message({
416-
command: 'UPDATE_FILE_IN_REVIEW',
417-
file: new_prepared_file.reviewable_file
418-
})
419-
}
412+
params.view_provider.send_message({
413+
command: 'UPDATE_FILE_IN_REVIEW',
414+
file: new_prepared_file.reviewable_file
415+
})
420416
}
421417
})
422418

419+
const file_delete_listener = vscode.workspace.onDidDeleteFiles((event) => {
420+
for (const uri of event.files) {
421+
if (uri.scheme != 'file') continue
422+
423+
const deleted_file_path = uri.fsPath
424+
const deleted_file_in_review = prepared_files.find(
425+
(pf) => pf.sanitized_path == deleted_file_path
426+
)
427+
428+
if (!deleted_file_in_review) {
429+
const workspace_folder = vscode.workspace.getWorkspaceFolder(uri)
430+
if (!workspace_folder) continue
431+
432+
if (prepared_files.some((pf) => pf.sanitized_path == uri.fsPath)) {
433+
continue
434+
}
435+
436+
const new_content = ''
437+
const relative_path = vscode.workspace
438+
.asRelativePath(uri, false)
439+
.replace(/\\/g, '/')
440+
441+
const original_content_for_undo = '' // Limitation: cannot get content of deleted file
442+
443+
const new_original_state: OriginalFileState = {
444+
file_path: relative_path,
445+
content: original_content_for_undo,
446+
is_new: false,
447+
workspace_name: workspace_folder.name
448+
}
449+
450+
const diff_stats = get_diff_stats({
451+
original_content: original_content_for_undo,
452+
new_content: new_content
453+
})
454+
455+
const sanitized_file_path = uri.fsPath
456+
const hash = crypto
457+
.createHash('md5')
458+
.update(sanitized_file_path)
459+
.digest('hex')
460+
const temp_filename = `cwc-review-${hash}.tmp`
461+
const temp_file_path = path.join(os.tmpdir(), temp_filename)
462+
463+
const reviewable_file: ReviewableFile = {
464+
file_path: relative_path,
465+
content: new_content,
466+
workspace_name: workspace_folder.name,
467+
is_new: false,
468+
is_deleted: true,
469+
lines_added: diff_stats.lines_added,
470+
lines_removed: diff_stats.lines_removed
471+
}
472+
473+
const new_prepared_file: PreparedFile = {
474+
reviewable_file,
475+
sanitized_path: sanitized_file_path,
476+
original_content: original_content_for_undo,
477+
temp_file_path,
478+
file_exists: false
479+
}
480+
481+
params.original_states.push(new_original_state)
482+
prepared_files.push(new_prepared_file)
483+
create_temp_files_with_original_content([new_prepared_file])
484+
params.view_provider.send_message({
485+
command: 'UPDATE_FILE_IN_REVIEW',
486+
file: new_prepared_file.reviewable_file
487+
})
488+
} else {
489+
deleted_file_in_review.reviewable_file.is_deleted = true
490+
deleted_file_in_review.reviewable_file.content = ''
491+
params.view_provider.send_message({
492+
command: 'UPDATE_FILE_IN_REVIEW',
493+
file: deleted_file_in_review.reviewable_file
494+
})
495+
}
496+
}
497+
})
498+
499+
const file_created_listener = vscode.workspace.onDidCreateFiles((event) => {
500+
for (const uri of event.files) {
501+
if (uri.scheme !== 'file') continue
502+
503+
const workspace_folder = vscode.workspace.getWorkspaceFolder(uri)
504+
if (!workspace_folder) continue
505+
506+
// Avoid duplicates if already tracked
507+
if (prepared_files.some((pf) => pf.sanitized_path === uri.fsPath)) {
508+
continue
509+
}
510+
511+
// Read the content of the newly created file (may be empty)
512+
let new_content = ''
513+
try {
514+
new_content = fs.readFileSync(uri.fsPath, 'utf8')
515+
} catch (e) {
516+
new_content = ''
517+
}
518+
519+
const relative_path = vscode.workspace
520+
.asRelativePath(uri, false)
521+
.replace(/\\/g, '/')
522+
523+
const original_content = '' // New file had no previous content
524+
const is_new = true
525+
526+
const new_original_state: OriginalFileState = {
527+
file_path: relative_path,
528+
content: original_content,
529+
is_new: is_new,
530+
workspace_name: workspace_folder.name
531+
}
532+
533+
const diff_stats = get_diff_stats({
534+
original_content: original_content,
535+
new_content: new_content
536+
})
537+
538+
const sanitized_file_path = uri.fsPath
539+
const hash = crypto
540+
.createHash('md5')
541+
.update(sanitized_file_path)
542+
.digest('hex')
543+
const temp_filename = `cwc-review-${hash}.tmp`
544+
const temp_file_path = path.join(os.tmpdir(), temp_filename)
545+
546+
const reviewable_file: ReviewableFile = {
547+
file_path: relative_path,
548+
content: new_content,
549+
workspace_name: workspace_folder.name,
550+
is_new: true,
551+
is_deleted: false,
552+
lines_added: diff_stats.lines_added,
553+
lines_removed: diff_stats.lines_removed
554+
}
555+
556+
const new_prepared_file: PreparedFile = {
557+
reviewable_file,
558+
sanitized_path: sanitized_file_path,
559+
original_content: original_content,
560+
temp_file_path,
561+
file_exists: false
562+
}
563+
564+
params.original_states.push(new_original_state)
565+
prepared_files.push(new_prepared_file)
566+
567+
// Create temp file with the original (empty) content for diff view
568+
create_temp_files_with_original_content([new_prepared_file])
569+
570+
// Notify the panel to include this file in the review UI
571+
params.view_provider.send_message({
572+
command: 'UPDATE_FILE_IN_REVIEW',
573+
file: new_prepared_file.reviewable_file
574+
})
575+
}
576+
})
577+
423578
try {
424579
prepared_files = await prepare_files_from_original_states({
425580
original_states: params.original_states,
@@ -621,6 +776,8 @@ export const review = async (params: {
621776
return { accepted_files, rejected_states }
622777
} finally {
623778
text_document_change_listener.dispose()
779+
file_delete_listener.dispose()
780+
file_created_listener.dispose()
624781
await close_review_diff_editors(prepared_files)
625782
cleanup_temp_files(prepared_files)
626783
toggle_file_review_state = undefined

0 commit comments

Comments
 (0)