diff --git a/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java b/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java index a9721731e7..d4553e9659 100644 --- a/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java +++ b/src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java @@ -2,12 +2,23 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.fileChooser.FileChooserFactory; +import com.intellij.openapi.fileChooser.FileSaverDescriptor; +import com.intellij.openapi.fileChooser.FileSaverDialog; +import com.intellij.openapi.vfs.VirtualFileWrapper; import com.sourcegraph.cody.agent.protocol.*; import com.sourcegraph.cody.agent.protocol_generated.DisplayCodeLensParams; import com.sourcegraph.cody.agent.protocol_generated.EditTask; +import com.sourcegraph.cody.agent.protocol_generated.SaveDialogOptionsParams; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; +import java.util.stream.Collectors; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; import org.jetbrains.annotations.NotNull; @@ -202,4 +213,22 @@ public void webviewPostMessage(@NotNull WebviewPostMessageParams params) { logger.debug(String.format("webview/postMessage %s: %s", params.getId(), params.getMessage())); } + + @JsonRequest("window/showSaveDialog") + public CompletableFuture window_showSaveDialog(SaveDialogOptionsParams params) { + Path defaultPath = Paths.get(Objects.requireNonNull(params.getDefaultUri())); + Map> filters = params.getFilters(); + List allValues = + Objects.requireNonNull(filters).values().stream() + .flatMap(List::stream) + .collect(Collectors.toList()); + + FileSaverDescriptor descriptor = + new FileSaverDescriptor("Save", "Save file", allValues.toArray(new String[0])); + FileSaverDialog saveFileDialog = + FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project); + VirtualFileWrapper savedFile = + saveFileDialog.save(defaultPath, defaultPath.getFileName().toString()); + return CompletableFuture.completedFuture(savedFile.getFile().toString()); + } } diff --git a/src/test/kotlin/com/sourcegraph/cody/agent/CodyAgentClientTest.kt b/src/test/kotlin/com/sourcegraph/cody/agent/CodyAgentClientTest.kt index 5ac97b21fa..cd5f84deb6 100644 --- a/src/test/kotlin/com/sourcegraph/cody/agent/CodyAgentClientTest.kt +++ b/src/test/kotlin/com/sourcegraph/cody/agent/CodyAgentClientTest.kt @@ -13,8 +13,7 @@ class CodyAgentClientTest : BasePlatformTestCase() { const val WEBVIEW_ID: String = "unused-webview-id" } - @Volatile - var lastMessage: ConfigFeatures? = null + @Volatile var lastMessage: ConfigFeatures? = null // Use lock/condition to synchronize between observer being invoked // and the test being able to assert. @@ -43,11 +42,11 @@ class CodyAgentClientTest : BasePlatformTestCase() { WebviewPostMessageParams( id = WEBVIEW_ID, message = - ExtensionMessage( - type = ExtensionMessage.Type.SET_CONFIG_FEATURES, - errors = null, - configFeatures = expected, - ))) + ExtensionMessage( + type = ExtensionMessage.Type.SET_CONFIG_FEATURES, + errors = null, + configFeatures = expected, + ))) PlatformTestUtil.dispatchAllEventsInIdeEventQueue() lock.lock() try {