-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored: SmkUnrecognizedSectionInspection was divided into two dif…
…ferent inspections, Resolves: #408
- Loading branch information
Dmitry Kochik
committed
Aug 12, 2021
1 parent
288d8fa
commit 1076920
Showing
7 changed files
with
172 additions
and
105 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnexpectedSectionInspection.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,57 @@ | ||
package com.jetbrains.snakecharm.inspections | ||
|
||
import com.intellij.codeInspection.LocalInspectionToolSession | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.util.elementType | ||
import com.intellij.psi.util.parentOfType | ||
import com.jetbrains.snakecharm.SnakemakeBundle | ||
import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI | ||
import com.jetbrains.snakecharm.lang.psi.* | ||
import com.jetbrains.snakecharm.lang.psi.elementTypes.SmkElementTypes | ||
|
||
class SmkUnexpectedSectionInspection : SnakemakeInspection() { | ||
override fun buildVisitor( | ||
holder: ProblemsHolder, | ||
isOnTheFly: Boolean, | ||
session: LocalInspectionToolSession | ||
) = object : SnakemakeInspectionVisitor(holder, session) { | ||
override fun visitSmkSubworkflowArgsSection(st: SmkSubworkflowArgsSection) { | ||
isSectionRecognized(st, SnakemakeAPI.SUBWORKFLOW_SECTIONS_KEYWORDS) | ||
} | ||
|
||
override fun visitSmkRuleOrCheckpointArgsSection(st: SmkRuleOrCheckpointArgsSection) { | ||
if (st.originalElement.elementType == SmkElementTypes.USE_ARGS_SECTION_STATEMENT) { | ||
isSectionRecognized(st, SnakemakeAPI.USE_SECTIONS_KEYWORDS) | ||
} else { | ||
isSectionRecognized(st, SnakemakeAPI.RULE_OR_CHECKPOINT_ARGS_SECTION_KEYWORDS) | ||
} | ||
} | ||
|
||
override fun visitSmkModuleArgsSection(st: SmkModuleArgsSection) { | ||
isSectionRecognized(st, SnakemakeAPI.MODULE_SECTIONS_KEYWORDS) | ||
} | ||
|
||
private fun isSectionRecognized( | ||
argsSection: SmkArgsSection, | ||
setOfValidNames: Set<String> | ||
) { | ||
val sectionNamePsi = argsSection.nameIdentifier | ||
val sectionKeyword = argsSection.sectionKeyword | ||
// Not argsSection.getParentRuleOrCheckPoint() because it is widely used in the project | ||
val sectionName = argsSection.parentOfType<SmkRuleLike<*>>()?.sectionKeyword ?: return | ||
if (sectionNamePsi == null || sectionKeyword == null || sectionKeyword in setOfValidNames) { | ||
return | ||
} | ||
val appropriateSection = SmkUnrecognizedSectionInspection.getSectionBySubsection(sectionKeyword) ?: return | ||
registerProblem( | ||
sectionNamePsi, | ||
SnakemakeBundle.message( | ||
"INSP.NAME.section.unexpected", | ||
sectionKeyword, | ||
sectionName, | ||
appropriateSection | ||
) | ||
) | ||
} | ||
} | ||
} |
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
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
6 changes: 6 additions & 0 deletions
6
src/main/resources/inspectionDescriptions/SmkUnexpectedSectionInspection.html
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,6 @@ | ||
<html> | ||
<body> | ||
This inspection highlights the subsections which can't be used in current section, | ||
but can be used in other sections. | ||
</body> | ||
</html> |
75 changes: 75 additions & 0 deletions
75
src/test/resources/features/highlighting/inspections/unexpected_section_inspection.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,75 @@ | ||
Feature: Inspection if subsection is unexpected for section but it i appropriate for another section | ||
|
||
Scenario: When 'use' section contains execution subsections | ||
Given a snakemake project | ||
Given I open a file "foo.smk" with text | ||
""" | ||
use rule RULE as NEW_RULE with: | ||
run: "" | ||
shell: "" | ||
notebook: "" | ||
script: "" | ||
cwl: "" | ||
wrapper: "" | ||
""" | ||
And SmkUnexpectedSectionInspection inspection is enabled | ||
Then I expect inspection error on <run> with message | ||
""" | ||
Section 'run' can't be used in 'use' but can be in 'rule' | ||
""" | ||
Then I expect inspection error on <shell> with message | ||
""" | ||
Section 'shell' can't be used in 'use' but can be in 'rule' | ||
""" | ||
Then I expect inspection error on <notebook> with message | ||
""" | ||
Section 'notebook' can't be used in 'use' but can be in 'rule' | ||
""" | ||
Then I expect inspection error on <script> with message | ||
""" | ||
Section 'script' can't be used in 'use' but can be in 'rule' | ||
""" | ||
Then I expect inspection error on <cwl> with message | ||
""" | ||
Section 'cwl' can't be used in 'use' but can be in 'rule' | ||
""" | ||
Then I expect inspection error on <wrapper> with message | ||
""" | ||
Section 'wrapper' can't be used in 'use' but can be in 'rule' | ||
""" | ||
When I check highlighting errors | ||
|
||
Scenario: Wrong sections | ||
Given a snakemake project | ||
Given I open a file "foo.smk" with text | ||
""" | ||
rule NAME: | ||
skip_validation: True | ||
checkpoint NAME2: | ||
meta_wrapper: "wrapper" | ||
subworkflow NAME3: | ||
log: "log.txt" | ||
module m: | ||
workdir: "dir" | ||
""" | ||
And SmkUnexpectedSectionInspection inspection is enabled | ||
Then I expect inspection error on <skip_validation> with message | ||
""" | ||
Section 'skip_validation' can't be used in 'rule' but can be in 'module' | ||
""" | ||
Then I expect inspection error on <meta_wrapper> with message | ||
""" | ||
Section 'meta_wrapper' can't be used in 'checkpoint' but can be in 'module' | ||
""" | ||
Then I expect inspection error on <log> with message | ||
""" | ||
Section 'log' can't be used in 'subworkflow' but can be in 'rule' | ||
""" | ||
Then I expect inspection error on <workdir> with message | ||
""" | ||
Section 'workdir' can't be used in 'module' but can be in 'subworkflow' | ||
""" | ||
When I check highlighting errors |
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