From c013b4f3298bfec99456d1af281ea9ab485d4055 Mon Sep 17 00:00:00 2001 From: Andrei Eternal Date: Mon, 30 Jun 2025 15:39:09 -1000 Subject: [PATCH] [PROTOBUS] showTextDocument host bridge (#4586) * showTextDocument host bridge * format fix * sjf review cleanups * prefer paths over URI based on chats with sjf * also document_uri -> document_path * remove unused metadata import --------- Co-authored-by: Andrei Edell --- .gitignore | 3 +- proto/build-proto-config.js | 1 + proto/host/window.proto | 32 ++++++++++++++ src/core/controller/file/getRelativePaths.ts | 3 +- src/hosts/host-provider-types.ts | 2 + src/hosts/vscode/client/host-grpc-client.ts | 1 + src/hosts/vscode/window/showTextDocument.ts | 26 +++++++++++ src/integrations/editor/DiffViewProvider.ts | 44 ++++++++++++++----- .../git/commit-message-generator.ts | 8 +++- src/integrations/misc/export-markdown.ts | 9 +++- src/integrations/misc/open-file.ts | 9 +++- src/services/mcp/McpHub.ts | 1 - src/standalone/host-bridge-client-manager.ts | 4 ++ 13 files changed, 125 insertions(+), 18 deletions(-) create mode 100644 proto/host/window.proto create mode 100644 src/hosts/vscode/window/showTextDocument.ts diff --git a/.gitignore b/.gitignore index 4dfad92c..5915653b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ tmp *.vsix .DS_Store +.idea pnpm-lock.yaml @@ -37,4 +38,4 @@ src/hosts/vscode/*/methods.ts src/hosts/vscode/*/index.ts src/hosts/vscode/client/host-grpc-client.ts src/hosts/vscode/host-grpc-service-config.ts -src/standalone/server-setup.ts \ No newline at end of file +src/standalone/server-setup.ts diff --git a/proto/build-proto-config.js b/proto/build-proto-config.js index 0fee8494..cd8b4b7c 100644 --- a/proto/build-proto-config.js +++ b/proto/build-proto-config.js @@ -26,5 +26,6 @@ export const hostServiceNameMap = { watch: "host.WatchService", workspace: "host.WorkspaceService", env: "host.EnvService", + window: "host.WindowService", // Add new host services here } diff --git a/proto/host/window.proto b/proto/host/window.proto new file mode 100644 index 00000000..820a78c5 --- /dev/null +++ b/proto/host/window.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +package host; +option java_package = "bot.cline.host.proto"; +option java_multiple_files = true; + +import "common.proto"; + +// Provides methods for working with IDE windows and editors. +service WindowService { + // Opens a text document in the editor and returns editor information. + rpc showTextDocument(ShowTextDocumentRequest) returns (TextEditorInfo); +} + +message ShowTextDocumentRequest { + cline.Metadata metadata = 1; + string path = 2; + optional ShowTextDocumentOptions options = 3; +} + +// See https://code.visualstudio.com/api/references/vscode-api#TextDocumentShowOptions +message ShowTextDocumentOptions { + optional bool preview = 1; + optional bool preserve_focus = 2; + optional int32 view_column = 3; +} + +message TextEditorInfo { + string document_path = 1; + optional int32 view_column = 2; + bool is_active = 3; +} diff --git a/src/core/controller/file/getRelativePaths.ts b/src/core/controller/file/getRelativePaths.ts index 684bbce3..bda41ca2 100644 --- a/src/core/controller/file/getRelativePaths.ts +++ b/src/core/controller/file/getRelativePaths.ts @@ -3,7 +3,7 @@ import { RelativePathsRequest, RelativePaths } from "@shared/proto/file" import { FileMethodHandler } from "./index" import * as vscode from "vscode" import * as path from "path" -import { Metadata, StringRequest } from "@shared/proto/common" +import { StringRequest } from "@shared/proto/common" import { getHostBridgeProvider } from "@hosts/host-providers" /** @@ -22,7 +22,6 @@ export const getRelativePaths: FileMethodHandler = async ( // Use the host URI service client instead of directly using vscode.Uri.parse const parseResponse = await getHostBridgeProvider().uriServiceClient.parse( StringRequest.create({ - metadata: Metadata.create({}), value: uriString, }), ) diff --git a/src/hosts/host-provider-types.ts b/src/hosts/host-provider-types.ts index 43b5ec44..8290518f 100644 --- a/src/hosts/host-provider-types.ts +++ b/src/hosts/host-provider-types.ts @@ -3,6 +3,7 @@ import { WatchServiceClientInterface, WorkspaceServiceClientInterface, EnvServiceClientInterface, + WindowServiceClientInterface, } from "@generated/hosts/host-bridge-client-types" /** @@ -13,6 +14,7 @@ export interface HostBridgeClientProvider { watchServiceClient: WatchServiceClientInterface workspaceClient: WorkspaceServiceClientInterface envClient: EnvServiceClientInterface + windowClient: WindowServiceClientInterface } /** diff --git a/src/hosts/vscode/client/host-grpc-client.ts b/src/hosts/vscode/client/host-grpc-client.ts index 04b06a54..033e38b7 100644 --- a/src/hosts/vscode/client/host-grpc-client.ts +++ b/src/hosts/vscode/client/host-grpc-client.ts @@ -7,4 +7,5 @@ export const vscodeHostBridgeClient: HostBridgeClientProvider = { watchServiceClient: createGrpcClient(host.WatchServiceDefinition), workspaceClient: createGrpcClient(host.WorkspaceServiceDefinition), envClient: createGrpcClient(host.EnvServiceDefinition), + windowClient: createGrpcClient(host.WindowServiceDefinition), } diff --git a/src/hosts/vscode/window/showTextDocument.ts b/src/hosts/vscode/window/showTextDocument.ts new file mode 100644 index 00000000..9bdbf320 --- /dev/null +++ b/src/hosts/vscode/window/showTextDocument.ts @@ -0,0 +1,26 @@ +import * as vscode from "vscode" +import { ShowTextDocumentRequest, TextEditorInfo } from "@/shared/proto/host/window" + +export async function showTextDocument(request: ShowTextDocumentRequest): Promise { + // Convert file path to URI + const uri = vscode.Uri.file(request.path) + const options: vscode.TextDocumentShowOptions = {} + + if (request.options?.preview !== undefined) { + options.preview = request.options.preview + } + if (request.options?.preserveFocus !== undefined) { + options.preserveFocus = request.options.preserveFocus + } + if (request.options?.viewColumn !== undefined) { + options.viewColumn = request.options.viewColumn + } + + const editor = await vscode.window.showTextDocument(uri, options) + + return TextEditorInfo.create({ + documentPath: editor.document.uri.fsPath, + viewColumn: editor.viewColumn, + isActive: vscode.window.activeTextEditor === editor, + }) +} diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index c7dc331d..b801ce49 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -9,6 +9,8 @@ import * as diff from "diff" import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics" import { detectEncoding } from "../misc/extract-text" import * as iconv from "iconv-lite" +import { getHostBridgeProvider } from "@/hosts/host-providers" +import { ShowTextDocumentRequest, ShowTextDocumentOptions, TextEditorInfo } from "@/shared/proto/host/window" export const DIFF_VIEW_URI_SCHEME = "cline-diff" @@ -236,10 +238,15 @@ export class DiffViewProvider { // get text after save in case there is any auto-formatting done by the editor const postSaveContent = updatedDocument.getText() - await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { - preview: false, - preserveFocus: true, - }) + await getHostBridgeProvider().windowClient.showTextDocument( + ShowTextDocumentRequest.create({ + path: absolutePath, + options: ShowTextDocumentOptions.create({ + preview: false, + preserveFocus: true, + }), + }), + ) await this.closeAllDiffViews() /* @@ -337,10 +344,15 @@ export class DiffViewProvider { await updatedDocument.save() console.log(`File ${absolutePath} has been reverted to its original content.`) if (this.documentWasOpen) { - await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { - preview: false, - preserveFocus: true, - }) + await getHostBridgeProvider().windowClient.showTextDocument( + ShowTextDocumentRequest.create({ + path: absolutePath, + options: ShowTextDocumentOptions.create({ + preview: false, + preserveFocus: true, + }), + }), + ) } await this.closeAllDiffViews() } @@ -376,9 +388,19 @@ export class DiffViewProvider { arePathsEqual(tab.input.modified.fsPath, uri.fsPath), ) if (diffTab && diffTab.input instanceof vscode.TabInputTextDiff) { - const editor = await vscode.window.showTextDocument(diffTab.input.modified, { - preserveFocus: true, - }) + const editorInfo = await getHostBridgeProvider().windowClient.showTextDocument( + ShowTextDocumentRequest.create({ + path: diffTab.input.modified.fsPath, + options: ShowTextDocumentOptions.create({ + preserveFocus: true, + }), + }), + ) + // Find the editor that matches the returned path + const editor = vscode.window.visibleTextEditors.find((e) => e.document.uri.fsPath === editorInfo.documentPath) + if (!editor) { + throw new Error("Failed to find opened text editor") + } return editor } // Open new diff editor diff --git a/src/integrations/git/commit-message-generator.ts b/src/integrations/git/commit-message-generator.ts index 72878b28..a92e5a9a 100644 --- a/src/integrations/git/commit-message-generator.ts +++ b/src/integrations/git/commit-message-generator.ts @@ -1,6 +1,8 @@ import * as vscode from "vscode" import { getWorkingState } from "@utils/git" import { writeTextToClipboard } from "@utils/env" +import { getHostBridgeProvider } from "@/hosts/host-providers" +import { ShowTextDocumentRequest } from "@/shared/proto/host/window" /** * Formats the git diff into a prompt for the AI @@ -130,6 +132,10 @@ async function editCommitMessage(message: string): Promise { language: "markdown", }) - await vscode.window.showTextDocument(document) + await getHostBridgeProvider().windowClient.showTextDocument( + ShowTextDocumentRequest.create({ + path: document.uri.fsPath, + }), + ) vscode.window.showInformationMessage("Edit the commit message and copy when ready") } diff --git a/src/integrations/misc/export-markdown.ts b/src/integrations/misc/export-markdown.ts index 3a78748f..380b61d7 100644 --- a/src/integrations/misc/export-markdown.ts +++ b/src/integrations/misc/export-markdown.ts @@ -2,6 +2,8 @@ import { Anthropic } from "@anthropic-ai/sdk" import os from "os" import * as path from "path" import * as vscode from "vscode" +import { getHostBridgeProvider } from "@/hosts/host-providers" +import { ShowTextDocumentRequest, ShowTextDocumentOptions } from "@/shared/proto/host/window" export async function downloadTask(dateTs: number, conversationHistory: Anthropic.MessageParam[]) { // File name @@ -38,7 +40,12 @@ export async function downloadTask(dateTs: number, conversationHistory: Anthropi try { // Write content to the selected location await vscode.workspace.fs.writeFile(saveUri, new TextEncoder().encode(markdownContent)) - vscode.window.showTextDocument(saveUri, { preview: true }) + await getHostBridgeProvider().windowClient.showTextDocument( + ShowTextDocumentRequest.create({ + path: saveUri.fsPath, + options: ShowTextDocumentOptions.create({ preview: true }), + }), + ) } catch (error) { vscode.window.showErrorMessage( `Failed to save markdown file: ${error instanceof Error ? error.message : String(error)}`, diff --git a/src/integrations/misc/open-file.ts b/src/integrations/misc/open-file.ts index d4604841..54bac06b 100644 --- a/src/integrations/misc/open-file.ts +++ b/src/integrations/misc/open-file.ts @@ -2,6 +2,8 @@ import * as path from "path" import * as os from "os" import * as vscode from "vscode" import { arePathsEqual } from "@utils/path" +import { getHostBridgeProvider } from "@/hosts/host-providers" +import { ShowTextDocumentRequest, ShowTextDocumentOptions } from "@/shared/proto/host/window" export async function openImage(dataUri: string) { const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/) @@ -42,7 +44,12 @@ export async function openFile(absolutePath: string) { } catch {} // not essential, sometimes tab operations fail const document = await vscode.workspace.openTextDocument(uri) - await vscode.window.showTextDocument(document, { preview: false }) + await getHostBridgeProvider().windowClient.showTextDocument( + ShowTextDocumentRequest.create({ + path: document.uri.fsPath, + options: ShowTextDocumentOptions.create({ preview: false }), + }), + ) } catch (error) { vscode.window.showErrorMessage(`Could not open file!`) } diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index 7e0be7bc..dd47ddc6 100644 --- a/src/services/mcp/McpHub.ts +++ b/src/services/mcp/McpHub.ts @@ -139,7 +139,6 @@ export class McpHub { console.log("[DEBUG] subscribing to mcp file changes") const cancelSubscription = getHostBridgeProvider().watchServiceClient.subscribeToFile( SubscribeToFileRequest.create({ - metadata: Metadata.create({}), path: settingsPath, }), { diff --git a/src/standalone/host-bridge-client-manager.ts b/src/standalone/host-bridge-client-manager.ts index f6bb26e4..927bbc88 100644 --- a/src/standalone/host-bridge-client-manager.ts +++ b/src/standalone/host-bridge-client-manager.ts @@ -4,12 +4,14 @@ import { WatchServiceClientImpl, WorkspaceServiceClientImpl, EnvServiceClientImpl, + WindowServiceClientImpl, } from "@generated/standalone/host-bridge-clients" import { UriServiceClientInterface, WatchServiceClientInterface, WorkspaceServiceClientInterface, EnvServiceClientInterface, + WindowServiceClientInterface, } from "@generated/hosts/host-bridge-client-types" import { HostBridgeClientProvider } from "@/hosts/host-provider-types" @@ -23,6 +25,7 @@ export class ExternalHostBridgeClientManager implements HostBridgeClientProvider watchServiceClient: WatchServiceClientInterface workspaceClient: WorkspaceServiceClientInterface envClient: EnvServiceClientInterface + windowClient: WindowServiceClientInterface constructor() { const address = process.env.HOST_BRIDGE_ADDRESS || "localhost:50052" @@ -32,6 +35,7 @@ export class ExternalHostBridgeClientManager implements HostBridgeClientProvider this.watchServiceClient = new WatchServiceClientImpl(this.channel) this.workspaceClient = new WorkspaceServiceClientImpl(this.channel) this.envClient = new EnvServiceClientImpl(this.channel) + this.windowClient = new WindowServiceClientImpl(this.channel) } public close(): void {