Skip to content

Commit b2dc418

Browse files
committed
Refactor the code review process to correctly handle file acceptance/rejection logic and ensure that newly changed files not initially in the review are dynamically added and tracked
1 parent 7bd0f84 commit b2dc418

2 files changed

Lines changed: 124 additions & 18 deletions

File tree

  • packages/vscode/src

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

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ const show_diff_with_actions = async (
287287

288288
export const review = async (params: {
289289
original_states: OriginalFileState[]
290-
view_provider?: ViewProvider
290+
view_provider: ViewProvider
291291
raw_instructions?: string
292292
}): Promise<{
293293
accepted_files: ReviewableFile[]
@@ -318,7 +318,7 @@ export const review = async (params: {
318318

319319
const changed_doc_path = event.document.uri.fsPath
320320
const changed_file_in_review = prepared_files.find(
321-
(pf) => pf.sanitized_path === changed_doc_path
321+
(pf) => pf.sanitized_path == changed_doc_path
322322
)
323323

324324
if (changed_file_in_review) {
@@ -339,6 +339,84 @@ export const review = async (params: {
339339
command: 'UPDATE_FILE_IN_REVIEW',
340340
file: changed_file_in_review.reviewable_file
341341
})
342+
} else {
343+
// A file not in the review has been changed. Add it to the review.
344+
const doc = event.document
345+
if (doc.uri.scheme != 'file') return
346+
const workspace_folder = vscode.workspace.getWorkspaceFolder(doc.uri)
347+
if (!workspace_folder) return
348+
349+
// Check if it's already added to avoid races
350+
if (prepared_files.some((pf) => pf.sanitized_path == doc.uri.fsPath)) {
351+
return
352+
}
353+
354+
const new_content = doc.getText()
355+
const relative_path = vscode.workspace
356+
.asRelativePath(doc.uri, false)
357+
.replace(/\\/g, '/')
358+
359+
let original_content = ''
360+
let is_new = false
361+
if (doc.isUntitled) {
362+
is_new = true
363+
} else {
364+
try {
365+
original_content = fs.readFileSync(doc.uri.fsPath, 'utf8')
366+
} catch (e) {
367+
is_new = true // File on disk does not exist, but buffer does.
368+
}
369+
}
370+
371+
const new_original_state: OriginalFileState = {
372+
file_path: relative_path,
373+
content: original_content,
374+
is_new: is_new,
375+
workspace_name: workspace_folder.name
376+
}
377+
378+
const diff_stats = get_diff_stats({
379+
original_content: original_content,
380+
new_content: new_content
381+
})
382+
383+
const sanitized_file_path = doc.uri.fsPath
384+
const hash = crypto
385+
.createHash('md5')
386+
.update(sanitized_file_path)
387+
.digest('hex')
388+
const temp_filename = `cwc-review-${hash}.tmp`
389+
const temp_file_path = path.join(os.tmpdir(), temp_filename)
390+
const is_deleted =
391+
!is_new && new_content == '' && original_content != ''
392+
393+
const reviewable_file: ReviewableFile = {
394+
file_path: relative_path,
395+
content: new_content,
396+
workspace_name: workspace_folder.name,
397+
is_new: is_new,
398+
is_deleted: is_deleted,
399+
lines_added: diff_stats.lines_added,
400+
lines_removed: diff_stats.lines_removed
401+
}
402+
403+
const new_prepared_file: PreparedFile = {
404+
reviewable_file,
405+
sanitized_path: sanitized_file_path,
406+
original_content: original_content,
407+
temp_file_path,
408+
file_exists: !is_new
409+
}
410+
411+
params.original_states.push(new_original_state)
412+
prepared_files.push(new_prepared_file)
413+
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+
}
342420
}
343421
})
344422

@@ -474,15 +552,32 @@ export const review = async (params: {
474552
)
475553
)
476554

477-
review_items.forEach((item) => {
478-
const file_identifier = `${
479-
item.file.reviewable_file.workspace_name || ''
480-
}:${item.file.reviewable_file.file_path}`
481-
item.status = accepted_file_identifiers.has(file_identifier)
482-
? 'accepted'
483-
: 'rejected'
484-
})
485-
break
555+
const accepted_files = prepared_files
556+
.filter((pf) => {
557+
const identifier = `${pf.reviewable_file.workspace_name || ''}:${
558+
pf.reviewable_file.file_path
559+
}`
560+
return accepted_file_identifiers.has(identifier)
561+
})
562+
.map((pf) => pf.reviewable_file)
563+
564+
const rejected_states = prepared_files
565+
.filter((pf) => {
566+
const identifier = `${pf.reviewable_file.workspace_name || ''}:${
567+
pf.reviewable_file.file_path
568+
}`
569+
return !accepted_file_identifiers.has(identifier)
570+
})
571+
.map((item) => {
572+
return params.original_states.find(
573+
(state) =>
574+
state.file_path == item.reviewable_file.file_path &&
575+
state.workspace_name == item.reviewable_file.workspace_name
576+
)
577+
})
578+
.filter((state): state is OriginalFileState => state !== undefined)
579+
580+
return { accepted_files, rejected_states }
486581
}
487582

488583
if ('jump_to' in decision) {

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,25 @@ export const use_panel = (vscode: any) => {
135135
)
136136
set_raw_instructions(message.raw_instructions)
137137
} else if (message.command == 'UPDATE_FILE_IN_REVIEW') {
138-
set_files_to_review((current_files) =>
139-
current_files?.map((f) =>
140-
f.file_path == message.file.file_path &&
141-
f.workspace_name == message.file.workspace_name
142-
? { ...message.file, is_checked: f.is_checked }
143-
: f
138+
set_files_to_review((current_files) => {
139+
const files = current_files ?? []
140+
const existing_file_index = files.findIndex(
141+
(f) =>
142+
f.file_path == message.file.file_path &&
143+
f.workspace_name == message.file.workspace_name
144144
)
145-
)
145+
146+
if (existing_file_index != -1) {
147+
const new_files = [...files]
148+
new_files[existing_file_index] = {
149+
...message.file,
150+
is_checked: files[existing_file_index].is_checked
151+
}
152+
return new_files
153+
} else {
154+
return [...files, { ...message.file, is_checked: true }]
155+
}
156+
})
146157
} else if (message.command == 'CODE_REVIEW_FINISHED') {
147158
set_files_to_review(undefined)
148159
set_raw_instructions(undefined)

0 commit comments

Comments
 (0)