From cb276a3f917ab03f764af0cc0d9c11c97663dd9f Mon Sep 17 00:00:00 2001 From: Piotr Kukielka Date: Thu, 11 Jul 2024 18:02:37 +0200 Subject: [PATCH 1/2] Allow clients to redefine untitled files protocol during their creation --- .../cody/agent/CodyAgentClient.java | 4 +-- .../cody/agent/CodyAgentService.kt | 12 +++---- .../agent/protocol/ProtocolTextDocument.kt | 2 +- .../com/sourcegraph/utils/CodyEditorUtil.kt | 32 ++++++------------- 4 files changed, 17 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 ce71dc816f..a9721731e7 100644 --- a/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java +++ b/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java @@ -48,7 +48,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; @@ -108,7 +108,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 4a5dd90eaa..c2952245b3 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt @@ -8,6 +8,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 @@ -100,14 +101,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 373ced2854..7a7ab5f736 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/protocol/ProtocolTextDocument.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/protocol/ProtocolTextDocument.kt @@ -15,7 +15,7 @@ import com.sourcegraph.cody.agent.protocol_extensions.Position import com.sourcegraph.cody.agent.protocol_generated.Range import java.awt.Point import java.nio.file.FileSystems -import java.util.* +import java.util.Locale import kotlin.math.max import kotlin.math.min diff --git a/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt b/src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt index 63b05ed848..db921914a5 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,7 +26,6 @@ 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_extensions.* @@ -36,7 +33,6 @@ import com.sourcegraph.cody.agent.protocol_generated.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 @@ -62,19 +58,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) @@ -216,18 +199,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()) } } From 0b94299a1c29adbdbb456e9f198a1987791a0037 Mon Sep 17 00:00:00 2001 From: Piotr Kukielka Date: Wed, 31 Jul 2024 14:12:15 +0200 Subject: [PATCH 2/2] Bump cody commit --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index fac528567e..508df5b6df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,4 @@ kotlin.stdlib.default.dependency=false nodeBinaries.commit=8755ae4c05fd476cd23f2972049111ba436c86d4 nodeBinaries.version=v20.12.2 cody.autocomplete.enableFormatting=true -cody.commit=280a91999947fb7829d56163d3a13f9910b0ac05 +cody.commit=6443a0b6eccb4aa1442f9c5a2b09f2755a3d89da