@@ -17,6 +17,7 @@ export class DiffViewProvider {
17
17
originalContent : string | undefined
18
18
private createdDirs : string [ ] = [ ]
19
19
private documentWasOpen = false
20
+ private originalViewColumn ?: vscode . ViewColumn // Store the original view column
20
21
private relPath ?: string
21
22
private newContent ?: string
22
23
private activeDiffEditor ?: vscode . TextEditor
@@ -65,11 +66,22 @@ export class DiffViewProvider {
65
66
. filter (
66
67
( tab ) => tab . input instanceof vscode . TabInputText && arePathsEqual ( tab . input . uri . fsPath , absolutePath ) ,
67
68
)
69
+ // Check if the document is already open and store its state
70
+ // DO NOT close the original tab to preserve pin status
68
71
for ( const tab of tabs ) {
69
- if ( ! tab . isDirty ) {
70
- await vscode . window . tabGroups . close ( tab )
72
+ if ( tab . input instanceof vscode . TabInputText && arePathsEqual ( tab . input . uri . fsPath , absolutePath ) ) {
73
+ this . originalViewColumn = tab . group . viewColumn
74
+ this . documentWasOpen = true
75
+ // Ensure the tab is not dirty before proceeding, but don't close it
76
+ if ( tab . isDirty ) {
77
+ // Find the document associated with the tab and save it
78
+ const doc = vscode . workspace . textDocuments . find ( ( d ) => arePathsEqual ( d . uri . fsPath , absolutePath ) )
79
+ if ( doc ) {
80
+ await doc . save ( )
81
+ }
82
+ }
83
+ break // Found the relevant tab, no need to check others
71
84
}
72
- this . documentWasOpen = true
73
85
}
74
86
this . activeDiffEditor = await this . openDiffEditor ( )
75
87
this . fadedOverlayController = new DecorationController ( "fadedOverlay" , this . activeDiffEditor )
@@ -156,9 +168,31 @@ export class DiffViewProvider {
156
168
await updatedDocument . save ( )
157
169
}
158
170
159
- await vscode . window . showTextDocument ( vscode . Uri . file ( absolutePath ) , { preview : false } )
171
+ // Close the diff view first
160
172
await this . closeAllDiffViews ( )
161
173
174
+ // If the original document was open, try to focus it.
175
+ // VS Code should handle showing the updated content automatically since the file was saved.
176
+ if ( this . documentWasOpen && this . originalViewColumn ) {
177
+ // Find the editor for the original document and reveal it
178
+ const originalEditor = vscode . window . visibleTextEditors . find (
179
+ ( editor ) =>
180
+ arePathsEqual ( editor . document . uri . fsPath , absolutePath ) &&
181
+ editor . viewColumn === this . originalViewColumn ,
182
+ )
183
+ if ( originalEditor ) {
184
+ // Reveal a range (e.g., the start) to ensure focus
185
+ const position = new vscode . Position ( 0 , 0 )
186
+ originalEditor . revealRange ( new vscode . Range ( position , position ) , vscode . TextEditorRevealType . AtTop )
187
+ } else {
188
+ // Fallback if editor not found (shouldn't happen often if documentWasOpen is true)
189
+ await vscode . window . showTextDocument ( vscode . Uri . file ( absolutePath ) , {
190
+ preview : false ,
191
+ viewColumn : this . originalViewColumn ,
192
+ } )
193
+ }
194
+ }
195
+
162
196
/*
163
197
Getting diagnostics before and after the file edit is a better approach than
164
198
automatically tracking problems in real-time. This method ensures we only
@@ -237,12 +271,28 @@ export class DiffViewProvider {
237
271
await vscode . workspace . applyEdit ( edit )
238
272
await updatedDocument . save ( )
239
273
console . log ( `File ${ absolutePath } has been reverted to its original content.` )
240
- if ( this . documentWasOpen ) {
241
- await vscode . window . showTextDocument ( vscode . Uri . file ( absolutePath ) , {
242
- preview : false ,
243
- } )
244
- }
274
+ // Close the diff view first
245
275
await this . closeAllDiffViews ( )
276
+
277
+ // If the document was originally open, ensure it's focused.
278
+ // The revert logic already applied the original content and saved.
279
+ if ( this . documentWasOpen && this . originalViewColumn ) {
280
+ const originalEditor = vscode . window . visibleTextEditors . find (
281
+ ( editor ) =>
282
+ arePathsEqual ( editor . document . uri . fsPath , absolutePath ) &&
283
+ editor . viewColumn === this . originalViewColumn ,
284
+ )
285
+ if ( originalEditor ) {
286
+ const position = new vscode . Position ( 0 , 0 )
287
+ originalEditor . revealRange ( new vscode . Range ( position , position ) , vscode . TextEditorRevealType . AtTop )
288
+ } else {
289
+ // Fallback
290
+ await vscode . window . showTextDocument ( vscode . Uri . file ( absolutePath ) , {
291
+ preview : false ,
292
+ viewColumn : this . originalViewColumn ,
293
+ } )
294
+ }
295
+ }
246
296
}
247
297
248
298
// edit is done
@@ -358,6 +408,7 @@ export class DiffViewProvider {
358
408
this . originalContent = undefined
359
409
this . createdDirs = [ ]
360
410
this . documentWasOpen = false
411
+ this . originalViewColumn = undefined // Reset stored view column
361
412
this . activeDiffEditor = undefined
362
413
this . fadedOverlayController = undefined
363
414
this . activeLineController = undefined
0 commit comments