Skip to content

Commit

Permalink
Exceptions handling was implemented.
Browse files Browse the repository at this point in the history
Resolves: #277
  • Loading branch information
Dmitry committed Dec 2, 2021
1 parent 128c924 commit 5b0c244
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/SnakemakeBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b0c244

Please sign in to comment.