Skip to content

Commit

Permalink
Add handling for create-file workspace op
Browse files Browse the repository at this point in the history
  • Loading branch information
mkondratek committed Aug 14, 2024
1 parent 2d53103 commit 10485f4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sourcegraph/cody/agent/CodyAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private constructor(
fun create(project: Project): CompletableFuture<CodyAgent> {
try {
val conn = startAgentProcess()
val client = CodyAgentClient(WebUIServiceWebviewProvider(project), project)
val client = CodyAgentClient(project, WebUIServiceWebviewProvider(project))
client.onSetConfigFeatures = project.service<CurrentConfigFeatures>()
val launcher = startAgentLauncher(conn, client)
val server = launcher.remoteProxy
Expand Down
26 changes: 25 additions & 1 deletion src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.sourcegraph.cody.agent

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileChooser.FileChooserFactory
import com.intellij.openapi.fileChooser.FileSaverDescriptor
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.sourcegraph.cody.agent.protocol.DebugMessage
import com.sourcegraph.cody.agent.protocol.OpenExternalParams
import com.sourcegraph.cody.agent.protocol.ProtocolTextDocument
Expand All @@ -23,7 +30,7 @@ import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
* and notifications sent by the agent.
*/
@Suppress("unused")
class CodyAgentClient(private val webview: NativeWebviewProvider) {
class CodyAgentClient(private val project: Project, private val webview: NativeWebviewProvider) {
companion object {
private val logger = Logger.getInstance(CodyAgentClient::class.java)
}
Expand Down Expand Up @@ -253,4 +260,21 @@ class CodyAgentClient(private val webview: NativeWebviewProvider) {

logger.debug("webview/postMessage ${params.id}: ${params.message}")
}

@JsonRequest("window/showSaveDialog")
fun window_showSaveDialog(): CompletableFuture<String> {
var outputDir: VirtualFile? = project.guessProjectDir()
if (outputDir == null || !outputDir.exists()) {
outputDir = VfsUtil.getUserHomeDir()
}

val descriptor = FileSaverDescriptor("Cody: Save as New File", "Save file")
val saveFileFuture = CompletableFuture<String>()
runInEdt {
val dialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project)
val result = dialog.save(outputDir, "Untitled")
saveFileFuture.complete(result?.file?.path)
}
return saveFileFuture
}
}
15 changes: 5 additions & 10 deletions src/main/kotlin/com/sourcegraph/cody/edit/EditService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class EditService(val project: Project) {
document.insertString(edit.position.toOffset(document), edit.value)
true
}
else -> {
logger.warn("Unknown edit type: $edit")
false
}
}
}
}
Expand All @@ -74,8 +70,11 @@ class EditService(val project: Project) {
// TODO: We need to support the file-level operations.
when (op) {
is CreateFileOperation -> {
logger.warn("Workspace edit operation created a file: ${op.uri}")
return false
logger.info("Workspace edit operation created a file: ${op.uri}")
val file =
CodyEditorUtil.createFileOrScratchFromUntitled(project, op.uri, content = "")
?: return false
CodyEditorUtil.showDocument(project, file)
}
is RenameFileOperation -> {
logger.warn("Workspace edit operation renamed a file: ${op.oldUri} -> ${op.newUri}")
Expand All @@ -89,10 +88,6 @@ class EditService(val project: Project) {
logger.info("Applying workspace edit to a file: ${op.uri}")
performTextEdits(op.uri, op.edits)
}
else -> {
logger.warn("DocumentCommand session received unknown workspace edit operation: $op")
return false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CodyAgentClientTest : BasePlatformTestCase() {
private val condition = lock.newCondition()

private fun client(): CodyAgentClient {
val client = CodyAgentClient(StubWebviewProvider(), project)
val client = CodyAgentClient(project, StubWebviewProvider())
client.onSetConfigFeatures = ConfigFeaturesObserver {
lock.lock()
try {
Expand Down

0 comments on commit 10485f4

Please sign in to comment.