Skip to content

Commit

Permalink
Allow clients to redefine untitled files protocol during their creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pkukielka committed Jul 16, 2024
1 parent d65ffc9 commit 2b590f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class CodyAgentClient {
@Nullable Function<TextDocumentShowParams, Boolean> onTextDocumentShow;

// Callback for the "textDocument/openUntitledDocument" request from the agent.
@Nullable Function<UntitledTextDocument, Boolean> onOpenUntitledDocument;
@Nullable Function<UntitledTextDocument, ProtocolTextDocument> onOpenUntitledDocument;

// Callback for the "workspace/edit" request from the agent.
@Nullable Function<WorkspaceEditParams, Boolean> onWorkspaceEdit;
Expand Down Expand Up @@ -98,7 +98,7 @@ public CompletableFuture<Boolean> textDocumentShow(TextDocumentShowParams params
}

@JsonRequest("textDocument/openUntitledDocument")
public CompletableFuture<Boolean> openUntitledDocument(UntitledTextDocument params) {
public CompletableFuture<ProtocolTextDocument> openUntitledDocument(UntitledTextDocument params) {
if (onOpenUntitledDocument == null) {
return CompletableFuture.failedFuture(
new Exception("No callback registered for textDocument/openUntitledDocument"));
Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,14 +123,11 @@ class CodyAgentService(private val project: Project) : Disposable {
}

agent.client.onOpenUntitledDocument = Function { params ->
val result = CompletableFuture<Boolean>()
val result = CompletableFuture<ProtocolTextDocument?>()
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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
32 changes: 9 additions & 23 deletions src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -61,19 +57,6 @@ object CodyEditorUtil {
*/
@JvmStatic val KEY_EDITOR_WANTS_AUTOCOMPLETE = Key.create<Boolean>("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<CommonCodeStyleSettings, RuntimeException> {
Optional.ofNullable(CodeStyle.getLanguageSettings(editor))
.orElse(CodeStyle.getDefaultSettings())
}
}

@JvmStatic
fun getTextRange(document: Document, range: Range): TextRange {
val start = range.start.toOffset(document)
Expand Down Expand Up @@ -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()) }
}
Expand Down

0 comments on commit 2b590f6

Please sign in to comment.