Skip to content

Commit e9b87d8

Browse files
committed
Refactor FileInReview type to include is_checked status directly, removing the need for a separate CheckedFileInReview type in the UI layer and ensuring file review state is consistently tracked across the application
1 parent 0b8435b commit e9b87d8

10 files changed

Lines changed: 175 additions & 132 deletions

File tree

‎packages/shared/src/types/file-in-review.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type FileInReview = {
22
file_path: string
3+
is_checked: boolean
34
workspace_name?: string
45
is_new?: boolean
56
is_deleted?: boolean

‎packages/ui/src/components/editor/panel/ResponseReview/ResponseReview.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Checkbox } from '../../common/Checkbox'
77
import { IconButton } from '../IconButton/IconButton'
88

99
type Props = {
10-
files: (FileInReview & { is_checked: boolean })[]
10+
files: FileInReview[]
1111
has_multiple_workspaces: boolean
1212
on_discard: () => void
1313
on_approve: (files: FileInReview[]) => void
@@ -46,9 +46,7 @@ export const ResponseReview: FC<Props> = ({
4646
const fallback_count = files.filter((f) => f.is_fallback).length
4747

4848
const sorted_files = useMemo(() => {
49-
const get_sort_score = (
50-
file: FileInReview & { is_checked: boolean }
51-
): number => {
49+
const get_sort_score = (file: FileInReview): number => {
5250
if (file.diff_fallback_method == 'search_and_replace') {
5351
return 0
5452
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const apply_chat_response_command = (
2424
return vscode.commands.registerCommand(
2525
'codeWebChat.applyChatResponse',
2626
async (args?: CommandArgs) => {
27+
console.log('xxx', args)
2728
if (!vscode.workspace.workspaceFolders?.length) {
2829
vscode.window.showErrorMessage(
2930
dictionary.error_message.NO_WORKSPACE_FOLDER_OPEN
@@ -132,7 +133,8 @@ export const apply_chat_response_command = (
132133
is_fallback: state.is_fallback,
133134
is_replaced: state.is_replaced,
134135
diff_fallback_method: state.diff_fallback_method,
135-
content: current_content
136+
content: current_content,
137+
is_checked: true
136138
})
137139
}
138140

‎packages/vscode/src/commands/apply-chat-response-command/handlers/restore-review-handler.ts‎

Lines changed: 111 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -80,135 +80,138 @@ export const handle_restore_review = async (
8080
content: original_content,
8181
is_new: !file_exists,
8282
workspace_name: file.workspace_name,
83-
is_deleted: file.is_deleted
83+
is_deleted: file.is_deleted,
84+
is_checked: file.is_checked
8485
})
8586

86-
if (file.is_deleted) {
87-
if (file_exists) {
88-
try {
89-
const tabs_to_close: vscode.Tab[] = []
90-
for (const tab_group of vscode.window.tabGroups.all) {
91-
tabs_to_close.push(
92-
...tab_group.tabs.filter((tab) => {
93-
const tab_uri = (tab.input as any)?.uri as
94-
| vscode.Uri
95-
| undefined
96-
return tab_uri && tab_uri.fsPath === safe_path
97-
})
87+
if (file.is_checked !== false) {
88+
if (file.is_deleted) {
89+
if (file_exists) {
90+
try {
91+
const tabs_to_close: vscode.Tab[] = []
92+
for (const tab_group of vscode.window.tabGroups.all) {
93+
tabs_to_close.push(
94+
...tab_group.tabs.filter((tab) => {
95+
const tab_uri = (tab.input as any)?.uri as
96+
| vscode.Uri
97+
| undefined
98+
return tab_uri && tab_uri.fsPath === safe_path
99+
})
100+
)
101+
}
102+
103+
if (tabs_to_close.length > 0) {
104+
await vscode.window.tabGroups.close(tabs_to_close)
105+
}
106+
107+
fs.unlinkSync(safe_path)
108+
Logger.info({
109+
function_name: 'handle_restore_review',
110+
message: 'File deleted',
111+
data: safe_path
112+
})
113+
await remove_directory_if_empty({
114+
dir_path: path.dirname(safe_path),
115+
workspace_root
116+
})
117+
} catch (error) {
118+
Logger.error({
119+
function_name: 'handle_restore_review',
120+
message: 'Error deleting file',
121+
data: { safe_path, error }
122+
})
123+
vscode.window.showErrorMessage(
124+
`Failed to delete file: ${file.file_path}`
98125
)
99126
}
127+
}
128+
} else if (file.content !== undefined) {
129+
// Create or update
130+
if (file_exists) {
131+
try {
132+
const file_uri = vscode.Uri.file(safe_path)
133+
const document = await vscode.workspace.openTextDocument(file_uri)
134+
const editor = await vscode.window.showTextDocument(document)
135+
let final_content = file.content
136+
if (
137+
original_content.endsWith('\n') &&
138+
!final_content.endsWith('\n')
139+
) {
140+
final_content += '\n'
141+
}
142+
await editor.edit((edit) => {
143+
edit.replace(
144+
new vscode.Range(
145+
document.positionAt(0),
146+
document.positionAt(document.getText().length)
147+
),
148+
final_content
149+
)
150+
})
100151

101-
if (tabs_to_close.length > 0) {
102-
await vscode.window.tabGroups.close(tabs_to_close)
152+
await document.save()
153+
Logger.info({
154+
function_name: 'handle_restore_review',
155+
message: 'Existing file replaced and saved',
156+
data: safe_path
157+
})
158+
} catch (error) {
159+
Logger.error({
160+
function_name: 'handle_restore_review',
161+
message: 'Error updating file',
162+
data: { safe_path, error }
163+
})
164+
vscode.window.showErrorMessage(
165+
`Failed to update file: ${file.file_path}`
166+
)
103167
}
104-
105-
fs.unlinkSync(safe_path)
106-
Logger.info({
107-
function_name: 'handle_restore_review',
108-
message: 'File deleted',
109-
data: safe_path
110-
})
111-
await remove_directory_if_empty({
112-
dir_path: path.dirname(safe_path),
113-
workspace_root
114-
})
115-
} catch (error) {
116-
Logger.error({
117-
function_name: 'handle_restore_review',
118-
message: 'Error deleting file',
119-
data: { safe_path, error }
120-
})
121-
vscode.window.showErrorMessage(
122-
`Failed to delete file: ${file.file_path}`
123-
)
124-
}
125-
}
126-
} else if (file.content !== undefined) {
127-
// Create or update
128-
if (file_exists) {
129-
try {
130-
const file_uri = vscode.Uri.file(safe_path)
131-
const document = await vscode.workspace.openTextDocument(file_uri)
132-
const editor = await vscode.window.showTextDocument(document)
133-
let final_content = file.content
134-
if (
135-
original_content.endsWith('\n') &&
136-
!final_content.endsWith('\n')
137-
) {
138-
final_content += '\n'
168+
} else {
169+
// New file
170+
const directory = path.dirname(safe_path)
171+
if (!fs.existsSync(directory)) {
172+
try {
173+
fs.mkdirSync(directory, { recursive: true })
174+
Logger.info({
175+
function_name: 'handle_restore_review',
176+
message: 'Directory created',
177+
data: directory
178+
})
179+
} catch (error) {
180+
Logger.error({
181+
function_name: 'handle_restore_review',
182+
message: 'Failed to create directory',
183+
data: { directory, error, file_path: file.file_path }
184+
})
185+
vscode.window.showErrorMessage(
186+
dictionary.error_message.FAILED_TO_CREATE_DIRECTORY(
187+
file.file_path
188+
)
189+
)
190+
continue
191+
}
139192
}
140-
await editor.edit((edit) => {
141-
edit.replace(
142-
new vscode.Range(
143-
document.positionAt(0),
144-
document.positionAt(document.getText().length)
145-
),
146-
final_content
147-
)
148-
})
149193

150-
await document.save()
151-
Logger.info({
152-
function_name: 'handle_restore_review',
153-
message: 'Existing file replaced and saved',
154-
data: safe_path
155-
})
156-
} catch (error) {
157-
Logger.error({
158-
function_name: 'handle_restore_review',
159-
message: 'Error updating file',
160-
data: { safe_path, error }
161-
})
162-
vscode.window.showErrorMessage(
163-
`Failed to update file: ${file.file_path}`
164-
)
165-
}
166-
} else {
167-
// New file
168-
const directory = path.dirname(safe_path)
169-
if (!fs.existsSync(directory)) {
170194
try {
171-
fs.mkdirSync(directory, { recursive: true })
195+
fs.writeFileSync(safe_path, file.content)
172196
Logger.info({
173197
function_name: 'handle_restore_review',
174-
message: 'Directory created',
175-
data: directory
198+
message: 'New file created',
199+
data: safe_path
176200
})
201+
const document = await vscode.workspace.openTextDocument(safe_path)
202+
await vscode.window.showTextDocument(document, { preview: false })
177203
} catch (error) {
178204
Logger.error({
179205
function_name: 'handle_restore_review',
180-
message: 'Failed to create directory',
181-
data: { directory, error, file_path: file.file_path }
206+
message: 'Failed to write new file',
207+
data: { safe_path, error, file_path: file.file_path }
182208
})
183209
vscode.window.showErrorMessage(
184-
dictionary.error_message.FAILED_TO_CREATE_DIRECTORY(
185-
file.file_path
186-
)
210+
dictionary.error_message.FAILED_TO_WRITE_FILE(file.file_path)
187211
)
188212
continue
189213
}
190214
}
191-
192-
try {
193-
fs.writeFileSync(safe_path, file.content)
194-
Logger.info({
195-
function_name: 'handle_restore_review',
196-
message: 'New file created',
197-
data: safe_path
198-
})
199-
const document = await vscode.workspace.openTextDocument(safe_path)
200-
await vscode.window.showTextDocument(document, { preview: false })
201-
} catch (error) {
202-
Logger.error({
203-
function_name: 'handle_restore_review',
204-
message: 'Failed to write new file',
205-
data: { safe_path, error, file_path: file.file_path }
206-
})
207-
vscode.window.showErrorMessage(
208-
dictionary.error_message.FAILED_TO_WRITE_FILE(file.file_path)
209-
)
210-
continue
211-
}
212215
}
213216
}
214217
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,23 @@ export const process_chat_response = async (
4646
if (args?.files_with_content) {
4747
const result = await handle_restore_review(args.files_with_content)
4848
if (result.success && result.original_states) {
49+
const augmented_states = result.original_states.map((state) => {
50+
const file_in_review = args?.files_with_content!.find(
51+
(f) =>
52+
f.file_path === state.file_path &&
53+
f.workspace_name === state.workspace_name
54+
)
55+
return { ...state, ai_content: file_in_review?.content }
56+
})
4957
update_undo_button_state({
5058
context,
5159
panel_provider,
52-
states: result.original_states,
60+
states: augmented_states,
5361
applied_content: chat_response,
5462
original_editor_state: args?.original_editor_state
5563
})
5664
return {
57-
original_states: result.original_states,
65+
original_states: augmented_states,
5866
chat_response
5967
}
6068
}

‎packages/vscode/src/commands/apply-chat-response-command/types/original-file-state.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export type OriginalFileState = {
1010
diff_fallback_method?: 'recount' | 'search_and_replace'
1111
is_replaced?: boolean
1212
is_deleted?: boolean
13+
is_checked?: boolean
14+
ai_content?: string
1315
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ export const prepare_files_from_original_states = async (params: {
4040
continue
4141
}
4242

43+
let new_content_for_diff = current_content
44+
if (state.is_checked === false && state.ai_content !== undefined) {
45+
current_content = state.ai_content
46+
new_content_for_diff = state.content
47+
}
48+
4349
const hash = crypto
4450
.createHash('md5')
4551
.update(sanitized_file_path)
@@ -49,7 +55,7 @@ export const prepare_files_from_original_states = async (params: {
4955

5056
const diff_stats = get_diff_stats({
5157
original_content: state.content,
52-
new_content: current_content
58+
new_content: new_content_for_diff
5359
})
5460
const is_deleted =
5561
state.is_deleted ||
@@ -65,7 +71,8 @@ export const prepare_files_from_original_states = async (params: {
6571
lines_removed: diff_stats.lines_removed,
6672
is_fallback: state.is_fallback,
6773
is_replaced: state.is_replaced,
68-
diff_fallback_method: state.diff_fallback_method
74+
diff_fallback_method: state.diff_fallback_method,
75+
is_checked: state.is_checked ?? true
6976
}
7077

7178
prepared_files.push({
@@ -109,7 +116,8 @@ export const prepare_files_from_original_states = async (params: {
109116
is_new: false,
110117
is_deleted: true,
111118
lines_added: restored_diff_stats.lines_added,
112-
lines_removed: restored_diff_stats.lines_removed
119+
lines_removed: restored_diff_stats.lines_removed,
120+
is_checked: state.is_checked ?? true
113121
}
114122

115123
prepared_files.push({

0 commit comments

Comments
 (0)