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
This commit is contained in:
1 parent
5fba39f886
commit
f04788c2ec
4 files changed
+64
-35
No files matched your search
@@ -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<void> {
|
||||
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<void> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<void>
|
||||
|
||||
/**
|
||||
* 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<void>
|
||||
|
||||
/**
|
||||
* 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<void>
|
||||
|
||||
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
|
||||
|
||||
@@ -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<void> {
|
||||
console.log(`Called ExternalDiffViewProvider.scrollEditorToLine(${line}) stub`)
|
||||
}
|
||||
|
||||
override async scrollAnimation(startLine: number, endLine: number): Promise<void> {
|
||||
console.log(`Called ExternalDiffViewProvider.scrollAnimation(${startLine}, ${endLine}) stub`)
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user