Browse Source

migrate save textDocument (#5088)

celestial-vault 5 months ago
parent
commit
ad2923cd74

+ 9 - 0
proto/host/workspace.proto

@@ -4,10 +4,14 @@ package host;
 option java_package = "bot.cline.host.proto";
 option java_multiple_files = true;
 
+import "common.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);
+  // Saves an open document if it's dirty
+  rpc saveOpenDocumentIfDirty(SaveOpenDocumentIfDirtyRequest) returns (cline.Empty);
 }
 
 message GetWorkspacePathsRequest {
@@ -22,3 +26,8 @@ message GetWorkspacePathsResponse {
   optional string id = 1;
   repeated string paths = 2;
 }
+
+message SaveOpenDocumentIfDirtyRequest {
+  cline.Metadata metadata = 1;
+  string file_path = 2;
+}

+ 14 - 0
src/hosts/vscode/hostbridge/workspace/saveOpenDocumentIfDirty.ts

@@ -0,0 +1,14 @@
+import { SaveOpenDocumentIfDirtyRequest } from "@/shared/proto/index.host"
+import { Empty } from "@/shared/proto/common"
+import * as vscode from "vscode"
+import { arePathsEqual } from "@utils/path"
+
+export async function saveOpenDocumentIfDirty(request: SaveOpenDocumentIfDirtyRequest): Promise<Empty> {
+	const existingDocument = vscode.workspace.textDocuments.find((doc) => arePathsEqual(doc.uri.fsPath, request.filePath))
+
+	if (existingDocument && existingDocument.isDirty) {
+		await existingDocument.save()
+	}
+
+	return Empty.create({})
+}

+ 3 - 7
src/integrations/editor/DiffViewProvider.ts

@@ -1,4 +1,3 @@
-import * as vscode from "vscode"
 import * as path from "path"
 import * as fs from "fs/promises"
 import { createDirectoriesForFile } from "@utils/fs"
@@ -31,12 +30,9 @@ 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()
-			}
+			await HostProvider.workspace.saveOpenDocumentIfDirty({
+				filePath: this.absolutePath!,
+			})
 
 			const fileBuffer = await fs.readFile(this.absolutePath)
 			this.fileEncoding = await detectEncoding(fileBuffer)