From 2b590f6569eedb3d2197ffa811c32c1f27b2ef41 Mon Sep 17 00:00:00 2001 From: Piotr Kukielka Date: Thu, 11 Jul 2024 18:02:37 +0200 Subject: [PATCH] Allow clients to redefine untitled files protocol during their creation --- .../cody/agent/CodyAgentClient.java | 4 +-- .../cody/agent/CodyAgentService.kt | 12 +++---- .../agent/protocol/ProtocolTextDocument.kt | 15 ++++++++- .../com/sourcegraph/utils/CodyEditorUtil.kt | 32 ++++++------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java b/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java index 6d6276785c..0466afb9c8 100644 --- a/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java +++ b/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java @@ -43,7 +43,7 @@ public class CodyAgentClient { @Nullable Function onTextDocumentShow; // Callback for the "textDocument/openUntitledDocument" request from the agent. - @Nullable Function onOpenUntitledDocument; + @Nullable Function onOpenUntitledDocument; // Callback for the "workspace/edit" request from the agent. @Nullable Function onWorkspaceEdit; @@ -98,7 +98,7 @@ public CompletableFuture textDocumentShow(TextDocumentShowParams params } @JsonRequest("textDocument/openUntitledDocument") - public CompletableFuture openUntitledDocument(UntitledTextDocument params) { + public CompletableFuture openUntitledDocument(UntitledTextDocument params) { if (onOpenUntitledDocument == null) { return CompletableFuture.failedFuture( new Exception("No callback registered for textDocument/openUntitledDocument")); diff --git a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt index 00b60a4ef2..17025ff3c8 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt @@ -9,6 +9,7 @@ import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.project.Project import com.intellij.util.net.HttpConfigurable +import com.sourcegraph.cody.agent.protocol.ProtocolTextDocument import com.sourcegraph.cody.chat.AgentChatSessionService import com.sourcegraph.cody.config.CodyApplicationSettings import com.sourcegraph.cody.context.RemoteRepoSearcher @@ -122,14 +123,11 @@ class CodyAgentService(private val project: Project) : Disposable { } agent.client.onOpenUntitledDocument = Function { params -> - val result = CompletableFuture() + val result = CompletableFuture() ApplicationManager.getApplication().invokeAndWait { - val vf = CodyEditorUtil.createFileOrScratch(project, params.uri, params.content) - if (vf == null) { - result.complete(false) - return@invokeAndWait - } - result.complete(true) + val vf = + CodyEditorUtil.createFileOrScratchFromUntitled(project, params.uri, params.content) + result.complete(if (vf == null) null else ProtocolTextDocument.fromVirtualFile(vf)) } result.get() } diff --git a/src/main/kotlin/com/sourcegraph/cody/agent/protocol/ProtocolTextDocument.kt b/src/main/kotlin/com/sourcegraph/cody/agent/protocol/ProtocolTextDocument.kt index 606f81e3a6..b2ec2fde44 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/protocol/ProtocolTextDocument.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/protocol/ProtocolTextDocument.kt @@ -13,7 +13,7 @@ import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.concurrency.annotations.RequiresEdt import java.awt.Point import java.nio.file.FileSystems -import java.util.* +import java.util.Locale import kotlin.math.max import kotlin.math.min @@ -165,6 +165,19 @@ private constructor( return fromVirtualFile(editor, file) } + @JvmStatic + @RequiresEdt + fun fromVirtualFile(file: VirtualFile): ProtocolTextDocument { + val content = FileDocumentManager.getInstance().getDocument(file)?.text + val uri = uriFor(file) + return ProtocolTextDocument( + uri = uri, + content = content, + selection = null, + visibleRange = null, + testing = getTestingParams(uri = uri, content = content, selection = null)) + } + @JvmStatic @RequiresEdt fun fromVirtualFile( diff --git a/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt b/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt index 4670d4b9d7..32f90f6586 100644 --- a/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt +++ b/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt @@ -1,13 +1,11 @@ package com.sourcegraph.utils -import com.intellij.application.options.CodeStyle import com.intellij.ide.scratch.ScratchFileService import com.intellij.ide.scratch.ScratchRootType import com.intellij.injected.editor.EditorWindow import com.intellij.lang.Language import com.intellij.lang.LanguageUtil import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.application.ReadAction import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.editor.Document @@ -28,14 +26,12 @@ import com.intellij.openapi.util.TextRange import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.ex.temp.TempFileSystem -import com.intellij.psi.codeStyle.CommonCodeStyleSettings import com.intellij.util.concurrency.annotations.RequiresEdt import com.intellij.util.withScheme import com.sourcegraph.cody.agent.protocol.Range import com.sourcegraph.config.ConfigUtil import java.net.URI import java.net.URISyntaxException -import java.util.* import kotlin.io.path.createDirectories import kotlin.io.path.createFile import kotlin.io.path.exists @@ -61,19 +57,6 @@ object CodyEditorUtil { */ @JvmStatic val KEY_EDITOR_WANTS_AUTOCOMPLETE = Key.create("cody.editorWantsAutocomplete") - /** - * @param editor given editor - * @return code style settings for the given editor, if null defaults to default app code style - * settings - */ - @JvmStatic - fun codeStyleSettings(editor: Editor): CommonCodeStyleSettings { - return ReadAction.compute { - Optional.ofNullable(CodeStyle.getLanguageSettings(editor)) - .orElse(CodeStyle.getDefaultSettings()) - } - } - @JvmStatic fun getTextRange(document: Document, range: Range): TextRange { val start = range.start.toOffset(document) @@ -215,18 +198,21 @@ object CodyEditorUtil { } } - fun createFileOrScratch( + fun createFileOrScratchFromUntitled( project: Project, uriString: String, content: String? = null ): VirtualFile? { try { - val uri = URI.create(uriString).withScheme("file") - if (!uri.toPath().exists()) { - uri.toPath().parent?.createDirectories() - uri.toPath().createFile() + val uri = URI.create(uriString) + + val fileUri = uri.withScheme("file") + if (!fileUri.toPath().exists()) { + fileUri.toPath().parent?.createDirectories() + fileUri.toPath().createFile() } - val vf = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(uri.toPath()) + + val vf = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(fileUri.toPath()) content?.let { WriteCommandAction.runWriteCommandAction(project) { vf?.setBinaryContent(it.toByteArray()) } }