Skip to content

Commit

Permalink
Cosmetics.
Browse files Browse the repository at this point in the history
Resolves: #277
  • Loading branch information
Dmitry committed Dec 18, 2021
1 parent c4d4a35 commit ed06af7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
27 changes: 7 additions & 20 deletions src/main/kotlin/com/jetbrains/snakecharm/SmkNotifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -79,23 +91,30 @@ 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
LocalFileSystem.getInstance().createChildFile(this, directoryVirtualFile, targetFilePath.name)
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 {
Expand All @@ -110,6 +129,9 @@ class CreateMissedFile(
}

override fun getAffectedDocuments(): Array<DocumentReference> =
// 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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/SnakemakeBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed06af7

Please sign in to comment.