Skip to content

Commit 3967d26

Browse files
committed
Transition to workspace-level edits and remove automatic document opening across chat response handlers
1 parent 5c18433 commit 3967d26

6 files changed

Lines changed: 41 additions & 58 deletions

File tree

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ export const handle_conflict_markers = async (
200200
}
201201

202202
await fs.promises.writeFile(safe_path, content_to_write, 'utf8')
203-
const document = await vscode.workspace.openTextDocument(safe_path)
204-
await vscode.window.showTextDocument(document, { preview: false })
205203

206204
original_states.push({
207205
file_path: file.file_path,
@@ -217,23 +215,23 @@ export const handle_conflict_markers = async (
217215
const document = await vscode.workspace.openTextDocument(safe_path)
218216
const original_content = document.getText()
219217

220-
const editor = await vscode.window.showTextDocument(document)
221218
let final_content = file.content
222219
if (
223220
original_content.endsWith('\n') &&
224221
!final_content.endsWith('\n')
225222
) {
226223
final_content += '\n'
227224
}
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-
})
225+
const edit = new vscode.WorkspaceEdit()
226+
edit.replace(
227+
document.uri,
228+
new vscode.Range(
229+
document.positionAt(0),
230+
document.positionAt(document.getText().length)
231+
),
232+
final_content
233+
)
234+
await vscode.workspace.applyEdit(edit)
237235
await document.save()
238236

239237
original_states.push({

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@ const handle_new_file_patch = async (
307307

308308
await fs.promises.writeFile(safe_path, new_content, 'utf8')
309309

310-
const document = await vscode.workspace.openTextDocument(safe_path)
311-
await vscode.window.showTextDocument(document, { preview: false })
312-
313310
return { success: true, original_states }
314311
} catch (error: any) {
315312
Logger.error({

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ export const handle_fast_replace = async (
162162
Buffer.from(new_content, 'utf8')
163163
)
164164

165-
const document = await vscode.workspace.openTextDocument(safe_path)
166-
await vscode.window.showTextDocument(document, { preview: false })
167-
168165
original_states.push({
169166
file_path: file.file_path,
170167
content: rename_source_content,
@@ -201,23 +198,24 @@ export const handle_fast_replace = async (
201198
content: original_content,
202199
workspace_name: file.workspace_name
203200
})
204-
const editor = await vscode.window.showTextDocument(document)
205201
let final_content = file.content
206202
if (
207203
original_content.endsWith('\n') &&
208204
!final_content.endsWith('\n')
209205
) {
210206
final_content += '\n'
211207
}
212-
await editor.edit((edit) => {
213-
edit.replace(
214-
new vscode.Range(
215-
document.positionAt(0),
216-
document.positionAt(document.getText().length)
217-
),
218-
final_content
219-
)
220-
})
208+
209+
const edit = new vscode.WorkspaceEdit()
210+
edit.replace(
211+
document.uri,
212+
new vscode.Range(
213+
document.positionAt(0),
214+
document.positionAt(document.getText().length)
215+
),
216+
final_content
217+
)
218+
await vscode.workspace.applyEdit(edit)
221219

222220
await document.save()
223221
Logger.info({
@@ -263,8 +261,6 @@ export const handle_fast_replace = async (
263261
message: 'New file created',
264262
data: safe_path
265263
})
266-
const document = await vscode.workspace.openTextDocument(safe_path)
267-
await vscode.window.showTextDocument(document, { preview: false })
268264
} catch (error) {
269265
Logger.error({
270266
function_name: 'handle_fast_replace',

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,16 @@ export const handle_intelligent_update = async (params: {
532532
continue
533533
}
534534
try {
535-
const editor = await vscode.window.showTextDocument(document)
536-
await editor.edit((edit) => {
537-
edit.replace(
538-
new vscode.Range(
539-
document.positionAt(0),
540-
document.positionAt(document.getText().length)
541-
),
542-
change.content
543-
)
544-
})
535+
const edit = new vscode.WorkspaceEdit()
536+
edit.replace(
537+
document.uri,
538+
new vscode.Range(
539+
document.positionAt(0),
540+
document.positionAt(document.getText().length)
541+
),
542+
change.content
543+
)
544+
await vscode.workspace.applyEdit(edit)
545545
await document.save()
546546
} catch (error) {
547547
Logger.error({

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,23 @@ export const handle_restore_preview = async (
136136
try {
137137
const file_uri = vscode.Uri.file(safe_path)
138138
const document = await vscode.workspace.openTextDocument(file_uri)
139-
const editor = await vscode.window.showTextDocument(document)
140139
let final_content = file.content
141140
if (
142141
original_content.endsWith('\n') &&
143142
!final_content.endsWith('\n')
144143
) {
145144
final_content += '\n'
146145
}
147-
await editor.edit((edit) => {
148-
edit.replace(
149-
new vscode.Range(
150-
document.positionAt(0),
151-
document.positionAt(document.getText().length)
152-
),
153-
final_content
154-
)
155-
})
146+
const edit = new vscode.WorkspaceEdit()
147+
edit.replace(
148+
document.uri,
149+
new vscode.Range(
150+
document.positionAt(0),
151+
document.positionAt(document.getText().length)
152+
),
153+
final_content
154+
)
155+
await vscode.workspace.applyEdit(edit)
156156

157157
await document.save()
158158
Logger.info({
@@ -202,8 +202,6 @@ export const handle_restore_preview = async (
202202
message: 'New file created',
203203
data: safe_path
204204
})
205-
const document = await vscode.workspace.openTextDocument(safe_path)
206-
await vscode.window.showTextDocument(document, { preview: false })
207205
} catch (error) {
208206
Logger.error({
209207
function_name: 'handle_restore_preview',

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ export const create_file_if_needed = async (params: {
152152
message: 'File created',
153153
data: safe_path
154154
})
155-
const document = await vscode.workspace.openTextDocument(safe_path)
156-
await vscode.window.showTextDocument(document, { preview: false })
157155
return true
158156
} catch (error) {
159157
Logger.error({
@@ -228,10 +226,6 @@ const relocate_file = async (params: {
228226
workspace_root: params.workspace_root
229227
})
230228

231-
// Open the file at new location
232-
const document = await vscode.workspace.openTextDocument(new_safe_path)
233-
await vscode.window.showTextDocument(document)
234-
235229
Logger.info({
236230
function_name: 'relocate_file',
237231
message: 'File successfully relocated',

0 commit comments

Comments
 (0)