Skip to content

Commit 003a31e

Browse files
committed
feat: keep diff target tab open on user interaction, mirror scroll position
1 parent e6d25d1 commit 003a31e

3 files changed

Lines changed: 468 additions & 5 deletions

File tree

‎.changeset/fix-diff-scroll-position.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
---
44

55
Fix diff view scroll position and tab handling when applying edits. The diff now opens scrolled to the first changed line (including end-of-file removals, which are clamped to a valid line in the modified document) instead of forcing the viewport to the top. After accepting or rejecting a diff, files that were already open are restored to their pre-edit scroll position, and files that were not open before the edit have their transiently opened tab closed -- unless the user activated that tab during the diff, in which case it is kept open. Focus is no longer pulled back to the edited file when the user has navigated elsewhere. A first file that was open in a preview tab is restored in a preview tab with the original scroll position, even if the target file replaced the preview tab and was automatically closed after the diff was accepted or rejected. A file that was pinned before the edit is re-pinned after the diff closes, so applying a diff no longer drops the tab's pinned state.
6+
7+
When a user clicks or edits inside the diff pane, the target file's tab is kept open after saving -- even if it was not previously open. If the user only scrolls in the diff pane, the existing close behavior is preserved. When the target file is re-revealed after a diff, it scrolls to the position most recently viewed by the user: if the user last scrolled in the diff pane, the target file mirrors that scroll position; if the user last scrolled in the target file's own editor, that position is used instead; otherwise the pre-edit scroll position is restored.

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,27 @@ export class DiffViewProvider {
4444
// diff session. When the file was not already open before the edit, we only
4545
// keep it open afterward if the user explicitly interacted with it.
4646
private userTouchedDocument = false
47+
// Tracks whether the user clicked or edited inside the diff editor itself.
48+
// A selection-change event fires on click/keyboard/edit but NOT on
49+
// scroll, so this reliably distinguishes interaction from passive viewing.
50+
// When true and the user saves (accepts) the diff, the target file tab is
51+
// kept open even if it was not open before the edit began.
52+
private userTouchedDiffEditor = false
53+
// Tracks the most recent scroll position seen in the diff editor. Updated
54+
// continuously so that when the user accepts the diff, the target file can
55+
// be revealed at the same line they were viewing in the diff.
56+
private diffScrollLine: number | undefined
57+
// Tracks the most recent scroll position seen in the target file's own
58+
// editor during the diff session. If the user opens the target file's tab
59+
// and scrolls there, that position overrides the diff scroll line.
60+
private targetFileScrollLine: number | undefined
61+
// Records which editor (diff or the target file itself) the user scrolled
62+
// most recently. The most-recently-scrolled source wins when choosing the
63+
// restore scroll line.
64+
private lastScrolledSource: "diff" | "targetFile" | undefined
4765
private activeEditorListener?: vscode.Disposable
66+
private diffEditorSelectionListener?: vscode.Disposable
67+
private diffScrollListener?: vscode.Disposable
4868
private deferredScrollTimer?: ReturnType<typeof setTimeout>
4969
// Snapshot of unrelated preview tabs (italicized, not-yet-edited) captured at
5070
// diff-open time. Opening the diff reuses the editor group's single preview
@@ -171,6 +191,49 @@ export class DiffViewProvider {
171191
this.userTouchedDocument = true
172192
})
173193
}
194+
195+
// Track whether the user clicks or edits inside the diff editor.
196+
// onDidChangeTextEditorSelection fires on click/keyboard/edit but NOT on
197+
// scroll. We additionally filter to Mouse and Keyboard kinds to exclude
198+
// programmatic selection changes (e.g. the selection anchor set by
199+
// revealDiffLine / scrollToFirstDiff), which VS Code reports with kind
200+
// undefined / Command and must not count as user interaction.
201+
this.userTouchedDiffEditor = false
202+
this.diffEditorSelectionListener = vscode.window.onDidChangeTextEditorSelection((event) => {
203+
if (
204+
this.activeDiffEditor &&
205+
event.textEditor.document === this.activeDiffEditor.document &&
206+
(event.kind === vscode.TextEditorSelectionChangeKind.Mouse ||
207+
event.kind === vscode.TextEditorSelectionChangeKind.Keyboard)
208+
) {
209+
this.userTouchedDiffEditor = true
210+
}
211+
})
212+
213+
// Track scroll position in both the diff editor and the target file's own
214+
// editor. Whichever the user scrolled most recently determines the line we
215+
// reveal when we re-open the target file after the diff closes.
216+
this.diffScrollLine = undefined
217+
this.targetFileScrollLine = undefined
218+
this.lastScrolledSource = undefined
219+
this.diffScrollListener = vscode.window.onDidChangeTextEditorVisibleRanges((event) => {
220+
if (this.activeDiffEditor && event.textEditor.document === this.activeDiffEditor.document) {
221+
const line = event.visibleRanges[0]?.start.line
222+
if (line !== undefined) {
223+
this.diffScrollLine = line
224+
this.lastScrolledSource = "diff"
225+
}
226+
} else if (
227+
event.textEditor.document.uri.scheme === "file" &&
228+
arePathsEqual(event.textEditor.document.uri.fsPath, absolutePath)
229+
) {
230+
const line = event.visibleRanges[0]?.start.line
231+
if (line !== undefined) {
232+
this.targetFileScrollLine = line
233+
this.lastScrolledSource = "targetFile"
234+
}
235+
}
236+
})
174237
}
175238

