Compare commits

...

1 Commits

Author SHA1 Message Date
abeatrix 90ccd73b1f Implement textDocuments RPC method for VS Code host bridge
Adds the `textDocuments` RPC method to the VS Code host bridge. This method allows the client to retrieve a list of currently open text documents in the VS Code workspace, along with their paths, active status, and view column information.

The changes include:

- Adding `textDocuments` RPC method to `WorkspaceService` in `proto/host/workspace.proto`.
- Creating `src/hosts/vscode/hostbridge/workspace/textDocuments.ts` to implement the `textDocuments` method, which retrieves the list of open documents from `vscode.workspace.textDocuments` and their associated metadata.
- Updating `DiffViewProvider.ts` to use the new `textDocuments` RPC method to check if a file is already open and dirty before getting its contents. This ensures that the latest version of the file is used.
2025-07-15 21:46:20 -07:00
3 changed files with 39 additions and 5 deletions
+16
View File
@@ -3,11 +3,13 @@ syntax = "proto3";
package host;
option java_package = "bot.cline.host.proto";
option java_multiple_files = true;
import "host/window.proto";
// Provides methods for working with workspaces/projects.
service WorkspaceService {
// Returns a list of the top level directories of the workspace.
rpc getWorkspacePaths(GetWorkspacePathsRequest) returns (GetWorkspacePathsResponse);
rpc textDocuments(TextDocumentsRequest) returns (TextDocumentsResponse);
}
message GetWorkspacePathsRequest {
@@ -22,3 +24,17 @@ message GetWorkspacePathsResponse {
optional string id = 1;
repeated string paths = 2;
}
message TextDocumentsRequest {
// The unique ID for the workspace/project.
// This is currently optional in vscode. It is required in other environments where cline is running at
// the application level, and the user can open multiple projects.
optional string id = 1;
}
message TextDocumentsResponse {
// The unique ID for the workspace/project.
optional string id = 1;
// The list of text documents in the workspace.
repeated TextEditorInfo editors = 2;
}
@@ -0,0 +1,17 @@
import { TextDocumentsRequest, TextDocumentsResponse, TextEditorInfo } from "@/shared/proto/index.host"
import * as vscode from "vscode"
import { ViewColumn } from "vscode"
export async function textDocuments(_: TextDocumentsRequest): Promise<TextDocumentsResponse> {
const visibleEditors = vscode.window.visibleTextEditors
const editors =
vscode.workspace.textDocuments?.map((doc) => ({
documentPath: doc.uri.toString(),
isActive: visibleEditors.some((editor) => editor.document === doc),
viewColumn: visibleEditors.find((editor) => editor.document === doc)?.viewColumn ?? ViewColumn.Active,
})) || []
return TextDocumentsResponse.create({
editors: editors.map((editor) => TextEditorInfo.create(editor)),
})
}
+6 -5
View File
@@ -11,6 +11,7 @@ import { detectEncoding } from "../misc/extract-text"
import * as iconv from "iconv-lite"
import { getHostBridgeProvider } from "@/hosts/host-providers"
import { ShowTextDocumentRequest, ShowTextDocumentOptions } from "@/shared/proto/host/window"
import { TextDocumentsResponse } from "@/shared/proto/index.host"
export const DIFF_VIEW_URI_SCHEME = "cline-diff"
@@ -41,11 +42,11 @@ export abstract class DiffViewProvider {
// if the file is already open, ensure it's not dirty before getting its contents
if (fileExists) {
const existingDocument = vscode.workspace.textDocuments.find((doc) =>
arePathsEqual(doc.uri.fsPath, this.absolutePath),
)
if (existingDocument && existingDocument.isDirty) {
await existingDocument.save()
const textDocuments = await getHostBridgeProvider().workspaceClient.textDocuments(TextDocumentsResponse.create({}))
const existingEditor = textDocuments?.editors?.find((editor) => arePathsEqual(editor.documentPath, this.absolutePath))
const existingDoc = existingEditor ? await vscode.workspace.openTextDocument(existingEditor.documentPath) : undefined
if (existingDoc?.isDirty) {
await existingDoc.save()
}
const fileBuffer = await fs.readFile(this.absolutePath)