From 8d88ffb0006ba35fc3e8eff3a03888355d3208bc Mon Sep 17 00:00:00 2001 From: Dmitry Kochik Date: Mon, 16 Aug 2021 14:51:25 +0300 Subject: [PATCH] fixed: execution section inspection is fixed refactored: tests and error messages Resolves: #408 --- ...ecutionSubsectionInUseSectionInspection.kt | 27 +++++++ .../SmkUnexpectedSectionInspection.kt | 57 -------------- .../SmkUnrecognizedSectionInspection.kt | 33 +++----- src/main/resources/META-INF/plugin.xml | 8 +- src/main/resources/SnakemakeBundle.properties | 4 +- ...utionSubsectionInUseSectionInspection.html | 6 ++ .../SmkUnexpectedSectionInspection.html | 6 -- ...bsection_in_use_section_inspection.feature | 42 +++++++++++ .../unexpected_section_inspection.feature | 75 ------------------- .../unrecognized_section_inspection.feature | 48 ++++++++---- 10 files changed, 125 insertions(+), 181 deletions(-) create mode 100644 src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkExecutionSubsectionInUseSectionInspection.kt delete mode 100644 src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnexpectedSectionInspection.kt create mode 100644 src/main/resources/inspectionDescriptions/SmkExecutionSubsectionInUseSectionInspection.html delete mode 100644 src/main/resources/inspectionDescriptions/SmkUnexpectedSectionInspection.html create mode 100644 src/test/resources/features/highlighting/inspections/execution_subsection_in_use_section_inspection.feature delete mode 100644 src/test/resources/features/highlighting/inspections/unexpected_section_inspection.feature diff --git a/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkExecutionSubsectionInUseSectionInspection.kt b/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkExecutionSubsectionInUseSectionInspection.kt new file mode 100644 index 00000000..383ada2f --- /dev/null +++ b/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkExecutionSubsectionInUseSectionInspection.kt @@ -0,0 +1,27 @@ +package com.jetbrains.snakecharm.inspections + +import com.intellij.codeInspection.LocalInspectionToolSession +import com.intellij.codeInspection.ProblemsHolder +import com.jetbrains.snakecharm.SnakemakeBundle +import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI +import com.jetbrains.snakecharm.lang.SnakemakeNames +import com.jetbrains.snakecharm.lang.psi.* + +class SmkExecutionSubsectionInUseSectionInspection : SnakemakeInspection() { + override fun buildVisitor( + holder: ProblemsHolder, + isOnTheFly: Boolean, + session: LocalInspectionToolSession + ) = object : SnakemakeInspectionVisitor(holder, session) { + + override fun visitSmkRuleOrCheckpointArgsSection(st: SmkRuleOrCheckpointArgsSection) { + val sectionNamePsi = st.nameIdentifier + val sectionKeyword = st.sectionKeyword + if (st.getParentRuleOrCheckPoint() is SmkUse && + sectionKeyword in (SnakemakeAPI.EXECUTION_SECTIONS_KEYWORDS + SnakemakeNames.SECTION_RUN) + ) { + registerProblem(sectionNamePsi, SnakemakeBundle.message("INSP.NAME.unexpected.execution.section")) + } + } + } +} diff --git a/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnexpectedSectionInspection.kt b/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnexpectedSectionInspection.kt deleted file mode 100644 index 01aa52b3..00000000 --- a/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnexpectedSectionInspection.kt +++ /dev/null @@ -1,57 +0,0 @@ -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 - ) { - val sectionNamePsi = argsSection.nameIdentifier - val sectionKeyword = argsSection.sectionKeyword - // Not argsSection.getParentRuleOrCheckPoint() because it is widely used in the project - val sectionName = argsSection.parentOfType>()?.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 - ) - ) - } - } -} diff --git a/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnrecognizedSectionInspection.kt b/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnrecognizedSectionInspection.kt index f2594318..d566dda4 100644 --- a/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnrecognizedSectionInspection.kt +++ b/src/main/kotlin/com/jetbrains/snakecharm/inspections/SmkUnrecognizedSectionInspection.kt @@ -5,18 +5,15 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.util.elementType import com.intellij.codeInspection.ui.ListEditForm import com.jetbrains.snakecharm.SnakemakeBundle +import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI.EXECUTION_SECTIONS_KEYWORDS import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI.MODULE_SECTIONS_KEYWORDS import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI.RULE_OR_CHECKPOINT_ARGS_SECTION_KEYWORDS -import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI.RULE_OR_CHECKPOINT_SECTION_KEYWORDS import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI.SUBWORKFLOW_SECTIONS_KEYWORDS import com.jetbrains.snakecharm.codeInsight.SnakemakeAPI.USE_SECTIONS_KEYWORDS import com.jetbrains.snakecharm.lang.psi.* import com.jetbrains.snakecharm.lang.psi.elementTypes.SmkElementTypes import com.jetbrains.snakecharm.inspections.quickfix.AddIgnoredElementQuickFix -import com.jetbrains.snakecharm.lang.SnakemakeNames.MODULE_KEYWORD -import com.jetbrains.snakecharm.lang.SnakemakeNames.RULE_KEYWORD -import com.jetbrains.snakecharm.lang.SnakemakeNames.SUBWORKFLOW_KEYWORD -import com.jetbrains.snakecharm.lang.SnakemakeNames.USE_KEYWORD +import com.jetbrains.snakecharm.lang.SnakemakeNames.SECTION_RUN import javax.swing.JComponent class SmkUnrecognizedSectionInspection : SnakemakeInspection() { @@ -56,16 +53,14 @@ class SmkUnrecognizedSectionInspection : SnakemakeInspection() { val sectionKeyword = argsSection.sectionKeyword if (sectionNamePsi != null && sectionKeyword != null && sectionKeyword !in setOfValidNames - && sectionKeyword !in ignoredItems + && sectionKeyword !in ignoredItems && !(argsSection.getParentRuleOrCheckPoint() is SmkUse + && sectionKeyword in (EXECUTION_SECTIONS_KEYWORDS + SECTION_RUN)) ) { - val appropriateSection = getSectionBySubsection(sectionKeyword) - if (appropriateSection == null) { - registerProblem( - sectionNamePsi, - SnakemakeBundle.message("INSP.NAME.section.unrecognized.message", sectionKeyword), - AddIgnoredElementQuickFix(sectionKeyword) - ) - } + registerProblem( + sectionNamePsi, + SnakemakeBundle.message("INSP.NAME.section.unrecognized.message", sectionKeyword), + AddIgnoredElementQuickFix(sectionKeyword) + ) } } @@ -78,15 +73,5 @@ class SmkUnrecognizedSectionInspection : SnakemakeInspection() { override fun createOptionsPanel(): JComponent? = ListEditForm(SnakemakeBundle.message("INSP.NAME.section.unrecognized.ignored"), ignoredItems).contentPanel - - companion object { - fun getSectionBySubsection(name: String) = when (name) { - in RULE_OR_CHECKPOINT_SECTION_KEYWORDS -> RULE_KEYWORD - in SUBWORKFLOW_SECTIONS_KEYWORDS -> SUBWORKFLOW_KEYWORD - in MODULE_SECTIONS_KEYWORDS -> MODULE_KEYWORD - in USE_SECTIONS_KEYWORDS -> USE_KEYWORD - else -> null - } - } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 91cb47e9..554df123 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -183,14 +183,14 @@ /> + +This inspection highlights the sections which can't be overridden in 'use rule' ('shell', 'script', +'notebook', 'cwl' and 'run' sections). + + \ No newline at end of file diff --git a/src/main/resources/inspectionDescriptions/SmkUnexpectedSectionInspection.html b/src/main/resources/inspectionDescriptions/SmkUnexpectedSectionInspection.html deleted file mode 100644 index d0b940fa..00000000 --- a/src/main/resources/inspectionDescriptions/SmkUnexpectedSectionInspection.html +++ /dev/null @@ -1,6 +0,0 @@ - - -This inspection highlights the subsections which can't be used in current section, -but can be used in other sections. - - \ No newline at end of file diff --git a/src/test/resources/features/highlighting/inspections/execution_subsection_in_use_section_inspection.feature b/src/test/resources/features/highlighting/inspections/execution_subsection_in_use_section_inspection.feature new file mode 100644 index 00000000..f8979936 --- /dev/null +++ b/src/test/resources/features/highlighting/inspections/execution_subsection_in_use_section_inspection.feature @@ -0,0 +1,42 @@ +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: "" + input: "file1" + script: "" + cwl: "" + wrapper: "" + output: "file2" + """ + And SmkExecutionSubsectionInUseSectionInspection inspection is enabled + Then I expect inspection error on with message + """ + Execution sections can't be used in 'use rule' + """ + Then I expect inspection error on with message + """ + Execution sections can't be used in 'use rule' + """ + Then I expect inspection error on with message + """ + Execution sections can't be used in 'use rule' + """ + Then I expect inspection error on