176239
async update(accumulatedContent: string, isFinal: boolean) {
@@ -277,7 +340,7 @@ export class DiffViewProvider {
277340

278341
await this.closeAllDiffViews()
279342

280-
await this.keepOrCloseEditedFile(absolutePath)
343+
await this.keepOrCloseEditedFile(absolutePath, this.userTouchedDiffEditor)
281344

282345
// Restore any preview tabs the diff evicted, reconstructing the user's
283346
// prior not-yet-edited tab state.
@@ -527,6 +590,10 @@ export class DiffViewProvider {
527590
private disposeActiveEditorListener(): void {
528591
this.activeEditorListener?.dispose()
529592
this.activeEditorListener = undefined
593+
this.diffEditorSelectionListener?.dispose()
594+
this.diffEditorSelectionListener = undefined
595+
this.diffScrollListener?.dispose()
596+
this.diffScrollListener = undefined
530597
}
531598

532599
// Cancel any pending deferred scroll-to-diff. Must run as soon as the user
@@ -544,8 +611,10 @@ export class DiffViewProvider {
544611
// Shared accept/deny cleanup: keep the edited file open if it was already open
545612
// before the edit or the user interacted with it during the diff; otherwise
546613
// close the tab that was opened transiently for the diff.
547-
private async keepOrCloseEditedFile(absolutePath: string): Promise<void> {
548-
if (this.documentWasOpen || this.userTouchedDocument) {
614+
// Pass keepIfTouchedDiff=true (from saveChanges) to also keep the file when
615+
// the user clicked or edited inside the diff editor itself.
616+
private async keepOrCloseEditedFile(absolutePath: string, keepIfTouchedDiff = false): Promise<void> {
617+
if (this.documentWasOpen || this.userTouchedDocument || keepIfTouchedDiff) {
549618
await this.showEditedFileWithoutDisruptingFocus(absolutePath)
550619
} else {
551620
await this.closeFileTab(absolutePath)
@@ -575,9 +644,19 @@ export class DiffViewProvider {
575644
preview: false,
576645
preserveFocus: !this.documentWasPinned,
577646
})
578-
if (this.preEditScrollLine !== undefined) {
647+
// Determine the scroll line to restore. Prefer the most-recently-scrolled
648+
// source: if the user scrolled in the diff, reveal that line; if they
649+
// scrolled in the target file's own editor, use that position; otherwise
650+
// fall back to the scroll position the file had before the edit began.
651+
const restoreScrollLine =
652+
this.lastScrolledSource === "targetFile"
653+
? this.targetFileScrollLine
654+
: this.lastScrolledSource === "diff"
655+
? this.diffScrollLine
656+
: this.preEditScrollLine
657+
if (restoreScrollLine !== undefined) {
579658
editor.revealRange(
580-
new vscode.Range(this.preEditScrollLine, 0, this.preEditScrollLine, 0),
659+
new vscode.Range(restoreScrollLine, 0, restoreScrollLine, 0),
581660
vscode.TextEditorRevealType.AtTop,
582661
)
583662
}
@@ -953,7 +1032,11 @@ export class DiffViewProvider {
9531032
this.streamedLines = []
9541033
this.preDiagnostics = []
9551034
this.preEditScrollLine = undefined
1035+
this.diffScrollLine = undefined
1036+
this.targetFileScrollLine = undefined
1037+
this.lastScrolledSource = undefined
9561038
this.userTouchedDocument = false
1039+
this.userTouchedDiffEditor = false
9571040
this.snapshotPreviewTabs = []
9581041
this.disposeActiveEditorListener()
9591042
}

0 commit comments

Comments
 (0)