Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding Delete file command #315

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Any>) : 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
}
}
10 changes: 6 additions & 4 deletions src/main/kotlin/com/vaadin/plugin/copilot/handler/UndoHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ open class UndoHandler(project: Project, data: Map<String, Any>) : AbstractHandl
} else {
// if we want to undo file removal we need to create empty virtual file to write
// content
WriteAction.run<Throwable> {
val parent = VfsUtil.createDirectories(file.parent)
vfsFile = parent.createChildData(this, file.name)
vfsFiles.add(vfsFile!!)
runInEdt {
WriteAction.run<Throwable> {
val parent = VfsUtil.createDirectories(file.parent)
vfsFile = parent.createChildData(this, file.name)
vfsFiles.add(vfsFile!!)
}
}
}
} else {
Expand Down
Loading