From 5c973b7914e3d6bd81eb1ab3ba13ad5e363cb079 Mon Sep 17 00:00:00 2001 From: Peter Czuczor Date: Wed, 12 Feb 2025 17:04:34 +0100 Subject: [PATCH] fixes: - compilation error (merge mistakes), - rework the DeleteFileHandler to be more consistent with other handlers, --- .../plugin/copilot/CopilotPluginUtil.kt | 2 + .../copilot/handler/DeleteFileHandler.kt | 55 ++++++++++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt index d1e6b85..8cd39f7 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt @@ -33,6 +33,7 @@ import com.vaadin.plugin.copilot.handler.ShowInIdeHandler import com.vaadin.plugin.copilot.handler.UndoHandler import com.vaadin.plugin.copilot.handler.WriteBase64FileHandler import com.vaadin.plugin.copilot.handler.WriteFileHandler +import com.vaadin.plugin.copilot.handler.DeleteFileHandler import com.vaadin.plugin.utils.VaadinIcons import io.netty.handler.codec.http.HttpResponseStatus import java.io.BufferedWriter @@ -69,6 +70,7 @@ class CopilotPluginUtil { private enum class HANDLERS(val command: String) { WRITE("write"), + WRITE_BASE64("writeBase64"), DELETE("delete"), UNDO("undo"), REDO("redo"), diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt b/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt index 3b1ec69..001606d 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt @@ -1,7 +1,11 @@ package com.vaadin.plugin.copilot.handler -import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.runInEdt +import com.intellij.openapi.command.CommandProcessor +import com.intellij.openapi.command.UndoConfirmationPolicy +import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.ReadonlyStatusHandler import com.intellij.openapi.vfs.VfsUtil import java.io.File @@ -9,19 +13,42 @@ class DeleteFileHandler(project: Project, data: Map) : AbstractHand private val ioFile: File = File(data["file"] as String) - override fun run() { - if (isFileInsideProject(project, ioFile)) { - val vfsFile = VfsUtil.findFileByIoFile(ioFile, true) - if (vfsFile?.exists() == true) { - ApplicationManager.getApplication().runWriteAction { - vfsFile.delete(this) - LOG.info("File $ioFile deleted") - } - } else { - LOG.warn("File $ioFile does not exist") - } - } else { + override fun run(): HandlerResponse { + if (!isFileInsideProject(project, ioFile)) { LOG.warn("File $ioFile is not a part of a project") + return RESPONSE_BAD_REQUEST + } + + val vfsFile = VfsUtil.findFileByIoFile(ioFile, true) ?: return RESPONSE_ERROR + if (!vfsFile.exists()) { + LOG.warn("File $ioFile does not exist") + return RESPONSE_ERROR + } + + runInEdt { + if (!ReadonlyStatusHandler.ensureFilesWritable(project, vfsFile)) { + LOG.warn("File $ioFile is not writable, cannot delete") + return@runInEdt + } + + CommandProcessor.getInstance().executeCommand( + project, + { + WriteCommandAction.runWriteCommandAction(project) { + try { + vfsFile.delete(this) + LOG.info("File $ioFile deleted") + } catch (e: Exception) { + LOG.error("Failed to delete file $ioFile", e) + } + } + }, + "Vaadin Copilot Delete File", + null, + UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION + ) } + + return RESPONSE_OK } -} \ No newline at end of file +}