diff --git a/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt b/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt index 53ec0bc9..4d027944 100644 --- a/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt +++ b/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt @@ -30,6 +30,22 @@ object SmkNotifier { }).notify(module.project) } + fun notifyImpossibleToCreateFileOrDirectory(name: String, project: Project){ + NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_GROUP_ID).createNotification( + title = SnakemakeBundle.message("notifier.msg.create.env.file.title"), + content = SnakemakeBundle.message("notifier.msg.create.env.file.io.exception", name), + type = NotificationType.ERROR + ).notify(project) + } + + fun notifyTargetFileIsInvalid(name: String, project: Project) { + NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_GROUP_ID).createNotification( + title = SnakemakeBundle.message("notifier.msg.create.env.file.title"), + content = SnakemakeBundle.message("notifier.msg.create.env.file.invalid.file.exception", name), + type = NotificationType.ERROR + ).notify(project) + } + fun notify(content: String, type: NotificationType = NotificationType.INFORMATION, project: Project? = null) = NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_GROUP_ID) .createNotification(content, type).also { diff --git a/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateEnvFile.kt b/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateEnvFile.kt index e4d2963b..9d1d7db3 100644 --- a/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateEnvFile.kt +++ b/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateEnvFile.kt @@ -2,11 +2,14 @@ package com.jetbrains.snakecharm.inspections.quickfix import com.intellij.codeInspection.LocalQuickFixOnPsiElement import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.InvalidVirtualFileAccessException import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.PsiManager +import com.jetbrains.snakecharm.SmkNotifier import com.jetbrains.snakecharm.SnakemakeBundle +import java.io.IOException class CreateEnvFile(expr: PsiElement, private val fileName: String) : LocalQuickFixOnPsiElement(expr) { private val defaultContext = """ @@ -22,20 +25,25 @@ class CreateEnvFile(expr: PsiElement, private val fileName: String) : LocalQuick val tokens = fileName.split('/') val targetName = tokens.lastOrNull() ?: return var currentFile = file.virtualFile.parent - // TODO: maybe use try-catch and Notifications if any part of path is invalid - tokens.forEach { - if (it == targetName) { - val resultVirtualFile = currentFile.createChildData(this, targetName) - val resultPsiFile = PsiManager.getInstance(project).findFile(resultVirtualFile) ?: return - val doc = PsiDocumentManager.getInstance(project).getDocument(resultPsiFile) ?: return - doc.insertString(0, defaultContext) - return - } - currentFile = if (it == "..") { - currentFile.parent ?: return - } else { - currentFile.findChild(it) ?: currentFile.createChildDirectory(this, it) + try { + tokens.forEach { + if (it == targetName) { + val resultVirtualFile = currentFile.createChildData(this, targetName) + val resultPsiFile = PsiManager.getInstance(project).findFile(resultVirtualFile) ?: return + val doc = PsiDocumentManager.getInstance(project).getDocument(resultPsiFile) ?: return + doc.insertString(0, defaultContext) + return + } + currentFile = if (it == "..") { + currentFile.parent ?: return + } else { + currentFile.findChild(it) ?: currentFile.createChildDirectory(this, it) + } } + } catch (e: InvalidVirtualFileAccessException) { + SmkNotifier.notifyTargetFileIsInvalid(currentFile.name, project) + } catch (e: IOException) { + SmkNotifier.notifyImpossibleToCreateFileOrDirectory(currentFile.name, project) } } diff --git a/src/main/resources/SnakemakeBundle.properties b/src/main/resources/SnakemakeBundle.properties index c322ad1d..d0f21186 100644 --- a/src/main/resources/SnakemakeBundle.properties +++ b/src/main/resources/SnakemakeBundle.properties @@ -256,6 +256,9 @@ notifier.group.title=SnakeCharm plugin notifications notifier.msg.framework.by.snakefile.title=Snakemake framework detected notifier.msg.framework.by.snakefile.action.configure=Configure Framework... notifier.msg.framework.by.snakefile=Snakefile was found in ''{0}''. +notifier.msg.create.env.file.title=Impossible to create .yaml/.yml file +notifier.msg.create.env.file.io.exception=Impossible to create ''{0}''. Check permissions and the target path correctness. +notifier.msg.create.env.file.invalid.file.exception=One of path parts with name ''{0}'' was changed during its handling. Please try again later. ####################### # Facet