Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 0605187

Browse files
committed
feat: restore editor scroll position after closing diff view
- Capture visible ranges from diff editor before closing - Restore scroll position when showing text document - Applies to both saveChanges() and revertChanges() methods - Added comprehensive tests for scroll position preservation Fixes #9282
1 parent 4e316e0 commit 0605187

2 files changed

Lines changed: 147 additions & 3 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,24 @@ export class DiffViewProvider {
206206
const updatedDocument = this.activeDiffEditor.document
207207
const editedContent = updatedDocument.getText()
208208

209+
// Capture the visible ranges before closing the diff view to restore scroll position later
210+
const visibleRanges = this.activeDiffEditor.visibleRanges
211+
209212
if (updatedDocument.isDirty) {
210213
await updatedDocument.save()
211214
}
212215

213-
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true })
216+
const editor = await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
217+
preview: false,
218+
preserveFocus: true,
219+
})
214220
await this.closeAllDiffViews()
215221

222+
// Restore the scroll position from the diff view
223+
if (visibleRanges && visibleRanges.length > 0) {
224+
editor.revealRange(visibleRanges[0], vscode.TextEditorRevealType.InCenter)
225+
}
226+
216227
// Getting diagnostics before and after the file edit is a better approach than
217228
// automatically tracking problems in real-time. This method ensures we only
218229
// report new problems that are a direct result of this specific edit.
@@ -378,6 +389,9 @@ export class DiffViewProvider {
378389
const updatedDocument = this.activeDiffEditor.document
379390
const absolutePath = path.resolve(this.cwd, this.relPath)
380391

392+
// Capture the visible ranges before closing the diff view to restore scroll position later
393+
const visibleRanges = this.activeDiffEditor.visibleRanges
394+
381395
if (!fileExists) {
382396
if (updatedDocument.isDirty) {
383397
await updatedDocument.save()
@@ -408,10 +422,15 @@ export class DiffViewProvider {
408422
await updatedDocument.save()
409423

410424
if (this.documentWasOpen) {
411-
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
425+
const editor = await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
412426
preview: false,
413427
preserveFocus: true,
414428
})
429+
430+
// Restore the scroll position from the diff view
431+
if (visibleRanges && visibleRanges.length > 0) {
432+
editor.revealRange(visibleRanges[0], vscode.TextEditorRevealType.InCenter)
433+
}
415434
}
416435

417436
await this.closeAllDiffViews()

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,14 @@ describe("DiffViewProvider", () => {
450450
isDirty: false,
451451
save: vi.fn().mockResolvedValue(undefined),
452452
},
453+
visibleRanges: [{ start: { line: 10 }, end: { line: 20 } }],
453454
}
454455
;(diffViewProvider as any).preDiagnostics = []
455456

456457
// Mock vscode functions
457-
vi.mocked(vscode.window.showTextDocument).mockResolvedValue({} as any)
458+
vi.mocked(vscode.window.showTextDocument).mockResolvedValue({
459+
revealRange: vi.fn(),
460+
} as any)
458461
vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([])
459462
})
460463

