-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- compilation error (merge mistakes), - rework the DeleteFileHandler to be more consistent with other handlers,
- Loading branch information
Showing
2 changed files
with
43 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 41 additions & 14 deletions
55
src/main/kotlin/com/vaadin/plugin/copilot/handler/DeleteFileHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |