From 57087e3a1d914198593e4089981d66971538bbef Mon Sep 17 00:00:00 2001 From: Peter Czuczor Date: Wed, 12 Feb 2025 16:46:18 +0100 Subject: [PATCH 1/4] feat: Adding Delete file command. --- .../plugin/copilot/CopilotPluginUtil.kt | 3 ++- .../copilot/handler/DeleteFileHandler.kt | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt index cb28342..d1e6b85 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt @@ -69,7 +69,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 +98,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..3b1ec69 --- /dev/null +++ b/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt @@ -0,0 +1,27 @@ +package com.vaadin.plugin.copilot.handler + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +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() { + 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 { + LOG.warn("File $ioFile is not a part of a project") + } + } +} \ No newline at end of file From 5c973b7914e3d6bd81eb1ab3ba13ad5e363cb079 Mon Sep 17 00:00:00 2001 From: Peter Czuczor Date: Wed, 12 Feb 2025 17:04:34 +0100 Subject: [PATCH 2/4] 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 +} From fbebe956bd29e846606fbe6e2120e4dabd41aa62 Mon Sep 17 00:00:00 2001 From: Peter Czuczor Date: Wed, 12 Feb 2025 18:29:14 +0100 Subject: [PATCH 3/4] ./gradlew :spotlessApply // this is needed as well it seems, could be automated maybe for automatic running for commits :) --- .../plugin/copilot/CopilotPluginUtil.kt | 2 +- .../copilot/handler/DeleteFileHandler.kt | 32 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt index 8cd39f7..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 @@ -33,7 +34,6 @@ 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 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 001606d..b8d7016 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt @@ -22,7 +22,7 @@ class DeleteFileHandler(project: Project, data: Map) : AbstractHand val vfsFile = VfsUtil.findFileByIoFile(ioFile, true) ?: return RESPONSE_ERROR if (!vfsFile.exists()) { LOG.warn("File $ioFile does not exist") - return RESPONSE_ERROR + return RESPONSE_ERROR } runInEdt { @@ -31,22 +31,22 @@ class DeleteFileHandler(project: Project, data: Map) : AbstractHand 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) + 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 - ) + }, + "Vaadin Copilot Delete File", + null, + UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION) } return RESPONSE_OK From 4cbacd667876ec740a4c27664788fc05c2b14af3 Mon Sep 17 00:00:00 2001 From: marcin Date: Thu, 13 Feb 2025 09:06:20 +0100 Subject: [PATCH 4/4] Fix for undo --- .../com/vaadin/plugin/copilot/handler/UndoHandler.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 {