Skip to content

Commit 008af04

Browse files
committed
Add active editor state restoration after applying chat response changes
1 parent ffd69b6 commit 008af04

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@ export const preview_handler = async (params: {
113113
original_editor_state: params.original_editor_state
114114
})
115115

116+
if (preview_result.active_editor_state) {
117+
try {
118+
const uri = vscode.Uri.file(
119+
preview_result.active_editor_state.file_path
120+
)
121+
const file_exists = await vscode.workspace.fs.stat(uri).then(
122+
() => true,
123+
() => false
124+
)
125+
126+
if (file_exists) {
127+
const document = await vscode.workspace.openTextDocument(uri)
128+
const editor = await vscode.window.showTextDocument(document, {
129+
preview: false
130+
})
131+
const position = preview_result.active_editor_state.position
132+
editor.selection = new vscode.Selection(position, position)
133+
editor.revealRange(
134+
new vscode.Range(position, position),
135+
vscode.TextEditorRevealType.InCenter
136+
)
137+
}
138+
} catch (error) {
139+
Logger.error({
140+
function_name: 'preview_handler',
141+
message: 'Failed to restore active editor state',
142+
data: error
143+
})
144+
}
145+
}
146+
116147
return true
117148
} else {
118149
update_undo_button_state({

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const preview = async (params: {
2727
accepted_files: ReviewableFile[]
2828
rejected_states: OriginalFileState[]
2929
created_at?: number
30+
active_editor_state?: {
31+
file_path: string
32+
position: vscode.Position
33+
}
3034
} | null> => {
3135
if (!vscode.workspace.workspaceFolders?.length) {
3236
vscode.window.showErrorMessage(
@@ -206,10 +210,29 @@ export const preview = async (params: {
206210
})
207211
.filter((state): state is OriginalFileState => state !== undefined)
208212

213+
let active_editor_state:
214+
| { file_path: string; position: vscode.Position }
215+
| undefined
216+
217+
if (result.active_file_path && result.active_position) {
218+
const active_path = result.active_file_path
219+
const matching_prepared = prepared_files.find(
220+
(pf) => pf.temp_file_path === active_path
221+
)
222+
223+
active_editor_state = {
224+
file_path: matching_prepared
225+
? matching_prepared.sanitized_path
226+
: active_path,
227+
position: result.active_position
228+
}
229+
}
230+
209231
return {
210232
accepted_files,
211233
rejected_states,
212-
created_at: decision.created_at
234+
created_at: decision.created_at,
235+
active_editor_state
213236
}
214237
}
215238

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FileInPreview } from '@shared/types/file-in-preview'
2+
import * as vscode from 'vscode'
23

34
export type CodeReviewDecision =
45
| { jump_to: { file_path: string; workspace_name?: string } }
@@ -8,6 +9,8 @@ export type CodeReviewResult = {
89
decision: CodeReviewDecision
910
new_content: string
1011
temp_file_path: string
12+
active_file_path?: string
13+
active_position?: vscode.Position
1114
}
1215

1316
export type ReviewableFile = FileInPreview & {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,21 @@ export const show_diff_with_actions = async (
5454
final_content = right_doc.getText()
5555
} catch (error) {}
5656

57+
const active_editor = vscode.window.activeTextEditor
58+
let active_file_path: string | undefined
59+
let active_position: vscode.Position | undefined
60+
61+
if (active_editor) {
62+
active_file_path = active_editor.document.uri.fsPath
63+
active_position = active_editor.selection.active
64+
}
65+
5766
resolve({
5867
decision,
5968
new_content: final_content,
60-
temp_file_path: prepared_file.temp_file_path
69+
temp_file_path: prepared_file.temp_file_path,
70+
active_file_path,
71+
active_position
6172
})
6273
}
6374
}).finally(() => {

0 commit comments

Comments
 (0)