From a2817cdf86cc63bb1f50fc31f148b8ef470c7bcd Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 5 Jul 2025 11:27:05 -0700 Subject: [PATCH] autoscroll to the first diff after apply --- .../vscode/src/diff/vertical/handler.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 68c3436e8..339f9e84b 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -174,9 +174,12 @@ export class VerticalDiffHandler implements vscode.Disposable { // Clear deletion buffer await this.insertDeletionBuffer(); - await this.reapplyWithMyersDiff(diffLines); + const myersDiffs = await this.reapplyWithMyersDiff(diffLines); - const range = new vscode.Range(this.startLine, 0, this.startLine, 0); + // Scroll to the first diff + const scrollToLine = + this.getFirstChangedLine(myersDiffs) ?? this.startLine; + const range = new vscode.Range(scrollToLine, 0, scrollToLine, 0); this.editor.revealRange(range, vscode.TextEditorRevealType.Default); this.options.onStatusUpdate( @@ -370,6 +373,8 @@ export class VerticalDiffHandler implements vscode.Disposable { this.editorToVerticalDiffCodeLens.set(this.fileUri, codeLensBlocks); this.refreshCodeLens(); + + return myersDiffs; } private async insertDeletionBuffer() { @@ -550,4 +555,17 @@ export class VerticalDiffHandler implements vscode.Disposable { this.refreshCodeLens(); } + + /** + * Gets the first line number that was changed in a diff + */ + private getFirstChangedLine(diff: DiffLine[]): number | null { + for (let i = 0; i < diff.length; i++) { + const item = diff[i]; + if (item.type === "old" || item.type === "new") { + return this.startLine + i; + } + } + return null; + } }