From ed06af7a7ca125eb1a3913e12d1b50ab575ba22c Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sat, 18 Dec 2021 11:18:50 +0300 Subject: [PATCH] Cosmetics. Resolves: #277 --- .../com/jetbrains/snakecharm/SmkNotifier.kt | 27 ++++--------- .../inspections/quickfix/CreateMissedFile.kt | 38 +++++++++++++++---- src/main/resources/SnakemakeBundle.properties | 1 + 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt b/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt index 4d027944..5536f0f2 100644 --- a/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt +++ b/src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt @@ -30,25 +30,12 @@ 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) = + fun notify( + title: String = "", + content: String, + type: NotificationType = NotificationType.INFORMATION, + project: Project? = null + ) = NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_GROUP_ID) - .createNotification(content, type).also { - it.notify(project) - } + .createNotification(title, content, type).notify(project) } \ No newline at end of file diff --git a/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateMissedFile.kt b/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateMissedFile.kt index c6cac780..d21329be 100644 --- a/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateMissedFile.kt +++ b/src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateMissedFile.kt @@ -1,6 +1,7 @@ package com.jetbrains.snakecharm.inspections.quickfix import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement +import com.intellij.notification.NotificationType import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ModalityState import com.intellij.openapi.command.undo.* @@ -18,6 +19,7 @@ import com.jetbrains.snakecharm.lang.SnakemakeNames import org.apache.commons.io.FileUtils import java.io.IOException import java.nio.file.Files +import java.nio.file.InvalidPathException import java.nio.file.Paths import kotlin.io.path.isDirectory import kotlin.io.path.name @@ -59,11 +61,21 @@ class CreateMissedFile( startElement: PsiElement, endElement: PsiElement ) { - val dir = + val relativeDirectory = if (searchRelativelyToCurrentFolder) file.virtualFile.parent else ProjectRootManager.getInstance(project).fileIndex.getContentRootForFile( file.virtualFile ) ?: return - val targetFilePath = Paths.get(dir.path, fileName) + val targetFilePath = try { + Paths.get(relativeDirectory.path, fileName) + } catch (e: InvalidPathException) { + SmkNotifier.notify( + title = SnakemakeBundle.message("notifier.msg.create.env.file.title"), + content = SnakemakeBundle.message("notifier.msg.create.env.file.name.invalid.file.exception", fileName), + type = NotificationType.ERROR, + project = project + ) + return + } var firstAffectedFile = targetFilePath while (firstAffectedFile.parent.notExists()) { firstAffectedFile = firstAffectedFile.parent ?: break @@ -79,9 +91,6 @@ class CreateMissedFile( VirtualFileManager.getInstance().asyncRefresh { } } val redo = Runnable { - if (!supportedSections.containsKey(sectionName)) { - return@Runnable - } try { val directoryPath = Files.createDirectories(targetFilePath.parent) val directoryVirtualFile = VfsUtil.findFile(directoryPath, true) ?: return@Runnable @@ -89,13 +98,23 @@ class CreateMissedFile( VirtualFileManager.getInstance().asyncRefresh { } val context = supportedSections[sectionName] if (context != null) { - // We don't use the result of 'createChildFile()' because it has inappropriate type (and throw UnsupportedOperationException) + // We don't use the result of 'createChildFile()' because it has inappropriate type (and throws UnsupportedOperationException) targetFilePath.toFile().appendText(context) } } catch (e: SecurityException) { - SmkNotifier.notifyTargetFileIsInvalid(fileName, project) + SmkNotifier.notify( + title = SnakemakeBundle.message("notifier.msg.create.env.file.title"), + content = SnakemakeBundle.message("notifier.msg.create.env.file.invalid.file.exception", fileName), + type = NotificationType.ERROR, + project = project + ) } catch (e: IOException) { - SmkNotifier.notifyImpossibleToCreateFileOrDirectory(fileName, project) + SmkNotifier.notify( + title = SnakemakeBundle.message("notifier.msg.create.env.file.title"), + content = SnakemakeBundle.message("notifier.msg.create.env.file.io.exception", fileName), + type = NotificationType.ERROR, + project = project + ) } } val action = object : UndoableAction { @@ -110,6 +129,9 @@ class CreateMissedFile( } override fun getAffectedDocuments(): Array = + // We mark only the current file as affected to make feature convenient. + // Otherwise, new file opening will be regarded as a new action + // and 'undone' will be banned arrayOf(DocumentReferenceManager.getInstance().create(file.virtualFile)) override fun isGlobal() = true diff --git a/src/main/resources/SnakemakeBundle.properties b/src/main/resources/SnakemakeBundle.properties index eb8b4932..d7c95181 100644 --- a/src/main/resources/SnakemakeBundle.properties +++ b/src/main/resources/SnakemakeBundle.properties @@ -265,6 +265,7 @@ notifier.msg.framework.by.snakefile=Snakefile was found in ''{0}''. notifier.msg.create.env.file.title=Failed to create file notifier.msg.create.env.file.io.exception=Failed to create ''{0}''. Check permissions and the target path correctness. notifier.msg.create.env.file.invalid.file.exception=One of the parts of ''{0}'' was changed during its handling. Please try again later. +notifier.msg.create.env.file.name.invalid.file.exception=File has invalid name: ''{0}''. ####################### # Facet