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

Create a Backing File if The Image File is Not On Disk #2872

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions images/resources/messages/ImagesBundle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
error.title.launching.external.editor=Problem Launching External Executable
error.cannot.edit.image.external.editor=Can't edit image in external editor

select.external.executable.title=Select Editor
select.external.executable.message=Select external graphics editor
Expand Down
117 changes: 108 additions & 9 deletions images/src/org/intellij/images/actions/EditExternallyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@ import com.intellij.execution.ExecutionException
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.util.ExecUtil
import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys
import com.intellij.openapi.application.EDT
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.toNioPathOrNull
import com.intellij.ui.UIBundle
import com.intellij.util.EnvironmentUtil
import com.intellij.util.io.delete
import kotlinx.coroutines.*
import org.intellij.images.ImagesBundle
import org.intellij.images.fileTypes.ImageFileTypeManager
import org.intellij.images.options.impl.ImagesConfigurable
import org.intellij.images.ui.ImageComponentDecorator
import java.awt.Desktop
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption.REPLACE_EXISTING

/**
* Open image file externally.
Expand All @@ -31,6 +46,29 @@ import java.io.IOException
internal class EditExternallyAction : DumbAwareAction() {
override fun actionPerformed(e: AnActionEvent) {
val imageFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
if (imageFile.toNioPathOrNull() != null) {
actionPerformed(e, imageFile)
}
else {
performActionWithBackingFile(e, imageFile)
}
}

override fun update(e: AnActionEvent) {
val file = e.getData(CommonDataKeys.VIRTUAL_FILE)
val enabled = file != null && ImageFileTypeManager.getInstance().isImage(file)
if (e.isFromContextMenu) {
e.presentation.isVisible = enabled
}

e.presentation.isEnabled = enabled
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}

private fun actionPerformed(e: AnActionEvent, imageFile: VirtualFile) {
var executablePath = PropertiesComponent.getInstance().getValue(EditExternalImageEditorAction.EXT_PATH_KEY, "")
if (!StringUtil.isEmpty(executablePath)) {
EnvironmentUtil.getEnvironmentMap().forEach { (varName, varValue) ->
Expand Down Expand Up @@ -75,17 +113,78 @@ internal class EditExternallyAction : DumbAwareAction() {
}
}

override fun update(e: AnActionEvent) {
val file = e.getData(CommonDataKeys.VIRTUAL_FILE)
val enabled = file != null && ImageFileTypeManager.getInstance().isImage(file)
if (e.isFromContextMenu) {
e.presentation.isVisible = enabled
// Try to create a temporary backing file for the external editor to use
private fun performActionWithBackingFile(e: AnActionEvent, imageFile: VirtualFile) {
try {
val disposable = e.getDisposable()
disposable.launch {
try {
val backingFile = imageFile.copyToBackingFile(disposable)
actionPerformed(e, backingFile)
}
catch (e: IllegalStateException) {
thisLogger().warn("Failed to open external image editor", e)
withContext(Dispatchers.EDT) {
Messages.showErrorDialog(ImagesBundle.message("error.cannot.edit.image.external.editor"), UIBundle.message("error.dialog.title"))
}
}
}
}
catch (e: IllegalStateException) {
thisLogger().warn("Failed to open external image editor", e)
Messages.showErrorDialog(ImagesBundle.message("error.cannot.edit.image.external.editor"), UIBundle.message("error.dialog.title"))
}
}
}

e.presentation.isEnabled = enabled
private fun VirtualFile.copyToBackingFile(disposable: Disposable): VirtualFile {
val filePath = Files.createTempFile("EditExternallyAction", name)
Disposer.register(disposable, Disposable {
filePath.safeDelete()
})
inputStream.use { inputStream ->
try {
Files.copy(inputStream, filePath, REPLACE_EXISTING)
}
catch (e: IOException) {
filePath.safeDelete()
throw IllegalStateException("Failed to create backing file", e)
}
}
return LocalFileSystem.getInstance().refreshAndFindFileByNioFile(filePath) ?: throw IllegalStateException("Failed to create virtual file")
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
private fun Path.safeDelete() {
try {
delete()
}
catch (ignore: IOException) {
}
}

private fun Disposable.launch(block: suspend CoroutineScope.() -> Unit) {
val job = SupervisorJob()
@Suppress("SSBasedInspection")
CoroutineScope(job + Dispatchers.IO).launch {
block()
}
Disposer.register(this, Disposable {
job.cancel()
})
}

/**
* Tries to get a Disposable from the event
*
* If `ImageComponentDecorator.DATA_KEY` exists and is a `Disposable`, use it (ImageEditorUI returns a
* [org.intellij.images.editor.ImageEditor] which is a `Disposable`)
* Otherwise, use PlatformCoreDataKeys.FILE_EDITOR.
*/
private fun AnActionEvent.getDisposable(): Disposable {
val data = getData(ImageComponentDecorator.DATA_KEY)
if (data is Disposable) {
return data
}
}
return getData(PlatformCoreDataKeys.FILE_EDITOR)
?: throw IllegalStateException("Component does not provide a Disposable object")
}