-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
128c924
Quickfix was implemented.
5b0c244
Exceptions handling was implemented.
8cf1859
Smart file path collecting. CHANGELOG was updated.
037cd71
Minor changes in messages and 'CrateMissedFile' behaviour. 'UndoableA…
2d6f1e2
Merge branch 'master' of https://github.com/JetBrains-Research/snakec…
088bdbc
Quick fix now available for the following sections: conda, notebook, …
754511f
Fix in extension behaviour.
c4d4a35
Minor fix in tests. Maybe asynchronous creation of file somehow affec…
ed06af7
Cosmetics.
603b190
Fix for absolute path, error message fix, logging was added, VFS api …
b883378
Now files aren't created in the UI thread.
a2baf11
Merge branch 'master' into features/#277-create-missing-env-file
dakochik 0521695
refactor: one long method split in several short
iromeo 304a448
refactor: CreateMissedFile quickfix split in 2 classes
iromeo acbbf59
fix: refresh only one VFS folder + do it async
iromeo b4eee94
fix: sometimes VFS refresh doesn't see FS changes due to some FS lag,…
iromeo 2e9e433
refactor: comments updated
iromeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
.../kotlin/com/jetbrains/snakecharm/inspections/SmkUnresolvedReferenceInspectionExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.jetbrains.snakecharm.inspections | ||
|
||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.psi.* | ||
import com.jetbrains.python.inspections.PyUnresolvedReferenceQuickFixProvider | ||
import com.jetbrains.python.psi.PyStringLiteralExpression | ||
import com.jetbrains.snakecharm.inspections.quickfix.CreateEnvFile | ||
import com.jetbrains.snakecharm.lang.SnakemakeNames | ||
import com.jetbrains.snakecharm.lang.psi.SmkRuleOrCheckpointArgsSection | ||
|
||
class SmkUnresolvedReferenceInspectionExtension : PyUnresolvedReferenceQuickFixProvider { | ||
override fun registerQuickFixes(reference: PsiReference, existing: MutableList<LocalQuickFix>) { | ||
val sec = reference.element as? SmkRuleOrCheckpointArgsSection ?: return | ||
if (sec.sectionKeyword == SnakemakeNames.SECTION_CONDA) { | ||
handleCondaSection(sec, existing) | ||
} | ||
} | ||
|
||
private fun handleCondaSection(st: SmkRuleOrCheckpointArgsSection, existing: MutableList<LocalQuickFix>) { | ||
val arg = st.argumentList?.arguments | ||
?.firstOrNull { it is PyStringLiteralExpression } | ||
as? PyStringLiteralExpression ?: return | ||
val name = arg.text.replace("\"", "") | ||
existing.add(CreateEnvFile(st, name)) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/jetbrains/snakecharm/inspections/quickfix/CreateEnvFile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 = """ | ||
channels: | ||
dependencies: | ||
""".trimIndent() | ||
|
||
override fun getFamilyName() = SnakemakeBundle.message("INSP.NAME.conda.env.missing.fix", fileName) | ||
|
||
override fun getText() = familyName | ||
|
||
override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) { | ||
val tokens = fileName.split('/') | ||
val targetName = tokens.lastOrNull() ?: return | ||
var currentFile = file.virtualFile.parent | ||
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) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/resources/features/highlighting/inspections/unresolved_reference_extension.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Feature: Inspection: SmkUnresolvedReferenceInspectionExtension | ||
Checks, that extension + inspection work | ||
|
||
Scenario Outline: Unresolved conda file | ||
Given a snakemake project | ||
Given I open a file "foo.smk" with text | ||
""" | ||
rule NAME: | ||
conda: | ||
"<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: | ||
""" | ||
rule NAME: | ||
conda: | ||
"<path>" | ||
""" | ||
Then the file "<path>" should have text | ||
""" | ||
channels: | ||
dependencies: | ||
""" | ||
Examples: | ||
| path | | ||
| NAME.yaml | | ||
| envs/NAME.yaml | | ||
| ../envs/NAME.yaml | |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is reasonable to expect, that in such complicated application, like IntelliJ platform the task of creating file by relative path + create intermediate dirs isn't a rare case and already solved via some API. The code below is bicycle invention in a rather complected way. E.g. java.io already have methods for creating files and folders. So here we could use smth like:
or just: