Skip to content

Commit 200db6b

Browse files
committed
Fix diff rename file deletion when response is re-applied
1 parent 3f2c26d commit 200db6b

7 files changed

Lines changed: 52 additions & 15 deletions

File tree

packages/ui/src/components/editor/panel/ResponsePreview/ResponsePreview.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,24 @@ export const ResponsePreview: FC<Props> = (props) => {
218218
}}
219219
/>
220220
</div>
221-
{file.file_state != 'deleted' && (
222-
<div className={styles['list__file__line-numbers']}>
221+
<div className={styles['list__file__line-numbers']}>
222+
{file.file_state != 'deleted' && (
223223
<span
224224
className={styles['list__file__line-numbers__added']}
225225
>
226226
+{file.lines_added}
227227
</span>
228+
)}
229+
{file.file_state != 'new' && (
228230
<span
229231
className={
230232
styles['list__file__line-numbers__removed']
231233
}
232234
>
233235
-{file.lines_removed}
234236
</span>
235-
</div>
236-
)}
237+
)}
238+
</div>
237239
</div>
238240
</div>
239241
)

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ export const apply_chat_response_command = (params: {
183183
continue
184184
}
185185

186+
const is_rename = !!state.file_path_to_restore
187+
186188
const diff_stats = get_diff_stats({
187-
original_content: state.content,
189+
original_content: is_rename ? '' : state.content,
188190
new_content: current_content
189191
})
190192

@@ -201,7 +203,7 @@ export const apply_chat_response_command = (params: {
201203
file_path: state.file_path,
202204
workspace_name: state.workspace_name,
203205
file_state:
204-
state.file_state == 'new'
206+
state.file_state == 'new' || is_rename
205207
? 'new'
206208
: is_deleted
207209
? 'deleted'
@@ -213,6 +215,27 @@ export const apply_chat_response_command = (params: {
213215
content: current_content,
214216
is_checked: true
215217
})
218+
219+
if (state.file_path_to_restore) {
220+
const deleted_diff_stats = get_diff_stats({
221+
original_content: state.content,
222+
new_content: ''
223+
})
224+
225+
total_lines_removed += deleted_diff_stats.lines_removed
226+
227+
files_for_history.push({
228+
type: 'file',
229+
file_path: state.file_path_to_restore,
230+
workspace_name: state.workspace_name,
231+
file_state: 'deleted',
232+
lines_added: 0,
233+
lines_removed: deleted_diff_stats.lines_removed,
234+
is_replaced: false,
235+
content: '',
236+
is_checked: true
237+
})
238+
}
216239
}
217240
const history = params.panel_provider.response_history
218241

packages/vscode/src/commands/apply-chat-response-command/utils/preview/file-preparer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ export const prepare_files_from_original_states = async (params: {
5656
const temp_filename = `cwc-preview-${hash}.tmp`
5757
const temp_file_path = path.join(os.tmpdir(), temp_filename)
5858

59+
const original_content_for_diff = state.file_path_to_restore
60+
? ''
61+
: state.content
62+
5963
const diff_stats = get_diff_stats({
60-
original_content: state.content,
64+
original_content: original_content_for_diff,
6165
new_content: new_content_for_diff
6266
})
6367
const is_deleted =
@@ -86,7 +90,7 @@ export const prepare_files_from_original_states = async (params: {
8690
prepared_files.push({
8791
reviewable_file,
8892
sanitized_path: sanitized_file_path,
89-
original_content: state.content,
93+
original_content: original_content_for_diff,
9094
temp_file_path,
9195
file_exists: state.file_state !== 'new'
9296
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export const preview = async (params: {
112112
}
113113

114114
if (params.panel_provider) {
115+
console.log('xxx', items_for_preview)
115116
params.panel_provider.send_message({
116117
command: 'RESPONSE_PREVIEW_STARTED',
117118
items: items_for_preview,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,12 @@ export const setup_workspace_listeners = (
447447
existing.reviewable_file.file_state = 'new'
448448
existing.reviewable_file.content = new_content
449449

450+
const old_original_content = existing.original_content
451+
existing.original_content = ''
452+
fs.writeFileSync(existing.temp_file_path, '')
453+
450454
const diff_stats_updated = get_diff_stats({
451-
original_content: existing.original_content,
455+
original_content: '',
452456
new_content
453457
})
454458
existing.reviewable_file.lines_added = diff_stats_updated.lines_added
@@ -465,7 +469,7 @@ export const setup_workspace_listeners = (
465469
const oldTemp = path.join(os.tmpdir(), `cwc-${oldHash}.tmp`)
466470

467471
const deleted_diff_stats = get_diff_stats({
468-
original_content: existing.original_content,
472+
original_content: old_original_content,
469473
new_content: ''
470474
})
471475

@@ -485,7 +489,7 @@ export const setup_workspace_listeners = (
485489
const deleted_prepared: PreparedFile = {
486490
reviewable_file: deleted_reviewable,
487491
sanitized_path: oldSanitized,
488-
original_content: existing.original_content,
492+
original_content: old_original_content,
489493
temp_file_path: oldTemp,
490494
file_exists: false
491495
}
@@ -496,7 +500,7 @@ export const setup_workspace_listeners = (
496500
// Track state for downstream consumers (rename grouping)
497501
original_states.push({
498502
file_path: new_relative,
499-
content: existing.original_content,
503+
content: old_original_content,
500504
file_state: 'new',
501505
workspace_name: new_workspace_folder.name,
502506
file_path_to_restore: old_relative

packages/vscode/src/constants/edit-format-instructions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const EDIT_FORMAT_INSTRUCTIONS_WHOLE = `Whenever showing a new or updated file, provide brief explanation, then print path in a markdown heading (e.g. ### New file: \`src/hello.py\`, ### Updated file: \`src/hello.py\`, ### Deleted file: \`src/hello.py\`, ### Renamed file: \`src/hello.py\` (new) \`src/welcome.py\` (old)), followed by a markdown code block with full contents—as I have a disability which means I can't type and need to be able to just copy and paste. Example:
1+
export const EDIT_FORMAT_INSTRUCTIONS_WHOLE = `Whenever showing a new or updated file, provide brief explanation, then print path in a markdown heading (e.g. ### New file: \`src/hello.py\`, ### Updated file: \`src/hello.py\`, ### Deleted file: \`src/hello.py\`, ### Renamed file: \`src/hello.py\` (old) \`src/welcome.py\` (new)), followed by a markdown code block with full contents—as I have a disability which means I can't type and need to be able to just copy and paste. Example:
22
Updated the greeting text.
33
44
### Updated file: \`src/hello.py\`
@@ -10,7 +10,7 @@ def show_greeting():
1010
print(GREETING)
1111
\`\`\``
1212

13-
export const EDIT_FORMAT_INSTRUCTIONS_TRUNCATED = `Whenever showing a new or updated file, provide brief explanation, then print path in a markdown heading (e.g. ### New file: \`src/hello.py\`, ### Updated file: \`src/hello.py\`, ### Deleted file: \`src/hello.py\`, ### Renamed file: \`src/hello.py\` (new) \`src/welcome.py\` (old)), followed by a markdown code block with truncated contents—as a space saving measure unchanged parts should be hidden behind ellipsis comments, e.g. "# ... ". Example:
13+
export const EDIT_FORMAT_INSTRUCTIONS_TRUNCATED = `Whenever showing a new or updated file, provide brief explanation, then print path in a markdown heading (e.g. ### New file: \`src/hello.py\`, ### Updated file: \`src/hello.py\`, ### Deleted file: \`src/hello.py\`, ### Renamed file: \`src/hello.py\` (old) \`src/welcome.py\` (new)), followed by a markdown code block with truncated contents—as a space saving measure unchanged parts should be hidden behind ellipsis comments, e.g. "# ... ". Example:
1414
Updated the greeting text.
1515
1616
### Updated file: \`src/hello.py\`
@@ -22,7 +22,7 @@ def show_greeting():
2222
# ... the function remains unchanged
2323
\`\`\``
2424

25-
export const EDIT_FORMAT_INSTRUCTIONS_DIFF = `Whenever showing a new, updated, renamed or deleted file, provide brief explanation, then print path in a markdown heading (e.g. ### New file: \`src/hello.py\`, ### Updated file: \`src/hello.py\`, ### Deleted file: \`src/hello.py\`, ### Renamed file: \`src/hello.py\` (new) \`src/welcome.py\` (old)), followed by a unified diff within a markdown code block. Example:
25+
export const EDIT_FORMAT_INSTRUCTIONS_DIFF = `Whenever showing a new, updated, renamed or deleted file, provide brief explanation, then print path in a markdown heading (e.g. ### New file: \`src/hello.py\`, ### Updated file: \`src/hello.py\`, ### Deleted file: \`src/hello.py\`, ### Renamed file: \`src/hello.py\` (old) \`src/welcome.py\` (new)), followed by a unified diff within a markdown code block. Example:
2626
Updated the greeting text.
2727
2828
### Updated file: \`src/hello.py\`

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ export const use_panel = (vscode: any) => {
168168
set_items_to_review(message.items)
169169
set_raw_instructions(message.raw_instructions)
170170
set_preview_item_created_at(message.created_at)
171+
if (message.created_at) {
172+
set_selected_history_item_created_at(message.created_at)
173+
}
171174
} else if (message.command == 'UPDATE_FILE_IN_REVIEW') {
172175
set_items_to_review((current_items) => {
173176
const items = current_items ?? []

0 commit comments

Comments
 (0)