Skip to content

Commit 5c18433

Browse files
committed
Refine file handling logic to prevent automatic deletion of empty files and enable parsing of files containing only comments or whitespace
1 parent 3ac66fa commit 5c18433

8 files changed

Lines changed: 94 additions & 297 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,10 @@ export const apply_chat_response_command = (params: {
184184
}
185185

186186
let current_content = ''
187+
let file_exists = false
187188
try {
188189
if (fs.existsSync(sanitized_file_path)) {
190+
file_exists = true
189191
const document =
190192
await vscode.workspace.openTextDocument(sanitized_file_path)
191193
current_content = document.getText()
@@ -205,9 +207,7 @@ export const apply_chat_response_command = (params: {
205207
total_lines_removed += diff_stats.lines_removed
206208

207209
const is_deleted =
208-
state.file_state != 'new' &&
209-
current_content == '' &&
210-
state.content != ''
210+
state.file_state != 'new' && !file_exists && state.content != ''
211211

212212
files_for_history.push({
213213
type: 'file',

‎packages/vscode/src/commands/apply-chat-response-command/handlers/conflict-markers-handler.ts‎

Lines changed: 49 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ export const handle_conflict_markers = async (
102102
file.content
103103
)
104104
} else {
105-
if (file.content !== '') {
106-
new_content = file.content
107-
}
105+
new_content = file.content
108106
}
109107

110108
const tabs_to_close: vscode.Tab[] = []
@@ -129,31 +127,21 @@ export const handle_conflict_markers = async (
129127
workspace_root
130128
})
131129

132-
if (new_content.trim() == '') {
133-
original_states.push({
134-
file_path: file.file_path,
135-
content: rename_source_content,
136-
workspace_name: file.workspace_name,
137-
file_path_to_restore: file.renamed_from,
138-
file_state: 'deleted'
139-
})
140-
} else {
141-
const directory = path.dirname(safe_path)
142-
if (!fs.existsSync(directory)) {
143-
await fs.promises.mkdir(directory, { recursive: true })
144-
}
145-
await vscode.workspace.fs.writeFile(
146-
vscode.Uri.file(safe_path),
147-
Buffer.from(new_content, 'utf8')
148-
)
149-
150-
original_states.push({
151-
file_path: file.file_path,
152-
content: rename_source_content,
153-
workspace_name: file.workspace_name,
154-
file_path_to_restore: file.renamed_from
155-
})
130+
const directory = path.dirname(safe_path)
131+
if (!fs.existsSync(directory)) {
132+
await fs.promises.mkdir(directory, { recursive: true })
156133
}
134+
await vscode.workspace.fs.writeFile(
135+
vscode.Uri.file(safe_path),
136+
Buffer.from(new_content, 'utf8')
137+
)
138+
139+
original_states.push({
140+
file_path: file.file_path,
141+
content: rename_source_content,
142+
workspace_name: file.workspace_name,
143+
file_path_to_restore: file.renamed_from
144+
})
157145
} catch (error) {
158146
failed_files.push(file)
159147
}
@@ -166,50 +154,19 @@ export const handle_conflict_markers = async (
166154
file.content
167155
)
168156

169-
if (current_content.trim() == '') {
170-
const tabs_to_close: vscode.Tab[] = []
171-
for (const tab_group of vscode.window.tabGroups.all) {
172-
tabs_to_close.push(
173-
...tab_group.tabs.filter((tab) => {
174-
const tab_uri = (tab.input as any)?.uri as
175-
| vscode.Uri
176-
| undefined
177-
return tab_uri && tab_uri.fsPath === safe_path
178-
})
179-
)
180-
}
181-
182-
if (tabs_to_close.length > 0) {
183-
await vscode.window.tabGroups.close(tabs_to_close)
184-
}
185-
186-
await vscode.workspace.fs.delete(vscode.Uri.file(safe_path))
187-
await remove_directory_if_empty({
188-
dir_path: path.dirname(safe_path),
189-
workspace_root
190-
})
191-
192-
original_states.push({
193-
file_path: file.file_path,
194-
content: original_content,
195-
workspace_name: file.workspace_name,
196-
file_state: 'deleted'
197-
})
198-
} else {
199-
if (current_content !== original_content) {
200-
await vscode.workspace.fs.writeFile(
201-
vscode.Uri.file(safe_path),
202-
Buffer.from(current_content, 'utf8')
203-
)
204-
}
205-
206-
original_states.push({
207-
file_path: file.file_path,
208-
content: original_content,
209-
workspace_name: file.workspace_name
210-
})
157+
if (current_content !== original_content) {
158+
await vscode.workspace.fs.writeFile(
159+
vscode.Uri.file(safe_path),
160+
Buffer.from(current_content, 'utf8')
161+
)
211162
}
212163

164+
original_states.push({
165+
file_path: file.file_path,
166+
content: original_content,
167+
workspace_name: file.workspace_name
168+
})
169+
213170
Logger.info({
214171
function_name: 'handle_conflict_markers',
215172
message: 'Applied conflict markers edit to file',
@@ -224,16 +181,6 @@ export const handle_conflict_markers = async (
224181
failed_files.push(file)
225182
}
226183
} else if (!file_exists) {
227-
if (file.content == '') {
228-
original_states.push({
229-
file_path: file.file_path,
230-
content: '',
231-
file_state: 'new',
232-
workspace_name: file.workspace_name
233-
})
234-
continue
235-
}
236-
237184
try {
238185
const directory = path.dirname(safe_path)
239186
if (!fs.existsSync(directory)) {
@@ -270,61 +217,30 @@ export const handle_conflict_markers = async (
270217
const document = await vscode.workspace.openTextDocument(safe_path)
271218
const original_content = document.getText()
272219

273-
if (file.content.trim() == '') {
274-
const tabs_to_close: vscode.Tab[] = []
275-
for (const tab_group of vscode.window.tabGroups.all) {
276-
tabs_to_close.push(
277-
...tab_group.tabs.filter((tab) => {
278-
const tab_uri = (tab.input as any)?.uri as
279-
| vscode.Uri
280-
| undefined
281-
return tab_uri && tab_uri.fsPath === safe_path
282-
})
283-
)
284-
}
285-
286-
if (tabs_to_close.length > 0) {
287-
await vscode.window.tabGroups.close(tabs_to_close)
288-
}
289-
290-
await vscode.workspace.fs.delete(vscode.Uri.file(safe_path))
291-
await remove_directory_if_empty({
292-
dir_path: path.dirname(safe_path),
293-
workspace_root
294-
})
295-
296-
original_states.push({
297-
file_path: file.file_path,
298-
content: original_content,
299-
workspace_name: file.workspace_name,
300-
file_state: 'deleted'
301-
})
302-
} else {
303-
const editor = await vscode.window.showTextDocument(document)
304-
let final_content = file.content
305-
if (
306-
original_content.endsWith('\n') &&
307-
!final_content.endsWith('\n')
308-
) {
309-
final_content += '\n'
310-
}
311-
await editor.edit((edit) => {
312-
edit.replace(
313-
new vscode.Range(
314-
document.positionAt(0),
315-
document.positionAt(document.getText().length)
316-
),
317-
final_content
318-
)
319-
})
320-
await document.save()
321-
322-
original_states.push({
323-
file_path: file.file_path,
324-
content: original_content,
325-
workspace_name: file.workspace_name
326-
})
220+
const editor = await vscode.window.showTextDocument(document)
221+
let final_content = file.content
222+
if (
223+
original_content.endsWith('\n') &&
224+
!final_content.endsWith('\n')
225+
) {
226+
final_content += '\n'
327227
}
228+
await editor.edit((edit) => {
229+
edit.replace(
230+
new vscode.Range(
231+
document.positionAt(0),
232+
document.positionAt(document.getText().length)
233+
),
234+
final_content
235+
)
236+
})
237+
await document.save()
238+
239+
original_states.push({
240+
file_path: file.file_path,
241+
content: original_content,
242+
workspace_name: file.workspace_name
243+
})
328244
} catch (error: any) {
329245
Logger.error({
330246
function_name: 'handle_conflict_markers',

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -237,43 +237,7 @@ async function process_modified_files(
237237
continue
238238
}
239239

240-
if (fs.existsSync(safe_path)) {
241-
const document = await vscode.workspace.openTextDocument(safe_path)
242-
if (document.getText().trim() == '') {
243-
try {
244-
const uri = vscode.Uri.file(safe_path)
245-
const text_editors = vscode.window.visibleTextEditors.filter(
246-
(editor) => editor.document.uri.toString() == uri.toString()
247-
)
248-
for (const editor of text_editors) {
249-
await vscode.window.showTextDocument(editor.document, {
250-
preview: false,
251-
preserveFocus: false
252-
})
253-
await vscode.commands.executeCommand(
254-
'workbench.action.closeActiveEditor'
255-
)
256-
}
257-
258-
fs.unlinkSync(safe_path)
259-
Logger.info({
260-
function_name: 'process_modified_files',
261-
message: 'File was empty after patch, removed from disk.',
262-
data: { file_path }
263-
})
264-
await remove_directory_if_empty({
265-
dir_path: path.dirname(safe_path),
266-
workspace_root: workspace_path
267-
})
268-
} catch (error) {
269-
Logger.error({
270-
function_name: 'process_modified_files',
271-
message: 'Error deleting empty file',
272-
data: { file_path, error }
273-
})
274-
}
275-
}
276-
} else {
240+
if (!fs.existsSync(safe_path)) {
277241
Logger.info({
278242
function_name: 'process_modified_files',
279243
message:

0 commit comments

Comments
 (0)