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

Features/#277 create missing env file #445

Merged
merged 17 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Released on ...
- TODO (see [#NNN](https://github.com/JetBrains-Research/snakecharm/issues/NNN))

### Added
- Quick fix for unresolved files (conda, configfile. etc.)(see [#277](https://github.com/JetBrains-Research/snakecharm/issues/277))
- TODO (see [#NNN](https://github.com/JetBrains-Research/snakecharm/issues/NNN))

## [2021.3.661]
Expand Down
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
@@ -0,0 +1,24 @@
package com.jetbrains.snakecharm.inspections

import com.intellij.codeInspection.LocalQuickFix
import com.intellij.psi.*
import com.jetbrains.python.inspections.PyUnresolvedReferenceQuickFixProvider
import com.jetbrains.snakecharm.inspections.quickfix.CreateMissedFile
import com.jetbrains.snakecharm.lang.psi.SmkArgsSection
import com.jetbrains.snakecharm.lang.psi.SmkFileReference

class SmkUnresolvedReferenceInspectionExtension : PyUnresolvedReferenceQuickFixProvider {

override fun registerQuickFixes(reference: PsiReference, existing: MutableList<LocalQuickFix>) {
val section = reference.element as? SmkArgsSection ?: return
val sectionName = section.sectionKeyword ?: return
if (CreateMissedFile.supportedSections.containsKey(sectionName)) {
val fileReference = (section.reference as? SmkFileReference) ?: return
if (!fileReference.hasAppropriateSuffix()) {
return
}
val name = fileReference.path
existing.add(CreateMissedFile(section, name, sectionName, fileReference.searchRelativelyToCurrentFolder))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.jetbrains.snakecharm.inspections.quickfix

import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.command.undo.*
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.jetbrains.snakecharm.SmkNotifier
import com.jetbrains.snakecharm.SnakemakeBundle
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.Paths
import kotlin.io.path.isDirectory
import kotlin.io.path.name
import kotlin.io.path.notExists

class CreateMissedFile(
element: PsiElement,
private val fileName: String,
private val sectionName: String,
private val searchRelativelyToCurrentFolder: Boolean
) : LocalQuickFixAndIntentionActionOnPsiElement(element) {
companion object {
private val condaDefaultContext = """
channels:
dependencies:
""".trimIndent()

// Update it if wee need default text
val supportedSections = mapOf(
SnakemakeNames.SECTION_CONDA to condaDefaultContext,
SnakemakeNames.SECTION_NOTEBOOK to null,
SnakemakeNames.SECTION_SCRIPT to null,
SnakemakeNames.MODULE_SNAKEFILE_KEYWORD to null,

SnakemakeNames.WORKFLOW_CONFIGFILE_KEYWORD to null,
SnakemakeNames.WORKFLOW_PEPFILE_KEYWORD to null,
SnakemakeNames.WORKFLOW_PEPSCHEMA_KEYWORD to null
)
}

override fun getFamilyName() = SnakemakeBundle.message("INSP.NAME.conda.env.missing.fix", fileName)

override fun getText() = familyName

override fun invoke(
project: Project,
file: PsiFile,
editor: Editor?,
startElement: PsiElement,
endElement: PsiElement
) {
val dir =
if (searchRelativelyToCurrentFolder) file.virtualFile.parent else ProjectRootManager.getInstance(project).fileIndex.getContentRootForFile(
file.virtualFile
) ?: return
val targetFilePath = Paths.get(dir.path, fileName)
var firstAffectedFile = targetFilePath
while (firstAffectedFile.parent.notExists()) {
firstAffectedFile = firstAffectedFile.parent ?: break
}

val undo = Runnable {
firstAffectedFile ?: return@Runnable
if (firstAffectedFile.isDirectory()) {
FileUtils.deleteDirectory(firstAffectedFile.toFile())
} else {
Files.delete(firstAffectedFile)
}
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 { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What for do we need refresh here? And why async? Is it a workaround to resart inspections in the current file? According to source code com.intellij.openapi.vfs.impl.local.LocalFileSystemBase.createChildFile does some events publishing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, not sure that we need to interact with Virtual File System here. 'undo' uses just "java.io.File" standard Java SDK API. Here maybe is better to check existence/create file w/o using VFS api, refresing, etc.

val context = supportedSections[sectionName]
if (context != null) {
// We don't use the result of 'createChildFile()' because it has inappropriate type (and throw UnsupportedOperationException)
targetFilePath.toFile().appendText(context)
}
} catch (e: SecurityException) {
SmkNotifier.notifyTargetFileIsInvalid(fileName, project)
} catch (e: IOException) {
SmkNotifier.notifyImpossibleToCreateFileOrDirectory(fileName, project)
}
}
val action = object : UndoableAction {
override fun undo() {
// invoke later because changing document inside undo/redo is not allowed (see ChangeFileEncodingAction)
ApplicationManager.getApplication().invokeLater(undo, ModalityState.NON_MODAL, project.disposed)
}

override fun redo() {
// invoke later because changing document inside undo/redo is not allowed (see ChangeFileEncodingAction)
ApplicationManager.getApplication().invokeLater(redo, ModalityState.NON_MODAL, project.disposed)
}

override fun getAffectedDocuments(): Array<DocumentReference> =
arrayOf(DocumentReferenceManager.getInstance().create(file.virtualFile))

override fun isGlobal() = true
}
action.redo()
UndoManager.getInstance(project).undoableActionPerformed(action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ open class SmkFileReference(
element: SmkArgsSection,
private val textRange: TextRange,
private val stringLiteralExpression: PyStringLiteralExpression,
private val path: String,
private val searchRelativelyToCurrentFolder: Boolean = true,
val path: String,
val searchRelativelyToCurrentFolder: Boolean = true,
) : PsiReferenceBase<SmkArgsSection>(element, textRange), PsiReferenceEx {
// Reference caching can be implemented with the 'ResolveCache' class if needed

Expand Down Expand Up @@ -158,6 +158,8 @@ open class SmkFileReference(
}

override fun getUnresolvedDescription(): String? = null

open fun hasAppropriateSuffix(): Boolean = false
}

/**
Expand All @@ -173,6 +175,9 @@ class SmkIncludeReference(
override fun getVariants() = collectFileSystemItemLike {
it is SmkFile && it.originalFile != element.containingFile.originalFile
}

override fun hasAppropriateSuffix() =
(path.endsWith(".smk") || path == "Snakemake") && element.containingFile.virtualFile.path != path
}

/**
Expand All @@ -193,8 +198,10 @@ class SmkConfigfileReference(
searchRelativelyToCurrentFolder = false
) {
override fun getVariants() = collectFileSystemItemLike {
isYamlFile(it)
isYamlFile(it.name)
}

override fun hasAppropriateSuffix() = isYamlFile(path)
}

/**
Expand All @@ -215,8 +222,10 @@ class SmkPepfileReference(
searchRelativelyToCurrentFolder = false
) {
override fun getVariants() = collectFileSystemItemLike {
isYamlFile(it)
isYamlFile(it.name)
}

override fun hasAppropriateSuffix() = isYamlFile(path)
}

/**
Expand All @@ -230,8 +239,10 @@ class SmkPepschemaReference(
path: String
) : SmkFileReference(element, textRange, stringLiteralExpression, path) {
override fun getVariants() = collectFileSystemItemLike {
isYamlFile(it)
isYamlFile(it.name)
}

override fun hasAppropriateSuffix() = isYamlFile(path)
}

/**
Expand All @@ -245,11 +256,13 @@ class SmkCondaEnvReference(
path: String
) : SmkFileReference(element, textRange, stringLiteralExpression, path) {
override fun getVariants() = collectFileSystemItemLike {
isYamlFile(it)
isYamlFile(it.name)
}

override fun hasAppropriateSuffix() = isYamlFile(path.lowercase())
}

private fun isYamlFile(it: PsiFileSystemItem) = it.name.endsWith(".yaml") || it.name.endsWith(".yml")
private fun isYamlFile(it: String) = it.endsWith(".yaml") || it.endsWith(".yml")

/**
* The path must built from directory with current snakefile
Expand All @@ -265,6 +278,8 @@ class SmkNotebookReference(
val name = it.name.lowercase()
name.endsWith(".ipynb")
}

override fun hasAppropriateSuffix() = path.endsWith(".ipynb")
}

/**
Expand All @@ -279,8 +294,13 @@ class SmkScriptReference(
) : SmkFileReference(element, textRange, stringLiteralExpression, path) {
override fun getVariants() = collectFileSystemItemLike {
val name = it.name.lowercase()
name.endsWith(".py") or name.endsWith(".r") or name.endsWith(".rmd") or name.endsWith(".jl") or name.endsWith(".rs")
hasCorrectEnding(name)
}

override fun hasAppropriateSuffix() = hasCorrectEnding(path.lowercase())

private fun hasCorrectEnding(name: String) =
name.endsWith(".py") or name.endsWith(".r") or name.endsWith(".rmd") or name.endsWith(".jl") or name.endsWith(".rs")
}

/**
Expand All @@ -296,6 +316,8 @@ class SmkReportReference(
override fun getVariants() = collectFileSystemItemLike {
it.name.endsWith(".html")
}

override fun hasAppropriateSuffix() = path.endsWith(".html")
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -632,5 +632,7 @@
/>
<inspectionExtension
implementation="com.jetbrains.snakecharm.inspections.SmkIgnorePyInspectionExtension"/>
<unresolvedReferenceQuickFixProvider
implementation="com.jetbrains.snakecharm.inspections.SmkUnresolvedReferenceInspectionExtension"/>
</extensions>
</idea-plugin>
6 changes: 6 additions & 0 deletions src/main/resources/SnakemakeBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ INSP.NAME.wrapper.args.missed.message=Argument ''{0}'' missed in ''{1}''
INSP.NAME.wrapper.args.section.missed.message=Section ''{0}'' is missed
INSP.NAME.wrapper.args.section.with.args.missed.message=Section ''{0}'' with args ''{1}'' is missed

# SmkUnresolvedReferenceInspectionExtension
INSP.NAME.conda.env.missing.fix=Create ''{0}''

# SmkSubworkflowRedeclarationInspection
INSP.NAME.subworkflow.redeclaration=Only last subworkflow with the same name will be executed

Expand Down Expand Up @@ -259,6 +262,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=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.

#######################
# Facet
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: Inspection: SmkUnresolvedReferenceInspectionExtension

Scenario Outline: Quick fix fot missed files
Given a snakemake project
Given I open a file "foo.smk" with text
"""
<section>: "<path>"
"""
And PyUnresolvedReferencesInspection inspection is enabled
Then I expect inspection error on <<path>> with message
"""
Unresolved reference '<path>'
"""
When I check highlighting warnings
And I invoke quick fix Create '<path>' and see text:
"""
<section>: "<path>"
"""
Examples:
| path | section |
| NAME.yaml | rule NAME: conda |
| envs/NAME.yaml | rule NAME: conda |
| ../envs/NAME.yaml | rule NAME: conda |
| NAME.py.ipynb | rule NAME: notebook |
| NAME.py | rule NAME: script |
| boo.smk | module NAME: snakefile |
| NAME.yaml | configfile |
| NAME.yaml | pepfile |
| NAME.yml | pepschema |

# Impossible to check whether the file has been created because:
# 1) It is being creating asynchronously
# 2) So, we may need async refresh() (see LightTempDirTestFixtureImpl.java:137)
# It leads to Exception: "Do not perform a synchronous refresh under read lock ..."