@@ -517,4 +520,126 @@ describe("DiffViewProvider", () => {
517520
expect(vscode.languages.getDiagnostics).toHaveBeenCalled()
518521
})
519522
})
523+
524+
describe("scroll position preservation", () => {
525+
let mockEditor: any
526+
527+
beforeEach(() => {
528+
// Setup common mocks for scroll position tests
529+
mockEditor = {
530+
revealRange: vi.fn(),
531+
}
532+
;(diffViewProvider as any).relPath = "test.ts"
533+
;(diffViewProvider as any).newContent = "new content"
534+
;(diffViewProvider as any).originalContent = "original content"
535+
;(diffViewProvider as any).documentWasOpen = true
536+
;(diffViewProvider as any).editType = "modify"
537+
;(diffViewProvider as any).activeDiffEditor = {
538+
document: {
539+
getText: vi.fn().mockReturnValue("new content"),
540+
isDirty: false,
541+
save: vi.fn().mockResolvedValue(undefined),
542+
uri: { fsPath: `${mockCwd}/test.ts` },
543+
positionAt: vi.fn((offset) => ({ line: 0, character: offset })),
544+
},
545+
visibleRanges: [{ start: { line: 15 }, end: { line: 25 } }],
546+
}
547+
;(diffViewProvider as any).preDiagnostics = []
548+
549+
// Mock vscode functions
550+
vi.mocked(vscode.window.showTextDocument).mockResolvedValue(mockEditor)
551+
vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([])
552+
;(diffViewProvider as any).closeAllDiffViews = vi.fn().mockResolvedValue(undefined)
553+
554+
// Mock WorkspaceEdit
555+
const mockWorkspaceEdit = {
556+
replace: vi.fn(),
557+
}
558+
vi.mocked(vscode.WorkspaceEdit).mockImplementation(() => mockWorkspaceEdit as any)
559+
vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true)
560+
})
561+
562+
it("should restore scroll position in saveChanges", async () => {
563+
const result = await diffViewProvider.saveChanges(false, 0)
564+
565+
// Verify the editor was shown
566+
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
567+
expect.objectContaining({ fsPath: `${mockCwd}/test.ts` }),
568+
{ preview: false, preserveFocus: true },
569+
)
570+
571+
// Verify scroll position was restored
572+
expect(mockEditor.revealRange).toHaveBeenCalledWith(
573+
{ start: { line: 15 }, end: { line: 25 } },
574+
vscode.TextEditorRevealType.InCenter,
575+
)
576+
577+
expect(result.newProblemsMessage).toBe("")
578+
})
579+
580+
it("should restore scroll position in revertChanges for existing file", async () => {
581+
const result = await diffViewProvider.revertChanges()
582+
583+
// Verify the editor was shown (since documentWasOpen was true)
584+
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
585+
expect.objectContaining({ fsPath: `${mockCwd}/test.ts` }),
586+
{ preview: false, preserveFocus: true },
587+
)
588+
589+
// Verify scroll position was restored
590+
expect(mockEditor.revealRange).toHaveBeenCalledWith(
591+
{ start: { line: 15 }, end: { line: 25 } },
592+
vscode.TextEditorRevealType.InCenter,
593+
)
594+
})
595+
596+
it("should handle missing visible ranges gracefully", async () => {
597+
// Remove visible ranges
598+
;(diffViewProvider as any).activeDiffEditor.visibleRanges = undefined
599+
600+
const result = await diffViewProvider.saveChanges(false, 0)
601+
602+
// Verify the editor was shown
603+
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
604+
expect.objectContaining({ fsPath: `${mockCwd}/test.ts` }),
605+
{ preview: false, preserveFocus: true },
606+
)
607+
608+
// Verify revealRange was NOT called
609+
expect(mockEditor.revealRange).not.toHaveBeenCalled()
610+
611+
expect(result.newProblemsMessage).toBe("")
612+
})
613+
614+
it("should handle empty visible ranges array gracefully", async () => {
615+
// Set empty visible ranges
616+
;(diffViewProvider as any).activeDiffEditor.visibleRanges = []
617+
618+
const result = await diffViewProvider.saveChanges(false, 0)
619+
620+
// Verify the editor was shown
621+
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
622+
expect.objectContaining({ fsPath: `${mockCwd}/test.ts` }),
623+
{ preview: false, preserveFocus: true },
624+
)
625+
626+
// Verify revealRange was NOT called
627+
expect(mockEditor.revealRange).not.toHaveBeenCalled()
628+
629+
expect(result.newProblemsMessage).toBe("")
630+
})
631+
632+
it("should not restore scroll position if document was not previously open", async () => {
633+
// Set documentWasOpen to false
634+
;(diffViewProvider as any).documentWasOpen = false
635+
636+
await diffViewProvider.revertChanges()
637+
638+
// Verify the editor was NOT shown (since documentWasOpen was false)
639+
expect(vscode.window.showTextDocument).not.toHaveBeenCalled()
640+
641+
// Verify revealRange was NOT called
642+
expect(mockEditor.revealRange).not.toHaveBeenCalled()
643+
})
644+
})
520645
})

0 commit comments

Comments
 (0)