Skip to content

Commit 43fdeab

Browse files
committed
Refactor file change listeners to correctly update response history totals when files are modified, created, deleted, or renamed in the workspace
1 parent 765ff10 commit 43fdeab

2 files changed

Lines changed: 94 additions & 37 deletions

File tree

‎packages/vscode/package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@
582582
"scope": "resource",
583583
"description": "Style of generated code blocks in \"Edit context\" mode.",
584584
"default": {
585-
"whole": "Whenever showing a new or updated file, print its path and a Markdown Code Block with 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. Provide brief explanations, when you deem appropriate.",
586-
"truncated": "Whenever showing a new or updated file, print its path and a Markdown Code Block with truncated file's contents. To save space, unchanged parts should be hidden behind ellipsis comments, e.g. \"// ...\". Provide brief explanations, when you deem appropriate.",
587-
"diff": "Whenever showing a new or updated file, use the the unified diff (unidiff) format, within a Markdown Code Block. Provide brief explanations, when you deem appropriate."
585+
"whole": "Whenever showing a new or updated file, print its path and a Markdown Code Block with 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. Provide brief explanations to the proposed changes.",
586+
"truncated": "Whenever showing a new or updated file, print its path and a Markdown Code Block with truncated file's contents. To save space, unchanged parts should be hidden behind ellipsis comments, e.g. \"// ...\". Provide brief explanations to the proposed changes.",
587+
"diff": "Whenever showing a new or updated file, use the the unified diff within a Markdown Code Block. Provide brief explanations to the proposed changes."
588588
},
589589
"properties": {
590590
"whole": {

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview/workspace-listener.ts‎

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PanelProvider } from '@/views/panel/backend/panel-provider'
88
import { PreparedFile, ReviewableFile } from './types'
99
import { get_diff_stats } from './diff-utils'
1010
import { remove_directory_if_empty } from '../file-operations'
11+
import { ResponseHistoryItem } from '@shared/types/response-history-item'
1112
import { create_temp_files_with_original_content } from './temp-file-manager'
1213

1314
export let toggle_file_review_state:
@@ -18,6 +19,50 @@ export let toggle_file_review_state:
1819
}) => Promise<void>)
1920
| undefined
2021

