diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt index cb28342..c674959 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt @@ -23,6 +23,7 @@ import com.intellij.openapi.vfs.findDirectory import com.intellij.openapi.vfs.findFile import com.intellij.openapi.vfs.findOrCreateDirectory import com.vaadin.plugin.copilot.handler.CompileFilesHandler +import com.vaadin.plugin.copilot.handler.DeleteFileHandler import com.vaadin.plugin.copilot.handler.GetModulePathsHandler import com.vaadin.plugin.copilot.handler.Handler import com.vaadin.plugin.copilot.handler.HandlerResponse @@ -70,6 +71,7 @@ class CopilotPluginUtil { private enum class HANDLERS(val command: String) { WRITE("write"), WRITE_BASE64("writeBase64"), + DELETE("delete"), UNDO("undo"), REDO("redo"), REFRESH("refresh"), @@ -98,6 +100,7 @@ class CopilotPluginUtil { when (command) { HANDLERS.WRITE.command -> return WriteFileHandler(project, data) HANDLERS.WRITE_BASE64.command -> return WriteBase64FileHandler(project, data) + HANDLERS.DELETE.command -> return DeleteFileHandler(project, data) HANDLERS.UNDO.command -> return UndoHandler(project, data) HANDLERS.REDO.command -> return RedoHandler(project, data) HANDLERS.SHOW_IN_IDE.command -> return ShowInIdeHandler(project, data) diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt b/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt new file mode 100644 index 0000000..b8d7016 --- /dev/null +++ b/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt @@ -0,0 +1,54 @@ +package com.vaadin.plugin.copilot.handler + +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 + +class DeleteFileHandler(project: Project, data: Map) : AbstractHandler(project) { + + private val ioFile: File = File(data["file"] as String) + + 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 + } +} diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/handler/UndoHandler.kt b/src/main/kotlin/com/vaadin/plugin/copilot/handler/UndoHandler.kt index 2da38ef..cd4b7c1 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/handler/UndoHandler.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/handler/UndoHandler.kt @@ -26,10 +26,12 @@ open class UndoHandler(project: Project, data: Map) : AbstractHandl } else { // if we want to undo file removal we need to create empty virtual file to write // content - WriteAction.run { - val parent = VfsUtil.createDirectories(file.parent) - vfsFile = parent.createChildData(this, file.name) - vfsFiles.add(vfsFile!!) + runInEdt { + WriteAction.run { + val parent = VfsUtil.createDirectories(file.parent) + vfsFile = parent.createChildData(this, file.name) + vfsFiles.add(vfsFile!!) + } } } } else {