Skip to content

Commit

Permalink
fixes:
Browse files Browse the repository at this point in the history
- compilation error (merge mistakes),
- rework the DeleteFileHandler to be more consistent with other handlers,
  • Loading branch information
czp13 committed Feb 12, 2025
1 parent 57087e3 commit 5c973b7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,6 +70,7 @@ class CopilotPluginUtil {

private enum class HANDLERS(val command: String) {
WRITE("write"),
WRITE_BASE64("writeBase64"),
DELETE("delete"),
UNDO("undo"),
REDO("redo"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
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

class DeleteFileHandler(project: Project, data: Map<String, Any>) : 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 {
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
}
}
}

0 comments on commit 5c973b7

Please sign in to comment.