22+
const recalculate_history_item_totals = (item: ResponseHistoryItem) => {
23+
if (!item.files) {
24+
item.lines_added = 0
25+
item.lines_removed = 0
26+
return
27+
}
28+
item.lines_added = item.files
29+
.filter((f) => f.is_checked)
30+
.reduce((sum, f) => sum + f.lines_added, 0)
31+
item.lines_removed = item.files
32+
.filter((f) => f.is_checked)
33+
.reduce((sum, f) => sum + f.lines_removed, 0)
34+
}
35+
36+
const update_response_history = (
37+
panel_provider: PanelProvider,
38+
created_at: number | undefined,
39+
updated_file: ReviewableFile
40+
) => {
41+
if (!created_at) return
42+
43+
const history = panel_provider.response_history
44+
const item_to_update = history.find((i) => i.created_at === created_at)
45+
if (item_to_update) {
46+
if (!item_to_update.files) {
47+
item_to_update.files = []
48+
}
49+
const file_in_history_index = item_to_update.files.findIndex(
50+
(f) =>
51+
f.file_path === updated_file.file_path &&
52+
f.workspace_name === updated_file.workspace_name
53+
)
54+
55+
if (file_in_history_index !== -1) {
56+
item_to_update.files[file_in_history_index] = updated_file
57+
} else {
58+
item_to_update.files.push(updated_file)
59+
}
60+
61+
recalculate_history_item_totals(item_to_update)
62+
panel_provider.send_message({ command: 'RESPONSE_HISTORY', history })
63+
}
64+
}
65+
2166
export const setup_workspace_listeners = (
2267
prepared_files: PreparedFile[],
2368
original_states: OriginalFileState[],
@@ -69,40 +114,11 @@ export const setup_workspace_listeners = (
69114
changed_file_in_review.reviewable_file.content = new_content
70115
}
71116

72-
if (created_at) {
73-
const history = panel_provider.response_history
74-
const item_to_update = history.find(
75-
(i) => i.created_at === created_at
76-
)
77-
if (item_to_update && item_to_update.files) {
78-
const file_in_history = item_to_update.files.find(
79-
(f) =>
80-
f.file_path ==
81-
changed_file_in_review.reviewable_file.file_path &&
82-
f.workspace_name ==
83-
changed_file_in_review.reviewable_file.workspace_name
84-
)
85-
if (file_in_history) {
86-
if (changed_file_in_review.reviewable_file.is_checked) {
87-
file_in_history.content = new_content
88-
}
89-
file_in_history.lines_added = diff_stats.lines_added
90-
file_in_history.lines_removed = diff_stats.lines_removed
91-
let total_lines_added = 0
92-
let total_lines_removed = 0
93-
item_to_update.files.forEach((f) => {
94-
total_lines_added += f.lines_added
95-
total_lines_removed += f.lines_removed
96-
})
97-
item_to_update.lines_added = total_lines_added
98-
item_to_update.lines_removed = total_lines_removed
99-
panel_provider.send_message({
100-
command: 'RESPONSE_HISTORY',
101-
history
102-
})
103-
}
104-
}
105-
}
117+
update_response_history(
118+
panel_provider,
119+
created_at,
120+
changed_file_in_review.reviewable_file
121+
)
106122

107123
panel_provider.send_message({
108124
command: 'UPDATE_FILE_IN_REVIEW',
@@ -182,6 +198,11 @@ export const setup_workspace_listeners = (
182198
original_states.push(new_original_state)
183199
prepared_files.push(new_prepared_file)
184200
create_temp_files_with_original_content([new_prepared_file])
201+
update_response_history(
202+
panel_provider,
203+
created_at,
204+
new_prepared_file.reviewable_file
205+
)
185206
panel_provider.send_message({
186207
command: 'UPDATE_FILE_IN_REVIEW',
187208
file: new_prepared_file.reviewable_file
@@ -258,6 +279,11 @@ export const setup_workspace_listeners = (
258279
original_states.push(new_original_state)
259280
prepared_files.push(new_prepared_file)
260281
create_temp_files_with_original_content([new_prepared_file])
282+
update_response_history(
283+
panel_provider,
284+
created_at,
285+
new_prepared_file.reviewable_file
286+
)
261287
panel_provider.send_message({
262288
command: 'UPDATE_FILE_IN_REVIEW',
263289
file: new_prepared_file.reviewable_file
@@ -273,6 +299,11 @@ export const setup_workspace_listeners = (
273299
diff_stats.lines_added
274300
deleted_file_in_review.reviewable_file.lines_removed =
275301
diff_stats.lines_removed
302+
update_response_history(
303+
panel_provider,
304+
created_at,
305+
deleted_file_in_review.reviewable_file
306+
)
276307
panel_provider.send_message({
277308
command: 'UPDATE_FILE_IN_REVIEW',
278309
file: deleted_file_in_review.reviewable_file
@@ -355,6 +386,11 @@ export const setup_workspace_listeners = (
355386
// Create temp file with the original (empty) content for diff view
356387
create_temp_files_with_original_content([new_prepared_file])
357388

389+
update_response_history(
390+
panel_provider,
391+
created_at,
392+
new_prepared_file.reviewable_file
393+
)
358394
// Notify the panel to include this file in the review UI
359395
panel_provider.send_message({
360396
command: 'UPDATE_FILE_IN_REVIEW',
@@ -470,10 +506,20 @@ export const setup_workspace_listeners = (
470506
})
471507

472508
// Notify UI
509+
update_response_history(
510+
panel_provider,
511+
created_at,
512+
existing.reviewable_file
513+
)
473514
panel_provider.send_message({
474515
command: 'UPDATE_FILE_IN_REVIEW',
475516
file: existing.reviewable_file
476517
})
518+
update_response_history(
519+
panel_provider,
520+
created_at,
521+
deleted_prepared.reviewable_file
522+
)
477523
panel_provider.send_message({
478524
command: 'UPDATE_FILE_IN_REVIEW',
479525
file: deleted_prepared.reviewable_file
@@ -565,10 +611,20 @@ export const setup_workspace_listeners = (
565611
])
566612

567613
// Notify UI
614+
update_response_history(
615+
panel_provider,
616+
created_at,
617+
created_reviewable
618+
)
568619
panel_provider.send_message({
569620
command: 'UPDATE_FILE_IN_REVIEW',
570621
file: created_reviewable
571622
})
623+
update_response_history(
624+
panel_provider,
625+
created_at,
626+
deleted_reviewable
627+
)
572628
panel_provider.send_message({
573629
command: 'UPDATE_FILE_IN_REVIEW',
574630
file: deleted_reviewable
@@ -602,6 +658,7 @@ export const setup_workspace_listeners = (
602658
)
603659
if (file_in_history) {
604660
file_in_history.is_checked = is_checked
661+
recalculate_history_item_totals(item_to_update)
605662
panel_provider.send_message({ command: 'RESPONSE_HISTORY', history })
606663
}
607664
}

0 commit comments

Comments
 (0)