From f04788c2ec7419bd52059ea35eb176b996ae46f0 Mon Sep 17 00:00:00 2001 From: Sarah Fortune Date: Thu, 17 Jul 2025 11:26:40 -0700 Subject: [PATCH] Move the logic to scroll the diff editor window into the platform specific classes (#4977) * Move the logic to scroll the diff editor window into the platform specific classes. Move the current logic into the VscodeDiffViewProvider. The ExternalDiffViewProvider will use the host bridge to scroll the editor tab. But for now it doesn't do anything. * Add comment * Remove log messages --- src/hosts/vscode/VscodeDiffViewProvider.ts | 20 +++++++ src/integrations/editor/DiffViewProvider.ts | 65 +++++++++++---------- src/standalone/ExternalDiffviewProvider.ts | 9 +++ src/standalone/ExternalWebviewProvider.ts | 5 -- 4 files changed, 64 insertions(+), 35 deletions(-) diff --git a/src/hosts/vscode/VscodeDiffViewProvider.ts b/src/hosts/vscode/VscodeDiffViewProvider.ts index 09b8f0b7..94c82d9c 100644 --- a/src/hosts/vscode/VscodeDiffViewProvider.ts +++ b/src/hosts/vscode/VscodeDiffViewProvider.ts @@ -97,4 +97,24 @@ export class VscodeDiffViewProvider extends DiffViewProvider { this.activeLineController?.setActiveLine(currentLine) this.fadedOverlayController?.updateOverlayAfterLine(currentLine, document.lineCount) } + + override async scrollEditorToLine(line: number): Promise { + if (!this.activeDiffEditor) { + return + } + const scrollLine = line + 4 + this.activeDiffEditor.revealRange(new vscode.Range(scrollLine, 0, scrollLine, 0), vscode.TextEditorRevealType.InCenter) + } + + override async scrollAnimation(startLine: number, endLine: number): Promise { + const totalLines = endLine - startLine + const numSteps = 10 // Adjust this number to control animation speed + const stepSize = Math.max(1, Math.floor(totalLines / numSteps)) + + // Create and await the smooth scrolling animation + for (let line = startLine; line <= endLine; line += stepSize) { + this.activeDiffEditor?.revealRange(new vscode.Range(line, 0, line, 0), vscode.TextEditorRevealType.InCenter) + await new Promise((resolve) => setTimeout(resolve, 16)) // ~60fps + } + } } diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index e75061a8..e70ffd9f 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -62,7 +62,7 @@ export abstract class DiffViewProvider { await fs.writeFile(this.absolutePath, "") } await this.openDiffEditor() - this.scrollEditorToLine(0) // will this crash for new files? + await this.scrollEditorToLine(0) this.streamedLines = [] } @@ -80,13 +80,39 @@ export abstract class DiffViewProvider { */ protected abstract openDiffEditor(): Promise + /** + * Scrolls the diff editor to reveal a specific line. + * + * This abstract method must be implemented by subclasses to handle scrolling + * the diff editor view to ensure a specific line is visible. It's used during + * streaming updates to keep the user's view focused on the changing content. + * + * @param line The 0-based line number to scroll to + * @returns A promise that resolves when the scrolling operation is complete + */ + protected abstract scrollEditorToLine(line: number): Promise + + /** + * Creates a smooth scrolling animation between two lines in the diff editor. + * + * This abstract method must be implemented by subclasses to provide a visual + * animation effect when scrolling through large changes. It's typically used + * when updates contain many lines, to help the user visually track the flow + * of significant changes in the document. + * + * @param startLine The 0-based line number to begin the animation from + * @param endLine The 0-based line number to animate to + * @returns A promise that resolves when the animation is complete + */ + protected abstract scrollAnimation(startLine: number, endLine: number): Promise + async update( accumulatedContent: string, isFinal: boolean, changeLocation?: { startLine: number; endLine: number; startChar: number; endChar: number }, ) { - if (!this.relPath || !this.activeLineController || !this.fadedOverlayController) { - throw new Error("Required values not set") + if (!this.relPath) { + throw new Error("Required value relPath not set") } // --- Fix to prevent duplicate BOM --- @@ -129,30 +155,19 @@ export abstract class DiffViewProvider { if (changeLocation) { // We have the actual location of the change, scroll to it const targetLine = changeLocation.startLine - this.scrollEditorToLine(targetLine) + await this.scrollEditorToLine(targetLine) } else { // Fallback to the old logic for non-replacement updates if (diffLines.length <= 5) { // For small changes, just jump directly to the line - this.scrollEditorToLine(currentLine) + await this.scrollEditorToLine(currentLine) } else { // For larger changes, create a quick scrolling animation const startLine = this.streamedLines.length const endLine = currentLine - const totalLines = endLine - startLine - const numSteps = 10 // Adjust this number to control animation speed - const stepSize = Math.max(1, Math.floor(totalLines / numSteps)) - - // Create and await the smooth scrolling animation - for (let line = startLine; line <= endLine; line += stepSize) { - this.activeDiffEditor?.revealRange( - new vscode.Range(line, 0, line, 0), - vscode.TextEditorRevealType.InCenter, - ) - await new Promise((resolve) => setTimeout(resolve, 16)) // ~60fps - } + await this.scrollAnimation(startLine, endLine) // Ensure we end at the final line - this.scrollEditorToLine(currentLine) + await this.scrollEditorToLine(currentLine) } } } @@ -175,8 +190,8 @@ export abstract class DiffViewProvider { } } // Clear all decorations at the end (before applying final edit) - this.fadedOverlayController.clear() - this.activeLineController.clear() + this.fadedOverlayController?.clear() + this.activeLineController?.clear() } } @@ -354,16 +369,6 @@ export abstract class DiffViewProvider { } } - private scrollEditorToLine(line: number) { - if (this.activeDiffEditor) { - const scrollLine = line + 4 - this.activeDiffEditor.revealRange( - new vscode.Range(scrollLine, 0, scrollLine, 0), - vscode.TextEditorRevealType.InCenter, - ) - } - } - scrollToFirstDiff() { if (!this.activeDiffEditor) { return diff --git a/src/standalone/ExternalDiffviewProvider.ts b/src/standalone/ExternalDiffviewProvider.ts index edfbe347..4e224ecc 100644 --- a/src/standalone/ExternalDiffviewProvider.ts +++ b/src/standalone/ExternalDiffviewProvider.ts @@ -13,6 +13,7 @@ export class ExternalDiffViewProvider extends DiffViewProvider { }) this.activeDiffEditorId = response.diffId } + override async replaceText( content: string, rangeToReplace: { startLine: number; endLine: number }, @@ -25,4 +26,12 @@ export class ExternalDiffViewProvider extends DiffViewProvider { endLine: rangeToReplace.endLine, }) } + + protected override async scrollEditorToLine(line: number): Promise { + console.log(`Called ExternalDiffViewProvider.scrollEditorToLine(${line}) stub`) + } + + override async scrollAnimation(startLine: number, endLine: number): Promise { + console.log(`Called ExternalDiffViewProvider.scrollAnimation(${startLine}, ${endLine}) stub`) + } } diff --git a/src/standalone/ExternalWebviewProvider.ts b/src/standalone/ExternalWebviewProvider.ts index b2384456..10d454db 100644 --- a/src/standalone/ExternalWebviewProvider.ts +++ b/src/standalone/ExternalWebviewProvider.ts @@ -4,11 +4,6 @@ import * as vscode from "vscode" import { URI } from "vscode-uri" import { WebviewProvider } from "@core/webview" -/* -https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts -https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts -*/ - export class ExternalWebviewProvider extends WebviewProvider { // This hostname cannot be changed without updating the external webview handler. private RESOURCE_HOSTNAME: string = "internal.resources"