From b3fa3ad0d33b22c998d5d1e6e732162d9f2bfdf2 Mon Sep 17 00:00:00 2001 From: Sarah Fortune Date: Thu, 17 Jul 2025 18:36:43 -0700 Subject: [PATCH] Add new RPCs to the host bridge diff service (#4997) * Add truncate document to the hostbridge diff service. # Conflicts: # src/standalone/ExternalDiffviewProvider.ts * Fix typo * Add host bridge rpcs for the diff service * Add saveDocument and closeDiff to the host bridge. * Formatting * Use the host bridge in ExternalDiffviewProvider * Make comments less verbose --- proto/host/diff.proto | 31 +++++++++++++++++-- proto/host/watch.proto | 4 +++ src/hosts/vscode/hostbridge/diff/closeDiff.ts | 5 +++ src/hosts/vscode/hostbridge/diff/openDiff.ts | 2 +- .../vscode/hostbridge/diff/replaceText.ts | 2 +- .../vscode/hostbridge/diff/saveDocument.ts | 5 +++ .../hostbridge/diff/truncateDocument.ts | 5 +++ src/integrations/editor/DiffViewProvider.ts | 12 ++----- src/standalone/ExternalDiffviewProvider.ts | 25 +++++++++++++-- 9 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 src/hosts/vscode/hostbridge/diff/closeDiff.ts create mode 100644 src/hosts/vscode/hostbridge/diff/saveDocument.ts create mode 100644 src/hosts/vscode/hostbridge/diff/truncateDocument.ts diff --git a/proto/host/diff.proto b/proto/host/diff.proto index ef3d5f00..d14071e3 100644 --- a/proto/host/diff.proto +++ b/proto/host/diff.proto @@ -10,7 +10,14 @@ import "common.proto"; service DiffService { // Open the diff view/editor. rpc openDiff(OpenDiffRequest) returns (OpenDiffResponse); + // Replace a text selection in the diff. rpc replaceText(ReplaceTextRequest) returns (ReplaceTextResponse); + // Truncate the diff document. + rpc truncateDocument(TruncateDocumentRequest) returns (TruncateDocumentResponse); + // Save the diff document. + rpc saveDocument(SaveDocumentRequest) returns (SaveDocumentResponse); + // Close the diff editor UI. + rpc closeDiff(CloseDiffRequest) returns (CloseDiffResponse); } message OpenDiffRequest { @@ -34,6 +41,26 @@ message ReplaceTextRequest { optional int32 end_line = 5; } -message ReplaceTextResponse { - // TBD +message ReplaceTextResponse {} + +message TruncateDocumentRequest { + optional cline.Metadata metadata = 1; + optional string diff_id = 2; + optional int32 end_line = 5; } + +message TruncateDocumentResponse {} + +message CloseDiffRequest { + optional cline.Metadata metadata = 1; + optional string diff_id = 2; +} + +message CloseDiffResponse {} + +message SaveDocumentRequest { + optional cline.Metadata metadata = 1; + optional string diff_id = 2; +} + +message SaveDocumentResponse {} diff --git a/proto/host/watch.proto b/proto/host/watch.proto index b9c11bd8..4e4d7e36 100644 --- a/proto/host/watch.proto +++ b/proto/host/watch.proto @@ -6,6 +6,10 @@ option java_multiple_files = true; import "common.proto"; +/** + * The watch service is only here as example of a streaming rpc in the host bridge. + * This being replaced with a native JS file watcher. + */ // WatchService provides methods for watching files in the IDE service WatchService { // Subscribe to file changes diff --git a/src/hosts/vscode/hostbridge/diff/closeDiff.ts b/src/hosts/vscode/hostbridge/diff/closeDiff.ts new file mode 100644 index 00000000..eccdf66e --- /dev/null +++ b/src/hosts/vscode/hostbridge/diff/closeDiff.ts @@ -0,0 +1,5 @@ +import { CloseDiffRequest, CloseDiffResponse } from "@/shared/proto/index.host" + +export async function closeDiff(_request: CloseDiffRequest): Promise { + throw new Error("diffService is not supported. Use the VscodeDiffViewProvider.") +} diff --git a/src/hosts/vscode/hostbridge/diff/openDiff.ts b/src/hosts/vscode/hostbridge/diff/openDiff.ts index 5031328b..850fc525 100644 --- a/src/hosts/vscode/hostbridge/diff/openDiff.ts +++ b/src/hosts/vscode/hostbridge/diff/openDiff.ts @@ -1,5 +1,5 @@ import { OpenDiffRequest, OpenDiffResponse } from "@/shared/proto/index.host" export async function openDiff(_request: OpenDiffRequest): Promise { - throw new Error("diffService.openDiff is not supported. Use the VscodeDiffViewProvider.") + throw new Error("diffService is not supported. Use the VscodeDiffViewProvider.") } diff --git a/src/hosts/vscode/hostbridge/diff/replaceText.ts b/src/hosts/vscode/hostbridge/diff/replaceText.ts index 6696903a..c2e81e5a 100644 --- a/src/hosts/vscode/hostbridge/diff/replaceText.ts +++ b/src/hosts/vscode/hostbridge/diff/replaceText.ts @@ -1,5 +1,5 @@ import { ReplaceTextRequest, ReplaceTextResponse } from "@/shared/proto/index.host" export async function replaceText(_request: ReplaceTextRequest): Promise { - throw new Error("diffService.replaceText is not supported. Use the VscodeDiffViewProvider.") + throw new Error("diffService is not supported. Use the VscodeDiffViewProvider.") } diff --git a/src/hosts/vscode/hostbridge/diff/saveDocument.ts b/src/hosts/vscode/hostbridge/diff/saveDocument.ts new file mode 100644 index 00000000..6ad6b1b4 --- /dev/null +++ b/src/hosts/vscode/hostbridge/diff/saveDocument.ts @@ -0,0 +1,5 @@ +import { ReplaceTextRequest, ReplaceTextResponse, SaveDocumentRequest, SaveDocumentResponse } from "@/shared/proto/index.host" + +export async function saveDocument(_request: SaveDocumentRequest): Promise { + throw new Error("diffService is not supported. Use the VscodeDiffViewProvider.") +} diff --git a/src/hosts/vscode/hostbridge/diff/truncateDocument.ts b/src/hosts/vscode/hostbridge/diff/truncateDocument.ts new file mode 100644 index 00000000..18679832 --- /dev/null +++ b/src/hosts/vscode/hostbridge/diff/truncateDocument.ts @@ -0,0 +1,5 @@ +import { TruncateDocumentRequest, TruncateDocumentResponse } from "@/shared/proto/index.host" + +export async function truncateDocument(_request: TruncateDocumentRequest): Promise { + throw new Error("diffService is not supported. Use the VscodeDiffViewProvider.") +} diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index eb78afe0..c19b106b 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -66,10 +66,6 @@ export abstract class DiffViewProvider { /** * Opens a diff editor or viewer for the current file. * - * This abstract method must be implemented by subclasses to create and display - * a diff editor or viewer that shows the difference between the original and - * modified content. - * * Called automatically by the `open` method after ensuring the file exists and * creating any necessary directories. * @@ -80,9 +76,7 @@ export abstract class DiffViewProvider { /** * 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. + * 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 */ @@ -91,9 +85,7 @@ export abstract class DiffViewProvider { /** * 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 + * 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 diff --git a/src/standalone/ExternalDiffviewProvider.ts b/src/standalone/ExternalDiffviewProvider.ts index 866748cb..ccc85c52 100644 --- a/src/standalone/ExternalDiffviewProvider.ts +++ b/src/standalone/ExternalDiffviewProvider.ts @@ -3,6 +3,7 @@ import { DiffViewProvider } from "@/integrations/editor/DiffViewProvider" export class ExternalDiffViewProvider extends DiffViewProvider { private activeDiffEditorId: string | undefined + override async openDiffEditor(): Promise { if (!this.absolutePath) { return @@ -19,6 +20,9 @@ export class ExternalDiffViewProvider extends DiffViewProvider { rangeToReplace: { startLine: number; endLine: number }, _currentLine: number, ): Promise { + if (!this.activeDiffEditor) { + return + } await getHostBridgeProvider().diffClient.replaceText({ diffId: this.activeDiffEditorId, content: content, @@ -28,7 +32,20 @@ export class ExternalDiffViewProvider extends DiffViewProvider { } protected override async truncateDocument(lineNumber: number): Promise { - console.log(`Called ExternalDiffViewProvider.truncateDocument(${lineNumber}) stub`) + if (!this.activeDiffEditor) { + return + } + await getHostBridgeProvider().diffClient.truncateDocument({ + diffId: this.activeDiffEditorId, + endLine: lineNumber, + }) + } + + protected async saveDocument(): Promise { + if (!this.activeDiffEditor) { + return + } + await getHostBridgeProvider().diffClient.saveDocument({ diffId: this.activeDiffEditorId }) } protected override async scrollEditorToLine(line: number): Promise { @@ -39,7 +56,11 @@ export class ExternalDiffViewProvider extends DiffViewProvider { console.log(`Called ExternalDiffViewProvider.scrollAnimation(${startLine}, ${endLine}) stub`) } protected override async closeDiffView(): Promise { - console.log(`Called ExternalDiffViewProvider.closeDiffView() stub`) + if (!this.activeDiffEditor) { + return + } + await getHostBridgeProvider().diffClient.closeDiff({ diffId: this.activeDiffEditorId }) + this.activeDiffEditorId = undefined } protected override async resetDiffView(): Promise {